gunk-dev/the-valley

packages.nix

source69d7baef1f04main's published tip history
41.9 KiB
1# Everything this flake builds: the valley command, 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 # What no Go build or test here reads: prose and .gitignore files. A Go
30 # package's source leaves them out, so editing a module's README rebuilds
31 # nothing.
32 unread = lib.fileset.fileFilter (f: f.hasExt "md" || f.name == ".gitignore") ../.;
33
34 # The parts of the approval module (approval/) that only the approval
35 # package builds and tests: its command, the PIV signer's C source and
36 # the fixtures. A module that imports the approval library leaves them
37 # out of its source.
38 approvalExtras = [
39 ../approval/cmd
40 ../approval/piv
41 ../approval/testdata
42 ];
43
44 # Every Go test file in the tree. No package's build reads one.
45 goTests = lib.fileset.fileFilter (f: lib.hasSuffix "_test.go" f.name) ../.;
46
47 # A Go module of this repository, built as a package that runs no tests,
48 # with its unit tests beside it as the package's unit attribute.
49 #
50 # The package's source is what its build reads: the module's directory
51 # (dir) and the modules of this repository it uses (modules), which its
52 # go.mod replaces with their local directories, each in its place in the
53 # tree. Nothing shared this way is fetched. The source holds no test file
54 # and none of the files only the tests read (tests.reads), and the
55 # package's checkPhase is off. So an edit to a test, or to a file only the
56 # tests read, changes neither the package nor any check built on it.
57 #
58 # unit is the same build over the same source with the module's own tests
59 # and tests.reads added, and with its checkPhase on. The checkPhase runs
60 # go test, with the rest of tests: the tools the tests need, and what runs
61 # before and after them, such as go vet. Each module's unit is its unit
62 # check (nix/checks/), so its tests run in that check and in no package
63 # build.
64 #
65 # A file the tests read is in unit's source though it is prose, so a test
66 # may read the repository's documents.
67 #
68 # excluded names files of dir that neither the build nor the tests read:
69 # files only a flake check reads, or the part of a shared module that
70 # another package builds and tests.
71 goModule =
72 {
73 dir,
74 modules ? [ ],
75 excluded ? [ ],
76 tests ? { },
77 ...
78 }@args:
79 let
80 testReads = tests.reads or [ ];
81 source =
82 fileset:
83 lib.fileset.toSource {
84 root = ../.;
85 inherit fileset;
86 };
87 build = lib.fileset.difference (lib.fileset.unions ([ dir ] ++ modules)) (
88 lib.fileset.unions ([ unread goTests ] ++ excluded ++ testReads)
89 );
90 withTests = lib.fileset.union (lib.fileset.difference (lib.fileset.unions [
91 build
92 (lib.fileset.intersection goTests dir)
93 ]) (lib.fileset.unions ([ unread ] ++ excluded))) (lib.fileset.unions testReads);
94 in
95 pkgs.buildGoModule (
96 finalAttrs:
97 removeAttrs args [
98 "dir"
99 "modules"
100 "excluded"
101 "tests"
102 ]
103 // {
104 version = "0";
105 src = source build;
106 modRoot = lib.path.removePrefix ../. dir;
107 # Standard library only, so no module is fetched.
108 vendorHash = null;
109 doCheck = false;
110 passthru.unit = finalAttrs.finalPackage.overrideAttrs (
111 prev:
112 removeAttrs tests [ "reads" ]
113 // {
114 pname = "${prev.pname}-unit";
115 src = source withTests;
116 doCheck = true;
117 }
118 );
119 }
120 );
121
122 # vendorHash = null builds in vendor mode, which has no reading of a local
123 # replace. The replaced module is a directory beside the program and
124 # nothing is fetched either way, so these builds read go.mod as written.
125 readsGoMod = ''
126 export GOFLAGS="''${GOFLAGS//-mod=vendor/-mod=mod}"
127 '';
128
129 # Markdown prose is filled paragraphs hard-wrapped at 100 columns
130 # (ida-1ec03b1). One flag string backs both the formatter app and the
131 # prose-format check, so the two cannot drift. Embedded-language
132 # formatting is off so fenced code blocks pass through byte-for-byte.
133 proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off";
134
135 # The valley command (status/), the raw Go binary with nothing wrapped:
136 # the verbs that read a change's state from origin and write its record,
137 # review and approve, the plan graph's verbs, the bus's, checks and
138 # self-update. status reads one change's request and outcome record from
139 # origin, and prints them as sentences or as the valley-status/v1 JSON
140 # object front ends read. ready, unready and withdraw write a change's
141 # record, with the one reading of a record the push hook shares
142 # (../change). rebase rebases a change's whole stack and adds each
143 # change's rebased head to its record, through the rebase engine
144 # (../rebase), which also reads how far each version has landed for the
145 # listings. knot reads and writes the plan graph, with the one reading of
146 # a graph the push hook shares (../knot), and plan shows that graph as a
147 # view. review reads a version's file delta and its approved version's
148 # with the one reading of valley-delta-v1 (../delta). self-update
149 # installs the tip of a branch of the-valley into the user's nix profile
150 # (../selfupdate). checks prints which checks a change requires, as the
151 # deriver (../derive) derives them from the policy the composed-policy
152 # reader (../composed) reads. Go, standard library only. It runs
153 # whatever git, cue, less, attest and nats are on PATH, so checks that
154 # put a failing stand-in for one ahead of it run this binary rather than
155 # a wrapper. Its unit tests hold its shell quoting to bash, so they need
156 # bash, lay pending out over real repositories, so they need git, and
157 # derive a change's checks under the verification schema, so they need
158 # cue and the schema. The plan's golden files (status/testdata/plan/) are
159 # valley-plan's, and no Go test reads them.
160 valley-unwrapped = goModule {
161 pname = "valley-unwrapped";
162 dir = ../status;
163 modules = [
164 ../change
165 ../composed
166 ../delta
167 ../derive
168 ../knot
169 ../note
170 ../rebase
171 ../selfupdate
172 ../telemetry
173 ];
174 excluded = [ ../status/testdata/plan ];
175 preBuild = readsGoMod;
176 tests = {
177 reads = [ ../schema/verification.cue ];
178 nativeCheckInputs = [
179 pkgs.bash
180 pkgs.git
181 pkgs.cue
182 ];
183 # A test that cannot find a tool it needs fails rather than skipping.
184 preCheck = ''
185 export VALLEY_TEST_TOOLS=required
186 '';
187 };
188 meta.mainProgram = "valley";
189 };
190
191 # The valley command with the tools it runs: git, natscli (tail and
192 # replay), cue (checks and approving, which compose the policy layers),
193 # less (review's notes) and attest (approving, which runs the checks and
194 # signs them). The verification schema travels with it, as it does with
195 # the integrator: only the-valley's own tree carries schema/, so a
196 # project's checkout has none to read. --set-default leaves an explicit
197 # VALLEY_VERIFICATION_SCHEMA in charge. It carries no stamp, so the
198 # packages the checks and the integrator use do not change with every
199 # commit.
200 valley-unstamped =
201 pkgs.runCommand "valley"
202 {
203 nativeBuildInputs = [ pkgs.makeWrapper ];
204 meta.mainProgram = "valley";
205 }
206 ''
207 mkdir -p $out/bin
208 makeWrapper ${lib.getExe valley-unwrapped} $out/bin/valley \
209 --prefix PATH : ${
210 lib.makeBinPath [
211 pkgs.git
212 pkgs.natscli
213 pkgs.cue
214 pkgs.less
215 attest
216 ]
217 } \
218 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
219 '';
220
221 # The installed package: the valley command stamped with its version, the
222 # shell completions at the standard paths home-manager/NixOS auto-link,
223 # and the tiny policy self-update's smoke test derives checks from
224 # (examples/policy/smoke/). The stamp is set on this outer wrapper alone,
225 # so the packages the checks and the integrator use do not change with
226 # every commit. The name carries the version as well: nix profile names
227 # the element valley from it, and self-update reads a hand-installed
228 # valley's commit from it.
229 valley =
230 pkgs.runCommand "valley-${version}"
231 {
232 inherit version;
233 nativeBuildInputs = [
234 pkgs.makeWrapper
235 pkgs.installShellFiles
236 ];
237 meta.mainProgram = "valley";
238 }
239 ''
240 mkdir -p $out/bin $out/share/valley
241 makeWrapper ${lib.getExe valley-unstamped} $out/bin/valley \
242 --set VALLEY_VERSION ${version}
243 installShellCompletion --bash --name valley ${../completions/valley.bash}
244 installShellCompletion --zsh --name _valley ${../completions/_valley}
245 cp -r --no-preserve=mode ${../examples/policy/smoke} $out/share/valley/smoke-policy
246 '';
247
248 # The check worker: one pass of `valley work` (status/worker.go), which
249 # builds the nix checks ready changes require and publishes the evidence
250 # before anyone approves them. It runs this flake's git, cue and attest,
251 # and the verification schema travels with it, as it does with the CLI.
252 # nix is the machine's own, for the reason attest gives.
253 valley-worker =
254 pkgs.runCommand "valley-worker"
255 {
256 nativeBuildInputs = [ pkgs.makeWrapper ];
257 meta.mainProgram = "valley-worker";
258 }
259 ''
260 mkdir -p $out/bin
261 makeWrapper ${lib.getExe valley-unwrapped} $out/bin/valley-worker \
262 --add-flags work \
263 --prefix PATH : ${
264 lib.makeBinPath [
265 pkgs.git
266 pkgs.cue
267 attest
268 ]
269 } \
270 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
271 '';
272
273 # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns.
274 prose-fmt = pkgs.writeShellApplication {
275 name = "valley-fmt";
276 runtimeInputs = [
277 pkgs.git
278 pkgs.prettier
279 ];
280 text = ''
281 cd "$(git rev-parse --show-toplevel)"
282 git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \
283 prettier ${proseFmtArgs} --write
284 '';
285 };
286
287 # The Phase 2 attestation helper (dcr-0de694f). Go, standard library
288 # only, note format included, and hence no module fetch at build time.
289 # Its unit tests need git and ssh-keygen. It reads signed notes, and
290 # writes and reads the statements in them and the valley-tree-v1 digests
291 # they name, with the note module (../note), the one reading of the
292 # envelope and its statement shared by the programs that write, verify
293 # and admit attestations. The conformance
294 # vectors (attest/conformance/) are attest-conformance's, and no Go test
295 # reads them.
296 attest-unwrapped = goModule {
297 pname = "valley-attest";
298 dir = ../attest;
299 modules = [
300 ../note
301 ../telemetry
302 ];
303 excluded = [ ../attest/conformance ];
304 preBuild = readsGoMod;
305 tests.nativeCheckInputs = [
306 pkgs.git
307 pkgs.openssh
308 ];
309 meta.mainProgram = "attest";
310 };
311
312 # The shipping form. git and cue are pinned to this flake's nixpkgs,
313 # so every host composes and vets a statement with the same tools.
314 # Signing needs no tool at all: a note is signed on crypto/ed25519
315 # inside the binary. `nix` is deliberately NOT pinned: the check being
316 # attested to must be built by the nix the machine actually runs, and
317 # a nix carried in here would be a second one.
318 attest =
319 pkgs.runCommand "valley-attest"
320 {
321 nativeBuildInputs = [ pkgs.makeWrapper ];
322 meta.mainProgram = "attest";
323 }
324 ''
325 mkdir -p $out/bin
326 makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \
327 --prefix PATH : ${
328 lib.makeBinPath [
329 pkgs.git
330 pkgs.cue
331 ]
332 } \
333 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue}
334 '';
335
336 # The identity registry compiler (dcr-b87f6e8). Go, standard library
337 # only, note format included, same trade as attest and the integrator, so
338 # no module is fetched. It reads the registry's export with exactjson
339 # (../exactjson). Its unit tests pin the note format's key hash
340 # against the reference vector attest is held to, and verify a real
341 # YubiKey attestation against the Yubico roots it carries. The test
342 # vectors (identity/testdata/) are read by the tests and by identity-e2e,
343 # never by the build.
344 identity-unwrapped = goModule {
345 pname = "valley-identity";
346 dir = ../identity;
347 modules = [
348 ../note
349 ../exactjson
350 ];
351 preBuild = readsGoMod;
352 tests.reads = [ ../identity/testdata ];
353 meta.mainProgram = "identity";
354 };
355
356 # The shipping form. git reads the registry out of the instance
357 # repository's tip and cue vets and exports it, both pinned to this
358 # flake's nixpkgs so every host compiles a registry the same way. The
359 # schema travels with the binary: a compiler judging a registry against a
360 # schema the consumer supplied would be no floor at all.
361 identity =
362 pkgs.runCommand "valley-identity"
363 {
364 nativeBuildInputs = [ pkgs.makeWrapper ];
365 meta.mainProgram = "identity";
366 }
367 ''
368 mkdir -p $out/bin
369 makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \
370 --prefix PATH : ${
371 lib.makeBinPath [
372 pkgs.git
373 pkgs.cue
374 ]
375 } \
376 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
377 '';
378
379 # The security-key signature verifier (sigverify/README.md). Go,
380 # standard library only, so no module is fetched.
381 #
382 # The git its git-tag command runs is fixed at build time to this
383 # flake's git, as an absolute store path compiled into the binary. A
384 # wrapper that puts git on PATH would leave the binary inside it looking
385 # git up on PATH, and a caller's environment could change which git that
386 # is. Compiled in, there is no PATH lookup to change.
387 #
388 # Its tests run against real ssh-keygen and git. The security-key cases
389 # among them need OpenSSH's sk-dummy authenticator, which the
390 # sigverify-unit check supplies. Building this package runs no test, so a
391 # consumer building it never builds OpenSSH.
392 sigverify = goModule {
393 pname = "valley-sigverify";
394 dir = ../sigverify;
395 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
396 tests.nativeCheckInputs = [
397 pkgs.git
398 pkgs.openssh
399 ];
400 meta.mainProgram = "sigverify";
401 };
402
403 # Telemetry (../telemetry): the diagnostic records, the bounded streams
404 # that hold them and the telemetry command's grammar, which the
405 # producers, the git user's shell and the collector share, and the
406 # collector itself, valley-telemetry serve, which the host runs per
407 # connection to its socket. Go, standard library only.
408 telemetry = goModule {
409 pname = "valley-telemetry";
410 dir = ../telemetry;
411 meta.mainProgram = "valley-telemetry";
412 };
413
414 # The pre-receive hook's policy (valleyhook/): what a push may write to a
415 # project a valley host serves. It validates a push to the plan graph
416 # with the one reading of a graph the CLI shares (../knot), reads a
417 # pushed approval with the approval format (../approval), and reads the
418 # project's push policy with exactjson (../exactjson). The same
419 # binary is the git user's login shell, which derives the pushing
420 # principal and pauses pushes until the host has converged. Go, standard
421 # library only, so no module is fetched.
422 #
423 # The git it reads the repository with is fixed at build time, the way
424 # sigverify's is: an absolute store path compiled into the binary, so no
425 # PATH a hook runs under can change which git that is.
426 valleyhook = goModule {
427 pname = "valley-valleyhook";
428 dir = ../valleyhook;
429 modules = [
430 ../approval
431 ../note
432 ../change
433 ../knot
434 ../exactjson
435 ../telemetry
436 ];
437 # The approval library alone: its command, signer and fixtures are the
438 # approval package's.
439 excluded = approvalExtras;
440 preBuild = readsGoMod;
441 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
442 # The walk's and the change records' tests build hostile objects with
443 # real git.
444 tests.nativeCheckInputs = [ pkgs.git ];
445 meta.mainProgram = "valleyhook";
446 };
447
448 # The PIV session signer (approval/piv/): a small C program on libykpiv
449 # that holds a YubiKey exclusively for a run of approval signatures,
450 # asks for the PIN once on the terminal, and signs each statement with a
451 # touch. It is C so that the Go modules stay standard library only: the
452 # approval command runs it rather than linking a smart card library. It
453 # calls pcsc-lite, libykpiv's own transport, directly only to connect
454 # exclusively and to see a reset. libykpiv reconnects after a reset or a
455 # removal (_ykpiv_begin_transaction) in shared mode; the signer's
456 # libykpiv reconnects exclusively, so no command reaches the card over a
457 # shared connection even when the card changes between the helper's
458 # check and libykpiv's call. nixpkgs' default hardening applies.
459 piv-signer = pkgs.stdenv.mkDerivation {
460 pname = "valley-piv-signer";
461 version = "0";
462 src = ../approval/piv;
463 buildInputs = [
464 (pkgs.yubico-piv-tool.overrideAttrs (old: {
465 postPatch = (old.postPatch or "") + ''
466 substituteInPlace lib/ykpiv.c \
467 --replace-fail 'SCardReconnect(state->card, SCARD_SHARE_SHARED,' \
468 'SCardReconnect(state->card, SCARD_SHARE_EXCLUSIVE,' \
469 --replace-fail 'SCardConnect(state->context, state->reader, SCARD_SHARE_SHARED,' \
470 'SCardConnect(state->context, state->reader, SCARD_SHARE_EXCLUSIVE,'
471 '';
472 }))
473 pkgs.pcsclite
474 ];
475 buildPhase = ''
476 $CC -std=c11 -O2 -Wall -Wextra -Werror -o valley-piv-signer valley-piv-signer.c \
477 -I${lib.getDev pkgs.pcsclite}/include/PCSC -lykpiv -lpcsclite
478 '';
479 installPhase = ''
480 install -Dm755 valley-piv-signer $out/bin/valley-piv-signer
481 '';
482 meta.mainProgram = "valley-piv-signer";
483 };
484
485 # Approval statements (approval/): the-valley/approval/v1's written form,
486 # signing it with a software key or a YubiKey's PIV slot, and verifying
487 # an approval note under the approval signers files. Go, standard library
488 # only. The PIV signer it runs is fixed at build time, the way
489 # sigverify's git is: an absolute store path compiled into the binary.
490 # Its unit tests run that signer with no smart card service to reach, so
491 # no device is touched.
492 approval = goModule {
493 pname = "valley-approval";
494 dir = ../approval;
495 modules = [
496 ../note
497 ../change
498 ];
499 excluded = [ ../approval/piv ];
500 subPackages = [ "cmd/approval" ];
501 preBuild = readsGoMod;
502 ldflags = [ "-X main.pivSigner=${lib.getExe piv-signer}" ];
503 tests = {
504 reads = [ ../approval/testdata ];
505 subPackages = [
506 "."
507 "cmd/approval"
508 ];
509 VALLEY_PIV_SIGNER = lib.getExe piv-signer;
510 };
511 meta.mainProgram = "approval";
512 };
513
514 # The note envelope (note/). Its tests hold it to refusing every line a
515 # relayer could append that one of its readers would refuse.
516 note = goModule {
517 pname = "valley-note";
518 dir = ../note;
519 };
520
521 # The strict JSON reading (exactjson/) that the identity compiler, the
522 # pre-receive hook and read access read CUE's exports with: every key
523 # spelled exactly as the field it is read into, and no key twice in one
524 # object. Its tests
525 # hold it to each spelling encoding/json alone would fold into a field.
526 exactjson = goModule {
527 pname = "valley-exactjson";
528 dir = ../exactjson;
529 };
530
531 # The web UI (web/). Package export is the contract between the
532 # publisher on a valley host and the web server it pushes to: the public
533 # export and the projection onto it, the signed manifest, the upload and
534 # the receiver's state directory. It reads names and IDs by the plan
535 # graph's rules (../knot), the manifest with the note module (../note),
536 # and who may read a project through ../readaccess, which reads with
537 # ../exactjson, as the publisher reads the host declaration and package
538 # export reads export.json. The program
539 # built from it is the publisher, cmd/valley-web-publish, which
540 # valley-web-publish below wraps. The server and its command are built by
541 # valley-web below, and the receiver by valley-web-receive. The export's
542 # tests accept bundles and build the shared fixture's repositories with
543 # real git. The publisher's tests run it against a valley built with real
544 # git and the valley command's raw binary, read each project's policy/read and the instance's identity
545 # registry with cue under schema/read.cue and schema/identity.cue, and
546 # upload to a receiver over a real store, and to the real receiver, whose
547 # state directory the real valley-web serves. One lands a change as the
548 # integrator does, with a transfer statement attest signs under a key
549 # ssh-keygen makes, which the CLI verifies with attest. Each test's host
550 # declaration is exported by cue under schema/valley.cue, as the module
551 # exports the host's. go vet runs after them. The fixture's expected
552 # export (export/exporttest/testdata/) is read by the tests alone, and so
553 # are README.md and docs/, whose links package markdown's tests resolve. Go,
554 # standard library only, so no module is fetched.
555 web = goModule {
556 pname = "valley-web-module";
557 dir = ../web;
558 modules = [
559 ../knot
560 ../note
561 ../readaccess
562 ../exactjson
563 ];
564 excluded = [
565 ../web/server
566 ../web/readerstate
567 ../web/cmd/valley-web
568 ../web/cmd/valley-web-receive
569 ];
570 preBuild = readsGoMod;
571 tests = {
572 reads = [
573 ../web/export/exporttest/testdata
574 ../README.md
575 ../docs
576 ../schema/read.cue
577 ../schema/identity.cue
578 ../schema/valley.cue
579 ];
580 nativeCheckInputs = [
581 pkgs.git
582 pkgs.bash
583 pkgs.cue
584 pkgs.openssh
585 valley-unwrapped
586 attest
587 ];
588 # A test that cannot find a tool it needs fails rather than skipping.
589 preCheck = ''
590 export VALLEY_TEST_TOOLS=required
591 export VALLEY_WEB=${lib.getExe valley-web}
592 export VALLEY_WEB_RECEIVE=${lib.getExe valley-web-receive}
593 '';
594 postCheck = ''
595 go vet ./...
596 '';
597 };
598 meta.mainProgram = "valley-web-publish";
599 };
600
601 # Read access (readaccess/): the two consents a principal needs to read a
602 # project, read out of git commits with cue under schema/read.cue and
603 # schema/identity.cue, and the evaluator that intersects them. The web
604 # publisher reads through it, and its command is the coverage report,
605 # which valley-read-coverage below wraps. It reads cue's exports with
606 # exactjson (../exactjson). Its tests read real commits with real git and
607 # the real cue under both schemas, and go vet runs after them. Go,
608 # standard library only, so no module is fetched.
609 readaccess = goModule {
610 pname = "valley-readaccess";
611 dir = ../readaccess;
612 modules = [ ../exactjson ];
613 preBuild = readsGoMod;
614 tests = {
615 reads = [
616 ../schema/read.cue
617 ../schema/identity.cue
618 ];
619 nativeCheckInputs = [
620 pkgs.git
621 pkgs.cue
622 ];
623 # A test that cannot find a tool it needs fails rather than skipping.
624 preCheck = ''
625 export VALLEY_TEST_TOOLS=required
626 '';
627 postCheck = ''
628 go vet ./...
629 '';
630 };
631 meta.mainProgram = "valley-read-coverage";
632 };
633
634 # The coverage report: which projects a principal cannot read at a
635 # destination, and which consent each lacks. It runs this flake's git and
636 # cue, and both schemas travel with it.
637 valley-read-coverage =
638 pkgs.runCommand "valley-read-coverage"
639 {
640 nativeBuildInputs = [ pkgs.makeWrapper ];
641 meta.mainProgram = "valley-read-coverage";
642 }
643 ''
644 mkdir -p $out/bin
645 makeWrapper ${lib.getExe readaccess} $out/bin/valley-read-coverage \
646 --prefix PATH : ${
647 lib.makeBinPath [
648 pkgs.git
649 pkgs.cue
650 ]
651 } \
652 --set-default VALLEY_READ_SCHEMA ${../schema/read.cue} \
653 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
654 '';
655
656 # The web UI's server (web/server/) and its command, valley-web
657 # (web/cmd/valley-web/), which serves the active export of a receiver's
658 # state directory to its readers, and keeps their sessions in the
659 # browser state (web/readerstate/). It reads the export and the
660 # state directory with package export, so its source is the web
661 # module, less the fixture's expected export, which only web's tests
662 # read. Package export reads export.json with ../exactjson, so its
663 # source holds that module too. Of ../readaccess, which the publisher
664 # reads through and the server does not, it reads the go.mod alone,
665 # which go reads to resolve the module. The git it reads repositories
666 # with is fixed at build time, the way valleyhook's is. Its tests run the real program over the shared
667 # fixture and real git, and the server over the real publisher's output
668 # (web-published), and go vet runs after them. The crash tests and the
669 # browser test are skipped there: web-reader-state-crash and
670 # web-reader-browser-e2e (nix/checks/) run them from the same tests.
671 # Go, standard library only, so no module is fetched.
672 valley-web = goModule {
673 pname = "valley-web";
674 dir = ../web;
675 modules = [
676 ../knot
677 ../note
678 ../readaccess/go.mod
679 ../exactjson
680 ];
681 excluded = [
682 ../web/export/exporttest/testdata
683 ../web/cmd/valley-web-publish
684 ../web/cmd/valley-web-receive
685 ];
686 subPackages = [ "cmd/valley-web" ];
687 preBuild = readsGoMod;
688 ldflags = [ "-X the-valley/web/export.GitProgram=${lib.getExe pkgs.git}" ];
689 tests = {
690 subPackages = [
691 "server"
692 "readerstate"
693 "cmd/valley-web"
694 ];
695 checkFlags = [ "-skip=^(TestCrash|TestBrowser)" ];
696 nativeCheckInputs = [ pkgs.git ];
697 # A test that cannot find the publisher's output fails rather than
698 # skipping.
699 preCheck = ''
700 export VALLEY_TEST_TOOLS=required
701 export VALLEY_WEB_PUBLISHED=${web-published}
702 '';
703 postCheck = ''
704 go vet ./server/... ./readerstate/... ./cmd/valley-web/...
705 '';
706 };
707 meta.mainProgram = "valley-web";
708 };
709
710 # The real publisher's signed output over a small valley with a public
711 # project, two private projects each one named reader may read, and a
712 # project with no effective reader, written with --out by the packaged publisher and CLI
713 # (nix/checks/web-published.sh). valley-web's tests read it, so the
714 # server is tested against what the publisher writes. It is no check of
715 # its own.
716 web-published =
717 pkgs.runCommand "valley-web-published"
718 {
719 nativeBuildInputs = [
720 pkgs.git
721 pkgs.cue
722 pkgs.jq
723 valley-unstamped
724 valley-web-publish
725 ];
726 schema = ../schema/valley.cue;
727 }
728 (builtins.readFile ./checks/web-published.sh);
729
730 # The web export receiver, valley-web-receive (web/cmd/valley-web-receive/),
731 # which takes the publisher's uploads into a state directory that
732 # valley-web serves. Its source is the web module less the publisher,
733 # the server and its command, which it does not build, and the fixture's
734 # expected export. Package export reads export.json with ../exactjson, so
735 # its source holds that module too; of ../readaccess it reads the go.mod
736 # alone, as valley-web does. The git it accepts bundles with is fixed at
737 # build time. Its tests run the real program over the shared fixture and
738 # real git, with the real valley-web serving its state directory, and go
739 # vet runs after them. Go, standard library only.
740 valley-web-receive = goModule {
741 pname = "valley-web-receive";
742 dir = ../web;
743 modules = [
744 ../knot
745 ../note
746 ../readaccess/go.mod
747 ../exactjson
748 ];
749 excluded = [
750 ../web/export/exporttest/testdata
751 ../web/cmd/valley-web-publish
752 ../web/cmd/valley-web
753 ../web/server
754 ../web/readerstate
755 ];
756 subPackages = [ "cmd/valley-web-receive" ];
757 preBuild = readsGoMod;
758 ldflags = [ "-X the-valley/web/export.GitProgram=${lib.getExe pkgs.git}" ];
759 tests = {
760 nativeCheckInputs = [ pkgs.git ];
761 # A test that cannot find valley-web fails rather than skipping.
762 preCheck = ''
763 export VALLEY_TEST_TOOLS=required
764 export VALLEY_WEB=${lib.getExe valley-web}
765 '';
766 postCheck = ''
767 go vet ./cmd/valley-web-receive/...
768 '';
769 };
770 meta.mainProgram = "valley-web-receive";
771 };
772
773 # The edge's container image, for a web server that does not run NixOS:
774 # valley-web and valley-web-receive, and the git both run, in one image.
775 # The two run as two containers of it over one state directory, as two
776 # users of one group: the receiver as valley-web-receive, which writes
777 # the directory, and valley-web as valley-web, which reads it. The image
778 # names both users in its /etc/passwd, and holds the state directory,
779 # owned by the receiver and readable by the group alone, so a volume
780 # mounted there starts with that owner and mode. In it, browser/ is
781 # valley-web's alone, mode 0700: the browser state, the one place
782 # valley-web writes. It runs as valley-web unless it is told otherwise,
783 # and starts no program of its own: each container names its command.
784 edgeUsers = {
785 receiver = {
786 name = "valley-web-receive";
787 uid = 990;
788 };
789 web = {
790 name = "valley-web";
791 uid = 991;
792 };
793 gid = 990;
794 };
795 valley-web-image = pkgs.dockerTools.buildLayeredImage {
796 name = "valley-web";
797 contents = [
798 valley-web
799 valley-web-receive
800 (pkgs.dockerTools.fakeNss.override {
801 extraPasswdLines = with edgeUsers; [
802 "${receiver.name}:x:${toString receiver.uid}:${toString gid}:valley-web-receive:/var/empty:/bin/false"
803 "${edgeUsers.web.name}:x:${toString edgeUsers.web.uid}:${toString gid}:valley-web:/var/empty:/bin/false"
804 ];
805 extraGroupLines = [ "valley-web:x:${toString edgeUsers.gid}:" ];
806 })
807 ];
808 fakeRootCommands = ''
809 mkdir -p var/lib/valley-web/browser tmp
810 chown ${toString edgeUsers.receiver.uid}:${toString edgeUsers.gid} var/lib/valley-web
811 chmod 0750 var/lib/valley-web
812 chown ${toString edgeUsers.web.uid}:${toString edgeUsers.gid} var/lib/valley-web/browser
813 chmod 0700 var/lib/valley-web/browser
814 chmod 1777 tmp
815 '';
816 config = {
817 User = edgeUsers.web.name;
818 WorkingDirectory = "/";
819 Env = [ "PATH=/bin" ];
820 Volumes."/var/lib/valley-web" = { };
821 };
822 };
823
824 # The web publisher: one pass of valley-web-publish, which reads a valley
825 # host's public projects through the CLI's JSON commands and uploads them
826 # to a web receiver. It runs this flake's git, cue and CLI, and the two
827 # schemas it reads read access under travel with it, as the verification
828 # schema does with the CLI.
829 valley-web-publish =
830 pkgs.runCommand "valley-web-publish"
831 {
832 nativeBuildInputs = [ pkgs.makeWrapper ];
833 meta.mainProgram = "valley-web-publish";
834 }
835 ''
836 mkdir -p $out/bin
837 makeWrapper ${lib.getExe web} $out/bin/valley-web-publish \
838 --prefix PATH : ${
839 lib.makeBinPath [
840 pkgs.git
841 pkgs.cue
842 valley-unstamped
843 ]
844 } \
845 --set-default VALLEY_READ_SCHEMA ${../schema/read.cue} \
846 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
847 '';
848
849 # The plan graph (knot/): its canonical form, its layout, cycles and
850 # transitions, held by its own tests. The package is knotcheck, which
851 # knot-agree runs to hold the Go validator to the CUE schema and the
852 # knowledge lint.
853 knot = goModule {
854 pname = "valley-knot";
855 dir = ../knot;
856 meta.mainProgram = "knotcheck";
857 };
858
859 # The rebase engine (rebase/): a family of stacked changes captured from
860 # one snapshot of origin, rebased in a throwaway worktree, and published
861 # as one atomic branch push and one record append per change. It verifies
862 # the integrator's transfer statements with the note module (../note).
863 # Its tests run against real repositories, real pushes and real git, with
864 # go vet after them. Go, standard library only, so no module is fetched.
865 rebase = goModule {
866 pname = "valley-rebase";
867 dir = ../rebase;
868 modules = [
869 ../change
870 ../note
871 ];
872 preBuild = readsGoMod;
873 tests = {
874 nativeCheckInputs = [ pkgs.git ];
875 postCheck = ''
876 go vet ./...
877 '';
878 };
879 };
880
881 # Self-update (selfupdate/): the installed CLI brought up to the tip of a
882 # branch, built with nix and installed into the user's profile. Its tests,
883 # with go vet after them, use a real bare repository for ls-remote and a
884 # stand-in for nix whose builds are small scripts, so nothing is built and
885 # no profile is touched. Go, standard library only, so no module is
886 # fetched.
887 selfupdate = goModule {
888 pname = "valley-selfupdate";
889 dir = ../selfupdate;
890 tests = {
891 nativeCheckInputs = [ pkgs.git ];
892 postCheck = ''
893 go vet ./...
894 '';
895 };
896 };
897
898 # The deriver of required checks (derive/): a change's paths read from
899 # git diff -z, globs matched byte by byte, and the required checks, pins,
900 # carry mode and unclassified paths derived from the whole composed
901 # policy (../composed). Its tests, with go vet after them, derive over the
902 # repository's own policy and the example policies, with changes drawn
903 # from a fixed seed, so they read the schema and those policies, and need
904 # git and cue. Go, standard library only, so no module is fetched.
905 derive = goModule {
906 pname = "valley-derive";
907 dir = ../derive;
908 modules = [ ../composed ];
909 preBuild = readsGoMod;
910 tests = {
911 reads = [
912 ../schema/verification.cue
913 ../examples/policy
914 ../policy/project
915 ];
916 nativeCheckInputs = [
917 pkgs.git
918 pkgs.cue
919 ];
920 # A test that cannot find a tool it needs fails rather than skipping.
921 preCheck = ''
922 export VALLEY_TEST_TOOLS=required
923 '';
924 postCheck = ''
925 go vet ./...
926 '';
927 };
928 };
929
930 # The composed-policy reader (composed/): each layer read out of git,
931 # present, absent or refused. Its tests
932 # read layers from real repositories, some with objects removed, and go
933 # vet runs after them.
934 composed = goModule {
935 pname = "valley-composed";
936 dir = ../composed;
937 tests = {
938 nativeCheckInputs = [ pkgs.git ];
939 # A test that cannot find git fails rather than skipping.
940 preCheck = ''
941 export VALLEY_TEST_TOOLS=required
942 '';
943 postCheck = ''
944 go vet ./...
945 '';
946 };
947 };
948
949 # The change record (change/). Its tests cover its strict reading, its
950 # fuzz target's seed corpus, and its branch-name rule, which they hold to
951 # real git's check-ref-format.
952 change = goModule {
953 pname = "valley-change";
954 dir = ../change;
955 tests.nativeCheckInputs = [ pkgs.git ];
956 };
957
958 # valley-delta-v1 (delta/): the encoding's strict reading, and the delta
959 # read from real trees. Its unit tests run go vet, the conformance
960 # vectors' script in its --check mode, which holds the committed expected
961 # results to what the script computes, and go test, which builds each
962 # vector's trees with real git and holds the Go code to those results.
963 delta = goModule {
964 pname = "valley-delta";
965 dir = ../delta;
966 tests = {
967 reads = [ ../delta/testdata ];
968 nativeCheckInputs = [
969 pkgs.git
970 pkgs.python3
971 ];
972 preCheck = ''
973 go vet ./...
974 python3 -I testdata/vectors.py --check testdata
975 '';
976 };
977 };
978
979 # Review records (review/): the-valley/review/v1's strict reading, a
980 # review's binding to the version it judged, recomputed from git within a
981 # budget, and the append-only history a review is kept in, with the check
982 # an append is held to. It reads change records with the one reading of a
983 # record the push hook shares (../change), and the delta with
984 # valley-delta-v1's bounded reading (../delta). Its tests build every
985 # repository they read with real git and run its fuzz target's seed
986 # corpus, and go vet runs after them. Go, standard library only, so no
987 # module is fetched.
988 review = goModule {
989 pname = "valley-review";
990 dir = ../review;
991 modules = [
992 ../change
993 ../delta
994 ];
995 preBuild = readsGoMod;
996 tests = {
997 nativeCheckInputs = [ pkgs.git ];
998 postCheck = ''
999 go vet ./...
1000 '';
1001 };
1002 };
1003
1004 # The restacker (restack/): `valley-restacker run` reconciles on an
1005 # interval, and `once` is one pass. A pass reads origin through the
1006 # rebase engine (../rebase), plans which families to restack and which
1007 # to hold, keeps the writes in its journal, and pushes them;
1008 # `once --report-only` pushes none. It reads the cap on its
1009 # automatic versions through the composed-policy reader it shares with
1010 # the integrator (../composed), and a signer projects file through the
1011 # note module (../note). Its unit tests cover request association,
1012 # outcome currency, capacity and report bounds, and, against real git,
1013 # the journal of writes, recovery's decisions and publishing through the
1014 # rendered hook, its environment, its
1015 # scratch repository's identity, its state directory's lock and binding,
1016 # and the deadline on what it runs. go vet runs after them. Go, standard
1017 # library only, so no module is fetched.
1018 restack-unwrapped = goModule {
1019 pname = "valley-restacker";
1020 dir = ../restack;
1021 modules = [
1022 ../change
1023 ../composed
1024 ../note
1025 ../rebase
1026 ];
1027 # The program alone: e2e/transport is a module of restack-e2e's own,
1028 # which neither this build nor its tests read.
1029 excluded = [ ../restack/e2e ];
1030 subPackages = [ "." ];
1031 preBuild = readsGoMod;
1032 tests = {
1033 nativeCheckInputs = [ pkgs.git ];
1034 postCheck = ''
1035 go vet ./...
1036 '';
1037 };
1038 # The module is named for its directory; the program, for what it is.
1039 postInstall = ''
1040 mv $out/bin/restack $out/bin/valley-restacker
1041 '';
1042 meta.mainProgram = "valley-restacker";
1043 };
1044
1045 # The shipping form: git to read origin and cue to compose the policy,
1046 # both pinned to this flake's nixpkgs, with the packaged verification
1047 # schema as the default.
1048 valley-restacker =
1049 pkgs.runCommand "valley-restacker"
1050 {
1051 nativeBuildInputs = [ pkgs.makeWrapper ];
1052 meta.mainProgram = "valley-restacker";
1053 }
1054 ''
1055 mkdir -p $out/bin
1056 makeWrapper ${lib.getExe restack-unwrapped} $out/bin/valley-restacker \
1057 --prefix PATH : ${
1058 lib.makeBinPath [
1059 pkgs.git
1060 pkgs.cue
1061 ]
1062 } \
1063 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
1064 '';
1065
1066 # The Phase 3 integrator (dcr-439b771). Go, standard library only,
1067 # same trade as attest, so no module is fetched. It reads change records
1068 # with the one reading of a record the push hook shares (../change),
1069 # verifier keys with the one reading of a key the note format defines
1070 # (../note), and derives required checks with the deriver (../derive).
1071 # The instance identity it publishes is the approval format's
1072 # (../approval).
1073 integrator-unwrapped = goModule {
1074 pname = "valley-integrator";
1075 dir = ../integrator;
1076 modules = [
1077 ../approval
1078 ../change
1079 ../composed
1080 ../derive
1081 ../note
1082 ../telemetry
1083 ];
1084 # The scenarios (integrator/e2e/) are integrator-e2e's, and no Go test
1085 # reads them. Of the approval module, only its library is read.
1086 excluded = [ ../integrator/e2e ] ++ approvalExtras;
1087 preBuild = readsGoMod;
1088 tests = {
1089 # The policy bridge's tests compose a policy under the verification
1090 # schema with real cue, and derive over diffs in real git.
1091 reads = [ ../schema/verification.cue ];
1092 nativeCheckInputs = [
1093 pkgs.git
1094 pkgs.cue
1095 ];
1096 };
1097 meta.mainProgram = "integrator";
1098 };
1099
1100 # The shipping form. The integrator drives four programs rather than
1101 # reimplementing what they do: attest judges every statement and
1102 # every note, cue composes the policy, git holds the requests and the
1103 # refs, and nats publishes its events. All four are pinned to this
1104 # flake's nixpkgs so an instance's integrators agree; `nix` is
1105 # deliberately absent for the same reason it is absent from attest —
1106 # a closure must be recomputed by the nix the machine actually runs.
1107 integrator =
1108 pkgs.runCommand "valley-integrator"
1109 {
1110 nativeBuildInputs = [ pkgs.makeWrapper ];
1111 meta.mainProgram = "integrator";
1112 }
1113 ''
1114 mkdir -p $out/bin
1115 makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \
1116 --prefix PATH : ${
1117 lib.makeBinPath [
1118 pkgs.git
1119 pkgs.cue
1120 pkgs.natscli
1121 attest
1122 ]
1123 } \
1124 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \
1125 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \
1126 --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue}
1127 '';
1128}