gunk-dev/the-valley

nix

source9a63506a870fmain's published tip history
5.4 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
29# derive <tree> <attr>: evaluate one check, writing its derivation into the
30# store and printing the derivation's path. A check that declares a
31# failure (checks/<name>.fails) or an output it shares with other checks
32# (checks/<name>.output, as fixed-output derivations can) carries that
33# beside its derivation, for the build.
34derive() {
35 local tree="$1" attr="$2" list drv path f
36 list="$tree/checks/$attr.inputs"
37 [ -f "$list" ] || { echo "nix (stub): $tree declares no check $attr" >&2; return 1; }
38 drv="$(mktemp "$store/$attr-XXXXXX.drv")"
39 while read -r path; do
40 [ -n "$path" ] || continue
41 while IFS= read -r f; do
42 printf '%s %s\n' "$(sha256sum < "$f" | cut -d' ' -f1)" "${f#"$tree"/}" >> "$drv"
43 done < <(find "$tree/$path" -type f | LC_ALL=C sort)
44 done < "$list"
45 [ -s "$drv" ] || { echo "nix (stub): check $attr reads nothing" >&2; return 1; }
46 if [ -f "$tree/checks/$attr.fails" ]; then cp "$tree/checks/$attr.fails" "$drv.fails"; fi
47 if [ -f "$tree/checks/$attr.output" ]; then
48 printf '%s/%s.out\n' "$store" "$(cat "$tree/checks/$attr.output")" > "$drv.output"
49 fi
50 printf '%s' "$drv"
51}
52
53case "${1:-}" in
54 eval)
55 # The flake reference is the one argument naming a check attribute,
56 # and --apply, when given, is followed by the expression applied.
57 spec="" apply="" prev=""
58 for a in "$@"; do
59 case "$a" in *'#checks.'*) spec="$a" ;; esac
60 [ "$prev" != --apply ] || apply="$a"
61 prev="$a"
62 done
63 [ -n "$spec" ] || { echo "nix (stub): no #checks. reference in: $*" >&2; exit 2; }
64 tree="${spec%%#*}"
65 rest="${spec#*#checks.}"
66 if [ "$rest" = "${rest%.drvPath}" ] && [ "$apply" = builtins.attrNames ]; then
67 # `nix eval --json --apply builtins.attrNames …#checks.<system>`:
68 # every check the tree declares.
69 names=""
70 for f in "$tree"/checks/*.inputs; do
71 [ -e "$f" ] || continue
72 n="$(basename "$f" .inputs)"
73 names="${names:+$names,}\"$n\""
74 done
75 printf '[%s]\n' "$names"
76 exit 0
77 fi
78 if [ "$rest" = "${rest%.drvPath}" ]; then
79 # attest's shared evaluation, `nix eval --json --apply <expression>
80 # …#checks.<system>`: the derivation of every check the expression
81 # names, each as a quoted string, and null for a check that does not
82 # evaluate.
83 out=""
84 while IFS= read -r quoted; do
85 attr="${quoted//\"/}"
86 if drv="$(derive "$tree" "$attr" 2> /dev/null)"; then
87 value="\"$drv\""
88 else
89 value=null
90 fi
91 out="${out:+$out,}\"$attr\":$value"
92 done < <(grep -o '"[^"]*"' <<< "$apply")
93 printf '{%s}\n' "$out"
94 exit 0
95 fi
96 attr="${rest#*.}"
97 derive "$tree" "${attr%.drvPath}"
98 ;;
99 build)
100 # `nix build --no-link --print-out-paths <drv>^*`: every derivation
101 # named, each to its one output, where the nix-store stub finds it. The
102 # output is a function of the derivation, as a build's output ought to
103 # be, or, when checks share it, of its name. A check that declares a
104 # failure fails with what it declares, on standard error, and builds
105 # nothing.
106 status=0
107 for a in "$@"; do
108 case "$a" in
109 *.drv^*)
110 drv="${a%^*}"
111 if [ ! -f "$drv" ]; then
112 echo "nix (stub): no such derivation: $drv" >&2
113 status=1
114 continue
115 fi
116 if [ -f "$drv.fails" ]; then
117 cat "$drv.fails" >&2
118 status=1
119 continue
120 fi
121 # Written beside the store and moved into place, so two builds
122 # of one output at once leave it whole, as nix's locks do.
123 if [ -f "$drv.output" ]; then
124 out="$(cat "$drv.output")"
125 mkdir -p "$out"
126 tmp="$(mktemp "$store/.result-XXXXXX")"
127 basename "$out" .out > "$tmp"
128 else
129 out="${drv%.drv}.out"
130 mkdir -p "$out"
131 tmp="$(mktemp "$store/.result-XXXXXX")"
132 cp "$drv" "$tmp"
133 fi
134 mv -f "$tmp" "$out/result"
135 printf '%s\n' "$out"
136 ;;
137 esac
138 done
139 exit "$status"
140 ;;
141 *)
142 echo "nix (stub): unsupported command: ${1:-}" >&2
143 exit 2
144 ;;
145esac