Merge pull request 'chore(lib): prepare for refactoring into clanLib' (#3141) from hsjobeki/clan-core:clan-services into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3141
This commit is contained in:
hsjobeki
2025-03-30 15:51:17 +00:00
14 changed files with 58 additions and 44 deletions

View File

@@ -13,8 +13,8 @@
# { clanCore = «derivation JSON»; clanModules = { ${name} = «derivation JSON» }; } # { clanCore = «derivation JSON»; clanModules = { ${name} = «derivation JSON» }; }
jsonDocs = pkgs.callPackage ./get-module-docs.nix { jsonDocs = pkgs.callPackage ./get-module-docs.nix {
inherit (self) clanModules; inherit (self) clanModules;
evalClanModules = self.lib.evalClanModules; evalClanModules = self.lib.evalClan.evalClanModules;
modulesRolesOptions = self.lib.evalClanModulesWithRoles self.clanModules; modulesRolesOptions = self.lib.evalClan.evalClanModulesWithRoles self.clanModules;
}; };
# Frontmatter for clanModules # Frontmatter for clanModules

View File

@@ -148,7 +148,7 @@ in
clanModules = lib.mkOption { type = lib.types.raw; }; clanModules = lib.mkOption { type = lib.types.raw; };
source = lib.mkOption { type = lib.types.raw; }; source = lib.mkOption { type = lib.types.raw; };
meta = lib.mkOption { type = lib.types.raw; }; meta = lib.mkOption { type = lib.types.raw; };
lib = lib.mkOption { type = lib.types.raw; }; clanLib = lib.mkOption { type = lib.types.raw; };
all-machines-json = lib.mkOption { type = lib.types.raw; }; all-machines-json = lib.mkOption { type = lib.types.raw; };
machines = lib.mkOption { type = lib.types.raw; }; machines = lib.mkOption { type = lib.types.raw; };
machinesFunc = lib.mkOption { type = lib.types.raw; }; machinesFunc = lib.mkOption { type = lib.types.raw; };

View File

@@ -205,7 +205,7 @@ in
inherit lib inventory; inherit lib inventory;
flake = config.self; flake = config.self;
}; };
inherit (clan-core) clanModules; inherit (clan-core) clanModules clanLib;
inherit inventoryFile; inherit inventoryFile;
inventoryValuesPrios = inventoryValuesPrios =
# Temporary workaround # Temporary workaround
@@ -217,9 +217,6 @@ in
templates = config.templates; templates = config.templates;
inventory = config.inventory; inventory = config.inventory;
meta = config.inventory.meta; meta = config.inventory.meta;
lib = {
inherit (clan-core.lib) select;
};
source = "${clan-core}"; source = "${clan-core}";

View File

@@ -1,25 +1,35 @@
{ {
lib, lib,
clan-core, self,
nixpkgs, nixpkgs,
... ...
}: }:
let # Produces the
# 'clanLib' attribute set
# Wrapped with fix, so we can depend on other clanLib functions without passing the whole flake
lib.fix (clanLib: {
# TODO:
# SSome bad lib functions that depend on something in 'self'.
# We should reduce the dependency on 'self' aka the 'flake' object
# This makes it easier to test
# most of the time passing the whole flake is unnecessary
callLib = file: args: import file { inherit lib clanLib; } // args;
evalClan = import ./eval-clan-modules { evalClan = import ./eval-clan-modules {
inherit clan-core lib; inherit lib;
clan-core = self;
pkgs = nixpkgs.legacyPackages.x86_64-linux; pkgs = nixpkgs.legacyPackages.x86_64-linux;
}; };
in buildClan = import ./build-clan {
{ inherit lib nixpkgs;
inherit (evalClan) evalClanModules evalClanModulesWithRoles; clan-core = self;
buildClan = import ./build-clan { inherit lib nixpkgs clan-core; }; };
# ------------------------------------
# Lib functions that don't depend on 'self'
inventory = clanLib.callLib ./inventory { };
modules = clanLib.callLib ./frontmatter { };
facts = import ./facts.nix { inherit lib; }; facts = import ./facts.nix { inherit lib; };
inventory = import ./inventory { inherit lib clan-core; };
values = import ./values { inherit lib; }; values = import ./values { inherit lib; };
jsonschema = import ./jsonschema { inherit lib; }; jsonschema = import ./jsonschema { inherit lib; };
modules = import ./frontmatter {
inherit lib;
self = clan-core;
};
select = import ./select.nix; select = import ./select.nix;
} })

View File

@@ -10,7 +10,9 @@ let
pathExists pathExists
; ;
in in
{ rec {
# We should remove this.
# It would enforce treating at least 'lib' as a module in a whole
imports = filter pathExists [ imports = filter pathExists [
./jsonschema/flake-module.nix ./jsonschema/flake-module.nix
./inventory/flake-module.nix ./inventory/flake-module.nix
@@ -18,9 +20,10 @@ in
./values/flake-module.nix ./values/flake-module.nix
./distributed-service/flake-module.nix ./distributed-service/flake-module.nix
]; ];
flake.lib = import ./default.nix { flake.clanLib = import ./default.nix {
inherit lib inputs; inherit lib inputs self;
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs;
clan-core = self;
}; };
# TODO: remove this legacy alias
flake.lib = flake.clanLib;
} }

View File

