diff options
Diffstat (limited to 'system-profiles/rebuild-machines/default.nix')
-rw-r--r-- | system-profiles/rebuild-machines/default.nix | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/system-profiles/rebuild-machines/default.nix b/system-profiles/rebuild-machines/default.nix new file mode 100644 index 00000000..e2a15aae --- /dev/null +++ b/system-profiles/rebuild-machines/default.nix | |||
@@ -0,0 +1,111 @@ | |||
1 | { config, pkgs, hostName, lib, ... }: | ||
2 | |||
3 | with lib; | ||
4 | |||
5 | let | ||
6 | cfg = config.system.rebuild-machine; | ||
7 | |||
8 | sshConfig = pkgs.writeText "config" '' | ||
9 | UserKnownHostsFile ${knownHostsFile} | ||
10 | |||
11 | Host ${cfg.repoHost} | ||
12 | User ${cfg.repoUser} | ||
13 | IdentityFile ${if isNull cfg.sopsConfig then cfg.repoPrivkey else config.sops.secrets."${cfg.sopsName}".path} | ||
14 | IdentitiesOnly yes | ||
15 | ''; | ||
16 | |||
17 | knownHostsFile = pkgs.writeText "known_hosts" (concatMapStringsSep "\n" (kPath: cfg.repoHost + " " + readFile kPath) (attrValues cfg.repoPubkeys)); | ||
18 | |||
19 | rebuildScript = pkgs.stdenv.mkDerivation { | ||
20 | name = "rebuild-${hostName}"; | ||
21 | |||
22 | src = ./rebuild-machine.zsh; | ||
23 | |||
24 | buildInputs = with pkgs; [ makeWrapper ]; | ||
25 | |||
26 | phases = [ "buildPhase" "installPhase" ]; | ||
27 | |||
28 | inherit (pkgs) zsh coreutils openssh; | ||
29 | inherit (cfg) flake scriptName; | ||
30 | nixosRebuild = config.system.build.nixos-rebuild; | ||
31 | inherit (config.security) wrapperDir; | ||
32 | inherit sshConfig; | ||
33 | |||
34 | buildPhase = '' | ||
35 | substituteAll $src rebuild-machine.zsh | ||
36 | ''; | ||
37 | |||
38 | installPhase = '' | ||
39 | mkdir -p $out/bin | ||
40 | install -m 0755 rebuild-machine.zsh $out/bin/${cfg.scriptName} | ||
41 | ''; | ||
42 | }; | ||
43 | in { | ||
44 | options = { | ||
45 | system.rebuild-machine = { | ||
46 | scriptName = mkOption { | ||
47 | type = types.str; | ||
48 | default = "rebuild-${hostName}"; | ||
49 | description = '' | ||
50 | Name of the script wrapping <literal>nixos-rebuild</literal> | ||
51 | ''; | ||
52 | }; | ||
53 | |||
54 | flake = mkOption { | ||
55 | type = types.nullOr types.str; | ||
56 | default = "git+ssh://${cfg.repoHost}/nixos?ref=flakes#${hostName}"; | ||
57 | description = '' | ||
58 | The Flake URI of the NixOS configuration to build. | ||
59 | ''; | ||
60 | }; | ||
61 | |||
62 | repoHost = mkOption { | ||
63 | type = types.str; | ||
64 | default = "git.yggdrasil.li"; | ||
65 | }; | ||
66 | |||
67 | repoUser = mkOption { | ||
68 | type = types.str; | ||
69 | default = "gitolite"; | ||
70 | }; | ||
71 | |||
72 | repoPubkeys = mkOption { | ||
73 | type = types.attrsOf types.path; | ||
74 | default = genAttrs ["rsa" "ed25519"] (kType: ./ssh-pub + "/${cfg.repoHost}-${kType}.pub"); | ||
75 | }; | ||
76 | |||
77 | repoPrivkey = mkOption { | ||
78 | type = types.path; | ||
79 | default = ./ssh + "/${hostName}/private"; | ||
80 | }; | ||
81 | |||
82 | sopsName = mkOption { | ||
83 | type = types.nullOr types.str; | ||
84 | default = "rebuild-machines"; | ||
85 | }; | ||
86 | |||
87 | sopsConfig = mkOption { | ||
88 | type = types.nullOr types.attrs; | ||
89 | default = { | ||
90 | format = "binary"; | ||
91 | }; | ||
92 | }; | ||
93 | }; | ||
94 | }; | ||
95 | |||
96 | config = { | ||
97 | assertions = [ | ||
98 | { assertion = isNull cfg.sopsConfig || (!(isNull cfg.sopsName)); | ||
99 | message = "If option sopsConfig is not null option sopsName may not be null"; | ||
100 | } | ||
101 | ]; | ||
102 | |||
103 | sops.secrets = lib.mkIf (!(isNull cfg.sopsConfig)) { | ||
104 | "${cfg.sopsName}" = { | ||
105 | sopsFile = cfg.repoPrivkey; | ||
106 | } // cfg.sopsConfig; | ||
107 | }; | ||
108 | |||
109 | environment.systemPackages = [ rebuildScript ]; | ||
110 | }; | ||
111 | } | ||