From b7887228c4866b16b3d5cf7d923739ed9d7ea104 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 9 Sep 2025 20:19:12 -0700 Subject: make a really fancy test harness for nix module evaluation I've never done this before and am really proud of the code; I hope the comments help but feel free to ask questions. As you can see by looking at the diffs to `options.nix`, it did catch several issues that had gotten through up to this point. I'm pretty pleased with that. As before, `nix flake check` is all you need to do to run it. Change-Id: I99a550e92d7b4770e52b6aba763cff2bdc4c9287 --- flake.nix | 46 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) (limited to 'flake.nix') diff --git a/flake.nix b/flake.nix index 8df1039..c101218 100644 --- a/flake.nix +++ b/flake.nix @@ -28,6 +28,9 @@ in { nixosModules.default = { ... }: { imports = [ + # Please note that checks.nix also directly references options.nix, + # so if you add anything else to these imports, also add it in + # checks.nix. ./options.nix ]; }; @@ -47,44 +50,11 @@ }; }); + # The checks are quite verbose, so they're in a separate file. checks = forAllSystems (system: - let pkgs = nixpkgsFor.${system}; - mkNixEvalCheck = name: input: expected: pkgs.stdenv.mkDerivation { - name = "smalltech-nix-test-${name}"; - - src = pkgs.symlinkJoin { - name = "smalltech-nix-test-${name}-src"; - paths = [ - (pkgs.writeTextDir "input" input) - (pkgs.writeTextDir "expected" "${expected}\n") - ]; - }; - - dontUnpack = true; - - nativeBuildInputs = with pkgs; [ diffutils nix ]; - - buildPhase = '' - mkdir nix-store - ${pkgs.nix}/bin/nix \ - --extra-experimental-features nix-command \ - --store dummy:// \ - eval --json --file $src/input > $out - - if ! ${pkgs.diffutils}/bin/diff $src/expected $out; then - echo - echo "This is a nix evaluation test case. The expected eval" - echo "output differed from the actual output. In an ideal" - echo "world, the above diff would help you understand why." - echo - false - fi - ''; - }; - in { - nix-trivial = mkNixEvalCheck "trivial" "1 + 2" "3"; - - rust = self.packages.${system}.default; - }); + let pkgs = nixpkgsFor.${system}; + in import ./checks.nix { + inherit pkgs self system; + }); }; } -- cgit 1.4.1