From 4cef5afde870052ce82fb8a429e57c8125cdf629 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Mon, 21 Apr 2025 16:09:14 +0200 Subject: [PATCH] clanLib: init flakeModules for better testing --- lib/test/default.nix | 3 +++ lib/test/flakeModules.nix | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/test/flakeModules.nix diff --git a/lib/test/default.nix b/lib/test/default.nix index 072a45801..d6d140216 100644 --- a/lib/test/default.nix +++ b/lib/test/default.nix @@ -6,6 +6,9 @@ let ; in { + flakeModules = clanLib.callLib ./flakeModules.nix { }; + + # minifyModule = ./minify.nix; sopsModule = ./sops.nix; # A function that returns an extension to runTest diff --git a/lib/test/flakeModules.nix b/lib/test/flakeModules.nix new file mode 100644 index 000000000..0ab161f3f --- /dev/null +++ b/lib/test/flakeModules.nix @@ -0,0 +1,56 @@ +{ lib, clanLib }: +{ + + /** + This function is used to create a module that can be imported into flake-parts + and used to run eval tests. + + Usage: `lib.modules.importApply makeEvalChecks { module = module-under-test; testArgs = { }; }` + + returns a module to be imported into flake-parts + + Which in turn adds to the flake: + + - legacyPackages..eval-tests-: The attribute set passed to nix-unit. (Exposed for debugging i.e. via nix repl). + - checks..eval-tests-: A derivation that can be built and fails if nix-unit fails + */ + makeEvalChecks = + { + self, + inputs, + testName, + tests, + module, + testArgs ? { }, + }: + let + inputOverrides = builtins.concatStringsSep " " ( + builtins.map (input: " --override-input ${input} ${inputs.${input}}") (builtins.attrNames inputs) + ); + attrName = "eval-tests-${testName}"; + in + { + pkgs, + system, + ... + }: + { + legacyPackages.${attrName} = import tests ( + { + inherit clanLib lib module; + } + // testArgs + ); + checks.${attrName} = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } '' + export HOME="$(realpath .)" + + nix-unit --eval-store "$HOME" \ + --extra-experimental-features flakes \ + --show-trace \ + ${inputOverrides} \ + --flake ${self}#legacyPackages.${system}.${attrName} + touch $out + ''; + + }; +}