diff --git a/flakeModules/clan.nix b/flakeModules/clan.nix index 76eb2177b..bb9d402da 100644 --- a/flakeModules/clan.nix +++ b/flakeModules/clan.nix @@ -56,15 +56,8 @@ in } ]; }; - apply = - config: - lib.deepSeq (lib.mapAttrs ( - id: check: - if check.ignore || check.assertion then - null - else - throw "clan.checks.${id} failed with message\n${check.message}" - ) config.checks) config; + # Important: !This logic needs to be kept in sync with lib.clan function! + apply = config: clan-core.lib.checkConfig config.checks config; }; # Mapped flake toplevel outputs diff --git a/lib/clan/checkConfig.nix b/lib/clan/checkConfig.nix new file mode 100644 index 000000000..3256f7945 --- /dev/null +++ b/lib/clan/checkConfig.nix @@ -0,0 +1,19 @@ +{ lib, ... }: +/** + Function to assert clan configuration checks. + + Arguments: + + - 'checks' attribute of clan configuration + - Any: the returned configuration (can be anything, is just passed through) +*/ +checks: +lib.deepSeq ( + lib.mapAttrs ( + id: check: + if check.ignore || check.assertion then + null + else + throw "clan.checks.${id} failed with message\n${check.message}" + ) checks +) diff --git a/lib/clan/default.nix b/lib/clan/default.nix index bdd41c88b..c8cd72a6b 100644 --- a/lib/clan/default.nix +++ b/lib/clan/default.nix @@ -33,20 +33,23 @@ let nixpkgs = self.inputs.nixpkgs or clan-core.inputs.nixpkgs; nix-darwin = self.inputs.nix-darwin or clan-core.inputs.nix-darwin; + configuration = ( + lib.evalModules { + class = "clan"; + specialArgs = { + inherit + self + ; + inherit + nixpkgs + nix-darwin + ; + }; + modules = [ + clan-core.modules.clan.default + m + ]; + } + ); in -lib.evalModules { - class = "clan"; - specialArgs = { - inherit - self - ; - inherit - nixpkgs - nix-darwin - ; - }; - modules = [ - clan-core.modules.clan.default - m - ]; -} +clan-core.clanLib.checkConfig configuration.config.checks configuration diff --git a/lib/default.nix b/lib/default.nix index 672b08515..41b21871e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -16,6 +16,8 @@ lib.fix ( */ callLib = file: args: import file ({ inherit lib clanLib; } // args); + checkConfig = clanLib.callLib ./clan/checkConfig.nix { }; + evalService = clanLib.callLib ./evalService.nix { }; # ------------------------------------ # ClanLib functions diff --git a/lib/tests.nix b/lib/tests.nix index 74afb8238..a087038a8 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -212,6 +212,36 @@ in }; }; + test_clan_check_simple_fail = + let + eval = clan { + checks.constFail = { + assertion = false; + message = "This is a constant failure"; + }; + }; + in + { + result = eval; + expr = eval.config; + expectedError.type = "ThrownError"; + expectedError.msg = "This is a constant failure"; + }; + test_clan_check_simple_pass = + let + eval = clan { + checks.constFail = { + assertion = true; + message = "This is a constant success"; + }; + }; + in + { + result = eval; + expr = lib.seq eval.config 42; + expected = 42; + }; + test_get_var_machine = let varsLib = import ./vars.nix { }; diff --git a/modules/clan/default.nix b/modules/clan/default.nix index 8a55c44a0..2c7d59b8e 100644 --- a/modules/clan/default.nix +++ b/modules/clan/default.nix @@ -1,3 +1,14 @@ +/** + Root 'clan' Module + + Defines lib.clan and flake-parts.clan options + and all common logic for the 'clan' module. + + - has Class _class = "clan" + + - _module.args.clan-core: reference to clan-core flake + - _module.args.clanLib: reference to lib.clan function +*/ { clan-core }: { _class = "clan";