gunk-dev/the-valley

cli.nix

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