Skip to content

Commit 66906dc

Browse files
committed
hosts: deduplicate host registration in default.nix
Define hosts once in hostModules and derive both flake.nixosModules and flake.nixosConfigurations from that registry. This removes the duplicated host lists in hosts/default.nix and reduces the risk of drift when adding or renaming hosts. Update docs/adding-a-host.md to document the new registration step and renumber the runbook accordingly. Signed-off-by: Henri Rosten <henri.rosten@unikie.com>
1 parent dea6503 commit 66906dc

File tree

2 files changed

+64
-93
lines changed

2 files changed

+64
-93
lines changed

docs/adding-a-host.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,22 @@ ghaf-example = {
9393
The `publicKey` field is populated after the first install (see
9494
[print-keys](./tasks.md#print-keys)).
9595

96-
### 4. Add NixOS module to `hosts/default.nix`
96+
### 4. Register host in `hosts/default.nix`
9797

98-
In the `flake.nixosModules` attrset, add:
98+
In `hosts/default.nix`, add the host to the `hostModules` attrset:
9999

100100
```nix
101-
nixos-ghaf-example = ./ghaf-example/configuration.nix;
101+
hostModules = {
102+
# ...
103+
ghaf-example = ./ghaf-example/configuration.nix;
104+
};
102105
```
103106

104-
### 5. Add to nixosConfigurations list
105-
106-
In the same file, add `"ghaf-example"` to the list passed to `builtins.map`.
107+
`flake.nixosModules.nixos-ghaf-example` and
108+
`flake.nixosConfigurations.ghaf-example` are generated from this entry
109+
automatically.
107110

108-
### 6. Add deploy-rs node to `nix/deployments.nix`
111+
### 5. Add deploy-rs node to `nix/deployments.nix`
109112

110113
Add the host to the appropriate node set (`x86-nodes` or `aarch64-nodes`):
111114

@@ -135,7 +138,7 @@ After the first install the host has generated its SSH host key. The
135138
following steps retrieve that key, add it to sops, and redeploy so the
136139
host receives its encrypted secrets.
137140

138-
### 7. Add host age key to `.sops.yaml`
141+
### 6. Add host age key to `.sops.yaml`
139142

140143
Retrieve the host's SSH public key and convert it to an age key:
141144

@@ -149,7 +152,7 @@ Add the resulting age key to the `keys` section of `.sops.yaml`:
149152
- &ghaf-example age1...
150153
```
151154

152-
### 8. Add creation rule in `.sops.yaml`
155+
### 7. Add creation rule in `.sops.yaml`
153156

154157
Add a `creation_rules` entry so sops knows which keys can decrypt the
155158
host's secrets:
@@ -162,7 +165,7 @@ host's secrets:
162165
- *your-admin-anchor
163166
```
164167

165-
### 9. Create secrets file
168+
### 8. Create secrets file
166169

167170
Copy the host's private SSH key from the remote host and store it as
168171
a sops secret:
@@ -180,15 +183,15 @@ rm /tmp/host-key
180183

181184
At minimum, the secrets file must contain the `ssh_host_ed25519_key`.
182185

183-
### 10. Run `inv update-sops-files`
186+
### 9. Run `inv update-sops-files`
184187

185188
Re-encrypt all sops files to reflect the updated `.sops.yaml` rules:
186189

187190
```sh
188191
inv update-sops-files
189192
```
190193

191-
### 11. Redeploy with secrets
194+
### 10. Redeploy with secrets
192195

193196
Deploy the configuration again so the host receives its secrets
194197
(see [deploy-rs.md](./deploy-rs.md)):

hosts/default.nix

Lines changed: 49 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -63,97 +63,65 @@ let
6363
}
6464
];
6565
};
66+
67+
# All host module paths in one place.
68+
# Most hosts can be instantiated with mkNixOS; hetzci-vm is created by mkHetzciVm.
69+
hostModules = {
70+
hetzarm = ./builders/hetzarm/configuration.nix;
71+
hetzarm-dbg-1 = ./builders/hetzarm-dbg-1/configuration.nix;
72+
hetzarm-rel-1 = ./builders/hetzarm-rel-1/configuration.nix;
73+
testagent-prod = ./testagent/prod/configuration.nix;
74+
testagent-dev = ./testagent/dev/configuration.nix;
75+
testagent2-prod = ./testagent/prod2/configuration.nix;
76+
testagent-release = ./testagent/release/configuration.nix;
77+
nethsm-gateway = ./nethsm-gateway/configuration.nix;
78+
ghaf-log = ./ghaf-log/configuration.nix;
79+
ghaf-webserver = ./ghaf-webserver/configuration.nix;
80+
ghaf-auth = ./ghaf-auth/configuration.nix;
81+
ghaf-monitoring = ./ghaf-monitoring/configuration.nix;
82+
ghaf-lighthouse = ./ghaf-lighthouse/configuration.nix;
83+
ghaf-fleetdm = ./ghaf-fleetdm/configuration.nix;
84+
ghaf-registry = ./ghaf-registry/configuration.nix;
85+
hetzci-dbg = ./hetzci/dbg/configuration.nix;
86+
hetzci-dev = ./hetzci/dev/configuration.nix;
87+
hetzci-prod = ./hetzci/prod/configuration.nix;
88+
hetzci-release = ./hetzci/release/configuration.nix;
89+
hetzci-vm = ./hetzci/vm/configuration.nix;
90+
hetz86-1 = ./builders/hetz86-1/configuration.nix;
91+
hetz86-builder = ./builders/hetz86-builder/configuration.nix;
92+
hetz86-dbg-1 = ./builders/hetz86-dbg-1/configuration.nix;
93+
hetz86-rel-2 = ./builders/hetz86-rel-2/configuration.nix;
94+
uae-lab-node1 = ./uae/lab/node1/configuration.nix;
95+
uae-nethsm-gateway = ./uae/nethsm-gateway/configuration.nix;
96+
uae-azureci-prod = ./uae/azureci/prod/configuration.nix;
97+
uae-azureci-az86-1 = ./uae/azureci/builders/az86-1/configuration.nix;
98+
uae-testagent-prod = ./uae/testagent/prod/configuration.nix;
99+
};
100+
101+
nixosModulesFromHosts = lib.mapAttrs' (
102+
name: path: lib.nameValuePair "nixos-${name}" path
103+
) hostModules;
104+
105+
nixosConfigurationsFromHosts = builtins.mapAttrs (name: _path: mkNixOS { systemName = name; }) (
106+
lib.removeAttrs hostModules [ "hetzci-vm" ]
107+
);
66108
in
67109
{
68110
flake.nixosModules = {
69111
# shared modules
70112
common = import ./common.nix;
71-
72-
# All flake.nixosConfigurations, before we call lib.nixosSystem over them.
73-
# We use a 'nixos-' prefix to distinguish them from regular modules.
74-
#
75-
# These are available to allow extending system configuration with
76-
# out-of-tree additional config (like additional trusted cache public keys)
77-
nixos-hetzarm = ./builders/hetzarm/configuration.nix;
78-
nixos-hetzarm-dbg-1 = ./builders/hetzarm-dbg-1/configuration.nix;
79-
nixos-hetzarm-rel-1 = ./builders/hetzarm-rel-1/configuration.nix;
80-
nixos-testagent-prod = ./testagent/prod/configuration.nix;
81-
nixos-testagent-dev = ./testagent/dev/configuration.nix;
82-
nixos-testagent2-prod = ./testagent/prod2/configuration.nix;
83-
nixos-testagent-release = ./testagent/release/configuration.nix;
84-
nixos-nethsm-gateway = ./nethsm-gateway/configuration.nix;
85-
nixos-ghaf-log = ./ghaf-log/configuration.nix;
86-
nixos-ghaf-webserver = ./ghaf-webserver/configuration.nix;
87-
nixos-ghaf-auth = ./ghaf-auth/configuration.nix;
88-
nixos-ghaf-monitoring = ./ghaf-monitoring/configuration.nix;
89-
nixos-ghaf-lighthouse = ./ghaf-lighthouse/configuration.nix;
90-
nixos-ghaf-fleetdm = ./ghaf-fleetdm/configuration.nix;
91-
nixos-ghaf-registry = ./ghaf-registry/configuration.nix;
92-
nixos-hetzci-dbg = ./hetzci/dbg/configuration.nix;
93-
nixos-hetzci-dev = ./hetzci/dev/configuration.nix;
94-
nixos-hetzci-prod = ./hetzci/prod/configuration.nix;
95-
nixos-hetzci-release = ./hetzci/release/configuration.nix;
96-
nixos-hetzci-vm = ./hetzci/vm/configuration.nix;
97-
nixos-hetz86-1 = ./builders/hetz86-1/configuration.nix;
98-
nixos-hetz86-builder = ./builders/hetz86-builder/configuration.nix;
99-
nixos-hetz86-dbg-1 = ./builders/hetz86-dbg-1/configuration.nix;
100-
nixos-hetz86-rel-2 = ./builders/hetz86-rel-2/configuration.nix;
101-
nixos-uae-lab-node1 = ./uae/lab/node1/configuration.nix;
102-
nixos-uae-nethsm-gateway = ./uae/nethsm-gateway/configuration.nix;
103-
nixos-uae-azureci-prod = ./uae/azureci/prod/configuration.nix;
104-
nixos-uae-azureci-az86-1 = ./uae/azureci/builders/az86-1/configuration.nix;
105-
nixos-uae-testagent-prod = ./uae/testagent/prod/configuration.nix;
106-
};
113+
}
114+
// nixosModulesFromHosts;
107115

108116
# Expose as flake.lib.mkNixOS.
109117
flake.lib = {
110118
inherit mkNixOS;
111119
};
112120

113-
# for each systemName, call mkNixOS on it, and set flake.nixosConfigurations
114-
# to an attrset from systemName to the result of that mkNixOS call.
115-
flake.nixosConfigurations =
116-
(builtins.listToAttrs (
117-
builtins.map
118-
(name: {
119-
inherit name;
120-
value = mkNixOS { systemName = name; };
121-
})
122-
[
123-
"hetzarm"
124-
"hetzarm-dbg-1"
125-
"hetzarm-rel-1"
126-
"testagent-prod"
127-
"testagent-dev"
128-
"testagent2-prod"
129-
"testagent-release"
130-
"nethsm-gateway"
131-
"ghaf-log"
132-
"ghaf-webserver"
133-
"ghaf-auth"
134-
"ghaf-monitoring"
135-
"ghaf-lighthouse"
136-
"ghaf-fleetdm"
137-
"ghaf-registry"
138-
"hetzci-dbg"
139-
"hetzci-dev"
140-
"hetzci-prod"
141-
"hetzci-release"
142-
"hetz86-1"
143-
"hetz86-builder"
144-
"hetz86-dbg-1"
145-
"hetz86-rel-2"
146-
"uae-lab-node1"
147-
"uae-nethsm-gateway"
148-
"uae-azureci-prod"
149-
"uae-azureci-az86-1"
150-
"uae-testagent-prod"
151-
]
152-
))
153-
// {
154-
hetzci-vm = mkHetzciVm { };
155-
hetzci-vm-no-host-nix-store = mkHetzciVm {
156-
mountHostNixStore = false;
157-
};
121+
flake.nixosConfigurations = nixosConfigurationsFromHosts // {
122+
hetzci-vm = mkHetzciVm { };
123+
hetzci-vm-no-host-nix-store = mkHetzciVm {
124+
mountHostNixStore = false;
158125
};
126+
};
159127
}

0 commit comments

Comments
 (0)