gunk-dev/the-valley

cli.nix

source10d26d40b95fmain's published tip history
19.0 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;
50in
51{
52 # The CLI must stay a lint-clean script whose help verb answers
53 # without a repo or a remote — the cheap end of its eviction clause
54 # (dcr-74c3158); anything needing more graduates instead. The
55 # completions are held to the same bar, and the installed package
56 # must carry them at the auto-linked paths.
57 valley-cli = pkgs.runCommand "valley-cli" {
58 nativeBuildInputs = [
59 pkgs.git
60 pkgs.shellcheck
61 pkgs.zsh
62 packages.valley
63 ];
64 cliScript = ../../bin/valley;
65 bashCompletion = ../../completions/valley.bash;
66 zshCompletion = ../../completions/_valley;
67 valley = packages.valley;
68 } (builtins.readFile ./valley-cli.sh);
69
70 # review's notes: the anchors are the whole claim, so they are
71 # pinned here rather than left to the reader of bin/valley. The
72 # session is real — a pty, real less, a pager filter that
73 # restructures the diff, and an $EDITOR that inserts lines while
74 # the pager is open — because every property under test is a
75 # property of that arrangement: that v is handed the raw buffer
76 # and not the rendering, that an inserted line becomes a note,
77 # and that the file and line it names are the ones it sat on. It goes
78 # on to review a stack layer by layer, and a stale stack: approving its
79 # top refused, its bottom approved, and review's [b]ase before the first
80 # page, after which the pages and the request are at the rebased heads,
81 # and the layer approved before the rebase is printed as a clean restack
82 # instead of paged. Last, a note written beside an edited line of a
83 # version paged as changed since its approval keeps its file and line.
84 review-notes = pkgs.runCommand "valley-review-notes" {
85 nativeBuildInputs = [
86 pkgs.git
87 pkgs.less
88 pkgs.util-linux # script(1), for the pty less insists on
89 packages.valley-script
90 ];
91 # The reviewer, as a script: four insertions and one edit of a
92 # line that was already there. The edit is deliberately far
93 # from every insertion, since -U0 would otherwise merge them
94 # into one hunk and the note would be dropped with it.
95 reviewer = pkgs.writeShellScript "valley-review-notes-editor" (
96 builtins.readFile ./review-notes-editor.sh
97 );
98 } (builtins.readFile ./review-notes.sh);
99
100 # The verification-policy deriver (dcr-f41f718). Composing the two
101 # layers is CUE's job and is pinned by cue-vet; matching paths is
102 # the script's own, and a path matcher is wrong exactly at the glob
103 # boundaries — `*` inside one segment against `**` across segments,
104 # `**` matching no segment at all — and at the sides of a diff a
105 # naive reader drops: a rename's old path and a deletion. Each is
106 # pinned by driving the real script over a scratch repository and
107 # the scratch policy in examples/policy/deriver/, whose floor is
108 # one glob boundary per class. A check's cost is pinned the same way:
109 # marking a check slow moves it between the summary lines and the
110 # --cost lists, and changes nothing required. So is the carry mode:
111 # overlapping classes, and paths in different classes, report the
112 # strictest of their modes, and an unclassified path is same-paths. A
113 # mode written by refining `policy` counts. A pattern holding a newline
114 # cannot forge a line of the render: the schema refuses it, the render
115 # refuses it under a schema that admits it, and the deriver refuses the
116 # second line under a render that admits it too.
117 #
118 # And the deriver fails closed: failNth stands in for git, sort and
119 # cue, and fails each call the deriver makes in turn. Every failure
120 # must end the derivation with nothing on stdout, because a failure
121 # read as an empty list is a change that requires nothing. cliScript is
122 # bin/valley itself, because the package's wrapper puts its own git
123 # ahead of anything on PATH.
124 policy-deriver = pkgs.runCommand "valley-policy-deriver" {
125 nativeBuildInputs = [
126 pkgs.git
127 pkgs.cue
128 packages.valley-script
129 ];
130 fixtures = ../../examples/policy/deriver;
131 schema = ../../schema/verification.cue;
132 cliScript = ../../bin/valley;
133 inherit failNth;
134 } (builtins.readFile ./policy-deriver.sh);
135
136 # the-valley's own project layer (policy/project), which is what gates
137 # every change to this repository. A layer that stopped composing would
138 # stop every derivation once it landed, the derivation of its own fix
139 # included, so it is held here before it lands. The floor it is composed
140 # with is the example one: the owning valley's is not in this tree. The
141 # check also holds every check the layer names to being an attribute of
142 # this flake's checks, and holds a docs-only change to requiring
143 # prose-format alone. And it holds a check's own files to requiring
144 # that check: every file in this directory named after a check, and every
145 # other script here, read from the directory itself so a new one is
146 # held as soon as it is added. It holds each scope to its carry mode,
147 # the scopes the carry classes classify to every check they required
148 # before, and the restacker's cap to the default.
149 own-policy = pkgs.runCommand "valley-own-policy" {
150 nativeBuildInputs = [
151 pkgs.git
152 pkgs.cue
153 packages.valley-script
154 ];
155 schema = ../../schema/verification.cue;
156 project = ../../policy/project;
157 floor = ../../examples/policy/instance/floor-format.cue;
158 template = ../../examples/policy/instance/template-docs.cue;
159 checkNames = builtins.attrNames self.checks.${system};
160 checkFiles = builtins.attrNames (builtins.readDir ./.);
161 } (builtins.readFile ./own-policy.sh);
162
163 # review's [a]pprove and the approve and rebase verbs, driven end to end
164 # over scratch repositories: a branch reviewed, the request filed, and
165 # what the receiving side then holds. The claim is about sequencing, so
166 # it is only observable from there — the bare repo logs every push it
167 # receives, and the check reads that log to see that ONE of them carried
168 # the evidence and the request together. A change whose version names no
169 # branch is approved at its recorded head the same way. Every check the scratch policy
170 # declares uses the command runner, for the reason attest-e2e gives: a
171 # nix build has no daemon. The packaged CLI carries its own schema, so
172 # the scratch projects that stand for projects other than the-valley
173 # carry no schema/. cliScript is bin/valley itself, for its other
174 # shipping mode: run straight from a the-valley checkout, it reads the
175 # schema there.
176 valley-request = pkgs.runCommand "valley-request" {
177 nativeBuildInputs = [
178 pkgs.git
179 pkgs.cue
180 pkgs.openssh
181 packages.attest
182 packages.valley-script
183 ];
184 schemaFile = ../../schema/verification.cue;
185 cliScript = ../../bin/valley;
186 inherit failNth;
187 } (builtins.readFile ./valley-request.sh);
188
189 # The rebase engine's tests (rebase/), run in its package's checkPhase
190 # with go vet: a chain, a shared lower layer, a conflict, an empty layer,
191 # a lease lost to a moved branch, a record append refused after the
192 # branches moved, a branch push and a record push that origin takes and
193 # never acknowledges, a cancellation before the push, and a cancellation
194 # while a hook or a filter blocks, each over real repositories and real
195 # pushes. valley-ready and integrator-e2e drive it through valley rebase.
196 rebase-unit = packages.rebase;
197
198 # The Go verbs' tests (status/), run in valley-go's checkPhase: the
199 # outcome records status accepts, the names its JSON refuses, its shell
200 # quoting held to the bash it replaced, and a rebase ended by a SIGTERM
201 # while a hook blocks it. valley-status and valley-ready drive the verbs
202 # through the CLI over real repositories.
203 valley-go-unit = packages.valley-go;
204
205 # Read origin's request/outcome refs and distinguish a pending request,
206 # a recorded decision and a landing, without moving the user's checkout.
207 # Each state is read again through --json, and jq holds the object to
208 # the valley-status/v1 contract. A change's restack report is shown as
209 # current, and as historical once a ref it observed has changed: its
210 # branch, main, its record, a branch it saw missing, or a request that
211 # held it back. Status, changes and pending all show it.
212 valley-status = pkgs.runCommand "valley-status" {
213 nativeBuildInputs = [
214 pkgs.git
215 pkgs.jq
216 packages.valley-script
217 ];
218 } (builtins.readFile ./valley-status.sh);
219
220 # ready, unready and withdraw, through real pushes to a bare repo wired
221 # with the rendered pre-receive hook, as each principal: a change created
222 # at v1, a second version, the same head refused, a withdrawal, revived
223 # at the same version and then at a new one, a principal that neither
224 # owns the change nor holds the request grant refused by the hook, and
225 # the operator, who holds it, let through. Then each source of a change's ID — the jj
226 # header, the Change-Id trailer, a generated ID, --stamp — a header and a
227 # trailer that disagree, and a change whose commits hold a merge. Last, a
228 # change named by its handle is approved, reviewed and rebased at the head
229 # its record names: a record pushed with its branch moved elsewhere in the
230 # same push is refused, the branch at the recorded head is filed at that
231 # head, and a later version whose branch is elsewhere is refused. A
232 # version that names no branch is approved at its head, and refused by
233 # review and rebase, which act on a branch. A name no record is on that
234 # is no branch name is refused. Then a
235 # draft: unready, ready again at the same head and at a new one, listed
236 # apart by pending, refused by approve, rebase and review's [a]pprove
237 # whether it is named by its handle or its branch, and a request pending
238 # under either name warned of by unready and withdraw, a request for an
239 # earlier version's branch too. A review left open while another
240 # principal unreadies the change, or readies a new version, refuses to
241 # act on it. A branch no record is on is acted on as a branch, and one
242 # two records are on is refused. Last, stacks rebased whole: every layer
243 # moved and each change given a version, by ancestry and by trees; a
244 # conflict in the middle layer; each refusal before the rebase; a branch
245 # moved, and a plain branch readied, after the rebase read the stack; a
246 # change unreadied between the branch push and its record write, and the
247 # command the manifest gives to finish it; and a second stack left on an
248 # old head. Then review set against a change's approved version: a clean
249 # restack after [b]ase, with the landing between the bases listed and no
250 # diff paged; a later version with an edited file, paged with the
251 # range-diff and its own diff of that file; a version whose message
252 # alone changed and one whose author alone did, each flagged; a change
253 # with no approved version, paged in full; and a stack whose lower layer
254 # is set against its own approval while its top, which has none, is
255 # paged. The hook is followed from the protected host's rendered init
256 # script, so the rules the record meets are the ones a host applies.
257 valley-ready = pkgs.runCommand "valley-ready" {
258 nativeBuildInputs = [
259 pkgs.git
260 pkgs.jq
261 pkgs.util-linux # script(1), for approve's confirmation on a terminal
262 packages.valley-script
263 ];
264 initScript = hosts.protectedHost.config.systemd.services.valley-init.script;
265 passAsFile = [ "initScript" ];
266 } (builtins.readFile ./valley-ready.sh);
267
268 # valley changes and valley pending over the projects of one test host,
269 # made by the integrator host's rendered valley-init, so discovery reads
270 # the project list a host publishes and every record goes through the
271 # hook it installs. Two repositories with ready, draft and withdrawn
272 # records; handles unique across them; one listed repository that cannot
273 # be read; a change ID recorded in several, with qualified handles; --repo;
274 # a version landed by fast-forward, one landed as a new commit with a
275 # transfer statement the integrator signed, one consumed with no
276 # evidence, one whose statement another key signed, one with a landed
277 # version's tree on another parent, a landing's tree replayed on a later
278 # main, and heads below a merge that dropped them, through either
279 # parent; a project named origin that shares a change ID; a
280 # policy/valley git would read as an option or
281 # as ext::; includes as the nearest ancestor only; pending's human shape,
282 # its drafts array and --checks; and a branch name read as a branch again
283 # once its change has landed, before the branch moves and after.
284 valley-changes = pkgs.runCommand "valley-changes" {
285 nativeBuildInputs = [
286 pkgs.git
287 pkgs.jq
288 pkgs.openssh
289 packages.valley-script
290 packages.valley-go
291 packages.attest
292 ];
293 initScript = hosts.integratorHost.config.systemd.services.valley-init.script;
294 passAsFile = [ "initScript" ];
295 # The floor --checks derives under.
296 floor = ../../examples/policy/instance;
297 } (builtins.readFile ./valley-changes.sh);
298
299 # valley knot against local bare repositories that run no hook, as any
300 # git remote would: every verb, the ref's shape after each write, another
301 # project named by --project and by a qualified id, and then each way a
302 # write may fail to land the first time. A stand-in for git, first on
303 # PATH, makes another writer land between a read and its push, drops the
304 # connection after a push lands and after one that did not, takes a
305 # claim or changes a field first, and releases and retakes a claim an
306 # unclaim read. Each write must then land exactly once, or be reported
307 # as a conflict. Two writers also run at once. cliScript
308 # is bin/valley itself, because the package's wrapper puts its own git
309 # ahead of the stand-in.
310 valley-knot = pkgs.runCommand "valley-knot" {
311 nativeBuildInputs = [
312 pkgs.git
313 pkgs.jq
314 packages.valley-go
315 ];
316 cliScript = ../../bin/valley;
317 } (builtins.readFile ./valley-knot.sh);
318
319 # valley plan against local bare repositories that run no hook: a graph
320 # built with the real knot verbs, each at a time VALLEY_NOW fixes, so
321 # every date and age it shows is the same on every run. The text, off a
322 # terminal, and the valley-plan/v1 document are compared with golden
323 # files kept beside the code that prints them (status/testdata/plan/).
324 # Then a project with no graph, --all over the projects the instance
325 # publishes with one that cannot be read, and the operator named by
326 # --owner and by no one. cliScript is bin/valley itself, as for
327 # valley-knot.
328 valley-plan = pkgs.runCommand "valley-plan" {
329 nativeBuildInputs = [
330 pkgs.git
331 pkgs.jq
332 pkgs.gawk
333 pkgs.diffutils
334 packages.valley-go
335 ];
336 cliScript = ../../bin/valley;
337 golden = ../../status/testdata/plan;
338 } (builtins.readFile ./valley-plan.sh);
339
340 # Self-update's own tests (selfupdate/), run in its package's checkPhase
341 # with go vet: a real bare repository for ls-remote and a stand-in for
342 # nix, over an install, an up-to-date run, each step of the smoke test
343 # failing and being skipped, --retry, a rollback and its return, a pin, a
344 # valley removed from the profile by hand, and the repositories and
345 # branches it refuses. valley-self-update drives the verb through the CLI.
346 selfupdate-unit = packages.selfupdate;
347
348 # valley --version and valley self-update through the installed package,
349 # against a real bare repository. A build has no nix, so a stand-in for
350 # nix builds a commit as a valley that runs the real, unstamped script
351 # under the stamp nix would give that commit, beside the package's own
352 # share/. The smoke test therefore runs the real CLI over the policy the
353 # package ships, under the schema it carries. A run installs main's tip;
354 # the next finds it up to date, also from a repository whose own git
355 # config names another; a commit whose build cannot compose a policy
356 # fails the smoke test, installs nothing, and is skipped. Then the
357 # home-manager module, evaluated over stand-ins for the home-manager
358 # options it sets: its timer, its service's PATH, and the arguments its
359 # script runs valley with. A real nix build into a throwaway profile
360 # needs a nix daemon and a writable store, which a build does not have.
361 valley-self-update = pkgs.runCommand "valley-self-update" {
362 nativeBuildInputs = [
363 pkgs.git
364 pkgs.jq
365 packages.valley
366 ];
367 inherit (packages.valley) version;
368 valley = packages.valley;
369 valleyScript = packages.valley-script;
370 cliScript = ../../bin/valley;
371 units = builtins.toJSON selfUpdateUnits;
372 passAsFile = [ "units" ];
373 } (builtins.readFile ./valley-self-update.sh);
374
375 # The CLI is offered for aarch64-darwin, which nothing here builds or
376 # tests (flake.nix). This check holds that offer to evaluating: an input
377 # with no Darwin build fails here rather than on a Mac. The package's
378 # drvPath is forced while this check is evaluated, and its string context
379 # is discarded, so the Darwin derivations are written to the store but
380 # are not inputs of this one. Building this check builds and substitutes
381 # nothing for Darwin.
382 valley-darwin-eval = pkgs.runCommand "valley-darwin-eval" {
383 darwinDrv = builtins.unsafeDiscardStringContext self.packages.aarch64-darwin.valley.drvPath;
384 darwinSystem = self.packages.aarch64-darwin.valley.system;
385 } (builtins.readFile ./valley-darwin-eval.sh);
386}