gunk-dev/the-valley

nix

source79a631efa975main's published tip history
2.7 KiB
1#!/usr/bin/env bash
2# A stand-in for nix, so the end-to-end check can exercise pure checks.
3#
4# The pure transfer rule is closure-digest equality, and a real nix cannot
5# run inside a nix build — there is no daemon in the sandbox. So this stub
6# stands in for the evaluator, and it models exactly the property the rule
7# depends on: a check's input closure is the content of the files it reads.
8#
9# A check named <name> declares what it reads in checks/<name>.inputs, one
10# path per line. Evaluating it writes a derivation listing the SHA-256 of
11# every file beneath those paths; nix-store (the sibling stub) turns that
12# list into the closure attest digests. Change a file the check reads and
13# the closure moves; change anything else and it does not.
14#
15# Everything downstream of the evaluation is the real thing: attest builds
16# the manifest and the digest, and the integrator compares them.
17set -euo pipefail
18
19store="${VALLEY_FAKE_STORE:?VALLEY_FAKE_STORE is not set}"
20mkdir -p "$store"
21
22case "${1:-}" in
23 eval)
24 # The flake reference is the one argument naming a check attribute.
25 spec=""
26 for a in "$@"; do case "$a" in *'#checks.'*) spec="$a" ;; esac; done
27 [ -n "$spec" ] || { echo "nix (stub): no #checks. reference in: $*" >&2; exit 2; }
28 tree="${spec%%#*}"
29 rest="${spec#*#checks.}"
30 if [ "$rest" = "${rest%.drvPath}" ]; then
31 # `nix eval --json --apply builtins.attrNames …#checks.<system>`:
32 # every check the tree declares.
33 names=""
34 for f in "$tree"/checks/*.inputs; do
35 [ -e "$f" ] || continue
36 n="$(basename "$f" .inputs)"
37 names="${names:+$names,}\"$n\""
38 done
39 printf '[%s]\n' "$names"
40 exit 0
41 fi
42 attr="${rest#*.}"
43 attr="${attr%.drvPath}"
44 list="$tree/checks/$attr.inputs"
45 [ -f "$list" ] || { echo "nix (stub): $tree declares no check $attr" >&2; exit 1; }
46 drv="$(mktemp "$store/$attr-XXXXXX.drv")"
47 while read -r path; do
48 [ -n "$path" ] || continue
49 while IFS= read -r f; do
50 printf '%s %s\n' "$(sha256sum < "$f" | cut -d' ' -f1)" "${f#"$tree"/}" >> "$drv"
51 done < <(find "$tree/$path" -type f | LC_ALL=C sort)
52 done < "$list"
53 [ -s "$drv" ] || { echo "nix (stub): check $attr reads nothing" >&2; exit 1; }
54 printf '%s' "$drv"
55 ;;
56 build)
57 # `nix build --no-link --print-out-paths <drv>^*`. The output is a
58 # function of the derivation, as a build's output ought to be.
59 drv=""
60 for a in "$@"; do case "$a" in *.drv^*) drv="${a%^*}" ;; esac; done
61 [ -f "$drv" ] || { echo "nix (stub): no such derivation: $drv" >&2; exit 1; }
62 out="$(mktemp -d "$store/out-XXXXXX")"
63 cp "$drv" "$out/result"
64 printf '%s\n' "$out"
65 ;;
66 *)
67 echo "nix (stub): unsupported command: ${1:-}" >&2
68 exit 2
69 ;;
70esac