2021-12-05 20:45:35 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2016-08-23 05:34:31 +01:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.networking.wireguard;
|
2021-12-05 20:45:35 +00:00
|
|
|
opt = options.networking.wireguard;
|
2016-08-23 05:34:31 +01:00
|
|
|
|
|
|
|
kernel = config.boot.kernelPackages;
|
|
|
|
|
|
|
|
# interface options
|
|
|
|
|
2018-07-20 21:56:59 +01:00
|
|
|
interfaceOpts = { ... }: {
|
2016-08-23 05:34:31 +01:00
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
ips = mkOption {
|
|
|
|
example = [ "192.168.2.1/24" ];
|
|
|
|
default = [];
|
|
|
|
type = with types; listOf str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "The IP addresses of the interface.";
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
privateKey = mkOption {
|
|
|
|
example = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=";
|
2017-07-17 22:55:31 +01:00
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Base64 private key generated by {command}`wg genkey`.
|
2017-07-17 22:55:31 +01:00
|
|
|
|
|
|
|
Warning: Consider using privateKeyFile instead if you do not
|
|
|
|
want to store the key in the world-readable Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-04-24 03:30:05 +01:00
|
|
|
generatePrivateKeyFile = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-04-24 03:42:37 +01:00
|
|
|
Automatically generate a private key with
|
2022-07-28 22:19:15 +01:00
|
|
|
{command}`wg genkey`, at the privateKeyFile location.
|
2019-04-24 03:30:05 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-07-17 22:55:31 +01:00
|
|
|
privateKeyFile = mkOption {
|
|
|
|
example = "/private/wireguard_key";
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Private key file as generated by {command}`wg genkey`.
|
2017-07-17 22:55:31 +01:00
|
|
|
'';
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
listenPort = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr int;
|
|
|
|
example = 51820;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-12-16 16:58:40 +00:00
|
|
|
16-bit port for listening. Optional; if not specified,
|
|
|
|
automatically generated based on interface name.
|
|
|
|
'';
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
preSetup = mkOption {
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''"''${pkgs.iproute2}/bin/ip netns add foo"'';
|
2018-04-01 16:22:14 +01:00
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-04-01 16:22:14 +01:00
|
|
|
Commands called at the start of the interface setup.
|
2016-12-16 16:58:40 +00:00
|
|
|
'';
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
postSetup = mkOption {
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
|
|
|
'''printf "nameserver 10.200.100.1" | ''${pkgs.openresolv}/bin/resolvconf -a wg0 -m 0'''
|
2018-04-01 16:22:14 +01:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Commands called at the end of the interface setup.";
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
postShutdown = mkOption {
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''"''${pkgs.openresolv}/bin/resolvconf -d wg0"'';
|
2018-04-01 16:22:14 +01:00
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Commands called after shutting down the interface.";
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
2017-08-08 00:45:19 +01:00
|
|
|
table = mkOption {
|
|
|
|
default = "main";
|
|
|
|
type = types.str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-07-19 13:41:36 +01:00
|
|
|
The kernel routing table to add this interface's
|
|
|
|
associated routes to. Setting this is useful for e.g. policy routing
|
|
|
|
("ip rule") or virtual routing and forwarding ("ip vrf"). Both
|
|
|
|
numeric table IDs and table names (/etc/rt_tables) can be used.
|
|
|
|
Defaults to "main".
|
|
|
|
'';
|
2017-08-08 00:45:19 +01:00
|
|
|
};
|
|
|
|
|
2016-08-23 05:34:31 +01:00
|
|
|
peers = mkOption {
|
|
|
|
default = [];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Peers linked to the interface.";
|
2016-08-23 05:34:31 +01:00
|
|
|
type = with types; listOf (submodule peerOpts);
|
|
|
|
};
|
|
|
|
|
2017-09-22 22:37:54 +01:00
|
|
|
allowedIPsAsRoutes = mkOption {
|
|
|
|
example = false;
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-09-22 22:37:54 +01:00
|
|
|
Determines whether to add allowed IPs as routes or not.
|
|
|
|
'';
|
|
|
|
};
|
2019-09-19 21:54:38 +01:00
|
|
|
|
|
|
|
socketNamespace = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr str;
|
|
|
|
example = "container";
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''The pre-existing network namespace in which the
|
2019-09-19 21:54:38 +01:00
|
|
|
WireGuard interface is created, and which retains the socket even if the
|
2022-08-03 21:46:41 +01:00
|
|
|
interface is moved via {option}`interfaceNamespace`. When
|
|
|
|
`null`, the interface is created in the init namespace.
|
|
|
|
See [documentation](https://www.wireguard.com/netns/).
|
2019-09-19 21:54:38 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
interfaceNamespace = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr str;
|
|
|
|
example = "init";
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''The pre-existing network namespace the WireGuard
|
|
|
|
interface is moved to. The special value `init` means
|
|
|
|
the init namespace. When `null`, the interface is not
|
2019-09-19 21:54:38 +01:00
|
|
|
moved.
|
2022-08-03 21:46:41 +01:00
|
|
|
See [documentation](https://www.wireguard.com/netns/).
|
2019-09-19 21:54:38 +01:00
|
|
|
'';
|
|
|
|
};
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
# peer options
|
|
|
|
|
|
|
|
peerOpts = {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
publicKey = mkOption {
|
|
|
|
example = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=";
|
|
|
|
type = types.str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "The base64 public key of the peer.";
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
2017-05-09 15:58:39 +01:00
|
|
|
presharedKey = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "rVXs/Ni9tu3oDBLS4hOyAUAa1qTWVA3loR8eL20os3I=";
|
|
|
|
type = with types; nullOr str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Base64 preshared key generated by {command}`wg genpsk`.
|
2019-04-24 03:42:37 +01:00
|
|
|
Optional, and may be omitted. This option adds an additional layer of
|
2017-07-17 22:55:31 +01:00
|
|
|
symmetric-key cryptography to be mixed into the already existing
|
|
|
|
public-key cryptography, for post-quantum resistance.
|
|
|
|
|
|
|
|
Warning: Consider using presharedKeyFile instead if you do not
|
|
|
|
want to store the key in the world-readable Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
presharedKeyFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/private/wireguard_psk";
|
|
|
|
type = with types; nullOr str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
File pointing to preshared key as generated by {command}`wg genpsk`.
|
2019-04-24 03:42:37 +01:00
|
|
|
Optional, and may be omitted. This option adds an additional layer of
|
2017-05-09 15:58:39 +01:00
|
|
|
symmetric-key cryptography to be mixed into the already existing
|
|
|
|
public-key cryptography, for post-quantum resistance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-08-23 05:34:31 +01:00
|
|
|
allowedIPs = mkOption {
|
|
|
|
example = [ "10.192.122.3/32" "10.192.124.1/24" ];
|
|
|
|
type = with types; listOf str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''List of IP (v4 or v6) addresses with CIDR masks from
|
2016-08-23 05:34:31 +01:00
|
|
|
which this peer is allowed to send incoming traffic and to which
|
|
|
|
outgoing traffic for this peer is directed. The catch-all 0.0.0.0/0 may
|
|
|
|
be specified for matching all IPv4 addresses, and ::/0 may be specified
|
|
|
|
for matching all IPv6 addresses.'';
|
|
|
|
};
|
|
|
|
|
|
|
|
endpoint = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "demo.wireguard.io:12913";
|
|
|
|
type = with types; nullOr str;
|
|
|
|
description = ''Endpoint IP or hostname of the peer, followed by a colon,
|
2021-04-30 22:00:29 +01:00
|
|
|
and then a port number of the peer.
|
|
|
|
|
|
|
|
Warning for endpoints with changing IPs:
|
|
|
|
The WireGuard kernel side cannot perform DNS resolution.
|
|
|
|
Thus DNS resolution is done once by the <literal>wg</literal> userspace
|
|
|
|
utility, when setting up WireGuard. Consequently, if the IP address
|
|
|
|
behind the name changes, WireGuard will not notice.
|
|
|
|
This is especially common for dynamic-DNS setups, but also applies to
|
|
|
|
any other DNS-based setup.
|
|
|
|
If you do not use IP endpoints, you likely want to set
|
|
|
|
<option>networking.wireguard.dynamicEndpointRefreshSeconds</option>
|
|
|
|
to refresh the IPs periodically.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dynamicEndpointRefreshSeconds = mkOption {
|
|
|
|
default = 0;
|
|
|
|
example = 5;
|
|
|
|
type = with types; int;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Periodically re-execute the `wg` utility every
|
2021-04-30 22:00:29 +01:00
|
|
|
this many seconds in order to let WireGuard notice DNS / hostname
|
|
|
|
changes.
|
|
|
|
|
2022-07-28 22:19:15 +01:00
|
|
|
Setting this to `0` disables periodic reexecution.
|
2021-04-30 22:00:29 +01:00
|
|
|
'';
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
persistentKeepalive = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr int;
|
|
|
|
example = 25;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''This is optional and is by default off, because most
|
2016-08-23 05:34:31 +01:00
|
|
|
users will not need it. It represents, in seconds, between 1 and 65535
|
|
|
|
inclusive, how often to send an authenticated empty packet to the peer,
|
|
|
|
for the purpose of keeping a stateful firewall or NAT mapping valid
|
|
|
|
persistently. For example, if the interface very rarely sends traffic,
|
|
|
|
but it might at anytime receive traffic from a peer, and it is behind
|
|
|
|
NAT, the interface might benefit from having a persistent keepalive
|
|
|
|
interval of 25 seconds; however, most users will not need this.'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-04-24 03:30:05 +01:00
|
|
|
generateKeyServiceUnit = name: values:
|
|
|
|
assert values.generatePrivateKeyFile;
|
|
|
|
nameValuePair "wireguard-${name}-key"
|
|
|
|
{
|
|
|
|
description = "WireGuard Tunnel - ${name} - Key Generator";
|
|
|
|
wantedBy = [ "wireguard-${name}.service" ];
|
|
|
|
requiredBy = [ "wireguard-${name}.service" ];
|
|
|
|
before = [ "wireguard-${name}.service" ];
|
2021-03-24 10:26:31 +00:00
|
|
|
path = with pkgs; [ wireguard-tools ];
|
2019-04-24 03:30:05 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
script = ''
|
2021-04-30 17:13:31 +01:00
|
|
|
set -e
|
|
|
|
|
|
|
|
# If the parent dir does not already exist, create it.
|
|
|
|
# Otherwise, does nothing, keeping existing permisions intact.
|
|
|
|
mkdir -p --mode 0755 "${dirOf values.privateKeyFile}"
|
|
|
|
|
2019-04-24 03:30:05 +01:00
|
|
|
if [ ! -f "${values.privateKeyFile}" ]; then
|
2021-04-30 17:13:31 +01:00
|
|
|
# Write private key file with atomically-correct permissions.
|
|
|
|
(set -e; umask 077; wg genkey > "${values.privateKeyFile}")
|
2019-04-24 03:30:05 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-30 22:00:29 +01:00
|
|
|
peerUnitServiceName = interfaceName: publicKey: dynamicRefreshEnabled:
|
2019-05-31 19:32:43 +01:00
|
|
|
let
|
|
|
|
keyToUnitName = replaceChars
|
|
|
|
[ "/" "-" " " "+" "=" ]
|
|
|
|
[ "-" "\\x2d" "\\x20" "\\x2b" "\\x3d" ];
|
2021-04-30 22:00:29 +01:00
|
|
|
unitName = keyToUnitName publicKey;
|
|
|
|
refreshSuffix = optionalString dynamicRefreshEnabled "-refresh";
|
|
|
|
in
|
|
|
|
"wireguard-${interfaceName}-peer-${unitName}${refreshSuffix}";
|
|
|
|
|
|
|
|
generatePeerUnit = { interfaceName, interfaceCfg, peer }:
|
|
|
|
let
|
2019-05-31 19:32:43 +01:00
|
|
|
psk =
|
|
|
|
if peer.presharedKey != null
|
|
|
|
then pkgs.writeText "wg-psk" peer.presharedKey
|
|
|
|
else peer.presharedKeyFile;
|
2019-09-19 21:54:38 +01:00
|
|
|
src = interfaceCfg.socketNamespace;
|
|
|
|
dst = interfaceCfg.interfaceNamespace;
|
|
|
|
ip = nsWrap "ip" src dst;
|
|
|
|
wg = nsWrap "wg" src dst;
|
2021-04-30 22:00:29 +01:00
|
|
|
dynamicRefreshEnabled = peer.dynamicEndpointRefreshSeconds != 0;
|
|
|
|
# We generate a different name (a `-refresh` suffix) when `dynamicEndpointRefreshSeconds`
|
|
|
|
# to avoid that the same service switches `Type` (`oneshot` vs `simple`),
|
|
|
|
# with the intent to make scripting more obvious.
|
|
|
|
serviceName = peerUnitServiceName interfaceName peer.publicKey dynamicRefreshEnabled;
|
|
|
|
in nameValuePair serviceName
|
2019-05-31 19:32:43 +01:00
|
|
|
{
|
|
|
|
description = "WireGuard Peer - ${interfaceName} - ${peer.publicKey}";
|
|
|
|
requires = [ "wireguard-${interfaceName}.service" ];
|
2022-03-23 09:52:35 +00:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "wireguard-${interfaceName}.service" "network-online.target" ];
|
|
|
|
wantedBy = [ "wireguard-${interfaceName}.service" ];
|
2019-05-31 19:32:43 +01:00
|
|
|
environment.DEVICE = interfaceName;
|
2019-05-31 19:40:02 +01:00
|
|
|
environment.WG_ENDPOINT_RESOLUTION_RETRIES = "infinity";
|
2021-03-14 16:05:16 +00:00
|
|
|
path = with pkgs; [ iproute2 wireguard-tools ];
|
2019-05-31 19:32:43 +01:00
|
|
|
|
2021-04-30 22:00:29 +01:00
|
|
|
serviceConfig =
|
|
|
|
if !dynamicRefreshEnabled
|
|
|
|
then
|
|
|
|
{
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Type = "simple"; # re-executes 'wg' indefinitely
|
|
|
|
# Note that `Type = "oneshot"` services with `RemainAfterExit = true`
|
|
|
|
# cannot be used with systemd timers (see `man systemd.timer`),
|
|
|
|
# which is why `simple` with a loop is the best choice here.
|
|
|
|
# It also makes starting and stopping easiest.
|
|
|
|
};
|
2019-05-31 19:32:43 +01:00
|
|
|
|
|
|
|
script = let
|
2021-04-30 21:56:38 +01:00
|
|
|
wg_setup = concatStringsSep " " (
|
|
|
|
[ ''${wg} set ${interfaceName} peer "${peer.publicKey}"'' ]
|
|
|
|
++ optional (psk != null) ''preshared-key "${psk}"''
|
|
|
|
++ optional (peer.endpoint != null) ''endpoint "${peer.endpoint}"''
|
|
|
|
++ optional (peer.persistentKeepalive != null) ''persistent-keepalive "${toString peer.persistentKeepalive}"''
|
|
|
|
++ optional (peer.allowedIPs != []) ''allowed-ips "${concatStringsSep "," peer.allowedIPs}"''
|
|
|
|
);
|
2019-05-31 19:32:43 +01:00
|
|
|
route_setup =
|
2019-09-19 21:54:38 +01:00
|
|
|
optionalString interfaceCfg.allowedIPsAsRoutes
|
2019-05-31 19:32:43 +01:00
|
|
|
(concatMapStringsSep "\n"
|
|
|
|
(allowedIP:
|
2021-04-30 21:56:38 +01:00
|
|
|
''${ip} route replace "${allowedIP}" dev "${interfaceName}" table "${interfaceCfg.table}"''
|
2019-05-31 19:32:43 +01:00
|
|
|
) peer.allowedIPs);
|
|
|
|
in ''
|
|
|
|
${wg_setup}
|
|
|
|
${route_setup}
|
2021-04-30 22:00:29 +01:00
|
|
|
|
|
|
|
${optionalString (peer.dynamicEndpointRefreshSeconds != 0) ''
|
|
|
|
# Re-execute 'wg' periodically to notice DNS / hostname changes.
|
|
|
|
# Note this will not time out on transient DNS failures such as DNS names
|
|
|
|
# because we have set 'WG_ENDPOINT_RESOLUTION_RETRIES=infinity'.
|
|
|
|
# Also note that 'wg' limits its maximum retry delay to 20 seconds as of writing.
|
|
|
|
while ${wg_setup}; do
|
|
|
|
sleep "${toString peer.dynamicEndpointRefreshSeconds}";
|
|
|
|
done
|
|
|
|
''}
|
2019-05-31 19:32:43 +01:00
|
|
|
'';
|
2019-04-24 03:30:05 +01:00
|
|
|
|
2019-05-31 19:32:43 +01:00
|
|
|
postStop = let
|
2019-09-19 21:54:38 +01:00
|
|
|
route_destroy = optionalString interfaceCfg.allowedIPsAsRoutes
|
2019-05-31 19:32:43 +01:00
|
|
|
(concatMapStringsSep "\n"
|
|
|
|
(allowedIP:
|
2021-04-30 21:56:38 +01:00
|
|
|
''${ip} route delete "${allowedIP}" dev "${interfaceName}" table "${interfaceCfg.table}"''
|
2019-05-31 19:32:43 +01:00
|
|
|
) peer.allowedIPs);
|
|
|
|
in ''
|
2021-04-30 21:56:38 +01:00
|
|
|
${wg} set "${interfaceName}" peer "${peer.publicKey}" remove
|
2019-05-31 19:32:43 +01:00
|
|
|
${route_destroy}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
generateInterfaceUnit = name: values:
|
2017-07-17 22:55:31 +01:00
|
|
|
# exactly one way to specify the private key must be set
|
2019-04-24 03:30:05 +01:00
|
|
|
#assert (values.privateKey != null) != (values.privateKeyFile != null);
|
2017-07-17 22:55:31 +01:00
|
|
|
let privKey = if values.privateKeyFile != null then values.privateKeyFile else pkgs.writeText "wg-key" values.privateKey;
|
2019-09-19 21:54:38 +01:00
|
|
|
src = values.socketNamespace;
|
|
|
|
dst = values.interfaceNamespace;
|
|
|
|
ipPreMove = nsWrap "ip" src null;
|
|
|
|
ipPostMove = nsWrap "ip" src dst;
|
|
|
|
wg = nsWrap "wg" src dst;
|
|
|
|
ns = if dst == "init" then "1" else dst;
|
|
|
|
|
2017-07-17 22:55:31 +01:00
|
|
|
in
|
2016-08-23 05:34:31 +01:00
|
|
|
nameValuePair "wireguard-${name}"
|
|
|
|
{
|
|
|
|
description = "WireGuard Tunnel - ${name}";
|
2022-03-23 09:52:35 +00:00
|
|
|
after = [ "network-pre.target" ];
|
|
|
|
wants = [ "network.target" ];
|
|
|
|
before = [ "network.target" ];
|
2016-11-30 22:54:14 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2017-08-23 22:02:47 +01:00
|
|
|
environment.DEVICE = name;
|
2021-03-14 16:05:16 +00:00
|
|
|
path = with pkgs; [ kmod iproute2 wireguard-tools ];
|
2017-07-17 22:55:31 +01:00
|
|
|
|
2016-08-23 05:34:31 +01:00
|
|
|
serviceConfig = {
|
2019-05-31 19:32:43 +01:00
|
|
|
Type = "oneshot";
|
2016-08-23 05:34:31 +01:00
|
|
|
RemainAfterExit = true;
|
2018-04-01 16:22:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
script = ''
|
2019-06-06 11:40:30 +01:00
|
|
|
${optionalString (!config.boot.isContainer) "modprobe wireguard || true"}
|
2018-04-01 16:22:14 +01:00
|
|
|
|
|
|
|
${values.preSetup}
|
2016-08-23 05:34:31 +01:00
|
|
|
|
2021-04-30 21:56:38 +01:00
|
|
|
${ipPreMove} link add dev "${name}" type wireguard
|
|
|
|
${optionalString (values.interfaceNamespace != null && values.interfaceNamespace != values.socketNamespace) ''${ipPreMove} link set "${name}" netns "${ns}"''}
|
2016-08-23 05:34:31 +01:00
|
|
|
|
2018-04-01 16:22:14 +01:00
|
|
|
${concatMapStringsSep "\n" (ip:
|
2021-04-30 21:56:38 +01:00
|
|
|
''${ipPostMove} address add "${ip}" dev "${name}"''
|
2018-04-01 16:22:14 +01:00
|
|
|
) values.ips}
|
2016-08-23 05:34:31 +01:00
|
|
|
|
2021-04-30 21:56:38 +01:00
|
|
|
${concatStringsSep " " (
|
|
|
|
[ ''${wg} set "${name}" private-key "${privKey}"'' ]
|
|
|
|
++ optional (values.listenPort != null) ''listen-port "${toString values.listenPort}"''
|
|
|
|
)}
|
2017-07-17 22:55:31 +01:00
|
|
|
|
2021-04-30 21:56:38 +01:00
|
|
|
${ipPostMove} link set up dev "${name}"
|
2018-04-01 16:22:14 +01:00
|
|
|
|
|
|
|
${values.postSetup}
|
|
|
|
'';
|
|
|
|
|
2018-09-02 16:07:55 +01:00
|
|
|
postStop = ''
|
2021-04-30 21:56:38 +01:00
|
|
|
${ipPostMove} link del dev "${name}"
|
2018-04-01 16:22:14 +01:00
|
|
|
${values.postShutdown}
|
|
|
|
'';
|
2016-08-23 05:34:31 +01:00
|
|
|
};
|
|
|
|
|
2019-09-19 21:54:38 +01:00
|
|
|
nsWrap = cmd: src: dst:
|
|
|
|
let
|
|
|
|
nsList = filter (ns: ns != null) [ src dst ];
|
|
|
|
ns = last nsList;
|
|
|
|
in
|
2021-04-30 21:56:38 +01:00
|
|
|
if (length nsList > 0 && ns != "init") then ''ip netns exec "${ns}" "${cmd}"'' else cmd;
|
2016-08-23 05:34:31 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
networking.wireguard = {
|
|
|
|
|
2019-04-29 16:09:33 +01:00
|
|
|
enable = mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Whether to enable WireGuard.";
|
2019-04-29 16:09:33 +01:00
|
|
|
type = types.bool;
|
|
|
|
# 2019-05-25: Backwards compatibility.
|
|
|
|
default = cfg.interfaces != {};
|
2021-12-05 20:45:35 +00:00
|
|
|
defaultText = literalExpression "config.${opt.interfaces} != { }";
|
2019-04-29 16:09:33 +01:00
|
|
|
example = true;
|
|
|
|
};
|
|
|
|
|
2016-08-23 05:34:31 +01:00
|
|
|
interfaces = mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "WireGuard interfaces.";
|
2016-08-23 05:34:31 +01:00
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
wg0 = {
|
|
|
|
ips = [ "192.168.20.4/24" ];
|
|
|
|
privateKey = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=";
|
|
|
|
peers = [
|
|
|
|
{ allowedIPs = [ "192.168.20.1/32" ];
|
|
|
|
publicKey = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=";
|
2017-08-04 20:00:45 +01:00
|
|
|
endpoint = "demo.wireguard.io:12913"; }
|
2016-08-23 05:34:31 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
type = with types; attrsOf (submodule interfaceOpts);
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2019-05-31 19:32:43 +01:00
|
|
|
config = mkIf cfg.enable (let
|
|
|
|
all_peers = flatten
|
|
|
|
(mapAttrsToList (interfaceName: interfaceCfg:
|
|
|
|
map (peer: { inherit interfaceName interfaceCfg peer;}) interfaceCfg.peers
|
|
|
|
) cfg.interfaces);
|
|
|
|
in {
|
2016-08-23 05:34:31 +01:00
|
|
|
|
2019-04-24 03:30:05 +01:00
|
|
|
assertions = (attrValues (
|
|
|
|
mapAttrs (name: value: {
|
|
|
|
assertion = (value.privateKey != null) != (value.privateKeyFile != null);
|
|
|
|
message = "Either networking.wireguard.interfaces.${name}.privateKey or networking.wireguard.interfaces.${name}.privateKeyFile must be set.";
|
|
|
|
}) cfg.interfaces))
|
|
|
|
++ (attrValues (
|
|
|
|
mapAttrs (name: value: {
|
|
|
|
assertion = value.generatePrivateKeyFile -> (value.privateKey == null);
|
2020-03-06 15:01:23 +00:00
|
|
|
message = "networking.wireguard.interfaces.${name}.generatePrivateKeyFile must not be set if networking.wireguard.interfaces.${name}.privateKey is set.";
|
2019-05-31 19:32:43 +01:00
|
|
|
}) cfg.interfaces))
|
|
|
|
++ map ({ interfaceName, peer, ... }: {
|
|
|
|
assertion = (peer.presharedKey == null) || (peer.presharedKeyFile == null);
|
|
|
|
message = "networking.wireguard.interfaces.${interfaceName} peer «${peer.publicKey}» has both presharedKey and presharedKeyFile set, but only one can be used.";
|
|
|
|
}) all_peers;
|
2019-04-24 03:30:05 +01:00
|
|
|
|
2020-02-21 23:26:26 +00:00
|
|
|
boot.extraModulePackages = optional (versionOlder kernel.kernel.version "5.6") kernel.wireguard;
|
2018-05-18 22:52:41 +01:00
|
|
|
environment.systemPackages = [ pkgs.wireguard-tools ];
|
2016-08-23 05:34:31 +01:00
|
|
|
|
2019-05-31 19:32:43 +01:00
|
|
|
systemd.services =
|
|
|
|
(mapAttrs' generateInterfaceUnit cfg.interfaces)
|
|
|
|
// (listToAttrs (map generatePeerUnit all_peers))
|
2019-04-24 03:30:05 +01:00
|
|
|
// (mapAttrs' generateKeyServiceUnit
|
|
|
|
(filterAttrs (name: value: value.generatePrivateKeyFile) cfg.interfaces));
|
|
|
|
|
2019-05-31 19:32:43 +01:00
|
|
|
});
|
2016-08-23 05:34:31 +01:00
|
|
|
|
|
|
|
}
|