diff --git a/docs/nix/flake-module.nix b/docs/nix/flake-module.nix index e38226f01..ec079d0a7 100644 --- a/docs/nix/flake-module.nix +++ b/docs/nix/flake-module.nix @@ -13,8 +13,8 @@ # { clanCore = «derivation JSON»; clanModules = { ${name} = «derivation JSON» }; } jsonDocs = pkgs.callPackage ./get-module-docs.nix { inherit (self) clanModules; - evalClanModules = self.lib.evalClanModules; - modulesRolesOptions = self.lib.evalClanModulesWithRoles self.clanModules; + evalClanModules = self.lib.evalClan.evalClanModules; + modulesRolesOptions = self.lib.evalClan.evalClanModulesWithRoles self.clanModules; }; # Frontmatter for clanModules diff --git a/lib/build-clan/interface.nix b/lib/build-clan/interface.nix index ed4268ec3..010d86819 100644 --- a/lib/build-clan/interface.nix +++ b/lib/build-clan/interface.nix @@ -148,7 +148,7 @@ in clanModules = lib.mkOption { type = lib.types.raw; }; source = 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; }; machines = lib.mkOption { type = lib.types.raw; }; machinesFunc = lib.mkOption { type = lib.types.raw; }; diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index f4bd10d58..8c689cc20 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -205,7 +205,7 @@ in inherit lib inventory; flake = config.self; }; - inherit (clan-core) clanModules; + inherit (clan-core) clanModules clanLib; inherit inventoryFile; inventoryValuesPrios = # Temporary workaround @@ -217,9 +217,6 @@ in templates = config.templates; inventory = config.inventory; meta = config.inventory.meta; - lib = { - inherit (clan-core.lib) select; - }; source = "${clan-core}"; diff --git a/lib/default.nix b/lib/default.nix index 692579a99..59b6a873c 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,25 +1,35 @@ { lib, - clan-core, + self, 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 { - inherit clan-core lib; + inherit lib; + clan-core = self; pkgs = nixpkgs.legacyPackages.x86_64-linux; }; -in -{ - inherit (evalClan) evalClanModules evalClanModulesWithRoles; - buildClan = import ./build-clan { inherit lib nixpkgs clan-core; }; + buildClan = import ./build-clan { + inherit lib nixpkgs; + clan-core = self; + }; + # ------------------------------------ + # Lib functions that don't depend on 'self' + inventory = clanLib.callLib ./inventory { }; + modules = clanLib.callLib ./frontmatter { }; facts = import ./facts.nix { inherit lib; }; - inventory = import ./inventory { inherit lib clan-core; }; values = import ./values { inherit lib; }; jsonschema = import ./jsonschema { inherit lib; }; - modules = import ./frontmatter { - inherit lib; - self = clan-core; - }; select = import ./select.nix; -} +}) diff --git a/lib/flake-module.nix b/lib/flake-module.nix index 219e1a230..5d729ee68 100644 --- a/lib/flake-module.nix +++ b/lib/flake-module.nix @@ -10,7 +10,9 @@ let pathExists ; in -{ +rec { + # We should remove this. + # It would enforce treating at least 'lib' as a module in a whole imports = filter pathExists [ ./jsonschema/flake-module.nix ./inventory/flake-module.nix @@ -18,9 +20,10 @@ in ./values/flake-module.nix ./distributed-service/flake-module.nix ]; - flake.lib = import ./default.nix { - inherit lib inputs; + flake.clanLib = import ./default.nix { + inherit lib inputs self; inherit (inputs) nixpkgs; - clan-core = self; }; + # TODO: remove this legacy alias + flake.lib = flake.clanLib; } diff --git a/lib/frontmatter/default.nix b/lib/frontmatter/default.nix index 9e34e8f68..1beeb21ac 100644 --- a/lib/frontmatter/default.nix +++ b/lib/frontmatter/default.nix @@ -1,9 +1,9 @@ -{ lib, self }: +{ lib, clanLib }: let # Trim the .nix extension from a filename trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name; - jsonWithoutHeader = self.lib.jsonschema { + jsonWithoutHeader = clanLib.jsonschema { includeDefaults = true; header = { }; }; @@ -13,7 +13,7 @@ let lib.mapAttrs ( _moduleName: rolesOptions: lib.mapAttrs (_roleName: options: jsonWithoutHeader.parseOptions options { }) rolesOptions - ) (self.lib.evalClanModulesWithRoles modules); + ) (clanLib.evalClan.evalClanModulesWithRoles modules); evalFrontmatter = { diff --git a/lib/inventory/build-inventory/builder/default.nix b/lib/inventory/build-inventory/builder/default.nix index fea60509b..e3b3bad38 100644 --- a/lib/inventory/build-inventory/builder/default.nix +++ b/lib/inventory/build-inventory/builder/default.nix @@ -1,7 +1,7 @@ { lib, config, - clan-core, + clanLib, ... }: let @@ -43,8 +43,7 @@ let checkService = modulepath: serviceName: - builtins.elem "inventory" - (clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ]; + builtins.elem "inventory" (clanLib.modules.getFrontmatter modulepath serviceName).features or [ ]; compileMachine = { machineConfig }: @@ -160,7 +159,7 @@ in inherit resolveTags inventory - clan-core + clanLib machineName serviceConfigs ; diff --git a/lib/inventory/build-inventory/builder/roles.nix b/lib/inventory/build-inventory/builder/roles.nix index 48adb6534..1eb29f47d 100644 --- a/lib/inventory/build-inventory/builder/roles.nix +++ b/lib/inventory/build-inventory/builder/roles.nix @@ -3,7 +3,7 @@ config, resolveTags, inventory, - clan-core, + clanLib, machineName, serviceConfigs, ... @@ -14,7 +14,7 @@ in { # Roles resolution # : List String - supportedRoles = clan-core.lib.modules.getRoles inventory.modules serviceName; + supportedRoles = clanLib.modules.getRoles inventory.modules serviceName; matchedRoles = builtins.attrNames ( lib.filterAttrs (_: ms: builtins.elem machineName ms) config.machinesRoles ); @@ -56,7 +56,7 @@ in assertions = lib.concatMapAttrs ( instanceName: resolvedRoles: - clan-core.lib.modules.checkConstraints { + clanLib.modules.checkConstraints { moduleName = serviceName; allModules = inventory.modules; inherit resolvedRoles instanceName; diff --git a/lib/inventory/build-inventory/default.nix b/lib/inventory/build-inventory/default.nix index 9f93a0a10..c11176305 100644 --- a/lib/inventory/build-inventory/default.nix +++ b/lib/inventory/build-inventory/default.nix @@ -1,6 +1,6 @@ # 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. -{ lib, clan-core }: +{ lib, clanLib }: let /* Returns a set with NixOS configuration for every machine in the inventory. @@ -11,7 +11,7 @@ let { inventory, directory }: (lib.evalModules { specialArgs = { - inherit clan-core; + inherit clanLib; }; modules = [ ./builder diff --git a/lib/inventory/build-inventory/interface.nix b/lib/inventory/build-inventory/interface.nix index 01c25c37b..b496bb962 100644 --- a/lib/inventory/build-inventory/interface.nix +++ b/lib/inventory/build-inventory/interface.nix @@ -343,7 +343,12 @@ in } ); 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 { description = '' diff --git a/lib/inventory/default.nix b/lib/inventory/default.nix index 4550a4299..cb6f3e11e 100644 --- a/lib/inventory/default.nix +++ b/lib/inventory/default.nix @@ -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; } diff --git a/lib/inventory/tests/default.nix b/lib/inventory/tests/default.nix index 8dbbf6d6f..3fd897c2a 100644 --- a/lib/inventory/tests/default.nix +++ b/lib/inventory/tests/default.nix @@ -2,7 +2,8 @@ let inventory = ( import ../build-inventory { - inherit lib clan-core; + inherit lib; + clanLib = clan-core.clanLib; } ); inherit (inventory) buildInventory; diff --git a/lib/inventory/tests/legacyModule/roles/default.nix b/lib/inventory/tests/legacyModule/roles/default.nix index 853ff22fe..2c7de4687 100644 --- a/lib/inventory/tests/legacyModule/roles/default.nix +++ b/lib/inventory/tests/legacyModule/roles/default.nix @@ -1,10 +1,9 @@ { lib, - config, clan-core, ... }: { # Just some random stuff - config.user.user = lib.mapAttrs clan-core.users.root; + options.test = lib.mapAttrs clan-core; } diff --git a/pkgs/clan-cli/clan_cli/flake.py b/pkgs/clan-cli/clan_cli/flake.py index e23d012b4..057454d60 100644 --- a/pkgs/clan-cli/clan_cli/flake.py +++ b/pkgs/clan-cli/clan_cli/flake.py @@ -86,7 +86,7 @@ class FlakeCacheEntry: self.selector = {int(selectors[0])} selector = int(selectors[0]) elif isinstance(selectors[0], str): - self.selector = {(selectors[0])} + self.selector = {selectors[0]} selector = selectors[0] elif isinstance(selectors[0], AllSelector): self.selector = AllSelector() @@ -481,7 +481,7 @@ class Flake: flake = builtins.getFlake("path:{self.store_path}?narHash={self.hash}"); in 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():