gunk-dev/the-valley

nix

source25cfce5168e0main's published tip history
3.0 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
19# A scenario that needs something to happen while a pass is working names
20# it here. Evaluating a check is the middle of a pass — after the requests
21# are read and before anything lands — so the stand-in runs it then.
22if [ -n "${VALLEY_E2E_DURING_PASS:-}" ]; then
23 sh -c "$VALLEY_E2E_DURING_PASS"
24fi
25
26store="${VALLEY_FAKE_STORE:?VALLEY_FAKE_STORE is not set}"
27mkdir -p "$store"
28
29case "${1:-}" in
30 eval)
31 # The flake reference is the one argument naming a check attribute.
32 spec=""
33 for a in "$@"; do case "$a" in *'#checks.'*) spec="$a" ;; esac; done
34 [ -n "$spec" ] || { echo "nix (stub): no #checks. reference in: $*" >&2; exit 2; }
35 tree="${spec%%#*}"
36 rest="${spec#*#checks.}"
37 if [ "$rest" = "${rest%.drvPath}" ]; then
38 # `nix eval --json --apply builtins.attrNames …#checks.<system>`:
39 # every check the tree declares.
40 names=""
41 for f in "$tree"/checks/*.inputs; do
42 [ -e "$f" ] || continue
43 n="$(basename "$f" .inputs)"
44 names="${names:+$names,}\"$n\""
45 done
46 printf '[%s]\n' "$names"
47 exit 0
48 fi
49 attr="${rest#*.}"
50 attr="${attr%.drvPath}"
51 list="$tree/checks/$attr.inputs"
52 [ -f "$list" ] || { echo "nix (stub): $tree declares no check $attr" >&2; exit 1; }
53 drv="$(mktemp "$store/$attr-XXXXXX.drv")"
54 while read -r path; do
55 [ -n "$path" ] || continue
56 while IFS= read -r f; do
57 printf '%s %s\n' "$(sha256sum < "$f" | cut -d' ' -f1)" "${f#"$tree"/}" >> "$drv"
58 done < <(find "$tree/$path" -type f | LC_ALL=C sort)
59 done < "$list"
60 [ -s "$drv" ] || { echo "nix (stub): check $attr reads nothing" >&2; exit 1; }
61 printf '%s' "$drv"
62 ;;
63 build)
64 # `nix build --no-link --print-out-paths <drv>^*`. The output is a
65 # function of the derivation, as a build's output ought to be.
66 drv=""
67 for a in "$@"; do case "$a" in *.drv^*) drv="${a%^*}" ;; esac; done
68 [ -f "$drv" ] || { echo "nix (stub): no such derivation: $drv" >&2; exit 1; }
69 out="$(mktemp -d "$store/out-XXXXXX")"
70 cp "$drv" "$out/result"
71 printf '%s\n' "$out"
72 ;;
73 *)
74 echo "nix (stub): unsupported command: ${1:-}" >&2
75 exit 2
76 ;;
77esac