| 1 | #!/usr/bin/env bash |
| 2 | # drv-churn: which flake checks an edit gives a new derivation. |
| 3 | # |
| 4 | # A check whose derivation an edit leaves unchanged is a cache hit, and its |
| 5 | # evidence carries to the edited tree under the pure transfer rule. This |
| 6 | # script measures that. It exports a revision's tree the way attest does |
| 7 | # (git archive into a plain directory, so the flake has no rev), applies one |
| 8 | # representative edit at a time to a copy of it, evaluates every check's |
| 9 | # drvPath, and prints which checks changed. Nothing is built, except what |
| 10 | # the checks' evaluation imports from derivations. |
| 11 | # |
| 12 | # nix/tools/drv-churn.sh measure HEAD's tree and definitions |
| 13 | # nix/tools/drv-churn.sh origin/main measure another revision's |
| 14 | # nix/tools/drv-churn.sh "$(git stash create)" |
| 15 | # measure uncommitted, staged work |
| 16 | # |
| 17 | # Options: |
| 18 | # --git evaluate each edit as a commit of a git flake, so self.rev |
| 19 | # changes with every edit, as it does for nix build run in |
| 20 | # a checkout. The default mode is attest's. |
| 21 | # --history RANGE instead of the fixed edits, replay the paths each |
| 22 | # non-merge commit in RANGE touched (for example |
| 23 | # origin/main~20..origin/main): every path the commit |
| 24 | # changed that exists in the measured tree is edited at |
| 25 | # once. A commit's changes to the Nix definitions |
| 26 | # themselves are replayed as comments, so this measures |
| 27 | # the measured revision's definitions, not the commit's. |
| 28 | # --expect FILE compare the fixed edits' results with FILE, a table |
| 29 | # written by --write, and fail on any difference. |
| 30 | # --write FILE write the fixed edits' results to FILE. |
| 31 | # Both use the fixed edits in the default mode. |
| 32 | # --edit LABEL measure only the fixed edit that FILE's table names |
| 33 | # LABEL, such as 'commit only'. Repeat it to measure |
| 34 | # several. Every check is still evaluated. --expect then |
| 35 | # compares only the measured rows, and --write replaces |
| 36 | # only those rows of FILE. |
| 37 | # |
| 38 | # Every edit appends a comment line in the file's own syntax, adds a new |
| 39 | # file, or deletes one. An edit to a file that holds no input of any check |
| 40 | # must change no derivation; an edit to a check's input must change that |
| 41 | # check's. |
| 42 | # |
| 43 | # The first edit, commit only, makes a new commit with the same tree. A |
| 44 | # tree exported as attest exports it has no commit, so in the default mode |
| 45 | # this edit is measured in git mode: the tree committed twice and |
| 46 | # evaluated as each commit. The checks it changes are those that read the |
| 47 | # commit, and a check that boots a machine must not be among them. |
| 48 | set -euo pipefail |
| 49 | |
| 50 | mode=path |
| 51 | history= |
| 52 | expect= |
| 53 | write= |
| 54 | rev=HEAD |
| 55 | only=() |
| 56 | while [ $# -gt 0 ]; do |
| 57 | case "$1" in |
| 58 | --git) mode=git ;; |
| 59 | --history) history="$2"; shift ;; |
| 60 | --expect) expect="$2"; shift ;; |
| 61 | --write) write="$2"; shift ;; |
| 62 | --edit) only+=("$2"); shift ;; |
| 63 | -h | --help) sed -n '2,/^set -euo/p' "$0" | sed '$d; s/^# \{0,1\}//'; exit 0 ;; |
| 64 | -*) echo "drv-churn: unknown option $1" >&2; exit 2 ;; |
| 65 | *) rev="$1" ;; |
| 66 | esac |
| 67 | shift |
| 68 | done |
| 69 | if { [ -n "$history" ] || [ "$mode" = git ]; } && { [ -n "$expect" ] || [ -n "$write" ]; }; then |
| 70 | echo "drv-churn: --expect and --write record the fixed edits in the default mode" >&2 |
| 71 | exit 2 |
| 72 | fi |
| 73 | if [ -n "$history" ] && [ ${#only[@]} -gt 0 ]; then |
| 74 | echo "drv-churn: --edit names a fixed edit, and --history replays commits instead" >&2 |
| 75 | exit 2 |
| 76 | fi |
| 77 | |
| 78 | repo="$(git rev-parse --show-toplevel)" |
| 79 | commit="$(git -C "$repo" rev-parse --verify "$rev^{commit}")" |
| 80 | system="$(nix eval --impure --raw --expr builtins.currentSystem)" |
| 81 | work="$(mktemp -d "${VALLEY_FLOW_SCRATCH:-${KLAUS_SCRATCH:-${TMPDIR:-/tmp}}}/drv-churn.XXXXXX")" |
| 82 | trap 'chmod -R u+w "$work" 2>/dev/null; rm -rf "$work"' EXIT |
| 83 | |
| 84 | base="$work/base" |
| 85 | mkdir "$base" |
| 86 | git -C "$repo" archive "$commit" | tar -x -C "$base" |
| 87 | |
| 88 | # One line per check: its name and its drvPath. |
| 89 | evaluate() { |
| 90 | local ref="$1" |
| 91 | # shellcheck disable=SC2016 # the expression is Nix, not shell |
| 92 | nix eval --raw "$ref#checks.$system" --apply ' |
| 93 | cs: builtins.concatStringsSep "" (map (n: n + " " + cs.${n}.drvPath + "\n") |
| 94 | (builtins.attrNames cs))' |
| 95 | } |
| 96 | |
| 97 | # Commit a tree and print the git flake reference for that commit. The |
| 98 | # commit has a fixed identity and date, so the same edit makes the same |
| 99 | # commit. The date is the epoch in a form git accepts: git refuses @0. |
| 100 | commitFlake() { |
| 101 | local dir="$1" label="$2" epoch=1970-01-01T00:00:00Z |
| 102 | git -C "$dir" add -A |
| 103 | GIT_AUTHOR_DATE=$epoch GIT_COMMITTER_DATE=$epoch git -C "$dir" \ |
| 104 | -c user.name=drv-churn -c user.email=drv-churn@localhost \ |
| 105 | commit --quiet --allow-empty --no-verify -m "$label" |
| 106 | echo "git+file://$dir?rev=$(git -C "$dir" rev-parse HEAD)" |
| 107 | } |
| 108 | |
| 109 | # The flake reference for a tree: a commit of it in git mode, and the |
| 110 | # plain directory otherwise. |
| 111 | flakeOf() { |
| 112 | if [ "$mode" = git ]; then |
| 113 | commitFlake "$@" |
| 114 | else |
| 115 | echo "path:$1" |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | # The comment line that file takes, by its name. |
| 120 | comment() { |
| 121 | case "$1" in |
| 122 | *.go | *.cue) echo "// drv-churn" ;; |
| 123 | *.md | *.txt | *.json) echo "drv-churn" ;; |
| 124 | *) echo "# drv-churn" ;; |
| 125 | esac |
| 126 | } |
| 127 | |
| 128 | # Apply one edit to dir: append to, add or delete path. |
| 129 | apply() { |
| 130 | local dir="$1" op="$2" path="$3" |
| 131 | case "$op" in |
| 132 | append) |
| 133 | [ -f "$dir/$path" ] || { echo "drv-churn: no file $path to edit" >&2; exit 1; } |
| 134 | printf '\n%s\n' "$(comment "$path")" >> "$dir/$path" |
| 135 | ;; |
| 136 | add) |
| 137 | [ ! -e "$dir/$path" ] || { echo "drv-churn: $path already exists" >&2; exit 1; } |
| 138 | mkdir -p "$(dirname "$dir/$path")" |
| 139 | comment "$path" > "$dir/$path" |
| 140 | ;; |
| 141 | delete) |
| 142 | [ -f "$dir/$path" ] || { echo "drv-churn: no file $path to delete" >&2; exit 1; } |
| 143 | rm "$dir/$path" |
| 144 | ;; |
| 145 | commit) ;; |
| 146 | esac |
| 147 | } |
| 148 | |
| 149 | # The fixed edits, as label, operation and path. A Go module's edit is to |
| 150 | # its first source file that is not a test. |
| 151 | edits() { |
| 152 | cat <<'EOF' |
| 153 | commit only commit - |
| 154 | help text append status/help/usage.txt |
| 155 | completions append completions/_valley |
| 156 | doc append docs/architecture.md |
| 157 | doc: README append README.md |
| 158 | doc: module README append sigverify/README.md |
| 159 | doc: archive append archive/design/roadmap.md |
| 160 | doc: research append research/README.md |
| 161 | doc: added add docs/churn-probe.md |
| 162 | schema: attestation append schema/attestation.cue |
| 163 | schema: events append schema/events.cue |
| 164 | schema: identity append schema/identity.cue |
| 165 | schema: node append schema/node.cue |
| 166 | schema: valley append schema/valley.cue |
| 167 | schema: verification append schema/verification.cue |
| 168 | check script append nix/checks/valley-status.sh |
| 169 | check script: shared append nix/checks/fail-nth.sh |
| 170 | check script: added add nix/checks/churn-probe.sh |
| 171 | nix: module append nix/valley-host.nix |
| 172 | nix: packages append nix/packages.nix |
| 173 | nix: knowledge lint append nix/knowledge-lint.py |
| 174 | flake.nix append flake.nix |
| 175 | policy append policy/project/policy.cue |
| 176 | fixture: host append examples/hosts/protected.cue |
| 177 | fixture: smoke policy append examples/policy/smoke/project/policy.cue |
| 178 | fixture: deriver policy append examples/policy/deriver/project.cue |
| 179 | fixture: registry append examples/identity/registry/registry.cue |
| 180 | fixture: graph append examples/graphs/consumer/README.md |
| 181 | fixture: graph deleted delete examples/graphs/consumer/docs/design.md |
| 182 | fixture: integrator e2e append integrator/e2e/run.sh |
| 183 | fixture: plan golden append status/testdata/plan/plan.txt |
| 184 | fixture: attest vectors append attest/conformance/README.md |
| 185 | go test: status append status/status_test.go |
| 186 | EOF |
| 187 | local mod file |
| 188 | while IFS= read -r mod; do |
| 189 | file="$(cd "$base/$mod" && find . -maxdepth 1 -name '*.go' ! -name '*_test.go' | LC_ALL=C sort | head -n1)" |
| 190 | [ -n "$file" ] || continue |
| 191 | printf 'go: %s\tappend\t%s\n' "$mod" "$mod/${file#./}" |
| 192 | done < <(cd "$base" && find . -name go.mod ! -path './archive/*' -printf '%h\n' | sed 's|^\./||' | LC_ALL=C sort) |
| 193 | } |
| 194 | |
| 195 | # Whether to measure the fixed edit labelled $1: every edit, unless |
| 196 | # --edit named some. |
| 197 | selected() { |
| 198 | [ ${#only[@]} -eq 0 ] && return 0 |
| 199 | local l |
| 200 | for l in "${only[@]}"; do |
| 201 | [ "$l" = "$1" ] && return 0 |
| 202 | done |
| 203 | return 1 |
| 204 | } |
| 205 | |
| 206 | # The history's edits: one per non-merge commit in the range, oldest |
| 207 | # first, as label and the paths it touched. |
| 208 | historyEdits() { |
| 209 | local c |
| 210 | git -C "$repo" rev-list --reverse --no-merges "$history" | while IFS= read -r c; do |
| 211 | printf '%s %s\t' "$(git -C "$repo" rev-parse --short "$c")" \ |
| 212 | "$(git -C "$repo" log -1 --format=%s "$c" | cut -c1-60)" |
| 213 | git -C "$repo" diff-tree --no-commit-id --name-only -r "$c" | tr '\n' ' ' |
| 214 | echo |
| 215 | done |
| 216 | } |
| 217 | |
| 218 | # The checks whose drvPath differs between two evaluations. |
| 219 | changed() { |
| 220 | join -j1 <(LC_ALL=C sort "$1") <(LC_ALL=C sort "$2") | |
| 221 | awk '$2 != $3 { print $1 }' | tr '\n' ' ' | sed 's/ $//' |
| 222 | } |
| 223 | |
| 224 | if [ "$mode" = git ]; then |
| 225 | git -C "$base" init --quiet --initial-branch=main |
| 226 | fi |
| 227 | evaluate "$(flakeOf "$base" base)" > "$work/base.drvs" |
| 228 | total="$(wc -l < "$work/base.drvs")" |
| 229 | echo "# drv-churn: $(git -C "$repo" rev-parse --short "$commit") ($mode mode), $total checks" >&2 |
| 230 | |
| 231 | results="$work/results" |
| 232 | : > "$results" |
| 233 | run() { |
| 234 | local label="$1" dir="$work/edit" before="$work/base.drvs" |
| 235 | shift |
| 236 | if [ "$mode" = git ]; then |
| 237 | dir="$base" |
| 238 | git -C "$dir" checkout --quiet --force --detach main |
| 239 | git -C "$dir" clean --quiet -fdx |
| 240 | else |
| 241 | rm -rf "$dir" |
| 242 | cp -a "$base" "$dir" |
| 243 | fi |
| 244 | if [ "$mode" = path ] && [ "${1:-}" = commit ]; then |
| 245 | # The commit-only edit in the default mode: the same tree committed |
| 246 | # twice, each commit evaluated. |
| 247 | git -C "$dir" init --quiet --initial-branch=main |
| 248 | before="$work/commit.drvs" |
| 249 | evaluate "$(commitFlake "$dir" base)" > "$before" |
| 250 | evaluate "$(commitFlake "$dir" "$label")" > "$work/edit.drvs" |
| 251 | else |
| 252 | while [ $# -gt 0 ]; do |
| 253 | apply "$dir" "$1" "$2" |
| 254 | shift 2 |
| 255 | done |
| 256 | evaluate "$(flakeOf "$dir" "$label")" > "$work/edit.drvs" |
| 257 | fi |
| 258 | local names |
| 259 | names="$(changed "$before" "$work/edit.drvs")" |
| 260 | printf '%s\t%s\n' "$label" "$names" >> "$results" |
| 261 | printf '%-34s %3d %s\n' "$label" "$(wc -w <<< "$names")" "$names" |
| 262 | } |
| 263 | |
| 264 | if [ -n "$history" ]; then |
| 265 | sum=0 |
| 266 | count=0 |
| 267 | while IFS=$'\t' read -r label paths; do |
| 268 | args=() |
| 269 | for p in $paths; do |
| 270 | [ -f "$base/$p" ] && args+=(append "$p") |
| 271 | done |
| 272 | run "$label" "${args[@]}" |
| 273 | sum=$((sum + $(tail -n1 "$results" | cut -f2 | wc -w))) |
| 274 | count=$((count + 1)) |
| 275 | done < <(historyEdits) |
| 276 | awk -v s="$sum" -v c="$count" -v t="$total" \ |
| 277 | 'BEGIN { printf "mean: %.1f of %d checks changed per commit, over %d commits\n", s / c, t, c }' |
| 278 | else |
| 279 | while IFS=$'\t' read -r label op path; do |
| 280 | if selected "$label"; then |
| 281 | run "$label" "$op" "$path" |
| 282 | fi |
| 283 | done < <(edits) |
| 284 | for label in "${only[@]}"; do |
| 285 | cut -f1 "$results" | grep -qxF -- "$label" || { |
| 286 | echo "drv-churn: no fixed edit is named $label" >&2 |
| 287 | exit 2 |
| 288 | } |
| 289 | done |
| 290 | fi |
| 291 | |
| 292 | if [ -n "$write" ] && [ ${#only[@]} -eq 0 ]; then |
| 293 | cp "$results" "$write" |
| 294 | elif [ -n "$write" ]; then |
| 295 | # Replace the measured rows only. A row the table lacks is a new edit, |
| 296 | # and the whole table is written again to add it. |
| 297 | while IFS= read -r label; do |
| 298 | cut -f1 "$write" | grep -qxF -- "$label" || { |
| 299 | echo "drv-churn: $write has no row $label; write the whole table" >&2 |
| 300 | exit 2 |
| 301 | } |
| 302 | done < <(cut -f1 "$results") |
| 303 | awk -F'\t' 'NR == FNR { row[$1] = $0; next } { print ($1 in row) ? row[$1] : $0 }' \ |
| 304 | "$results" "$write" > "$work/written" |
| 305 | cp "$work/written" "$write" |
| 306 | fi |
| 307 | if [ -n "$expect" ]; then |
| 308 | # The table's rows for the edits measured: the whole table, unless |
| 309 | # --edit chose some. |
| 310 | if [ ${#only[@]} -eq 0 ]; then |
| 311 | cp "$expect" "$work/expected" |
| 312 | else |
| 313 | awk -F'\t' 'NR == FNR { measured[$1] = 1; next } $1 in measured' \ |
| 314 | "$results" "$expect" > "$work/expected" |
| 315 | fi |
| 316 | diff -u "$work/expected" "$results" >&2 || { |
| 317 | echo "drv-churn: the edits changed other checks than $expect records" >&2 |
| 318 | exit 1 |
| 319 | } |
| 320 | fi |