gunk-dev/the-valley

packages.nix

source4803f2e46407main's published tip history
21.9 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 # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns.
167 prose-fmt = pkgs.writeShellApplication {
168 name = "valley-fmt";
169 runtimeInputs = [
170 pkgs.git
171 pkgs.prettier
172 ];
173 text = ''
174 cd "$(git rev-parse --show-toplevel)"
175 git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \
176 prettier ${proseFmtArgs} --write
177 '';
178 };
179
180 # The Phase 2 attestation helper (dcr-0de694f). Go, standard library
181 # only, note format included — hence vendorHash = null, and hence no
182 # module fetch at build time. Its unit tests run in the checkPhase and
183 # need git and ssh-keygen.
184 attest-unwrapped = pkgs.buildGoModule {
185 pname = "valley-attest";
186 version = "0";
187 src = withNote ../attest;
188 modRoot = "attest";
189 vendorHash = null;
190 preBuild = readsGoMod;
191 nativeCheckInputs = [
192 pkgs.git
193 pkgs.openssh
194 ];
195 meta.mainProgram = "attest";
196 };
197
198 # The shipping form. git and cue are pinned to this flake's nixpkgs,
199 # so every host composes and vets a statement with the same tools.
200 # Signing needs no tool at all: a note is signed on crypto/ed25519
201 # inside the binary. `nix` is deliberately NOT pinned: the check being
202 # attested to must be built by the nix the machine actually runs, and
203 # a nix carried in here would be a second one.
204 attest =
205 pkgs.runCommand "valley-attest"
206 {
207 nativeBuildInputs = [ pkgs.makeWrapper ];
208 meta.mainProgram = "attest";
209 }
210 ''
211 mkdir -p $out/bin
212 makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \
213 --prefix PATH : ${
214 lib.makeBinPath [
215 pkgs.git
216 pkgs.cue
217 ]
218 } \
219 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue}
220 '';
221
222 # The identity registry compiler (dcr-b87f6e8). Go, standard library
223 # only, same trade as attest and the integrator — hence vendorHash = null
224 # and no module fetch. Its unit tests pin the note format's key hash
225 # against the reference vector attest is held to.
226 identity-unwrapped = pkgs.buildGoModule {
227 pname = "valley-identity";
228 version = "0";
229 src = ../identity;
230 vendorHash = null;
231 meta.mainProgram = "identity";
232 };
233
234 # The shipping form. git reads the registry out of the instance
235 # repository's tip and cue vets and exports it, both pinned to this
236 # flake's nixpkgs so every host compiles a registry the same way. The
237 # schema travels with the binary: a compiler judging a registry against a
238 # schema the consumer supplied would be no floor at all.
239 identity =
240 pkgs.runCommand "valley-identity"
241 {
242 nativeBuildInputs = [ pkgs.makeWrapper ];
243 meta.mainProgram = "identity";
244 }
245 ''
246 mkdir -p $out/bin
247 makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \
248 --prefix PATH : ${
249 lib.makeBinPath [
250 pkgs.git
251 pkgs.cue
252 ]
253 } \
254 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
255 '';
256
257 # The security-key signature verifier (sigverify/README.md). Go,
258 # standard library only — hence vendorHash = null and no module fetch.
259 #
260 # The git its git-tag command runs is fixed at build time to this
261 # flake's git, as an absolute store path compiled into the binary. A
262 # wrapper that puts git on PATH would leave the binary inside it looking
263 # git up on PATH, and a caller's environment could change which git that
264 # is. Compiled in, there is no PATH lookup to change.
265 #
266 # Its tests run in the checkPhase against real ssh-keygen and git. The
267 # security-key cases among them need OpenSSH's sk-dummy authenticator,
268 # which only the sigverify-unit check supplies; here they skip, so a
269 # consumer building this package never builds OpenSSH.
270 sigverify = pkgs.buildGoModule {
271 pname = "valley-sigverify";
272 version = "0";
273 src = ../sigverify;
274 vendorHash = null;
275 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
276 nativeCheckInputs = [
277 pkgs.git
278 pkgs.openssh
279 ];
280 meta.mainProgram = "sigverify";
281 };
282
283 # The pre-receive hook's policy (valleyhook/): what a push may write to a
284 # project a valley host serves. It validates a push to the plan graph
285 # with the one reading of a graph the CLI shares (../knot). The same binary is the git user's login
286 # shell, which derives the pushing principal and pauses pushes until the
287 # host has converged. Go, standard library only — hence
288 # vendorHash = null and no module fetch. Its unit tests run in the
289 # checkPhase.
290 #
291 # The git it reads the repository with is fixed at build time, the way
292 # sigverify's is: an absolute store path compiled into the binary, so no
293 # PATH a hook runs under can change which git that is.
294 valleyhook = pkgs.buildGoModule {
295 pname = "valley-valleyhook";
296 version = "0";
297 src = withLocal ../valleyhook [
298 ../note
299 ../change
300 ../knot
301 ];
302 modRoot = "valleyhook";
303 vendorHash = null;
304 preBuild = readsGoMod;
305 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
306 # The walk's and the change records' tests build hostile objects with
307 # real git.
308 nativeCheckInputs = [ pkgs.git ];
309 meta.mainProgram = "valleyhook";
310 };
311
312 # The note envelope's own tests, which hold it to refusing every line a
313 # relayer could append that one of its readers would refuse.
314 note = pkgs.buildGoModule {
315 pname = "valley-note";
316 version = "0";
317 src = ../note;
318 vendorHash = null;
319 };
320
321 # The plan graph (knot/): its canonical form, its layout, cycles and
322 # transitions, held by its own tests. The package is knotcheck, which
323 # knot-agree runs to hold the Go validator to the CUE schema and the
324 # knowledge lint.
325 knot = pkgs.buildGoModule {
326 pname = "valley-knot";
327 version = "0";
328 src = ../knot;
329 vendorHash = null;
330 meta.mainProgram = "knotcheck";
331 };
332
333 # The rebase engine (rebase/): a family of stacked changes captured from
334 # one snapshot of origin, rebased in a throwaway worktree, and published
335 # as one atomic branch push and one record append per change. Building
336 # valley-go does not run a module's tests that it only imports, so this
337 # package runs the engine's: go test, against real repositories, real
338 # pushes and real git, and go vet. Go, standard library only — hence
339 # vendorHash = null and no module fetch.
340 rebase = pkgs.buildGoModule {
341 pname = "valley-rebase";
342 version = "0";
343 src = withLocal ../rebase [ ../change ];
344 modRoot = "rebase";
345 vendorHash = null;
346 preBuild = readsGoMod;
347 nativeCheckInputs = [ pkgs.git ];
348 postCheck = ''
349 go vet ./...
350 '';
351 };
352
353 # Self-update (selfupdate/): the installed CLI brought up to the tip of a
354 # branch, built with nix and installed into the user's profile. Building
355 # valley-go does not run the tests of a module it only imports, so this
356 # package runs them, and go vet: a real bare repository for ls-remote,
357 # and a stand-in for nix whose builds are small scripts, so nothing is
358 # built and no profile is touched. Go, standard library only — hence
359 # vendorHash = null and no module fetch.
360 selfupdate = pkgs.buildGoModule {
361 pname = "valley-selfupdate";
362 version = "0";
363 src = ../selfupdate;
364 vendorHash = null;
365 nativeCheckInputs = [ pkgs.git ];
366 postCheck = ''
367 go vet ./...
368 '';
369 };
370
371 # The deriver of required checks (derive/): a change's paths read from
372 # git diff -z, globs matched byte by byte, and the required checks, pins,
373 # carry mode and unclassified paths derived from the whole composed
374 # policy (../composed). Building valley-go does not run the tests of a
375 # module it only imports, so this package runs them, and go vet. They
376 # hold it to bin/valley's deriver, run over the repository's own policy,
377 # the example policies and paths drawn from a fixed seed, so the source
378 # carries bin/valley, the schema and those policies, and the tests need
379 # bash, git and cue. bin/valley is run under C.UTF-8 and en_US.UTF-8 as
380 # well as C, so the tests read the UTF-8 locales' archive. Go, standard
381 # library only — hence vendorHash = null and no module fetch.
382 derive = pkgs.buildGoModule {
383 pname = "valley-derive";
384 version = "0";
385 src = withLocal ../derive [
386 ../composed
387 ../bin/valley
388 ../schema/verification.cue
389 ../examples/policy
390 ../policy/project
391 ];
392 modRoot = "derive";
393 vendorHash = null;
394 preBuild = readsGoMod;
395 nativeCheckInputs = [
396 pkgs.bash
397 pkgs.git
398 pkgs.cue
399 ];
400 # A test that cannot find a tool it needs, or a locale, fails rather
401 # than skipping.
402 preCheck = ''
403 export VALLEY_TEST_TOOLS=required
404 export LOCALE_ARCHIVE=${pkgs.glibcLocalesUtf8}/lib/locale/locale-archive
405 '';
406 postCheck = ''
407 go vet ./...
408 '';
409 };
410
411 # The change record's own tests: its strict reading, its fuzz target's
412 # seed corpus, and its branch-name rule, which they hold to real git's
413 # check-ref-format.
414 change = pkgs.buildGoModule {
415 pname = "valley-change";
416 version = "0";
417 src = ../change;
418 vendorHash = null;
419 nativeCheckInputs = [ pkgs.git ];
420 };
421
422 # valley-delta-v1 (delta/): the encoding's strict reading, and the delta
423 # read from real trees. Its checkPhase runs go vet, the conformance
424 # vectors' script in its --check mode, which holds the committed expected
425 # results to what the script computes, and go test, which builds each
426 # vector's trees with real git and holds the Go code to those results.
427 delta = pkgs.buildGoModule {
428 pname = "valley-delta";
429 version = "0";
430 src = ../delta;
431 vendorHash = null;
432 nativeCheckInputs = [
433 pkgs.git
434 pkgs.python3
435 ];
436 preCheck = ''
437 go vet ./...
438 python3 -I testdata/vectors.py --check testdata
439 '';
440 };
441
442 # Review records (review/): the-valley/review/v1's strict reading, a
443 # review's binding to the version it judged, recomputed from git within a
444 # budget, and the append-only history a review is kept in, with the check
445 # an append is held to. It reads change records with the one reading of a
446 # record the push hook shares (../change), and the delta with
447 # valley-delta-v1's bounded reading (../delta). Its checkPhase runs go
448 # test, which builds every repository it reads with real git, its fuzz
449 # target's seed corpus, and then go vet. Go, standard library only —
450 # hence vendorHash = null and no module fetch.
451 review = pkgs.buildGoModule {
452 pname = "valley-review";
453 version = "0";
454 src = withLocal ../review [
455 ../change
456 ../delta
457 ];
458 modRoot = "review";
459 vendorHash = null;
460 preBuild = readsGoMod;
461 nativeCheckInputs = [ pkgs.git ];
462 postCheck = ''
463 go vet ./...
464 '';
465 };
466
467 # The restacker (restack/): `valley-restacker once --report-only` reads
468 # one snapshot of origin through the rebase engine (../rebase), says
469 # which families it would restack and which it would hold, and builds the
470 # writes a pass would push, pushing none. It reads the cap on its
471 # automatic versions through the composed-policy reader it shares with
472 # the integrator (../composed), and a signer projects file through the
473 # note module (../note). Its unit tests cover request association,
474 # outcome currency, capacity and report bounds, and, against real git,
475 # the journal of writes and recovery's decisions, its environment, its
476 # scratch repository's identity, its state directory's lock and binding,
477 # and the deadline on what it runs. go vet runs after them. Go, standard
478 # library only — hence vendorHash = null and no module fetch.
479 restack-unwrapped = pkgs.buildGoModule {
480 pname = "valley-restacker";
481 version = "0";
482 src = withLocal ../restack [
483 ../change
484 ../composed
485 ../note
486 ../rebase
487 ];
488 modRoot = "restack";
489 # The program alone: e2e/transport is a module of the tests' own.
490 subPackages = [ "." ];
491 vendorHash = null;
492 preBuild = readsGoMod;
493 nativeCheckInputs = [ pkgs.git ];
494 postCheck = ''
495 go vet ./...
496 '';
497 # The module is named for its directory; the program, for what it is.
498 postInstall = ''
499 mv $out/bin/restack $out/bin/valley-restacker
500 '';
501 meta.mainProgram = "valley-restacker";
502 };
503
504 # The shipping form: git to read origin, cue to compose the policy, and
505 # attest to check a transfer statement, all pinned to this flake's
506 # nixpkgs, with the packaged verification schema as the default.
507 valley-restacker =
508 pkgs.runCommand "valley-restacker"
509 {
510 nativeBuildInputs = [ pkgs.makeWrapper ];
511 meta.mainProgram = "valley-restacker";
512 }
513 ''
514 mkdir -p $out/bin
515 makeWrapper ${lib.getExe restack-unwrapped} $out/bin/valley-restacker \
516 --prefix PATH : ${
517 lib.makeBinPath [
518 pkgs.git
519 pkgs.cue
520 attest
521 ]
522 } \
523 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
524 '';
525
526 # The Phase 3 integrator (dcr-439b771). Go, standard library only,
527 # same trade as attest — hence vendorHash = null and no module fetch. It
528 # reads change records with the one reading of a record the push hook
529 # shares (../change), and verifier keys with the one reading of a key the
530 # note format defines (../note).
531 integrator-unwrapped = pkgs.buildGoModule {
532 pname = "valley-integrator";
533 version = "0";
534 # The verification schema is in the source for the policy bridge's
535 # tests, which compose a policy under it.
536 src = withLocal ../integrator [
537 ../change
538 ../composed
539 ../note
540 ../schema/verification.cue
541 ];
542 modRoot = "integrator";
543 vendorHash = null;
544 preBuild = readsGoMod;
545 # The policy bridge's tests check a deriver's report against real git,
546 # and compose a policy with real cue.
547 nativeCheckInputs = [
548 pkgs.git
549 pkgs.cue
550 ];
551 meta.mainProgram = "integrator";
552 };
553
554 # The shipping form. The integrator drives four programs rather than
555 # reimplementing what they do: attest judges every statement and
556 # every note, the valley deriver and cue compose the policy, and git
557 # holds the requests and the refs. All four are pinned to this
558 # flake's nixpkgs so an instance's integrators agree; `nix` is
559 # deliberately absent for the same reason it is absent from attest —
560 # a closure must be recomputed by the nix the machine actually runs.
561 integrator =
562 pkgs.runCommand "valley-integrator"
563 {
564 nativeBuildInputs = [ pkgs.makeWrapper ];
565 meta.mainProgram = "integrator";
566 }
567 ''
568 mkdir -p $out/bin
569 makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \
570 --prefix PATH : ${
571 lib.makeBinPath [
572 pkgs.git
573 pkgs.cue
574 pkgs.natscli
575 attest
576 valley-script
577 ]
578 } \
579 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \
580 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \
581 --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue}
582 '';
583}