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