gunk-dev/the-valley

cli.nix

source2d226693a6c1main's published tip history
24.9 KiB
1# bin/valley: that it stays a lint-clean script, what its review verb
2# anchors a note to, what its deriver makes of a policy and a diff, that
3# the-valley's own policy composes and names only checks that exist, what
4# its review verb publishes when it files an integration request, what its
5# Go verbs' unit tests and the rebase engine's hold, what its ready and withdraw verbs write to a
6# change's record through the real push hook, what its knot verbs write to
7# the plan graph, what its plan verb shows of that graph, what its
8# self-update verb installs and what the home-manager module runs it with,
9# and that its package evaluates for aarch64-darwin.
10{
11 pkgs,
12 lib,
13 self,
14 system,
15 packages,
16 hosts,
17 nixStandIns,
18 ...
19}:
20let
21 # A stand-in for git, sort or cue that fails one chosen call; see
22 # fail-nth.sh.
23 failNth = pkgs.writeShellScript "fail-nth" (builtins.readFile ./fail-nth.sh);
24
25 # The user units the home-manager module renders for a home with
26 # self-update on. The module is evaluated over stand-ins for the
27 # home-manager options it sets, because this flake has no home-manager.
28 selfUpdateUnits =
29 (lib.evalModules {
30 modules = [
31 self.homeManagerModules.valley
32 {
33 options = {
34 systemd.user.services = lib.mkOption { type = lib.types.attrsOf lib.types.anything; };
35 systemd.user.timers = lib.mkOption { type = lib.types.attrsOf lib.types.anything; };
36 home.homeDirectory = lib.mkOption { type = lib.types.str; };
37 home.username = lib.mkOption { type = lib.types.str; };
38 };
39 config = {
40 _module.args.pkgs = pkgs;
41 home.homeDirectory = "/home/someone";
42 home.username = "someone";
43 programs.valley.selfUpdate = {
44 enable = true;
45 repo = "git@valley-host:the-valley";
46 };
47 };
48 }
49 ];
50 }).config.systemd.user;
51
52 # Throwaway keys for valley-request's reuse scenarios, made once at build
53 # time: a check worker's and the operator's, which sign evidence, and the
54 # integrator's. They hold no authority anywhere. The protected host's
55 # hook, rendered with these as its known signers, admits evidence the
56 # worker and the operator sign.
57 reuseKeys =
58 pkgs.runCommand "valley-request-keys"
59 {
60 nativeBuildInputs = [
61 pkgs.openssh
62 packages.attest
63 ];
64 }
65 ''
66 mkdir $out
67 for who in worker operator integrator; do
68 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who"
69 done
70 {
71 attest key --key "$out/worker" --name worker.valley.invalid/attestations
72 attest key --key "$out/operator" --name operator.valley.invalid/attestations
73 } > "$out/hook_signers"
74 '';
75 reuseHost = hosts.protectedHost.extendModules {
76 modules = [
77 { services.valley.integrator.knownSignersFile = lib.mkForce "${reuseKeys}/hook_signers"; }
78 ];
79 };
80in
81{
82 # The CLI must stay a lint-clean script whose help verb answers
83 # without a repo or a remote — the cheap end of its eviction clause
84 # (dcr-74c3158); anything needing more graduates instead. The
85 # completions are held to the same bar, and the installed package
86 # must carry them at the auto-linked paths.
87 valley-cli = pkgs.runCommand "valley-cli" {
88 nativeBuildInputs = [
89 pkgs.git
90 pkgs.shellcheck
91 pkgs.zsh
92 packages.valley
93 ];
94 cliScript = ../../bin/valley;
95 bashCompletion = ../../completions/valley.bash;
96 zshCompletion = ../../completions/_valley;
97 valley = packages.valley;
98 } (builtins.readFile ./valley-cli.sh);
99
100 # review's notes: the anchors are the whole claim, so they are
101 # pinned here rather than left to the reader of bin/valley. The
102 # session is real — a pty, real less, a pager filter that
103 # restructures the diff, and an $EDITOR that inserts lines while
104 # the pager is open — because every property under test is a
105 # property of that arrangement: that v is handed the raw buffer
106 # and not the rendering, that an inserted line becomes a note,
107 # and that the file and line it names are the ones it sat on. It goes
108 # on to review a stack layer by layer, and a stale stack: approving its
109 # top refused, its bottom approved, and review's [b]ase before the first
110 # page, after which the pages and the request are at the rebased heads,
111 # and the layer approved before the rebase is printed as a clean restack
112 # instead of paged. Last, a note written beside an edited line of a
113 # version paged as changed since its approval keeps its file and line.
114 review-notes = pkgs.runCommand "valley-review-notes" {
115 nativeBuildInputs = [
116 pkgs.git
117 pkgs.less
118 pkgs.util-linux # script(1), for the pty less insists on
119 packages.valley-script
120 ];
121 # The reviewer, as a script: four insertions and one edit of a
122 # line that was already there. The edit is deliberately far
123 # from every insertion, since -U0 would otherwise merge them
124 # into one hunk and the note would be dropped with it.
125 reviewer = pkgs.writeShellScript "valley-review-notes-editor" (
126 builtins.readFile ./review-notes-editor.sh
127 );
128 } (builtins.readFile ./review-notes.sh);
129
130 # The verification-policy deriver (dcr-f41f718). Composing the two
131 # layers is CUE's job and is pinned by cue-vet; matching paths is
132 # the script's own, and a path matcher is wrong exactly at the glob
133 # boundaries — `*` inside one segment against `**` across segments,
134 # `**` matching no segment at all — and at the sides of a diff a
135 # naive reader drops: a rename's old path and a deletion. Each is
136 # pinned by driving the real script over a scratch repository and
137 # the scratch policy in examples/policy/deriver/, whose floor is
138 # one glob boundary per class. A check's cost is pinned the same way:
139 # marking a check slow moves it between the summary lines and the
140 # --cost lists, and changes nothing required. So is the carry mode:
141 # overlapping classes, and paths in different classes, report the
142 # strictest of their modes, and an unclassified path is same-paths. A
143 # mode written by refining `policy` counts. A pattern holding a newline
144 # cannot forge a line of the render: the schema refuses it, the render
145 # refuses it under a schema that admits it, and the deriver refuses the
146 # second line under a render that admits it too.
147 #
148 # And the deriver fails closed: failNth stands in for git, sort and
149 # cue, and fails each call the deriver makes in turn. Every failure
150 # must end the derivation with nothing on stdout, because a failure
151 # read as an empty list is a change that requires nothing. cliScript is
152 # bin/valley itself, because the package's wrapper puts its own git
153 # ahead of anything on PATH.
154 policy-deriver = pkgs.runCommand "valley-policy-deriver" {
155 nativeBuildInputs = [
156 pkgs.git
157 pkgs.cue
158 packages.valley-script
159 ];
160 fixtures = ../../examples/policy/deriver;
161 schema = ../../schema/verification.cue;
162 cliScript = ../../bin/valley;
163 inherit failNth;
164 } (builtins.readFile ./policy-deriver.sh);
165
166 # the-valley's own project layer (policy/project), which is what gates
167 # every change to this repository. A layer that stopped composing would
168 # stop every derivation once it landed, the derivation of its own fix
169 # included, so it is held here before it lands. The floor it is composed
170 # with is the example one: the owning valley's is not in this tree. The
171 # check also holds every check the layer names to being an attribute of
172 # this flake's checks, and holds a docs-only change to requiring
173 # prose-format alone. And it holds a check's own files to requiring
174 # that check: every file in this directory named after a check, and every
175 # other script here, read from the directory itself so a new one is
176 # held as soon as it is added. It holds each scope to its carry mode,
177 # the scopes the carry classes classify to every check they required
178 # before, and the restacker's cap to the default.
179 own-policy = pkgs.runCommand "valley-own-policy" {
180 nativeBuildInputs = [
181 pkgs.git
182 pkgs.cue
183 packages.valley-script
184 ];
185 schema = ../../schema/verification.cue;
186 project = ../../policy/project;
187 floor = ../../examples/policy/instance/floor-format.cue;
188 template = ../../examples/policy/instance/template-docs.cue;
189 checkNames = builtins.attrNames self.checks.${system};
190 checkFiles = builtins.attrNames (builtins.readDir ./.);
191 } (builtins.readFile ./own-policy.sh);
192
193 # review's [a]pprove and the approve and rebase verbs, driven end to end
194 # over scratch repositories: a branch reviewed, the request filed, and
195 # what the receiving side then holds. The claim is about sequencing, so
196 # it is only observable from there — the bare repo logs every push it
197 # receives, and the check reads that log to see that ONE of them carried
198 # the evidence and the request together. A change whose version names no
199 # branch is approved at its recorded head the same way. Every check the scratch policy
200 # declares uses the command runner, for the reason attest-e2e gives: a
201 # nix build has no daemon. The packaged CLI carries its own schema, so
202 # the scratch projects that stand for projects other than the-valley
203 # carry no schema/. cliScript is bin/valley itself, for its other
204 # shipping mode: run straight from a the-valley checkout, it reads the
205 # schema there.
206 #
207 # Last, evidence a check worker already published, over a second origin
208 # that runs the protected host's rendered hook and a project whose two
209 # checks use the nix runner, through integrator-e2e's stand-in for nix.
210 # Its owning valley publishes the signers clients verify with. Evidence
211 # for every required check is reused: approve runs no nix, needs no
212 # signing key, and pushes the request alone, and approving that head
213 # again pushes nothing. Evidence for one check is reused and the other
214 # runs: the worker's note for it names another flake attribute, so it is
215 # not reused. The operator's evidence goes in one push with the request,
216 # and the real integrator lands it on the two signers' evidence. Origin
217 # holding the operator's own ref for a tree, without the check still
218 # missing, is refused before anything runs (bd-7b7d58f). A listing or a
219 # fetch of origin's evidence that fails is refused, and nothing runs or
220 # is published. A request filed while a fully covered approval is
221 # prepared wins the lease. Then evidence approve must not reuse, each
222 # judged by approve and by the real integrator from the same refs: a
223 # worker the host has revoked and a local known-signers file still
224 # trusts, a local file that trusts less than the host, a failure and an
225 # observation that sit before a pass in ref order, a note without its
226 # last newline, a signer nobody knows, a note about another tree, an
227 # expired observation, and a note for another flake attribute. Notes the
228 # hook would refuse are planted past it. Then the host publishes a
229 # signer projects file, and each integrator pass is given it: a note
230 # whose only signer counts in another project is not reused, the
231 # integrator does not accept it, and approve builds that check; the same
232 # note countersigned by the integrator is not reused either; a published
233 # list that does not read, or is no file, reuses nothing; and a note
234 # from a signer restricted to this project is reused and lands.
235 #
236 # Then the check worker, as packaged, over the same origin and ready
237 # changes, with its key restricted to the project: it builds a change's nix checks and publishes the evidence
238 # before any request exists, and approve then reuses all of it with no
239 # key and the integrator lands it. Two passes at once take turns, and the
240 # second builds nothing. A failed batch waits 15 minutes, then twice as
241 # long, by VALLEY_NOW, and its record is gone once it passes. A push whose
242 # reply is lost, landed or not, is never built again: the next pass finds
243 # it on origin, or pushes the ref the first pass stored. A head
244 # superseded while it is built still has its evidence published, and the
245 # new version is built next. Origin holding the worker's own ref without a
246 # required check is reported and nothing is built, and approve builds the
247 # rest. An origin the pass cannot read stops it before it builds. On the
248 # real clock, a failed batch's wait runs from when the batch ended. A
249 # failed push waits a minute, then two, by the object it publishes, so
250 # two changes holding one tree try one push. A pass over another project
251 # keeps this project's waits. A pass killed mid-batch leaves attest
252 # holding the lock, so the next pass waits for it, then publishes what
253 # it stored. A tag in the cache clone named like the worker's own ref is
254 # not taken for evidence an earlier pass stored. A cache clone whose
255 # making was interrupted by git dying with its config.lock held is made
256 # again, and a half-made clone with a stale config.lock in the cache's
257 # place is set aside and made again.
258 #
259 # Last, approve reads each ref by its exact name. A branch on origin
260 # named like a missing request stops the push, and once it is deleted
261 # the request is filed. A tag named like the remote-tracking ref of a
262 # branch origin lacks, or of a main origin lacks, and an owning valley
263 # with no main each refuse the approval, and nothing is filed.
264 valley-request = pkgs.runCommand "valley-request" {
265 nativeBuildInputs = [
266 pkgs.git
267 pkgs.cue
268 pkgs.jq # the timing log's lines
269 pkgs.openssh
270 packages.attest
271 packages.valley-script
272 packages.valley-go
273 packages.valley-worker
274 packages.integrator
275 ];
276 schemaFile = ../../schema/verification.cue;
277 cliScript = ../../bin/valley;
278 inherit failNth reuseKeys;
279 reuseInitScript = reuseHost.config.systemd.services.valley-init.script;
280 passAsFile = [ "reuseInitScript" ];
281 integratorE2e = nixStandIns;
282 attestSchema = ../../schema/attestation.cue;
283 eventSchema = ../../schema/events.cue;
284 } (builtins.readFile ./valley-request.sh);
285
286 # The rebase engine's tests (rebase/), run with go vet: a chain, a
287 # shared lower layer, a conflict, an empty layer, a lease lost to a moved
288 # branch, a record append refused after the branches moved, a branch
289 # push and a record push that origin takes and never acknowledges, a
290 # cancellation before the push, and a cancellation while a hook or a
291 # filter blocks, each over real repositories and real pushes. A restack
292 # append kept and restored still reads the branch
293 # before its push, and is refused once the owner has moved it; a kept
294 # write that is malformed does not restore. valley-ready and
295 # integrator-e2e drive it through valley rebase.
296 rebase-unit = packages.rebase.unit;
297
298 # The Go verbs' tests (status/), in valley-go's unit build: the
299 # outcome records status accepts, the names its JSON refuses, its shell
300 # quoting held to the bash it replaced, a rebase ended by a SIGTERM
301 # while a hook blocks it, and the valley-checks/v1 document checks prints
302 # over a real repository, a path that is not UTF-8 in base64. valley-status
303 # and valley-ready drive the verbs through the CLI over real repositories.
304 valley-go-unit = packages.valley-go.unit;
305
306 # Read origin's request/outcome refs and distinguish a pending request,
307 # a recorded decision and a landing, without moving the user's checkout.
308 # Each state is read again through --json, and jq holds the object to
309 # the valley-status/v1 contract. A change's restack report is shown as
310 # current, and as historical once a ref it observed has changed: its
311 # branch, main, its record, a branch it saw missing, or a request that
312 # held it back. Status, changes and pending all show it.
313 valley-status = pkgs.runCommand "valley-status" {
314 nativeBuildInputs = [
315 pkgs.git
316 pkgs.jq
317 packages.valley-script
318 ];
319 } (builtins.readFile ./valley-status.sh);
320
321 # ready, unready and withdraw, through real pushes to a bare repo wired
322 # with the rendered pre-receive hook, as each principal: a change created
323 # at v1, a second version, the same head refused, a withdrawal, revived
324 # at the same version and then at a new one, a principal that neither
325 # owns the change nor holds the request grant refused by the hook, and
326 # the operator, who holds it, let through. Then each source of a change's ID — the jj
327 # header, the Change-Id trailer, a generated ID, --stamp — a header and a
328 # trailer that disagree, and a change whose commits hold a merge. Last, a
329 # change named by its handle is approved, reviewed and rebased at the head
330 # its record names: a record pushed with its branch moved elsewhere in the
331 # same push is refused, the branch at the recorded head is filed at that
332 # head, and a later version whose branch is elsewhere is refused. A
333 # version that names no branch is approved at its head, and refused by
334 # review and rebase, which act on a branch. A name no record is on that
335 # is no branch name is refused. Then a
336 # draft: unready, ready again at the same head and at a new one, listed
337 # apart by pending, refused by approve and rebase, and shown by review
338 # as a draft, with neither [a]pprove, [b]ase nor [r]eject offered,
339 # whether it is named by its handle or its branch, and a request pending
340 # under either name warned of by unready and withdraw, a request for an
341 # earlier version's branch too. review shows a withdrawn change the same
342 # way. A review left open while another principal unreadies the change,
343 # or readies a new version, refuses to act on it. A branch no record is
344 # on is acted on as a branch, and one two records are on is refused.
345 # Last, stacks rebased whole: every layer moved and each change given a
346 # version, by ancestry and by trees; a
347 # conflict in the middle layer; each refusal before the rebase; a branch
348 # moved, and a plain branch readied, after the rebase read the stack; a
349 # change unreadied between the branch push and its record write, and the
350 # command the manifest gives to finish it; and a second stack left on an
351 # old head. review of a stack whose middle layer is a draft, then
352 # withdrawn, offers [a]pprove under it and neither at it nor above it.
353 # Then review set against a change's approved version: a clean
354 # restack after [b]ase, with the landing between the bases listed and no
355 # diff paged; a later version with an edited file, paged with the
356 # range-diff and its own diff of that file; a version whose message
357 # alone changed and one whose author alone did, each flagged; a change
358 # with no approved version, paged in full; and a stack whose lower layer
359 # is set against its own approval while its top, which has none, is
360 # paged. The hook is followed from the protected host's rendered init
361 # script, so the rules the record meets are the ones a host applies.
362 valley-ready = pkgs.runCommand "valley-ready" {
363 nativeBuildInputs = [
364 pkgs.git
365 pkgs.jq
366 pkgs.util-linux # script(1), for approve's confirmation on a terminal
367 packages.valley-script
368 ];
369 initScript = hosts.protectedHost.config.systemd.services.valley-init.script;
370 passAsFile = [ "initScript" ];
371 } (builtins.readFile ./valley-ready.sh);
372
373 # valley changes and valley pending over the projects of one test host,
374 # made by the integrator host's rendered valley-init, so discovery reads
375 # the project list a host publishes and every record goes through the
376 # hook it installs. Two repositories with ready, draft and withdrawn
377 # records; handles unique across them; one listed repository that cannot
378 # be read; a change ID recorded in several, with qualified handles; --repo;
379 # a version landed by fast-forward, one landed as a new commit with a
380 # transfer statement the integrator signed, one consumed with no
381 # evidence, one whose statement another key signed, one with a landed
382 # version's tree on another parent, a landing's tree replayed on a later
383 # main, and heads below a merge that dropped them, through either
384 # parent; a project named origin that shares a change ID; a
385 # policy/valley git would read as an option or
386 # as ext::; includes as the nearest ancestor only; pending's human shape,
387 # its drafts array and --checks; and a branch name read as a branch again
388 # once its change has landed, before the branch moves and after.
389 valley-changes = pkgs.runCommand "valley-changes" {
390 nativeBuildInputs = [
391 pkgs.git
392 pkgs.jq
393 pkgs.openssh
394 packages.valley-script
395 packages.valley-go
396 packages.attest
397 ];
398 initScript = hosts.integratorHost.config.systemd.services.valley-init.script;
399 passAsFile = [ "initScript" ];
400 # The floor --checks derives under.
401 floor = ../../examples/policy/instance;
402 } (builtins.readFile ./valley-changes.sh);
403
404 # valley knot against local bare repositories that run no hook, as any
405 # git remote would: every verb, the ref's shape after each write, a
406 # comment's edits read back through log and show --at, another
407 # project named by --project and by a qualified id, and then each way a
408 # write may fail to land the first time. A stand-in for git, first on
409 # PATH, makes another writer land between a read and its push, drops the
410 # connection after a push lands and after one that did not, takes a
411 # claim, changes a field or edits a comment first, and releases and
412 # retakes a claim an unclaim read. Each write must then land exactly
413 # once, or be reported as a conflict. Two writers also run at once. cliScript
414 # is bin/valley itself, because the package's wrapper puts its own git
415 # ahead of the stand-in.
416 valley-knot = pkgs.runCommand "valley-knot" {
417 nativeBuildInputs = [
418 pkgs.git
419 pkgs.jq
420 packages.valley-go
421 ];
422 cliScript = ../../bin/valley;
423 } (builtins.readFile ./valley-knot.sh);
424
425 # valley plan against local bare repositories that run no hook: a graph
426 # built with the real knot verbs, each at a time VALLEY_NOW fixes, so
427 # every date and age it shows is the same on every run. The text, off a
428 # terminal, and the valley-plan/v1 document are compared with golden
429 # files kept beside the code that prints them (status/testdata/plan/).
430 # Then a project with no graph, --all over the projects the instance
431 # publishes with one that cannot be read, and the operator named by
432 # --owner and by no one. cliScript is bin/valley itself, as for
433 # valley-knot.
434 valley-plan = pkgs.runCommand "valley-plan" {
435 nativeBuildInputs = [
436 pkgs.git
437 pkgs.jq
438 pkgs.gawk
439 pkgs.diffutils
440 packages.valley-go
441 ];
442 cliScript = ../../bin/valley;
443 golden = ../../status/testdata/plan;
444 } (builtins.readFile ./valley-plan.sh);
445
446 # Self-update's own tests (selfupdate/), run with go vet: a real bare
447 # repository for ls-remote and a stand-in for nix, over an install, an
448 # up-to-date run, each step of the smoke test failing and being skipped,
449 # --retry, a rollback and its return, a pin, a valley removed from the
450 # profile by hand, and the repositories and branches it refuses.
451 # valley-self-update drives the verb through the CLI.
452 selfupdate-unit = packages.selfupdate.unit;
453
454 # valley --version and valley self-update through the installed package,
455 # against a real bare repository. A build has no nix, so a stand-in for
456 # nix builds a commit as a valley that runs the real, unstamped script
457 # under the stamp nix would give that commit, beside the package's own
458 # share/. The smoke test therefore runs the real CLI over the policy the
459 # package ships, under the schema it carries. A run installs main's tip;
460 # the next finds it up to date, also from a repository whose own git
461 # config names another; a commit whose build cannot compose a policy
462 # fails the smoke test, installs nothing, and is skipped. Then the
463 # home-manager module, evaluated over stand-ins for the home-manager
464 # options it sets: its timer, its service's PATH, and the arguments its
465 # script runs valley with. A real nix build into a throwaway profile
466 # needs a nix daemon and a writable store, which a build does not have.
467 valley-self-update = pkgs.runCommand "valley-self-update" {
468 nativeBuildInputs = [
469 pkgs.git
470 pkgs.jq
471 packages.valley
472 ];
473 inherit (packages.valley) version;
474 valley = packages.valley;
475 valleyScript = packages.valley-script;
476 cliScript = ../../bin/valley;
477 units = builtins.toJSON selfUpdateUnits;
478 passAsFile = [ "units" ];
479 } (builtins.readFile ./valley-self-update.sh);
480
481 # The CLI is offered for aarch64-darwin, which nothing here builds or
482 # tests (flake.nix). This check holds that offer to evaluating: an input
483 # with no Darwin build fails here rather than on a Mac. The package's
484 # drvPath is forced while this check is evaluated, and its string context
485 # is discarded, so the Darwin derivations are written to the store but
486 # are not inputs of this one. Building this check builds and substitutes
487 # nothing for Darwin.
488 valley-darwin-eval = pkgs.runCommand "valley-darwin-eval" {
489 darwinDrv = builtins.unsafeDiscardStringContext self.packages.aarch64-darwin.valley.drvPath;
490 darwinSystem = self.packages.aarch64-darwin.valley.system;
491 } (builtins.readFile ./valley-darwin-eval.sh);
492}