gunk-dev/the-valley

hosts.nix

source30e24c82839bmain's published tip history
20.7 KiB
1# The hosts the module checks are about: eval-only instantiations of the
2# NixOS module, one per declaration worth holding it to. Evaluating one
3# catches option-surface and cue-export regressions without building or
4# booting anything, and the hook checks read the scripts these render.
5{
6 pkgs,
7 lib,
8 self,
9 # Whether the hosts run stand-ins for this flake's programs rather than
10 # the programs themselves.
11 stubbed ? false,
12}:
13let
14 # The stand-ins: the integrator, the identity compiler, valleyhook, the
15 # restacker, pushreason and the onMain bridge, each a script with the
16 # program's name that fails if anything runs it.
17 # A host rendered over them renders the same units, hooks and scripts,
18 # with the stand-ins' store paths where the programs' would be. None of
19 # them changes when a program does, so a check that reads only what the
20 # module renders changes only when the module or the check does. A check
21 # that runs a program takes the hosts that run the real one.
22 stub =
23 name:
24 pkgs.writeShellScriptBin name ''
25 echo "${name}: a stand-in the module checks render hosts over, which runs nothing" >&2
26 exit 1
27 '';
28 stubs = lib.genAttrs [ "integrator" "identity" "valleyhook" "pushreason" "onmain" ] stub // {
29 restacker = stub "valley-restacker";
30 };
31
32 # Each check host shares the machine baseline and supplies its own
33 # declaration (and, where the declaration asks for backup, the secret
34 # paths).
35 #
36 # Each host is also handed the package set the checks already use,
37 # rather than importing nixpkgs again for itself. It is the same nixpkgs
38 # with the same configuration, so every host renders the same
39 # derivations. module-eval evaluates every host here, and importing
40 # nixpkgs once per host was half of its evaluation time.
41 mkHost =
42 module:
43 lib.nixosSystem {
44 inherit pkgs;
45 modules = [
46 self.nixosModules.default
47 {
48 fileSystems."/" = {
49 device = "/dev/disk/by-label/nixos";
50 fsType = "ext4";
51 };
52 boot.loader.grub.enable = false;
53 system.stateVersion = "25.11";
54 services.valley = {
55 enable = true;
56 authorizedKeys = [
57 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck0 valley-check"
58 ];
59 };
60 }
61 (lib.mkIf stubbed { services.valley.packages = stubs; })
62 module
63 ];
64 };
65
66 # The same controllers as restrictedHost's with the check worker on and
67 # the build slice on at its defaults, writing neither the worker's
68 # projects nor its signer's.
69 workerBase = {
70 services.valley = {
71 config = ../../examples/hosts/protected.cue;
72 buildSlice.enable = true;
73 integrator = {
74 enable = true;
75 signingKeyFile = "/run/agenix/valley-integrator-key";
76 knownSignersFile = "/var/lib/valley-instance/known_signers";
77 instanceProject = "sealed";
78 };
79 worker = {
80 enable = true;
81 signerName = "worker.valley.invalid/attestations";
82 };
83 };
84 };
85
86 # The check worker for two of the projects the controllers serve, its
87 # signer restricted to exactly those two. It is bound here so the hosts
88 # the module must refuse can be made from it.
89 workerHost = mkHost {
90 imports = [ workerBase ];
91 services.valley = {
92 integrator.signerProjects."worker.valley.invalid/attestations" = [
93 "sealed"
94 "guarded"
95 ];
96 worker.projects = [
97 "guarded"
98 "sealed"
99 ];
100 };
101 };
102in
103{
104 # The example declaration enables backup, so this host supplies
105 # the four secret paths (eval-only placeholders).
106 host = mkHost {
107 services.valley = {
108 config = ../../examples/host.cue;
109 backup = {
110 repositoryFile = "/run/agenix/valley-restic-repo";
111 passwordFile = "/run/agenix/valley-restic-password";
112 sshKeyFile = "/run/agenix/valley-git-ssh-key";
113 knownHostsFile = "/var/lib/valley-backup/known_hosts";
114 };
115 };
116 };
117
118 # A declaration with no optional field set at all — no backup, no
119 # protection — must keep evaluating exactly as it did before those
120 # fields existed: zero restic machinery, no hook.
121 noBackupHost = mkHost {
122 services.valley.config = ../../examples/hosts/no-backup.cue;
123 };
124
125 # The example declaration with the secret paths left unset: the
126 # module must refuse with assertions naming the missing options.
127 noSecretsHost = mkHost {
128 services.valley.config = ../../examples/host.cue;
129 };
130
131 # Bus enabled, minimal declaration. The bus-e2e check drives this
132 # host's rendered hooks and stream-init against a real server.
133 busHost = mkHost {
134 services.valley = {
135 config = ../../examples/hosts/bus.cue;
136 bus.enable = true;
137 };
138 };
139
140 # Two mirror URLs the sandbox can serve: git resolves a relative
141 # URL against the pushing repo's directory, so the mirror-e2e
142 # check drives the rendered script with no substitution at all —
143 # one reachable sibling repo, one that does not exist.
144 mirrorHost = mkHost {
145 services.valley.config = ../../examples/hosts/mirrors.cue;
146 # Client environment the push boundary does not read stays acceptable.
147 services.openssh.settings.AcceptEnv = "LANG LC_*";
148 };
149
150 # The protection declaration, plus the machine half it needs: keys
151 # bound to the principal names it uses, and the approve and restack
152 # grants held by hand. requester holds both grants, so the hook checks
153 # read a principal whose approve grant the restack grant leaves as it
154 # is. The approve grant is declared under both its names: requester is
155 # named under both, and approver under approve alone. The baseline key
156 # above is anonymous and stays in the list, so this host also renders
157 # the two forms of key side by side.
158 protectedHost = mkHost {
159 services.valley = {
160 config = ../../examples/hosts/protected.cue;
161 authorizedKeys = [
162 {
163 principal = "integrator";
164 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck1 integrator";
165 }
166 {
167 principal = "contributor";
168 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
169 }
170 {
171 principal = "requester";
172 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck3 requester";
173 }
174 {
175 principal = "restacker";
176 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck4 restacker";
177 }
178 {
179 principal = "approver";
180 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck5 approver";
181 }
182 ];
183 grants.request = [ "requester" ];
184 grants.approve = [
185 "approver"
186 "requester"
187 ];
188 grants.restack = [
189 "restacker"
190 "requester"
191 ];
192 # The keys attestations pushed here are checked against: the ones a
193 # controller would accept evidence from, though this host runs none.
194 integrator.knownSignersFile = "/var/lib/valley-instance/known_signers";
195 # released composes a hook of its own after the push policy: it
196 # refuses branches under frozen/, which the policy would accept.
197 extraPreReceive.released = pkgs.writeShellScript "valley-check-released-pre-receive" ''
198 while read -r _ _ ref; do
199 case "$ref" in
200 refs/heads/frozen/*)
201 echo "released: $ref is frozen" >&2
202 exit 1
203 ;;
204 esac
205 done
206 '';
207 };
208 };
209
210 # The same protection declaration with the controllers switched on. The
211 # integrator serves exactly what that declaration protects, so this host
212 # renders two controllers and no third; the paths are eval-only
213 # placeholders, like the backup host's.
214 integratorHost = mkHost {
215 services.valley = {
216 config = ../../examples/hosts/protected.cue;
217 integrator = {
218 enable = true;
219 signingKeyFile = "/run/agenix/valley-integrator-key";
220 knownSignersFile = "/var/lib/valley-instance/known_signers";
221 # The floor comes from "sealed", which declares protection, as the
222 # instance repository has to. Every other controller here reads the
223 # floor out of a repository that is not the one it serves, which is
224 # the cross-repository read the unit has to grant.
225 instanceProject = "sealed";
226 };
227 };
228 };
229
230 # The same controllers with a check worker's signer restricted to two of
231 # the projects they serve, and a retired signer restricted to none.
232 restrictedHost = mkHost {
233 services.valley = {
234 config = ../../examples/hosts/protected.cue;
235 integrator = {
236 enable = true;
237 signingKeyFile = "/run/agenix/valley-integrator-key";
238 knownSignersFile = "/var/lib/valley-instance/known_signers";
239 instanceProject = "sealed";
240 signerProjects = {
241 "worker.valley.invalid/attestations" = [
242 "guarded"
243 "sealed"
244 ];
245 "retired.valley.invalid/attestations" = [ ];
246 };
247 };
248 };
249 };
250
251 inherit workerHost;
252
253 # The check worker with neither list written: it builds for every project
254 # the controllers serve, and its signer counts in exactly those.
255 defaultWorkerHost = mkHost workerBase;
256
257 # The check worker narrowed to one project, its signer's list unwritten:
258 # the signer counts in that one project alone.
259 narrowedWorkerHost = mkHost {
260 imports = [ workerBase ];
261 services.valley.worker.projects = [ "guarded" ];
262 };
263
264 # Both lists written, the signer's narrower than the worker's. A module
265 # entry merged into the host's, rather than replaced by it, would make
266 # the two lists match.
267 mismatchedWorkerHost = mkHost {
268 imports = [ workerBase ];
269 services.valley = {
270 integrator.signerProjects."worker.valley.invalid/attestations" = [ "sealed" ];
271 worker.projects = [
272 "guarded"
273 "sealed"
274 ];
275 };
276 };
277
278 # The restacker on for one of the projects the controllers serve, with
279 # the floor read from the instance repository.
280 restackerHost = mkHost {
281 services.valley = {
282 config = ../../examples/hosts/protected.cue;
283 integrator = {
284 enable = true;
285 signingKeyFile = "/run/agenix/valley-integrator-key";
286 knownSignersFile = "/var/lib/valley-instance/known_signers";
287 instanceProject = "sealed";
288 };
289 restacker = {
290 enable = true;
291 projects = [ "guarded" ];
292 sshKeyFile = "/run/agenix/valley-restacker-key";
293 };
294 };
295 };
296
297 # The restacker with no list of projects: it serves every project the
298 # host integrates.
299 defaultRestackerHost = mkHost {
300 services.valley = {
301 config = ../../examples/hosts/protected.cue;
302 integrator = {
303 enable = true;
304 signingKeyFile = "/run/agenix/valley-integrator-key";
305 knownSignersFile = "/var/lib/valley-instance/known_signers";
306 instanceProject = "sealed";
307 };
308 restacker = {
309 enable = true;
310 sshKeyFile = "/run/agenix/valley-restacker-key";
311 };
312 };
313 };
314
315 # The restacker for a project this host does not serve.
316 unservedRestackerHost = mkHost {
317 services.valley = {
318 config = ../../examples/hosts/protected.cue;
319 integrator.instanceProject = "sealed";
320 restacker = {
321 enable = true;
322 projects = [ "nowhere" ];
323 sshKeyFile = "/run/agenix/valley-restacker-key";
324 };
325 };
326 };
327
328 # The restacker with no key to push with.
329 keylessRestackerHost = mkHost {
330 services.valley = {
331 config = ../../examples/hosts/protected.cue;
332 integrator.instanceProject = "sealed";
333 restacker = {
334 enable = true;
335 projects = [ "guarded" ];
336 };
337 };
338 };
339
340 # The collector on, with one unit of the host's own declared as a source.
341 telemetryHost = mkHost {
342 services.valley = {
343 config = ../../examples/hosts/protected.cue;
344 telemetry = {
345 enable = true;
346 observers = [ "operator" ];
347 localSources = [
348 {
349 name = "converge";
350 description = "the service that converges this host";
351 units = [ "converge.service" ];
352 }
353 ];
354 };
355 };
356 };
357
358 # Two units of the host's own, each started when a project's main moves,
359 # one bound unit naming an environment file of its own, with the
360 # collector on to catalogue them.
361 onMainHost = mkHost {
362 services.valley = {
363 config = ../../examples/hosts/protected.cue;
364 backup.sshKeyFile = "/run/agenix/valley-git-ssh-key";
365 onMain = {
366 deploy = {
367 project = "open";
368 unit = "converge.service";
369 };
370 docs = {
371 project = "sealed";
372 unit = "docs.service";
373 };
374 };
375 telemetry = {
376 enable = true;
377 observers = [ "operator" ];
378 };
379 };
380 systemd.services.converge = {
381 serviceConfig.Type = "oneshot";
382 serviceConfig.EnvironmentFile = "/etc/converge.env";
383 script = "true";
384 };
385 systemd.services.docs = {
386 serviceConfig.Type = "oneshot";
387 script = "true";
388 };
389 };
390
391 # The build slice alone, at caps of its own: every build of the daemon
392 # capped, and no worker.
393 buildSliceHost = mkHost {
394 services.valley = {
395 config = ../../examples/hosts/protected.cue;
396 buildSlice = {
397 enable = true;
398 cpuQuota = "200%";
399 memoryMax = "4G";
400 };
401 };
402 };
403
404 # The registry machinery switched on, over the same declaration. The
405 # signers file is not supplied: the compiler renders it, and a consumer
406 # that also named one would be asking which of the two won. The
407 # integrator's signing name is set here too, because a host that compiles
408 # a registry is a host whose registry publishes the integrator's key under
409 # a name, and the controller has to sign under that same name.
410 identityHost = mkHost {
411 services.valley = {
412 config = ../../examples/hosts/protected.cue;
413 identity.enable = true;
414 integrator = {
415 enable = true;
416 signingKeyFile = "/run/agenix/valley-integrator-key";
417 signingName = "integrator";
418 instanceProject = "sealed";
419 };
420 };
421 };
422
423 # The same host with the instance repository among the served projects —
424 # the shape a group's own instance takes, where the repository carrying
425 # the floor is itself integrated. The controller serving it reads the
426 # floor from the repository it is already serving.
427 integratorSelfHost = mkHost {
428 services.valley = {
429 config = ../../examples/hosts/protected.cue;
430 integrator = {
431 enable = true;
432 signingKeyFile = "/run/agenix/valley-integrator-key";
433 knownSignersFile = "/var/lib/valley-instance/known_signers";
434 instanceProject = "guarded";
435 };
436 };
437 };
438
439 # Hosts the module must refuse. Each is one way for the push policy to be
440 # quietly wrong, and each must fail an assertion naming what is wrong.
441 #
442 # The instance repository unprotected: the floor and the registry would
443 # be read from a main any key could push.
444 unprotectedInstanceHost = mkHost {
445 services.valley = {
446 config = ../../examples/hosts/protected.cue;
447 integrator = {
448 enable = true;
449 signingKeyFile = "/run/agenix/valley-integrator-key";
450 knownSignersFile = "/var/lib/valley-instance/known_signers";
451 instanceProject = "open";
452 };
453 };
454 };
455
456 # sshd accepting the principal variable from the client.
457 acceptEnvHost = mkHost {
458 services.valley.config = ../../examples/hosts/protected.cue;
459 services.openssh.settings.AcceptEnv = "LANG LC_* VALLEY_*";
460 };
461
462 # The same, in the git user's own Match block, with the keyword in another
463 # case: sshd reads keywords in any case.
464 matchAcceptEnvHost = mkHost {
465 services.valley.config = ../../examples/hosts/protected.cue;
466 services.openssh.extraConfig = lib.mkAfter ''
467 Match User git
468 ACCEPTENV GIT_CONFIG_*
469 Match All
470 '';
471 };
472
473 # The principal, single-quoted: sshd strips the quotes and admits it.
474 quotedAcceptEnvHost = mkHost {
475 services.valley.config = ../../examples/hosts/protected.cue;
476 services.openssh.extraConfig = lib.mkAfter "AcceptEnv LANG 'VALLEY_PRINCIPAL'";
477 };
478
479 # The principal, separated from the pattern before it by a tab.
480 tabAcceptEnvHost = mkHost {
481 services.valley.config = ../../examples/hosts/protected.cue;
482 services.openssh.settings.AcceptEnv = "LANG\tVALLEY_PRINCIPAL";
483 };
484
485 # A pattern that names neither variable outright and still admits one.
486 wildcardAcceptEnvHost = mkHost {
487 services.valley.config = ../../examples/hosts/protected.cue;
488 services.openssh.settings.AcceptEnv = "LANG G?T_*";
489 };
490
491 # sshd setting the principal itself, over every key.
492 setEnvHost = mkHost {
493 services.valley.config = ../../examples/hosts/protected.cue;
494 services.openssh.extraConfig = lib.mkAfter "SetEnv VALLEY_PRINCIPAL=operator";
495 };
496
497 # sshd honouring every variable a key's entry or ~/.ssh/environment sets.
498 permitUserEnvironmentHost = mkHost {
499 services.valley.config = ../../examples/hosts/protected.cue;
500 services.openssh.settings.PermitUserEnvironment = lib.mkForce "yes";
501 };
502
503 # Two projects written as one: rendered one signer per line, the worker
504 # would read back as restricted to guarded and sealed, two projects the
505 # declaration never listed for it.
506 spacedSignerProjectsHost = mkHost {
507 services.valley = {
508 config = ../../examples/hosts/protected.cue;
509 integrator = {
510 enable = true;
511 signingKeyFile = "/run/agenix/valley-integrator-key";
512 knownSignersFile = "/var/lib/valley-instance/known_signers";
513 instanceProject = "sealed";
514 signerProjects."worker.valley.invalid/attestations" = [ "guarded sealed" ];
515 };
516 };
517 };
518
519 # The same two projects joined by a no-break space, U+00A0, which Nix's
520 # [:space:] does not match and Go's unicode.IsSpace does. Nix strings
521 # have no escape for it, so it is decoded from JSON.
522 unicodeSpacedSignerProjectsHost = mkHost {
523 services.valley = {
524 config = ../../examples/hosts/protected.cue;
525 integrator = {
526 enable = true;
527 signingKeyFile = "/run/agenix/valley-integrator-key";
528 knownSignersFile = "/var/lib/valley-instance/known_signers";
529 instanceProject = "sealed";
530 signerProjects."worker.valley.invalid/attestations" = [
531 "guarded${builtins.fromJSON ''"\u00a0"''}sealed"
532 ];
533 };
534 };
535 };
536
537 # The check worker with its signer restricted to no project list at all:
538 # every controller holding its key would count its evidence everywhere.
539 unrestrictedWorkerHost = workerHost.extendModules {
540 modules = [ { services.valley.integrator.signerProjects = lib.mkForce { }; } ];
541 };
542
543 # The check worker given build capacity of its own: three batches at
544 # once, three checks at once in each, each building two derivations at
545 # once on two cores, read again every five minutes, keeping no outputs,
546 # and evaluating within a budget of its own.
547 capacityWorkerHost = workerHost.extendModules {
548 modules = [
549 {
550 services.valley.worker = {
551 batches = 3;
552 interval = "5min";
553 buildSlots = 3;
554 maxJobs = 2;
555 cores = 2;
556 keepOutputs = null;
557 evalMemoryBudget = "12G";
558 };
559 }
560 ];
561 };
562
563 # The check worker on a host whose builds are not capped: turning it on
564 # must not be what caps every build of the daemon.
565 noSliceWorkerHost = workerHost.extendModules {
566 modules = [ { services.valley.buildSlice.enable = lib.mkForce false; } ];
567 };
568
569 # The worker's signer restricted to a project the worker does not build:
570 # its key would be trusted where it does no work.
571 widerWorkerHost = workerHost.extendModules {
572 modules = [
573 {
574 services.valley.integrator.signerProjects = lib.mkForce {
575 "worker.valley.invalid/attestations" = [
576 "guarded"
577 "released"
578 "sealed"
579 ];
580 };
581 }
582 ];
583 };
584
585 # The worker signing under the controllers' own name.
586 integratorNamedWorkerHost = workerHost.extendModules {
587 modules = [
588 {
589 services.valley.integrator.signingName = "integrator";
590 services.valley.integrator.signerProjects = lib.mkForce {
591 integrator = [
592 "guarded"
593 "sealed"
594 ];
595 };
596 services.valley.worker.signerName = lib.mkForce "integrator";
597 }
598 ];
599 };
600
601 # One key declared untagged and tagged: which principal it pushes as
602 # would depend on which line sshd reads first.
603 conflictingKeysHost = mkHost {
604 services.valley = {
605 config = ../../examples/hosts/protected.cue;
606 authorizedKeys = [
607 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor"
608 {
609 principal = "contributor";
610 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
611 }
612 ];
613 };
614 };
615}