@@ -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 {
@@ -256,12 +281,18 @@ let
256281 '';
257282 };
258283
259− generatePeerUnit = { interfaceName, interfaceCfg, peer }:
284+ peerUnitServiceName = interfaceName: publicKey: dynamicRefreshEnabled:
260285 let
261286 keyToUnitName = replaceChars
262287 [ "/" "-" " " "+" "=" ]
263288 [ "-" "\\x2d" "\\x20" "\\x2b" "\\x3d" ];
264− unitName = keyToUnitName peer.publicKey;
289+ unitName = keyToUnitName publicKey;
290+ refreshSuffix = optionalString dynamicRefreshEnabled "-refresh";
291+ in
292+ "wireguard-${interfaceName}-peer-${unitName}${refreshSuffix}";
293+
294+ generatePeerUnit = { interfaceName, interfaceCfg, peer }:
295+ let
265296 psk =
266297 if peer.presharedKey != null
267298 then pkgs.writeText "wg-psk" peer.presharedKey
@@ -270,7 +301,12 @@ let
270301 dst = interfaceCfg.interfaceNamespace;
271302 ip = nsWrap "ip" src dst;
272303 wg = nsWrap "wg" src dst;
273− in nameValuePair "wireguard-${interfaceName}-peer-${unitName}"
304+ dynamicRefreshEnabled = peer.dynamicEndpointRefreshSeconds != 0;
305+ # We generate a different name (a `-refresh` suffix) when `dynamicEndpointRefreshSeconds`
306+ # to avoid that the same service switches `Type` (`oneshot` vs `simple`),
307+ # with the intent to make scripting more obvious.
308+ serviceName = peerUnitServiceName interfaceName peer.publicKey dynamicRefreshEnabled;
309+ in nameValuePair serviceName
274310 {
275311 description = "WireGuard Peer - ${interfaceName} - ${peer.publicKey}";
276312 requires = [ "wireguard-${interfaceName}.service" ];
@@ -280,10 +316,21 @@ let
280316 environment.WG_ENDPOINT_RESOLUTION_RETRIES = "infinity";
281317 path = with pkgs; [ iproute2 wireguard-tools ];
282318
283− serviceConfig = {
284− Type = "oneshot";
285− RemainAfterExit = true;
286− };
319+ serviceConfig =
320+ if !dynamicRefreshEnabled
321+ then
322+ {
323+ Type = "oneshot";
324+ RemainAfterExit = true;
325+ }
326+ else
327+ {
328+ Type = "simple"; # re-executes 'wg' indefinitely
329+ # Note that `Type = "oneshot"` services with `RemainAfterExit = true`
330+ # cannot be used with systemd timers (see `man systemd.timer`),
331+ # which is why `simple` with a loop is the best choice here.
332+ # It also makes starting and stopping easiest.
333+ };
287334
288335 script = let
289336 wg_setup = concatStringsSep " " (
@@ -302,6 +349,16 @@ let
302349 in ''
303350 ${wg_setup}
304351 ${route_setup}
352+
353+ ${optionalString (peer.dynamicEndpointRefreshSeconds != 0) ''
354+ # Re-execute 'wg' periodically to notice DNS / hostname changes.
355+ # Note this will not time out on transient DNS failures such as DNS names
356+ # because we have set 'WG_ENDPOINT_RESOLUTION_RETRIES=infinity'.
357+ # Also note that 'wg' limits its maximum retry delay to 20 seconds as of writing.
358+ while ${wg_setup}; do
359+ sleep "${toString peer.dynamicEndpointRefreshSeconds}";
360+ done
361+ ''}
305362 '';
306363
307364 postStop = let