diff options
| author | tv <tv@krebsco.de> | 2026-11-14 04:29:02 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-14 06:26:16 +0100 |
| commit | 87e5304d39ddf3a0c052cc13fcfdc2b36c3dad68 (patch) | |
| tree | 5b7ccfcc44527024a2c9a091eb8d84fdc7e5f786 /tests | |
| parent | 41833dc11d75718c279e682593580a46dcec0c73 (diff) | |
tests: pure evaluation
pure-lib evaluates krops' lib from a flake whose inputs are krops'
sources and nixpkgs' lib, checking that mkTarget leaves the user and the
host null, and evaluating sources of each type. pure-deploy, which is
exhaustive, deploys with writeDeploy from a flake whose inputs are krops'
and nixpkgs' sources, evaluating nixpkgs, with a target string.
The inputs are the sources in the store, without .git, copied there
during the tests' evaluation, which the VM shares with the host. The
flakes lock them as tarballs, by their hashes, so Nix uses the store
paths, without fetching them, or reading them, which a path: input
would, all of nixpkgs on every evaluation. The VM can't build what
writeDeploy makes, which needs building populate, so pure-deploy runs
what the host built of the same arguments, evaluated alike, failing if
pure evaluation makes another derivation. nixpkgs is a flake with lib
and legacyPackages, without nixpkgs' configuration and overlays, which
pure evaluation can't read, and the host's evaluation could.
pure-lib is in the smoke test.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/default.nix | 20 | ||||
| -rw-r--r-- | tests/pure-deploy.nix | 22 | ||||
| -rw-r--r-- | tests/pure.nix | 144 | ||||
| -rw-r--r-- | tests/scenarios.nix | 36 |
4 files changed, 216 insertions, 6 deletions
diff --git a/tests/default.nix b/tests/default.nix index 08f95a6..6c3bc59 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -1,10 +1,10 @@ # NixOS VM tests for krops, with the VM machinery and the scenarios of # populate's tests, in ../pkgs/populate/tests: deploy, for writeDeploy's -# arguments to nixos-rebuild, and populate, for pkgs.populate, running +# arguments to nixos-rebuild, populate, for pkgs.populate, running # populate's scenarios that expect populate to refuse its arguments, which # pkgs.populate does during evaluation here, and the scenarios of # populate's smoke test, with target attribute sets as mkTarget returns -# them. Build all with nix-build tests, or one with, e.g., nix-build tests +# them, and pure, for pure evaluation, see ./pure.nix. Build all with nix-build tests, or one with, e.g., nix-build tests # -A deploy. The exhaustive tests add the combinations of options that no # code path tells apart from the ones the others cover; build them with # nix-build tests -A exhaustive. The smoke test runs a few scenarios of @@ -38,8 +38,12 @@ let }; }; + pure = import ./pure.nix { inherit pkgs; }; + # A phase runs pkgs.populate, or writeDeploy with a stand-in for - # nixos-rebuild. + # nixos-rebuild, or, if deploy's pure is set, a pure evaluation: of + # krops' lib, if it's lib, or of writeDeploy, if it's deploy, running + # what that makes. run = { source, backup, force, prune, deploy, targetKind, targetPath, }: let @@ -47,10 +51,13 @@ let inherit source backup force prune; target = targets.${targetKind} targetPath; }; + deployArgs = args // { useNixOutputMonitor = false; } // deploy; in if deploy == null then pkgs.populate args - else pkgs.krops.writeDeploy "deploy" - (args // { useNixOutputMonitor = false; } // deploy); + else if deploy.pure or null == "lib" then pure.lib + else if deploy.pure or null == "deploy" then + pure.deploy (removeAttrs deployArgs [ "pure" ]) + else pkgs.krops.writeDeploy "deploy" deployArgs; builderArgs = { inherit lib run; @@ -70,13 +77,14 @@ let # Names of the scenarios of the smoke test, besides populate's. smokeNames = import (populateTests + "/smoke.nix") ++ [ + "pure-lib" "deploy-local-buildTarget0-etcFlake0-flakefile-targetString0" "deploy-remote-buildTarget0-etcFlake0-flakenone-targetString1" "deploy-sudo-allocateTTY" ]; scenarios = { - inherit (kropsScenarios) deploy; + inherit (kropsScenarios) deploy pure; populate = builtins.filter (scenario: scenario.refusals || builtins.elem scenario.name smokeNames) populateScenarios; diff --git a/tests/pure-deploy.nix b/tests/pure-deploy.nix new file mode 100644 index 0000000..6e34706 --- /dev/null +++ b/tests/pure-deploy.nix @@ -0,0 +1,22 @@ +# writeDeploy of krops' flake, with the arguments args, given krops' and +# nixpkgs' sources: what the flake ./pure.nix makes evaluates purely, and +# what the host builds. nixpkgs is a flake with lib and legacyPackages, +# without nixpkgs' configuration and overlays, which differ between the +# two, and which pure evaluation can't read anyway. +{ krops, nixpkgs, system, args }: + +let + nixpkgs' = { + lib = import "${nixpkgs}/lib"; + legacyPackages.${system} = import nixpkgs { + inherit system; + config = {}; + overlays = []; + }; + }; + kropsFlake = (import "${krops}/flake.nix").outputs { + self = kropsFlake; + nixpkgs = nixpkgs'; + }; +in + kropsFlake.lib.${system}.writeDeploy "deploy" args diff --git a/tests/pure.nix b/tests/pure.nix new file mode 100644 index 0000000..ce540b4 --- /dev/null +++ b/tests/pure.nix @@ -0,0 +1,144 @@ +# Flakes evaluating krops purely, as flakes evaluate by default, and scripts +# running them on the deploying machine. Their inputs are krops' and +# nixpkgs' sources in the store, without .git, locked as tarballs by their +# hashes, so Nix uses the store paths as they are, without fetching or +# reading them, and the VM shares them with the host's store. +{ pkgs }: + +let + inherit (pkgs) lib; + inherit (pkgs.stdenv.hostPlatform) system; + + kropsSrc = builtins.path { + name = "source"; + path = ../.; + filter = path: type: let name = baseNameOf path; in + name != ".git" && name != "target" && !lib.hasPrefix "result" name; + }; + nixpkgsSrc = builtins.path { + name = "source"; + path = pkgs.path; + filter = path: type: baseNameOf path != ".git"; + }; + nixpkgsLibSrc = builtins.path { + name = "source"; + path = pkgs.path + "/lib"; + }; + + # A directory with flake.nix, whose inputs are the store paths of inputs + # by name, not flakes, its lock file, and files, by name. + flake = { inputs, outputs, files ? {} }: pkgs.runCommand "pure-flake" { + nativeBuildInputs = [ pkgs.nix ]; + } '' + mkdir $out + cat > $out/flake.nix <<'EOF' + { + ${lib.concatStrings (lib.mapAttrsToList (name: _: '' + inputs.${name} = { url = "file:///${name}.tar.gz"; flake = false; }; + '') inputs)} + outputs = ${outputs}; + } + EOF + export HOME=$TMPDIR + { + echo '{ "nodes": { "root": { "inputs": {' + ${lib.concatStringsSep "\necho ,\n" (lib.mapAttrsToList (name: _: '' + printf '"%s": "%s"' ${name} ${name} + '') inputs)} + echo '} }' + ${lib.concatStrings (lib.mapAttrsToList (name: path: '' + printf ', "%s": { "flake": false, "locked": { "type": "tarball", "url": "file:///%s.tar.gz", "narHash": "%s" }, "original": { "type": "tarball", "url": "file:///%s.tar.gz" } }\n' \ + ${name} ${name} "$(nix-hash --type sha256 --sri ${path})" ${name} + '') inputs)} + echo '}, "root": "root", "version": 7 }' + } > $out/flake.lock + ${lib.concatStrings (lib.mapAttrsToList (name: file: '' + cp ${file} $out/${name} + '') files)} + ''; + + # A script evaluating what a flake's attribute attr is, purely, printing + # it, and referring to the store paths of the inputs, so the VM has them. + evaluate = name: dir: attr: inputs: pkgs.writers.writeDash name '' + set -efu + # ${toString (lib.attrValues inputs)} + exec nix eval --raw ${lib.escapeShellArg "path:${dir}#${attr}"} + ''; + + # krops' lib, from a flake with nixpkgs' lib only. mkTarget leaves the + # user and the host null, which pure evaluation can't tell, and the + # sources of each type evaluate. + libFlake = let + inputs = { krops = kropsSrc; lib = nixpkgsLibSrc; }; + in flake { + inherit inputs; + outputs = /* nix */ '' + { krops, lib, ... }: let + klib = import "''${krops}/lib/with.nix" { lib = import lib; }; + target = klib.mkTarget ""; + in { + check = + assert target.user == null && target.host == null; + assert target.path == "/var/src"; + builtins.toJSON (klib.evalSource [{ + a.derivation = "{}"; + b.file = "/fixtures/alt"; + c.flake = "/fixtures/top"; + d.flake-input = { url = "/fixtures/top"; name = "dep"; }; + e.git = { url = "git://deployer/gitsrc.git"; ref = "HEAD"; }; + f.pass = { dir = "/fixtures/pass"; name = "x"; }; + g.passage.dir = "/fixtures/passage/store"; + h.pipe = "/fixtures/pipe"; + i.symlink = "b"; + }]); + } + ''; + }; + + # What the host makes of writeDeploy's arguments args, as the flake. + deployOf = args: import ./pure-deploy.nix { + krops = kropsSrc; + nixpkgs = nixpkgsSrc; + inherit system args; + }; + +in { + lib = evaluate "pure-lib" libFlake "check" { inherit kropsSrc nixpkgsLibSrc; }; + + # A script deploying with writeDeploy's arguments args, which have to be + # JSON, from a flake evaluating it purely. Building it in the VM would + # need what building populate needs, so it runs what the host built of + # the same derivation, failing if pure evaluation makes another. + deploy = args: let + args' = builtins.fromJSON (builtins.toJSON args); + expected = deployOf args'; + dir = flake { + inputs = { krops = kropsSrc; nixpkgs = nixpkgsSrc; }; + outputs = /* nix */ '' + { krops, nixpkgs, ... }: { + deploy = import ./pure-deploy.nix { + inherit krops nixpkgs; + system = ${builtins.toJSON system}; + args = builtins.fromJSON (builtins.readFile ./args.json); + }; + } + ''; + files = { + "args.json" = pkgs.writeText "args.json" (builtins.toJSON args'); + "pure-deploy.nix" = ./pure-deploy.nix; + }; + }; + drvPath = evaluate "pure-deploy-drv" dir "deploy.drvPath" { + inherit kropsSrc nixpkgsSrc; + }; + in pkgs.writers.writeDash "pure-deploy" '' + set -efu + drv=$(${drvPath}) + if test "$drv" != ${builtins.unsafeDiscardStringContext expected.drvPath}; then + >&2 printf 'pure evaluation makes %s instead of %s\n' "$drv" \ + ${builtins.unsafeDiscardStringContext expected.drvPath} + exit 1 + fi + exec ${expected} + ''; +} diff --git a/tests/scenarios.nix b/tests/scenarios.nix index 0e90284..0f7a0f8 100644 --- a/tests/scenarios.nix +++ b/tests/scenarios.nix @@ -130,4 +130,40 @@ in }]; })) (if targetKind == "sudo" then [ false ] else [ false true ])) targetKinds; + + # Pure evaluation, as flakes evaluate by default: of krops' lib, and of + # writeDeploy, deploying what that makes, with a target string, which + # makes the same derivation as evaluating on the host, which builds it. + # That evaluates nixpkgs, so it's exhaustive. + pure = [ + (scenario { + name = "pure-lib"; + targetKind = "local"; + phases = [{ + sources.x.file = "/fixtures/alt"; + deploy.pure = "lib"; + }]; + }) + (scenario { + name = "pure-deploy"; + targetKind = "remote"; + exhaustive = true; + prepare = '' + rm -f /tmp/nixos-rebuild.log /etc/nixos/flake.nix + ''; + phases = [{ + sources.x.file = "/fixtures/alt"; + deploy = { + pure = "deploy"; + target = "root@${remoteHost}/var/krops/pure-deploy"; + }; + check = '' + test -e "$T"/x/flake.nix || fail "x/flake.nix missing" + expect_eq NIX_PATH "$(sed -n 2p /tmp/nixos-rebuild.log)" "$T" + expect_eq arguments "$(sed -n 3p /tmp/nixos-rebuild.log)" switch + ''; + }]; + }) + ]; + } |
