gunk-dev/the-valley

nix

sourcedd3d6fba36ddmain's published tip history
3.3 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 --keep-going <drv>^*...`: every derivation
65 # named, each to the one output beside it, where the nix-store stub
66 # finds it. The output is a function of the derivation, as a build's
67 # output ought to be.
68 status=0
69 for a in "$@"; do
70 case "$a" in
71 *.drv^*)
72 drv="${a%^*}"
73 if [ ! -f "$drv" ]; then
74 echo "nix (stub): no such derivation: $drv" >&2
75 status=1
76 continue
77 fi
78 out="${drv%.drv}.out"
79 mkdir -p "$out"
80 cp "$drv" "$out/result"
81 printf '%s\n' "$out"
82 ;;
83 esac
84 done
85 exit "$status"
86 ;;
87 *)
88 echo "nix (stub): unsupported command: ${1:-}" >&2
89 exit 2
90 ;;
91esac