gunk-dev/the-valley

host-module.nix

sourcede84d05c63b0main's published tip history
6.2 KiB
1# What the NixOS module renders from a declaration, and what it must refuse.
2# The refusals are eval-time: a host that must not evaluate is caught by a
3# throw here, since there is nothing to build once it has. What a host does
4# render is pinned by module-eval.sh over the rendered scripts.
5{
6 pkgs,
7 lib,
8 hosts,
9 ...
10}:
11let
12 inherit (hosts)
13 host
14 noBackupHost
15 noSecretsHost
16 protectedHost
17 busHost
18 mirrorHost
19 integratorHost
20 ;
21
22 failedAssertions = builtins.filter (a: !a.assertion) (
23 host.config.assertions
24 ++ noBackupHost.config.assertions
25 ++ busHost.config.assertions
26 ++ mirrorHost.config.assertions
27 ++ protectedHost.config.assertions
28 ++ integratorHost.config.assertions
29 );
30
31 missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions;
32
33 protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys;
34
35 resticRenderedWithoutDeclaration =
36 noBackupHost.config.systemd.services ? "restic-backups-valley"
37 || noBackupHost.config.systemd.timers ? "restic-backups-valley";
38
39 # The bus defaults off; a host that never asked for one must
40 # render zero bus machinery.
41 busRenderedWithoutEnable =
42 noBackupHost.config.systemd.services ? valley-bus
43 || noBackupHost.config.systemd.services ? valley-bus-init;
44
45 # A declaration written before the field renders the host it always
46 # did: no hook wired, no tag on a key, no sshd change. It still
47 # carries the sweep that removes a hook it once had โ€” the same
48 # discipline the mirror and bus hooks keep, and the only way
49 # protection can be turned off by editing a declaration.
50 protectionRenderedWithoutDeclaration =
51 noBackupHost.config.services.openssh.settings ? PermitUserEnvironment
52 || lib.any (lib.hasInfix "environment=") (
53 noBackupHost.config.users.users.git.openssh.authorizedKeys.keys
54 )
55 || lib.hasInfix "valley-protect-" noBackupHost.config.systemd.services.valley-init.script;
56
57 # The integrator's units. What a controller *does* is not checked here
58 # and no VM is booted: that is integrator-e2e's job (the real binaries
59 # over real repositories) and, past it, live exercise on a host. These
60 # assertions cover the wiring โ€” that a controller is rendered for what
61 # the declaration protects and for nothing else, under an identity that
62 # is not the git user's, pointed at the key and the signers the consumer
63 # supplied.
64 #
65 # Protection and integration are separable declarations: protectedHost
66 # declares the same protected projects with the service off, and must
67 # evaluate to no controller at all.
68 integratorRenderedWithoutEnable =
69 protectedHost.config.systemd.services ? "valley-integrator@"
70 || protectedHost.config.systemd.targets ? valley-integrators
71 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
72
73 controllers = integratorHost.config.systemd.targets.valley-integrators.wants;
74
75 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
76in
77{
78 module-eval =
79 if failedAssertions != [ ] then
80 throw "valley module-eval: failed assertions: ${
81 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
82 }"
83 else if resticRenderedWithoutDeclaration then
84 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
85 else if busRenderedWithoutEnable then
86 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
87 else if
88 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
89 then
90 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
91 else if protectionRenderedWithoutDeclaration then
92 throw "valley module-eval: write-protection machinery rendered for a declaration with no protection block"
93 else if
94 protectedHost.config.services.openssh.settings.PermitUserEnvironment or null != "VALLEY_PRINCIPAL"
95 then
96 throw "valley module-eval: a key naming a principal needs sshd to honour that one variable and no other"
97 else if integratorRenderedWithoutEnable then
98 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
99 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
100 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
101 else if
102 controllers != [
103 "valley-integrator@guarded.service"
104 "valley-integrator@released.service"
105 ]
106 then
107 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
108 else if integratorUser == integratorHost.config.services.valley.user then
109 throw "valley module-eval: the integrator must not run as the git user โ€” it has its own (bd-500adf7)"
110 else if !(integratorHost.config.users.users ? ${integratorUser}) then
111 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
112 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
113 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
114 else
115 pkgs.runCommand "valley-module-eval" {
116 initScript = host.config.systemd.services.valley-init.script;
117 sshdConfig = host.config.services.openssh.extraConfig;
118 resticService = host.config.systemd.units."restic-backups-valley.service".text;
119 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
120 protectedInit = protectedHost.config.systemd.services.valley-init.script;
121 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
122 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
123 integratorInit = integratorHost.config.systemd.services.valley-init.script;
124 passAsFile = [
125 "initScript"
126 "sshdConfig"
127 "resticService"
128 "resticTimer"
129 "protectedInit"
130 "protectedKeys"
131 "integratorUnit"
132 "integratorInit"
133 ];
134 } (builtins.readFile ./module-eval.sh);
135}