gunk-dev/the-valley

hosts.nix

sourcec0fb7f7caad5main's published tip history
18.5 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 and
15 # the restacker, each a script with the program's name that fails if
16 # 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" ] 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 for a project this host does not serve.
298 unservedRestackerHost = mkHost {
299 services.valley = {
300 config = ../../examples/hosts/protected.cue;
301 integrator.instanceProject = "sealed";
302 restacker = {
303 enable = true;
304 projects = [ "nowhere" ];
305 sshKeyFile = "/run/agenix/valley-restacker-key";
306 };
307 };
308 };
309
310 # The restacker with no key to push with.
311 keylessRestackerHost = mkHost {
312 services.valley = {
313 config = ../../examples/hosts/protected.cue;
314 integrator.instanceProject = "sealed";
315 restacker = {
316 enable = true;
317 projects = [ "guarded" ];
318 };
319 };
320 };
321
322 # The build slice alone, at caps of its own: every build of the daemon
323 # capped, and no worker.
324 buildSliceHost = mkHost {
325 services.valley = {
326 config = ../../examples/hosts/protected.cue;
327 buildSlice = {
328 enable = true;
329 cpuQuota = "200%";
330 memoryMax = "4G";
331 };
332 };
333 };
334
335 # The registry machinery switched on, over the same declaration. The
336 # signers file is not supplied: the compiler renders it, and a consumer
337 # that also named one would be asking which of the two won. The
338 # integrator's signing name is set here too, because a host that compiles
339 # a registry is a host whose registry publishes the integrator's key under
340 # a name, and the controller has to sign under that same name.
341 identityHost = mkHost {
342 services.valley = {
343 config = ../../examples/hosts/protected.cue;
344 identity.enable = true;
345 integrator = {
346 enable = true;
347 signingKeyFile = "/run/agenix/valley-integrator-key";
348 signingName = "integrator";
349 instanceProject = "sealed";
350 };
351 };
352 };
353
354 # The same host with the instance repository among the served projects —
355 # the shape a group's own instance takes, where the repository carrying
356 # the floor is itself integrated. The controller serving it reads the
357 # floor from the repository it is already serving.
358 integratorSelfHost = mkHost {
359 services.valley = {
360 config = ../../examples/hosts/protected.cue;
361 integrator = {
362 enable = true;
363 signingKeyFile = "/run/agenix/valley-integrator-key";
364 knownSignersFile = "/var/lib/valley-instance/known_signers";
365 instanceProject = "guarded";
366 };
367 };
368 };
369
370 # Hosts the module must refuse. Each is one way for the push policy to be
371 # quietly wrong, and each must fail an assertion naming what is wrong.
372 #
373 # The instance repository unprotected: the floor and the registry would
374 # be read from a main any key could push.
375 unprotectedInstanceHost = mkHost {
376 services.valley = {
377 config = ../../examples/hosts/protected.cue;
378 integrator = {
379 enable = true;
380 signingKeyFile = "/run/agenix/valley-integrator-key";
381 knownSignersFile = "/var/lib/valley-instance/known_signers";
382 instanceProject = "open";
383 };
384 };
385 };
386
387 # sshd accepting the principal variable from the client.
388 acceptEnvHost = mkHost {
389 services.valley.config = ../../examples/hosts/protected.cue;
390 services.openssh.settings.AcceptEnv = "LANG LC_* VALLEY_*";
391 };
392
393 # The same, in the git user's own Match block, with the keyword in another
394 # case: sshd reads keywords in any case.
395 matchAcceptEnvHost = mkHost {
396 services.valley.config = ../../examples/hosts/protected.cue;
397 services.openssh.extraConfig = lib.mkAfter ''
398 Match User git
399 ACCEPTENV GIT_CONFIG_*
400 Match All
401 '';
402 };
403
404 # The principal, single-quoted: sshd strips the quotes and admits it.
405 quotedAcceptEnvHost = mkHost {
406 services.valley.config = ../../examples/hosts/protected.cue;
407 services.openssh.extraConfig = lib.mkAfter "AcceptEnv LANG 'VALLEY_PRINCIPAL'";
408 };
409
410 # The principal, separated from the pattern before it by a tab.
411 tabAcceptEnvHost = mkHost {
412 services.valley.config = ../../examples/hosts/protected.cue;
413 services.openssh.settings.AcceptEnv = "LANG\tVALLEY_PRINCIPAL";
414 };
415
416 # A pattern that names neither variable outright and still admits one.
417 wildcardAcceptEnvHost = mkHost {
418 services.valley.config = ../../examples/hosts/protected.cue;
419 services.openssh.settings.AcceptEnv = "LANG G?T_*";
420 };
421
422 # sshd setting the principal itself, over every key.
423 setEnvHost = mkHost {
424 services.valley.config = ../../examples/hosts/protected.cue;
425 services.openssh.extraConfig = lib.mkAfter "SetEnv VALLEY_PRINCIPAL=operator";
426 };
427
428 # sshd honouring every variable a key's entry or ~/.ssh/environment sets.
429 permitUserEnvironmentHost = mkHost {
430 services.valley.config = ../../examples/hosts/protected.cue;
431 services.openssh.settings.PermitUserEnvironment = lib.mkForce "yes";
432 };
433
434 # Two projects written as one: rendered one signer per line, the worker
435 # would read back as restricted to guarded and sealed, two projects the
436 # declaration never listed for it.
437 spacedSignerProjectsHost = mkHost {
438 services.valley = {
439 config = ../../examples/hosts/protected.cue;
440 integrator = {
441 enable = true;
442 signingKeyFile = "/run/agenix/valley-integrator-key";
443 knownSignersFile = "/var/lib/valley-instance/known_signers";
444 instanceProject = "sealed";
445 signerProjects."worker.valley.invalid/attestations" = [ "guarded sealed" ];
446 };
447 };
448 };
449
450 # The same two projects joined by a no-break space, U+00A0, which Nix's
451 # [:space:] does not match and Go's unicode.IsSpace does. Nix strings
452 # have no escape for it, so it is decoded from JSON.
453 unicodeSpacedSignerProjectsHost = mkHost {
454 services.valley = {
455 config = ../../examples/hosts/protected.cue;
456 integrator = {
457 enable = true;
458 signingKeyFile = "/run/agenix/valley-integrator-key";
459 knownSignersFile = "/var/lib/valley-instance/known_signers";
460 instanceProject = "sealed";
461 signerProjects."worker.valley.invalid/attestations" = [
462 "guarded${builtins.fromJSON ''"\u00a0"''}sealed"
463 ];
464 };
465 };
466 };
467
468 # The check worker with its signer restricted to no project list at all:
469 # every controller holding its key would count its evidence everywhere.
470 unrestrictedWorkerHost = workerHost.extendModules {
471 modules = [ { services.valley.integrator.signerProjects = lib.mkForce { }; } ];
472 };
473
474 # The check worker given build capacity of its own: three checks at
475 # once, each building two derivations at once on two cores.
476 capacityWorkerHost = workerHost.extendModules {
477 modules = [
478 {
479 services.valley.worker = {
480 buildSlots = 3;
481 maxJobs = 2;
482 cores = 2;
483 };
484 }
485 ];
486 };
487
488 # The check worker on a host whose builds are not capped: turning it on
489 # must not be what caps every build of the daemon.
490 noSliceWorkerHost = workerHost.extendModules {
491 modules = [ { services.valley.buildSlice.enable = lib.mkForce false; } ];
492 };
493
494 # The worker's signer restricted to a project the worker does not build:
495 # its key would be trusted where it does no work.
496 widerWorkerHost = workerHost.extendModules {
497 modules = [
498 {
499 services.valley.integrator.signerProjects = lib.mkForce {
500 "worker.valley.invalid/attestations" = [
501 "guarded"
502 "released"
503 "sealed"
504 ];
505 };
506 }
507 ];
508 };
509
510 # The worker signing under the controllers' own name.
511 integratorNamedWorkerHost = workerHost.extendModules {
512 modules = [
513 {
514 services.valley.integrator.signingName = "integrator";
515 services.valley.integrator.signerProjects = lib.mkForce {
516 integrator = [
517 "guarded"
518 "sealed"
519 ];
520 };
521 services.valley.worker.signerName = lib.mkForce "integrator";
522 }
523 ];
524 };
525
526 # One key declared untagged and tagged: which principal it pushes as
527 # would depend on which line sshd reads first.
528 conflictingKeysHost = mkHost {
529 services.valley = {
530 config = ../../examples/hosts/protected.cue;
531 authorizedKeys = [
532 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor"
533 {
534 principal = "contributor";
535 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
536 }
537 ];
538 };
539 };
540}