diff --git a/lib/default.nix b/lib/default.nix index 8c3943e14..34d88241f 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -3,7 +3,7 @@ # TODO: Get rid of self here. # DONT add any new functions that depend on self here. # If a lib function depends on a piece in clan-core add that piece to the function arguments - self, + self ? throw "'self' should not be used in lib/default.nix, dont depend on it. It will be removed in short notice.", ... }: # Produces the @@ -48,5 +48,8 @@ lib.fix ( # flakes flakes = clanLib.callLib ./flakes.nix { }; + + # TODO: Flatten our lib functions like this: + resolveModule = clanLib.callLib ./resolve-module { }; } ) diff --git a/lib/flake-module.nix b/lib/flake-module.nix index 08497656a..c2d3f3545 100644 --- a/lib/flake-module.nix +++ b/lib/flake-module.nix @@ -4,6 +4,9 @@ self, ... }: +let + inputOverrides = self.clanLib.flake-inputs.getOverrides inputs; +in rec { # TODO: automatically generate this from the directory conventions imports = [ @@ -19,4 +22,47 @@ rec { }; # TODO: remove this legacy alias flake.lib = flake.clanLib; + + perSystem = + { + pkgs, + lib, + system, + ... + }: + let + # Common filtered source for module tests + inventoryTestsSrc = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + ../flake.nix + ../flake.lock + ../lib + (lib.fileset.fileFilter (file: file.name == "flake-module.nix") ../.) + ../flakeModules + # ../../nixosModules/clanCore + # ../../machines + # ../../inventory.json + ]; + }; + in + { + legacyPackages.eval-tests-resolve-module = import ./resolve-module/test.nix { + inherit lib; + }; + + checks = { + eval-lib-resolve-module = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } '' + export HOME="$(realpath .)" + nix-unit --eval-store "$HOME" \ + --extra-experimental-features flakes \ + --show-trace \ + ${inputOverrides} \ + --flake ${inventoryTestsSrc}#legacyPackages.${system}.eval-tests-resolve-module + + touch $out + ''; + }; + }; + } diff --git a/lib/modules/default.nix b/lib/modules/default.nix index 1c2625e16..87071b479 100644 --- a/lib/modules/default.nix +++ b/lib/modules/default.nix @@ -10,8 +10,8 @@ rec { module: lib.warn '' ==================== DEPRECATION NOTICE ==================== - Please migrate - from: 'clan = inputs..lib.buildClan' + Please migrate + from: 'clan = inputs..lib.buildClan' to : 'clan = inputs..lib.clan' in your flake.nix. @@ -23,9 +23,9 @@ rec { " in your flake.nix. - Reason: + Reason: - Improves consistency between flake-parts and non-flake-parts users. - + - It also allows us to use the top level attribute 'clan' to expose attributes that can be used for cross-clan functionality. ============================================================ diff --git a/lib/modules/inventory/distributed-service/flake-module.nix b/lib/modules/inventory/distributed-service/flake-module.nix index 5f0fb3ad6..2d313d7a1 100644 --- a/lib/modules/inventory/distributed-service/flake-module.nix +++ b/lib/modules/inventory/distributed-service/flake-module.nix @@ -32,9 +32,6 @@ in inherit lib; clanLib = self.clanLib; }; - legacyPackages.eval-tests-resolve-module = import ./test-resolve-module.nix { - inherit lib; - }; checks = { eval-lib-distributedServices = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } '' @@ -47,16 +44,6 @@ in touch $out ''; - eval-tests-resolve-module = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } '' - export HOME="$(realpath .)" - nix-unit --eval-store "$HOME" \ - --extra-experimental-features flakes \ - --show-trace \ - ${inputOverrides} \ - --flake ${inventoryTestsSrc}#legacyPackages.${system}.eval-tests-resolve-module - - touch $out - ''; }; }; } diff --git a/lib/modules/inventory/distributed-service/inventory-adapter.nix b/lib/modules/inventory/distributed-service/inventory-adapter.nix index 15796d559..8edab9be0 100644 --- a/lib/modules/inventory/distributed-service/inventory-adapter.nix +++ b/lib/modules/inventory/distributed-service/inventory-adapter.nix @@ -14,9 +14,6 @@ clanLib, ... }: -let - resolveModule = import ./resolveModule.nix { inherit lib; }; -in { mapInstances = { @@ -36,7 +33,7 @@ in importedModuleWithInstances = lib.mapAttrs ( instanceName: instance: let - resolvedModule = resolveModule { + resolvedModule = clanLib.resolveModule { moduleSpec = instance.module; inherit flakeInputs clanCoreModules; }; diff --git a/lib/modules/inventory/distributed-service/resolveModule.nix b/lib/resolve-module/default.nix similarity index 71% rename from lib/modules/inventory/distributed-service/resolveModule.nix rename to lib/resolve-module/default.nix index b4ba548e4..5b13b0577 100644 --- a/lib/modules/inventory/distributed-service/resolveModule.nix +++ b/lib/resolve-module/default.nix @@ -1,4 +1,12 @@ -{ lib }: +{ lib, ... }: +/** + Resolve a module from either the clan-core modules or from a flake input. + + The logic is as follows: + - If the moduleSpec.input is null then the module is looked up in the clanCore + - If the moduleSpec.input is set to a string then the module is looked up in the + flake input with that name. +*/ { moduleSpec, flakeInputs, @@ -9,13 +17,9 @@ let Flake doesn't provide input with name '${moduleSpec.input}' Choose one of the following inputs: - - ${ - builtins.concatStringsSep "\n- " ( - lib.attrNames (lib.filterAttrs (_name: input: input ? clan) flakeInputs) - ) - } + - ${builtins.concatStringsSep "\n- " (lib.attrNames (flakeInputs))} - To import a local module from 'clan.modules' remove the 'input' attribute from the module definition + To import any official module from '' remove the 'input' attribute from the module definition Remove the following line from the module definition: ... @@ -47,9 +51,10 @@ let ) "clan-core doesn't provide clan-module with name '${moduleSpec.name}'." } + Remove `module.input` (or set module.input = null) - to use a module' + Set `module.input = "self"` if the module is defined in your own flake. Set `module.input = "" if the module is defined by a flake input called ``. - Unset `module.input` (or set module.input = null) - to use the clan-core module '${moduleSpec.name}' ''); in resolvedModule diff --git a/lib/modules/inventory/distributed-service/test-resolve-module.nix b/lib/resolve-module/test.nix similarity index 92% rename from lib/modules/inventory/distributed-service/test-resolve-module.nix rename to lib/resolve-module/test.nix index 59c72ecb0..a17bf993b 100644 --- a/lib/modules/inventory/distributed-service/test-resolve-module.nix +++ b/lib/resolve-module/test.nix @@ -1,9 +1,10 @@ # Run: nix-unit ./test-resolve-module.nix { lib ? import , + clanLib ? import ../default.nix { inherit lib; }, }: let - resolveModule = import ./resolveModule.nix { inherit lib; }; + resolveModule = clanLib.resolveModule; fromSpec = moduleSpec: