gunk-dev/the-valley

host-module.nix

sourcee4ada5512f03main's published tip history
3.7 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 ;
20
21 failedAssertions = builtins.filter (a: !a.assertion) (
22 host.config.assertions
23 ++ noBackupHost.config.assertions
24 ++ busHost.config.assertions
25 ++ mirrorHost.config.assertions
26 ++ protectedHost.config.assertions
27 );
28
29 missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions;
30
31 protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys;
32
33 resticRenderedWithoutDeclaration =
34 noBackupHost.config.systemd.services ? "restic-backups-valley"
35 || noBackupHost.config.systemd.timers ? "restic-backups-valley";
36
37 # The bus defaults off; a host that never asked for one must
38 # render zero bus machinery.
39 busRenderedWithoutEnable =
40 noBackupHost.config.systemd.services ? valley-bus
41 || noBackupHost.config.systemd.services ? valley-bus-init;
42
43 # A declaration written before the field renders the host it always
44 # did: no hook wired, no tag on a key, no sshd change. It still
45 # carries the sweep that removes a hook it once had — the same
46 # discipline the mirror and bus hooks keep, and the only way
47 # protection can be turned off by editing a declaration.
48 protectionRenderedWithoutDeclaration =
49 noBackupHost.config.services.openssh.settings ? PermitUserEnvironment
50 || lib.any (lib.hasInfix "environment=") (
51 noBackupHost.config.users.users.git.openssh.authorizedKeys.keys
52 )
53 || lib.hasInfix "valley-protect-" noBackupHost.config.systemd.services.valley-init.script;
54in
55{
56 module-eval =
57 if failedAssertions != [ ] then
58 throw "valley module-eval: failed assertions: ${
59 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
60 }"
61 else if resticRenderedWithoutDeclaration then
62 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
63 else if busRenderedWithoutEnable then
64 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
65 else if
66 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
67 then
68 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
69 else if protectionRenderedWithoutDeclaration then
70 throw "valley module-eval: write-protection machinery rendered for a declaration with no protection block"
71 else if
72 protectedHost.config.services.openssh.settings.PermitUserEnvironment or null != "VALLEY_PRINCIPAL"
73 then
74 throw "valley module-eval: a key naming a principal needs sshd to honour that one variable and no other"
75 else
76 pkgs.runCommand "valley-module-eval" {
77 initScript = host.config.systemd.services.valley-init.script;
78 sshdConfig = host.config.services.openssh.extraConfig;
79 resticService = host.config.systemd.units."restic-backups-valley.service".text;
80 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
81 protectedInit = protectedHost.config.systemd.services.valley-init.script;
82 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
83 passAsFile = [
84 "initScript"
85 "sshdConfig"
86 "resticService"
87 "resticTimer"
88 "protectedInit"
89 "protectedKeys"
90 ];
91 } (builtins.readFile ./module-eval.sh);
92}