@@ -33,6 +33,15 @@ let
3333 '';
3434 };
3535
36+ generatePrivateKeyFile = mkOption {
37+ default = false;
38+ type = types.bool;
39+ description = ''
40+ Automatically generate a private key with `wg genkey`,
41+ at the privateKeyFile location.
42+ '';
43+ };
44+
3645 privateKeyFile = mkOption {
3746 example = "/private/wireguard_key";
3847 type = with types; nullOr str;
@@ -182,9 +191,48 @@ let
182191
183192 };
184193
185− generateUnit = name: values:
194+
195+ generatePathUnit = name: values:
196+ assert (values.privateKey == null);
197+ assert (values.privateKeyFile != null);
198+ nameValuePair "wireguard-${name}"
199+ {
200+ description = "WireGuard Tunnel - ${name} - Private Key";
201+ requiredBy = [ "wireguard-${name}.service" ];
202+ before = [ "wireguard-${name}.service" ];
203+ pathConfig.PathExists = values.privateKeyFile;
204+ };
205+
206+ generateKeyServiceUnit = name: values:
207+ assert values.generatePrivateKeyFile;
208+ nameValuePair "wireguard-${name}-key"
209+ {
210+ description = "WireGuard Tunnel - ${name} - Key Generator";
211+ wantedBy = [ "wireguard-${name}.service" ];
212+ requiredBy = [ "wireguard-${name}.service" ];
213+ before = [ "wireguard-${name}.service" ];
214+ path = with pkgs; [ wireguard ];
215+
216+ serviceConfig = {
217+ Type = "oneshot";
218+ RemainAfterExit = true;
219+ };
220+
221+ script = ''
222+ mkdir --mode 0644 -p "${dirOf values.privateKeyFile}"
223+ if [ ! -f "${values.privateKeyFile}" ]; then
224+ touch "${values.privateKeyFile}"
225+ chmod 0600 "${values.privateKeyFile}"
226+ wg genkey > "${values.privateKeyFile}"
227+ chmod 0400 "${values.privateKeyFile}"
228+ fi
229+ '';
230+ };
231+
232+
233+ generateSetupServiceUnit = name: values:
186234 # exactly one way to specify the private key must be set
187− assert (values.privateKey != null) != (values.privateKeyFile != null);
235+ #assert (values.privateKey != null) != (values.privateKeyFile != null);
188236 let privKey = if values.privateKeyFile != null then values.privateKeyFile else pkgs.writeText "wg-key" values.privateKey;
189237 in
190238 nameValuePair "wireguard-${name}"
@@ -279,10 +327,27 @@ in
279327
280328 config = mkIf (cfg.interfaces != {}) {
281329
330+ assertions = (attrValues (
331+ mapAttrs (name: value: {
332+ assertion = (value.privateKey != null) != (value.privateKeyFile != null);
333+ message = "Either networking.wireguard.interfaces.${name}.privateKey or networking.wireguard.interfaces.${name}.privateKeyFile must be set.";
334+ }) cfg.interfaces))
335+ ++ (attrValues (
336+ mapAttrs (name: value: {
337+ assertion = value.generatePrivateKeyFile -> (value.privateKey == null);
338+ message = "networking.wireguard.interfaces.${name}.generatePrivateKey must not be set if networking.wireguard.interfaces.${name}.privateKey is set.";
339+ }) cfg.interfaces));
340+
341+
282342 boot.extraModulePackages = [ kernel.wireguard ];
283343 environment.systemPackages = [ pkgs.wireguard-tools ];
284344
285− systemd.services = mapAttrs' generateUnit cfg.interfaces;
345+ systemd.services = (mapAttrs' generateSetupServiceUnit cfg.interfaces)
346+ // (mapAttrs' generateKeyServiceUnit
347+ (filterAttrs (name: value: value.generatePrivateKeyFile) cfg.interfaces));
348+
349+ systemd.paths = mapAttrs' generatePathUnit
350+ (filterAttrs (name: value: value.privateKeyFile != null) cfg.interfaces);
286351
287352 };
288353