@@ -198,7 +198,32 @@ let
198198 example = "demo.wireguard.io:12913";
199199 type = with types; nullOr str;
200200 description = ''Endpoint IP or hostname of the peer, followed by a colon,
201− and then a port number of the peer.'';
201+ and then a port number of the peer.
202+
203+ Warning for endpoints with changing IPs:
204+ The WireGuard kernel side cannot perform DNS resolution.
205+ Thus DNS resolution is done once by the <literal>wg</literal> userspace
206+ utility, when setting up WireGuard. Consequently, if the IP address
207+ behind the name changes, WireGuard will not notice.
208+ This is especially common for dynamic-DNS setups, but also applies to
209+ any other DNS-based setup.
210+ If you do not use IP endpoints, you likely want to set
211+ <option>networking.wireguard.dynamicEndpointRefreshSeconds</option>
212+ to refresh the IPs periodically.
213+ '';
214+ };
215+
216+ dynamicEndpointRefreshSeconds = mkOption {
217+ default = 0;
218+ example = 5;
219+ type = with types; int;
220+ description = ''
221+ Periodically re-execute the <literal>wg</literal> utility every
222+ this many seconds in order to let WireGuard notice DNS / hostname
223+ changes.
224+
225+ Setting this to <literal>0</literal> disables periodic reexecution.
226+ '';
202227 };
203228
204229 persistentKeepalive = mkOption {
@@ -259,12 +284,18 @@ let
259284 '';
260285 };
261286
262− generatePeerUnit = { interfaceName, interfaceCfg, peer }:
287+ peerUnitServiceName = interfaceName: publicKey: dynamicRefreshEnabled:
263288 let
264289 keyToUnitName = replaceChars
265290 [ "/" "-" " " "+" "=" ]
266291 [ "-" "\\x2d" "\\x20" "\\x2b" "\\x3d" ];
267− unitName = keyToUnitName peer.publicKey;
292+ unitName = keyToUnitName publicKey;
293+ refreshSuffix = optionalString dynamicRefreshEnabled "-refresh";
294+ in
295+ "wireguard-${interfaceName}-peer-${unitName}${refreshSuffix}";
296+
297+ generatePeerUnit = { interfaceName, interfaceCfg, peer }:
298+ let
268299 psk =
269300 if peer.presharedKey != null
270301 then pkgs.writeText "wg-psk" peer.presharedKey
@@ -273,7 +304,12 @@ let
273304 dst = interfaceCfg.interfaceNamespace;
274305 ip = nsWrap "ip" src dst;
275306 wg = nsWrap "wg" src dst;
276− in nameValuePair "wireguard-${interfaceName}-peer-${unitName}"
307+ dynamicRefreshEnabled = peer.dynamicEndpointRefreshSeconds != 0;
308+ # We generate a different name (a `-refresh` suffix) when `dynamicEndpointRefreshSeconds`
309+ # to avoid that the same service switches `Type` (`oneshot` vs `simple`),
310+ # with the intent to make scripting more obvious.
311+ serviceName = peerUnitServiceName interfaceName peer.publicKey dynamicRefreshEnabled;
312+ in nameValuePair serviceName
277313 {
278314 description = "WireGuard Peer - ${interfaceName} - ${peer.publicKey}";
279315 requires = [ "wireguard-${interfaceName}.service" ];
@@ -283,36 +319,59 @@ let
283319 environment.WG_ENDPOINT_RESOLUTION_RETRIES = "infinity";
284320 path = with pkgs; [ iproute2 wireguard-tools ];
285321
286− serviceConfig = {
287− Type = "oneshot";
288− RemainAfterExit = true;
289− };
322+ serviceConfig =
323+ if !dynamicRefreshEnabled
324+ then
325+ {
326+ Type = "oneshot";
327+ RemainAfterExit = true;
328+ }
329+ else
330+ {
331+ Type = "simple"; # re-executes 'wg' indefinitely
332+ # Note that `Type = "oneshot"` services with `RemainAfterExit = true`
333+ # cannot be used with systemd timers (see `man systemd.timer`),
334+ # which is why `simple` with a loop is the best choice here.
335+ # It also makes starting and stopping easiest.
336+ };
290337
291338 script = let
292− wg_setup = "${wg} set ${interfaceName} peer ${peer.publicKey}" +
293− optionalString (psk != null) " preshared-key ${psk}" +
294− optionalString (peer.endpoint != null) " endpoint ${peer.endpoint}" +
295− optionalString (peer.persistentKeepalive != null) " persistent-keepalive ${toString peer.persistentKeepalive}" +
296− optionalString (peer.allowedIPs != []) " allowed-ips ${concatStringsSep "," peer.allowedIPs}";
339+ wg_setup = concatStringsSep " " (
340+ [ ''${wg} set ${interfaceName} peer "${peer.publicKey}"'' ]
341+ ++ optional (psk != null) ''preshared-key "${psk}"''
342+ ++ optional (peer.endpoint != null) ''endpoint "${peer.endpoint}"''
343+ ++ optional (peer.persistentKeepalive != null) ''persistent-keepalive "${toString peer.persistentKeepalive}"''
344+ ++ optional (peer.allowedIPs != []) ''allowed-ips "${concatStringsSep "," peer.allowedIPs}"''
345+ );
297346 route_setup =
298347 optionalString interfaceCfg.allowedIPsAsRoutes
299348 (concatMapStringsSep "\n"
300349 (allowedIP:
301− "${ip} route replace ${allowedIP} dev ${interfaceName} table ${interfaceCfg.table}"
350+ ''${ip} route replace "${allowedIP}" dev "${interfaceName}" table "${interfaceCfg.table}"''
302351 ) peer.allowedIPs);
303352 in ''
304353 ${wg_setup}
305354 ${route_setup}
355+
356+ ${optionalString (peer.dynamicEndpointRefreshSeconds != 0) ''
357+ # Re-execute 'wg' periodically to notice DNS / hostname changes.
358+ # Note this will not time out on transient DNS failures such as DNS names
359+ # because we have set 'WG_ENDPOINT_RESOLUTION_RETRIES=infinity'.
360+ # Also note that 'wg' limits its maximum retry delay to 20 seconds as of writing.
361+ while ${wg_setup}; do
362+ sleep "${toString peer.dynamicEndpointRefreshSeconds}";
363+ done
364+ ''}
306365 '';
307366
308367 postStop = let
309368 route_destroy = optionalString interfaceCfg.allowedIPsAsRoutes
310369 (concatMapStringsSep "\n"
311370 (allowedIP:
312− "${ip} route delete ${allowedIP} dev ${interfaceName} table ${interfaceCfg.table}"
371+ ''${ip} route delete "${allowedIP}" dev "${interfaceName}" table "${interfaceCfg.table}"''
313372 ) peer.allowedIPs);
314373 in ''
315− ${wg} set ${interfaceName} peer ${peer.publicKey} remove
374+ ${wg} set "${interfaceName}" peer "${peer.publicKey}" remove
316375 ${route_destroy}
317376 '';
318377 };
@@ -348,23 +407,25 @@ let
348407
349408 ${values.preSetup}
350409
351− ${ipPreMove} link add dev ${name} type wireguard
352− ${optionalString (values.interfaceNamespace != null && values.interfaceNamespace != values.socketNamespace) "${ipPreMove} link set ${name} netns ${ns}"}
410+ ${ipPreMove} link add dev "${name}" type wireguard
411+ ${optionalString (values.interfaceNamespace != null && values.interfaceNamespace != values.socketNamespace) ''${ipPreMove} link set "${name}" netns "${ns}"''}
353412
354413 ${concatMapStringsSep "\n" (ip:
355− "${ipPostMove} address add ${ip} dev ${name}"
414+ ''${ipPostMove} address add "${ip}" dev "${name}"''
356415 ) values.ips}
357416
358− ${wg} set ${name} private-key ${privKey} ${
359− optionalString (values.listenPort != null) " listen-port ${toString values.listenPort}"}
417+ ${concatStringsSep " " (
418+ [ ''${wg} set "${name}" private-key "${privKey}"'' ]
419+ ++ optional (values.listenPort != null) ''listen-port "${toString values.listenPort}"''
420+ )}
360421
361− ${ipPostMove} link set up dev ${name}
422+ ${ipPostMove} link set up dev "${name}"
362423
363424 ${values.postSetup}
364425 '';
365426
366427 postStop = ''
367− ${ipPostMove} link del dev ${name}
428+ ${ipPostMove} link del dev "${name}"
368429 ${values.postShutdown}
369430 '';
370431 };
@@ -374,7 +435,7 @@ let
374435 nsList = filter (ns: ns != null) [ src dst ];
375436 ns = last nsList;
376437 in
377− if (length nsList > 0 && ns != "init") then "ip netns exec ${ns} ${cmd}" else cmd;
438+ if (length nsList > 0 && ns != "init") then ''ip netns exec "${ns}" "${cmd}"'' else cmd;
378439in
379440
380441{