gunk-dev/the-valley

packages.nix

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