diff options
| author | tv <tv@krebsco.de> | 2026-11-13 16:23:34 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-13 16:23:34 +0100 |
| commit | 50a2b8f9628bacdc8f13848e98b909e7be44c640 (patch) | |
| tree | 3ecc002e39c7cd7147f4a49d7c3e874fa4b344dd /pkgs/populate/checks.nix | |
| parent | 4afd7d2793e85f260a2a3c20a423f0d1a4844ea7 (diff) | |
populate: add a Rust implementation, selectable for the tests
pkgs.populate takes implementation: "shell", the default, generates a
script from populate's arguments, and "rust" passes them as JSON to the
program in pkgs/populate, built from the crate there by program.nix.
The program checks the spec as checks.nix does during evaluation, with the
same messages, and reports that delivering sources isn't implemented yet.
rustProgram replaces the program, e.g. with one built some other way.
checks.nix holds the checks of populate's arguments during evaluation.
Both implementations check the names of sources and the target path with
it, and the Rust one's wrapper also checks drawing inputs, which the shell
implementation checks where it draws them.
nix-build tests --argstr implementation rust runs the scenarios with the
Rust implementation. They expect it to refuse copying flakes to targets
using sudo when it runs, not during evaluation, because only then it
decides whether the target is local.
Diffstat (limited to 'pkgs/populate/checks.nix')
| -rw-r--r-- | pkgs/populate/checks.nix | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/pkgs/populate/checks.nix b/pkgs/populate/checks.nix new file mode 100644 index 0000000..7eab15b --- /dev/null +++ b/pkgs/populate/checks.nix @@ -0,0 +1,52 @@ +# Checks of populate's arguments during evaluation. Each returns true, or +# throws an error naming what's wrong. +with import ../../lib; + +{ + # Populate replaces and removes what's at the target paths of sources, so + # it refuses names that lead out of the target directory, to the directory + # itself, or to its own state. The remaining names are portable file + # names, which mean themselves to shells, find, and in URLs, and which the + # names of the scripts populating the sources may contain. + checkSourceName = name: + if elem name [ "." ".." ".populate" ] || + builtins.match "[0-9A-Za-z._][0-9A-Za-z._-]*" name == null then + throw "krops: source name ${toJSON name} can't be used: it must consist of letters, digits, and the characters ._-, not start with -, and be neither ., .., nor .populate" + else + true; + + # Populate removes what it finds in the target directory, so it refuses + # paths that aren't absolute, that are the root directory, or that the + # target would resolve to another directory than the one they name. + checkTargetPath = path: let + components = filter (x: x != "" && x != ".") (splitString "/" path); + in + if !isString path || substring 0 1 path != "/" then + throw "krops: target path ${toJSON path} can't be used: it must be absolute" + else if components == [] then + throw "krops: target path ${toJSON path} can't be used: it must not be the root directory" + else if elem ".." components || + builtins.match "[^[:cntrl:]]+" path == null then + throw "krops: target path ${toJSON path} can't be used: it must not contain .. or control characters" + else + true; + + # Inputs get drawn from other sources, so they must exist, and drawing + # must not go round in circles. + checkInputs = source: let + drawnFrom = name: map (x: x.source) (flakeFollows source.${name}); + check = name: from: + if from == name then + throw "krops: source ${name} draws inputs from itself" + else if !(source ? ${from}) then + throw "krops: source ${name} draws inputs from missing source ${from}" + else + true; + sorted = toposort (a: b: elem a (drawnFrom b)) (attrNames source); + in + all (name: all (check name) (drawnFrom name)) (attrNames source) && + (if sorted ? cycle then + throw "krops: sources draw inputs from each other: ${toString sorted.cycle}" + else + true); +} |
