gunk-dev/the-valley

packages.nix

source83edb7c4180fmain's published tip history
22.8 KiB
1# Everything this flake builds: the CLI and its Go verbs, the
2# formatter, the attestation helper, the integrator, the identity
3# compiler, the pre-receive hook's policy, the security-key signature
4# verifier. The flake wires these into packages, apps, and the checks that
5# drive them; nothing here knows about any of those outputs.
6{
7 pkgs,
8 lib,
9 # The commit the flake is built from: its full SHA, the SHA with -dirty
10 # after it for a tree with uncommitted changes, or null when nix knows no
11 # commit. Only the installed CLI's version stamp reads it.
12 rev ? null,
13}:
14rec {
15 # The CLI's version, which `valley --version` prints and self-update
16 # reads to tell which commit is installed (selfupdate/): cliVersion and
17 # the short commit, as 0.1.0-97db597. A dirty tree's has -dirty after
18 # it, and with no commit known it ends in -unknown, so neither names a
19 # commit.
20 cliVersion = "0.1.0";
21 version =
22 if rev == null then
23 "${cliVersion}-unknown"
24 else if lib.hasSuffix "-dirty" rev then
25 "${cliVersion}-${builtins.substring 0 7 rev}-dirty"
26 else
27 "${cliVersion}-${builtins.substring 0 7 rev}";
28
29 # The source of a Go program that uses modules of this repository: its own
30 # directory and those modules, which its go.mod replaces with their local
31 # directories. Nothing shared this way is fetched.
32 withLocal =
33 dir: modules:
34 lib.fileset.toSource {
35 root = ../.;
36 fileset = lib.fileset.unions ([ dir ] ++ modules);
37 };
38
39 # A program that reads signed notes takes the note module (../note). One
40 # reading of the envelope is shared by the programs that write, verify and
41 # admit attestations.
42 withNote = dir: withLocal dir [ ../note ];
43
44 # vendorHash = null builds in vendor mode, which has no reading of a local
45 # replace. The replaced module is a directory beside the program and
46 # nothing is fetched either way, so these builds read go.mod as written.
47 readsGoMod = ''
48 export GOFLAGS="''${GOFLAGS//-mod=vendor/-mod=mod}"
49 '';
50
51 # Markdown prose is filled paragraphs hard-wrapped at 100 columns
52 # (ida-1ec03b1). One flag string backs both the formatter app and the
53 # prose-format check, so the two cannot drift. Embedded-language
54 # formatting is off so fenced code blocks pass through byte-for-byte.
55 proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off";
56
57 # The integrator's CLI (bin/valley) wrapped for `nix run`. The script
58 # itself must keep running bare from any checkout — the package is the
59 # second of its two shipping modes, never a dependency of the first.
60 valley-script-unwrapped = pkgs.writeShellApplication {
61 name = "valley";
62 runtimeInputs = [
63 pkgs.git
64 pkgs.natscli # tail and replay only; every other verb needs just git
65 pkgs.cue # checks and approving — it composes the policy layers
66 pkgs.less # review only — its v key is what takes a note
67 attest # approving only — it runs the checks and signs them
68 valley-go # status, ready, unready, withdraw and rebase, naming a change by its ID, and pending's drafts
69 ];
70 text = builtins.readFile ../bin/valley;
71 };
72
73 # The shipping form. The verification schema travels with the script, as
74 # it does with the integrator: only the-valley's own tree carries
75 # schema/, so a project's checkout has none to read. --set-default leaves
76 # an explicit VALLEY_VERIFICATION_SCHEMA in charge.
77 valley-script =
78 pkgs.runCommand "valley"
79 {
80 nativeBuildInputs = [ pkgs.makeWrapper ];
81 meta.mainProgram = "valley";
82 }
83 ''
84 mkdir -p $out/bin
85 makeWrapper ${lib.getExe valley-script-unwrapped} $out/bin/valley \
86 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
87 '';
88
89 # The installed package: the wrapped script stamped with its version, the
90 # shell completions at the standard paths home-manager/NixOS auto-link,
91 # and the tiny policy self-update's smoke test derives checks from
92 # (examples/policy/smoke/). The stamp is set on this outer wrapper alone,
93 # so the packages the checks and the integrator use do not change with
94 # every commit. The name carries the version as well: nix profile names
95 # the element valley from it, and self-update reads a hand-installed
96 # valley's commit from it.
97 valley =
98 pkgs.runCommand "valley-${version}"
99 {
100 inherit version;
101 nativeBuildInputs = [
102 pkgs.makeWrapper
103 pkgs.installShellFiles
104 ];
105 meta.mainProgram = "valley";
106 }
107 ''
108 mkdir -p $out/bin $out/share/valley
109 makeWrapper ${lib.getExe valley-script} $out/bin/valley \
110 --set VALLEY_VERSION ${version}
111 installShellCompletion --bash --name valley ${../completions/valley.bash}
112 installShellCompletion --zsh --name _valley ${../completions/_valley}
113 cp -r --no-preserve=mode ${../examples/policy/smoke} $out/share/valley/smoke-policy
114 '';
115
116 # The verbs of the CLI written in Go (status/). status reads one change's
117 # request and outcome record from origin, and prints them as sentences or
118 # as the valley-status/v1 JSON object front ends read. ready, unready and
119 # withdraw write a change's record, with the one reading of a record the
120 # push hook shares (../change). rebase rebases a change's whole stack and
121 # adds each change's rebased head to its record, through the rebase engine
122 # (../rebase), which also reads how far each version has landed for the
123 # listings. knot reads and writes the plan graph, with the one reading of
124 # a graph the push hook shares (../knot), and plan shows that graph as a
125 # view. compare reads a version's file delta and its approved version's
126 # with the one reading of valley-delta-v1 (../delta), for review.
127 # self-update installs the tip of a branch of the-valley into the user's
128 # nix profile (../selfupdate). checks prints which checks a change
129 # requires, as the deriver (../derive) derives them from the policy the
130 # composed-policy reader (../composed) reads. Go,
131 # standard library only — hence vendorHash = null and no module fetch. It
132 # runs whatever git and cue are on PATH, which inside the CLI's package
133 # are the CLI's own. Its unit tests hold its shell quoting to the bash it
134 # replaced, so they need bash, lay pending out over real repositories,
135 # so they need git, and derive a change's checks under the verification
136 # schema, so they need cue and the schema.
137 valley-go = pkgs.buildGoModule {
138 pname = "valley-go";
139 version = "0";
140 src = withLocal ../status [
141 ../change
142 ../composed
143 ../delta
144 ../derive
145 ../knot
146 ../note
147 ../rebase
148 ../selfupdate
149 ../schema/verification.cue
150 ];
151 modRoot = "status";
152 vendorHash = null;
153 preBuild = readsGoMod;
154 nativeCheckInputs = [
155 pkgs.bash
156 pkgs.git
157 pkgs.cue
158 ];
159 # A test that cannot find a tool it needs fails rather than skipping.
160 preCheck = ''
161 export VALLEY_TEST_TOOLS=required
162 '';
163 meta.mainProgram = "valley-go";
164 };
165
166 # The check worker: one pass of `valley-go work` (status/worker.go), which
167 # builds the nix checks ready changes require and publishes the evidence
168 # before anyone approves them. It runs this flake's git, cue and attest,
169 # and the verification schema travels with it, as it does with the CLI.
170 # nix is the machine's own, for the reason attest gives.
171 valley-worker =
172 pkgs.runCommand "valley-worker"
173 {
174 nativeBuildInputs = [ pkgs.makeWrapper ];
175 meta.mainProgram = "valley-worker";
176 }
177 ''
178 mkdir -p $out/bin
179 makeWrapper ${lib.getExe valley-go} $out/bin/valley-worker \
180 --add-flags work \
181 --prefix PATH : ${
182 lib.makeBinPath [
183 pkgs.git
184 pkgs.cue
185 attest
186 ]
187 } \
188 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
189 '';
190
191 # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns.
192 prose-fmt = pkgs.writeShellApplication {
193 name = "valley-fmt";
194 runtimeInputs = [
195 pkgs.git
196 pkgs.prettier
197 ];
198 text = ''
199 cd "$(git rev-parse --show-toplevel)"
200 git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \
201 prettier ${proseFmtArgs} --write
202 '';
203 };
204
205 # The Phase 2 attestation helper (dcr-0de694f). Go, standard library
206 # only, note format included — hence vendorHash = null, and hence no
207 # module fetch at build time. Its unit tests run in the checkPhase and
208 # need git and ssh-keygen.
209 attest-unwrapped = pkgs.buildGoModule {
210 pname = "valley-attest";
211 version = "0";
212 src = withNote ../attest;
213 modRoot = "attest";
214 vendorHash = null;
215 preBuild = readsGoMod;
216 nativeCheckInputs = [
217 pkgs.git
218 pkgs.openssh
219 ];
220 meta.mainProgram = "attest";
221 };
222
223 # The shipping form. git and cue are pinned to this flake's nixpkgs,
224 # so every host composes and vets a statement with the same tools.
225 # Signing needs no tool at all: a note is signed on crypto/ed25519
226 # inside the binary. `nix` is deliberately NOT pinned: the check being
227 # attested to must be built by the nix the machine actually runs, and
228 # a nix carried in here would be a second one.
229 attest =
230 pkgs.runCommand "valley-attest"
231 {
232 nativeBuildInputs = [ pkgs.makeWrapper ];
233 meta.mainProgram = "attest";
234 }
235 ''
236 mkdir -p $out/bin
237 makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \
238 --prefix PATH : ${
239 lib.makeBinPath [
240 pkgs.git
241 pkgs.cue
242 ]
243 } \
244 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue}
245 '';
246
247 # The identity registry compiler (dcr-b87f6e8). Go, standard library
248 # only, same trade as attest and the integrator — hence vendorHash = null
249 # and no module fetch. Its unit tests pin the note format's key hash
250 # against the reference vector attest is held to.
251 identity-unwrapped = pkgs.buildGoModule {
252 pname = "valley-identity";
253 version = "0";
254 src = ../identity;
255 vendorHash = null;
256 meta.mainProgram = "identity";
257 };
258
259 # The shipping form. git reads the registry out of the instance
260 # repository's tip and cue vets and exports it, both pinned to this
261 # flake's nixpkgs so every host compiles a registry the same way. The
262 # schema travels with the binary: a compiler judging a registry against a
263 # schema the consumer supplied would be no floor at all.
264 identity =
265 pkgs.runCommand "valley-identity"
266 {
267 nativeBuildInputs = [ pkgs.makeWrapper ];
268 meta.mainProgram = "identity";
269 }
270 ''
271 mkdir -p $out/bin
272 makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \
273 --prefix PATH : ${
274 lib.makeBinPath [
275 pkgs.git
276 pkgs.cue
277 ]
278 } \
279 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
280 '';
281
282 # The security-key signature verifier (sigverify/README.md). Go,
283 # standard library only — hence vendorHash = null and no module fetch.
284 #
285 # The git its git-tag command runs is fixed at build time to this
286 # flake's git, as an absolute store path compiled into the binary. A
287 # wrapper that puts git on PATH would leave the binary inside it looking
288 # git up on PATH, and a caller's environment could change which git that
289 # is. Compiled in, there is no PATH lookup to change.
290 #
291 # Its tests run in the checkPhase against real ssh-keygen and git. The
292 # security-key cases among them need OpenSSH's sk-dummy authenticator,
293 # which only the sigverify-unit check supplies; here they skip, so a
294 # consumer building this package never builds OpenSSH.
295 sigverify = pkgs.buildGoModule {
296 pname = "valley-sigverify";
297 version = "0";
298 src = ../sigverify;
299 vendorHash = null;
300 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
301 nativeCheckInputs = [
302 pkgs.git
303 pkgs.openssh
304 ];
305 meta.mainProgram = "sigverify";
306 };
307
308 # The pre-receive hook's policy (valleyhook/): what a push may write to a
309 # project a valley host serves. It validates a push to the plan graph
310 # with the one reading of a graph the CLI shares (../knot). The same binary is the git user's login
311 # shell, which derives the pushing principal and pauses pushes until the
312 # host has converged. Go, standard library only — hence
313 # vendorHash = null and no module fetch. Its unit tests run in the
314 # checkPhase.
315 #
316 # The git it reads the repository with is fixed at build time, the way
317 # sigverify's is: an absolute store path compiled into the binary, so no
318 # PATH a hook runs under can change which git that is.
319 valleyhook = pkgs.buildGoModule {
320 pname = "valley-valleyhook";
321 version = "0";
322 src = withLocal ../valleyhook [
323 ../note
324 ../change
325 ../knot
326 ];
327 modRoot = "valleyhook";
328 vendorHash = null;
329 preBuild = readsGoMod;
330 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
331 # The walk's and the change records' tests build hostile objects with
332 # real git.
333 nativeCheckInputs = [ pkgs.git ];
334 meta.mainProgram = "valleyhook";
335 };
336
337 # The note envelope's own tests, which hold it to refusing every line a
338 # relayer could append that one of its readers would refuse.
339 note = pkgs.buildGoModule {
340 pname = "valley-note";
341 version = "0";
342 src = ../note;
343 vendorHash = null;
344 };
345
346 # The plan graph (knot/): its canonical form, its layout, cycles and
347 # transitions, held by its own tests. The package is knotcheck, which
348 # knot-agree runs to hold the Go validator to the CUE schema and the
349 # knowledge lint.
350 knot = pkgs.buildGoModule {
351 pname = "valley-knot";
352 version = "0";
353 src = ../knot;
354 vendorHash = null;
355 meta.mainProgram = "knotcheck";
356 };
357
358 # The rebase engine (rebase/): a family of stacked changes captured from
359 # one snapshot of origin, rebased in a throwaway worktree, and published
360 # as one atomic branch push and one record append per change. Building
361 # valley-go does not run a module's tests that it only imports, so this
362 # package runs the engine's: go test, against real repositories, real
363 # pushes and real git, and go vet. Go, standard library only — hence
364 # vendorHash = null and no module fetch.
365 rebase = pkgs.buildGoModule {
366 pname = "valley-rebase";
367 version = "0";
368 src = withLocal ../rebase [ ../change ];
369 modRoot = "rebase";
370 vendorHash = null;
371 preBuild = readsGoMod;
372 nativeCheckInputs = [ pkgs.git ];
373 postCheck = ''
374 go vet ./...
375 '';
376 };
377
378 # Self-update (selfupdate/): the installed CLI brought up to the tip of a
379 # branch, built with nix and installed into the user's profile. Building
380 # valley-go does not run the tests of a module it only imports, so this
381 # package runs them, and go vet: a real bare repository for ls-remote,
382 # and a stand-in for nix whose builds are small scripts, so nothing is
383 # built and no profile is touched. Go, standard library only — hence
384 # vendorHash = null and no module fetch.
385 selfupdate = pkgs.buildGoModule {
386 pname = "valley-selfupdate";
387 version = "0";
388 src = ../selfupdate;
389 vendorHash = null;
390 nativeCheckInputs = [ pkgs.git ];
391 postCheck = ''
392 go vet ./...
393 '';
394 };
395
396 # The deriver of required checks (derive/): a change's paths read from
397 # git diff -z, globs matched byte by byte, and the required checks, pins,
398 # carry mode and unclassified paths derived from the whole composed
399 # policy (../composed). Building valley-go does not run the tests of a
400 # module it only imports, so this package runs them, and go vet. They
401 # hold it to bin/valley's deriver, run over the repository's own policy,
402 # the example policies and paths drawn from a fixed seed, so the source
403 # carries bin/valley, the schema and those policies, and the tests need
404 # bash, git and cue. bin/valley is run under C.UTF-8 and en_US.UTF-8 as
405 # well as C, so the tests read the UTF-8 locales' archive. Go, standard
406 # library only — hence vendorHash = null and no module fetch.
407 derive = pkgs.buildGoModule {
408 pname = "valley-derive";
409 version = "0";
410 src = withLocal ../derive [
411 ../composed
412 ../bin/valley
413 ../schema/verification.cue
414 ../examples/policy
415 ../policy/project
416 ];
417 modRoot = "derive";
418 vendorHash = null;
419 preBuild = readsGoMod;
420 nativeCheckInputs = [
421 pkgs.bash
422 pkgs.git
423 pkgs.cue
424 ];
425 # A test that cannot find a tool it needs, or a locale, fails rather
426 # than skipping.
427 preCheck = ''
428 export VALLEY_TEST_TOOLS=required
429 export LOCALE_ARCHIVE=${pkgs.glibcLocalesUtf8}/lib/locale/locale-archive
430 '';
431 postCheck = ''
432 go vet ./...
433 '';
434 };
435
436 # The change record's own tests: its strict reading, its fuzz target's
437 # seed corpus, and its branch-name rule, which they hold to real git's
438 # check-ref-format.
439 change = pkgs.buildGoModule {
440 pname = "valley-change";
441 version = "0";
442 src = ../change;
443 vendorHash = null;
444 nativeCheckInputs = [ pkgs.git ];
445 };
446
447 # valley-delta-v1 (delta/): the encoding's strict reading, and the delta
448 # read from real trees. Its checkPhase runs go vet, the conformance
449 # vectors' script in its --check mode, which holds the committed expected
450 # results to what the script computes, and go test, which builds each
451 # vector's trees with real git and holds the Go code to those results.
452 delta = pkgs.buildGoModule {
453 pname = "valley-delta";
454 version = "0";
455 src = ../delta;
456 vendorHash = null;
457 nativeCheckInputs = [
458 pkgs.git
459 pkgs.python3
460 ];
461 preCheck = ''
462 go vet ./...
463 python3 -I testdata/vectors.py --check testdata
464 '';
465 };
466
467 # Review records (review/): the-valley/review/v1's strict reading, a
468 # review's binding to the version it judged, recomputed from git within a
469 # budget, and the append-only history a review is kept in, with the check
470 # an append is held to. It reads change records with the one reading of a
471 # record the push hook shares (../change), and the delta with
472 # valley-delta-v1's bounded reading (../delta). Its checkPhase runs go
473 # test, which builds every repository it reads with real git, its fuzz
474 # target's seed corpus, and then go vet. Go, standard library only —
475 # hence vendorHash = null and no module fetch.
476 review = pkgs.buildGoModule {
477 pname = "valley-review";
478 version = "0";
479 src = withLocal ../review [
480 ../change
481 ../delta
482 ];
483 modRoot = "review";
484 vendorHash = null;
485 preBuild = readsGoMod;
486 nativeCheckInputs = [ pkgs.git ];
487 postCheck = ''
488 go vet ./...
489 '';
490 };
491
492 # The restacker (restack/): `valley-restacker once --report-only` reads
493 # one snapshot of origin through the rebase engine (../rebase), says
494 # which families it would restack and which it would hold, and builds the
495 # writes a pass would push, pushing none. It reads the cap on its
496 # automatic versions through the composed-policy reader it shares with
497 # the integrator (../composed), and a signer projects file through the
498 # note module (../note). Its unit tests cover request association,
499 # outcome currency, capacity and report bounds, and, against real git,
500 # the journal of writes and recovery's decisions, its environment, its
501 # scratch repository's identity, its state directory's lock and binding,
502 # and the deadline on what it runs. go vet runs after them. Go, standard
503 # library only — hence vendorHash = null and no module fetch.
504 restack-unwrapped = pkgs.buildGoModule {
505 pname = "valley-restacker";
506 version = "0";
507 src = withLocal ../restack [
508 ../change
509 ../composed
510 ../note
511 ../rebase
512 ];
513 modRoot = "restack";
514 # The program alone: e2e/transport is a module of the tests' own.
515 subPackages = [ "." ];
516 vendorHash = null;
517 preBuild = readsGoMod;
518 nativeCheckInputs = [ pkgs.git ];
519 postCheck = ''
520 go vet ./...
521 '';
522 # The module is named for its directory; the program, for what it is.
523 postInstall = ''
524 mv $out/bin/restack $out/bin/valley-restacker
525 '';
526 meta.mainProgram = "valley-restacker";
527 };
528
529 # The shipping form: git to read origin, cue to compose the policy, and
530 # attest to check a transfer statement, all pinned to this flake's
531 # nixpkgs, with the packaged verification schema as the default.
532 valley-restacker =
533 pkgs.runCommand "valley-restacker"
534 {
535 nativeBuildInputs = [ pkgs.makeWrapper ];
536 meta.mainProgram = "valley-restacker";
537 }
538 ''
539 mkdir -p $out/bin
540 makeWrapper ${lib.getExe restack-unwrapped} $out/bin/valley-restacker \
541 --prefix PATH : ${
542 lib.makeBinPath [
543 pkgs.git
544 pkgs.cue
545 attest
546 ]
547 } \
548 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
549 '';
550
551 # The Phase 3 integrator (dcr-439b771). Go, standard library only,
552 # same trade as attest — hence vendorHash = null and no module fetch. It
553 # reads change records with the one reading of a record the push hook
554 # shares (../change), and verifier keys with the one reading of a key the
555 # note format defines (../note).
556 integrator-unwrapped = pkgs.buildGoModule {
557 pname = "valley-integrator";
558 version = "0";
559 # The verification schema is in the source for the policy bridge's
560 # tests, which compose a policy under it.
561 src = withLocal ../integrator [
562 ../change
563 ../composed
564 ../note
565 ../schema/verification.cue
566 ];
567 modRoot = "integrator";
568 vendorHash = null;
569 preBuild = readsGoMod;
570 # The policy bridge's tests check a deriver's report against real git,
571 # and compose a policy with real cue.
572 nativeCheckInputs = [
573 pkgs.git
574 pkgs.cue
575 ];
576 meta.mainProgram = "integrator";
577 };
578
579 # The shipping form. The integrator drives four programs rather than
580 # reimplementing what they do: attest judges every statement and
581 # every note, the valley deriver and cue compose the policy, and git
582 # holds the requests and the refs. All four are pinned to this
583 # flake's nixpkgs so an instance's integrators agree; `nix` is
584 # deliberately absent for the same reason it is absent from attest —
585 # a closure must be recomputed by the nix the machine actually runs.
586 integrator =
587 pkgs.runCommand "valley-integrator"
588 {
589 nativeBuildInputs = [ pkgs.makeWrapper ];
590 meta.mainProgram = "integrator";
591 }
592 ''
593 mkdir -p $out/bin
594 makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \
595 --prefix PATH : ${
596 lib.makeBinPath [
597 pkgs.git
598 pkgs.cue
599 pkgs.natscli
600 attest
601 valley-script
602 ]
603 } \
604 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \
605 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \
606 --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue}
607 '';
608}