@@ -1,9 +1,9 @@
{ lib, self }: { lib, clanLib }:
let let
# Trim the .nix extension from a filename # Trim the .nix extension from a filename
trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name; trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name;
jsonWithoutHeader = self.lib.jsonschema { jsonWithoutHeader = clanLib.jsonschema {
includeDefaults = true; includeDefaults = true;
header = { }; header = { };
}; };
@@ -13,7 +13,7 @@ let
lib.mapAttrs ( lib.mapAttrs (
_moduleName: rolesOptions: _moduleName: rolesOptions:
lib.mapAttrs (_roleName: options: jsonWithoutHeader.parseOptions options { }) rolesOptions lib.mapAttrs (_roleName: options: jsonWithoutHeader.parseOptions options { }) rolesOptions
) (self.lib.evalClanModulesWithRoles modules); ) (clanLib.evalClan.evalClanModulesWithRoles modules);
evalFrontmatter = evalFrontmatter =
{ {

View File

@@ -1,7 +1,7 @@
{ {
lib, lib,
config, config,
clan-core, clanLib,
... ...
}: }:
let let
@@ -43,8 +43,7 @@ let
checkService = checkService =
modulepath: serviceName: modulepath: serviceName:
builtins.elem "inventory" builtins.elem "inventory" (clanLib.modules.getFrontmatter modulepath serviceName).features or [ ];
(clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ];
compileMachine = compileMachine =
{ machineConfig }: { machineConfig }:
@@ -160,7 +159,7 @@ in
inherit inherit
resolveTags resolveTags
inventory inventory
clan-core clanLib
machineName machineName
serviceConfigs serviceConfigs
; ;

View File

@@ -3,7 +3,7 @@
config, config,
resolveTags, resolveTags,
inventory, inventory,
clan-core, clanLib,
machineName, machineName,
serviceConfigs, serviceConfigs,
... ...
@@ -14,7 +14,7 @@ in
{ {
# Roles resolution # Roles resolution
# : List String # : List String
supportedRoles = clan-core.lib.modules.getRoles inventory.modules serviceName; supportedRoles = clanLib.modules.getRoles inventory.modules serviceName;
matchedRoles = builtins.attrNames ( matchedRoles = builtins.attrNames (
lib.filterAttrs (_: ms: builtins.elem machineName ms) config.machinesRoles lib.filterAttrs (_: ms: builtins.elem machineName ms) config.machinesRoles
); );
@@ -56,7 +56,7 @@ in
assertions = lib.concatMapAttrs ( assertions = lib.concatMapAttrs (
instanceName: resolvedRoles: instanceName: resolvedRoles:
clan-core.lib.modules.checkConstraints { clanLib.modules.checkConstraints {
moduleName = serviceName; moduleName = serviceName;
allModules = inventory.modules; allModules = inventory.modules;
inherit resolvedRoles instanceName; inherit resolvedRoles instanceName;

View File

@@ -1,6 +1,6 @@
# Generate partial NixOS configurations for every machine in the inventory # Generate partial NixOS configurations for every machine in the inventory
# This function is responsible for generating the module configuration for every machine in the inventory. # This function is responsible for generating the module configuration for every machine in the inventory.
{ lib, clan-core }: { lib, clanLib }:
let let
/* /*
Returns a set with NixOS configuration for every machine in the inventory. Returns a set with NixOS configuration for every machine in the inventory.
@@ -11,7 +11,7 @@ let
{ inventory, directory }: { inventory, directory }:
(lib.evalModules { (lib.evalModules {
specialArgs = { specialArgs = {
inherit clan-core; inherit clanLib;
}; };
modules = [ modules = [
./builder ./builder

View File

@@ -343,7 +343,12 @@ in
} }
); );
default = { }; default = { };
apply = lib.warn "Inventory.instances and related features are still under development. Please use with care."; apply =
v:
if v == { } then
v
else
lib.warn "Inventory.instances and related features are still under development. Please use with care." v;
}; };
services = lib.mkOption { services = lib.mkOption {
description = '' description = ''

View File

@@ -1,5 +1,5 @@
{ lib, clan-core }: { lib, clanLib }:
{ {
inherit (import ./build-inventory { inherit lib clan-core; }) buildInventory; inherit (import ./build-inventory { inherit lib clanLib; }) buildInventory;
interface = ./build-inventory/interface.nix; interface = ./build-inventory/interface.nix;
} }

View File

@@ -2,7 +2,8 @@
let let
inventory = ( inventory = (
import ../build-inventory { import ../build-inventory {
inherit lib clan-core; inherit lib;
clanLib = clan-core.clanLib;
} }
); );
inherit (inventory) buildInventory; inherit (inventory) buildInventory;

View File

@@ -1,10 +1,9 @@
{ {
lib, lib,
config,
clan-core, clan-core,
... ...
}: }:
{ {
# Just some random stuff # Just some random stuff
config.user.user = lib.mapAttrs clan-core.users.root; options.test = lib.mapAttrs clan-core;
} }

View File

@@ -86,7 +86,7 @@ class FlakeCacheEntry:
self.selector = {int(selectors[0])} self.selector = {int(selectors[0])}
selector = int(selectors[0]) selector = int(selectors[0])
elif isinstance(selectors[0], str): elif isinstance(selectors[0], str):
self.selector = {(selectors[0])} self.selector = {selectors[0]}
selector = selectors[0] selector = selectors[0]
elif isinstance(selectors[0], AllSelector): elif isinstance(selectors[0], AllSelector):
self.selector = AllSelector() self.selector = AllSelector()
@@ -481,7 +481,7 @@ class Flake:
flake = builtins.getFlake("path:{self.store_path}?narHash={self.hash}"); flake = builtins.getFlake("path:{self.store_path}?narHash={self.hash}");
in in
flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" ( flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" (
builtins.toJSON [ ({" ".join([f"flake.clanInternals.lib.select ''{attr}'' flake" for attr in selectors])}) ] builtins.toJSON [ ({" ".join([f"flake.clanInternals.clanLib.select ''{attr}'' flake" for attr in selectors])}) ]
) )
""" """
if tmp_store := nix_test_store(): if tmp_store := nix_test_store():