gunk-dev/the-valley

packages.nix

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