2018-12-29 16:48:20 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.networking.wg-quick;
|
|
|
|
|
|
|
|
kernel = config.boot.kernelPackages;
|
|
|
|
|
|
|
|
# interface options
|
|
|
|
|
|
|
|
interfaceOpts = { ... }: {
|
|
|
|
options = {
|
2022-05-01 18:38:24 +02:00
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
example = "/secret/wg0.conf";
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2022-05-01 18:38:24 +02:00
|
|
|
wg-quick .conf file, describing the interface.
|
2023-09-24 08:45:00 +02:00
|
|
|
Using this option can be a useful means of configuring WireGuard if
|
|
|
|
one has an existing .conf file.
|
2022-05-01 18:38:24 +02:00
|
|
|
This overrides any other configuration interface configuration options.
|
|
|
|
See wg-quick manpage for more details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-12-29 16:48:20 +01:00
|
|
|
address = mkOption {
|
|
|
|
example = [ "192.168.2.1/24" ];
|
|
|
|
default = [];
|
|
|
|
type = with types; listOf str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "The IP addresses of the interface.";
|
2018-12-29 16:48:20 +01:00
|
|
|
};
|
|
|
|
|
2022-03-22 14:25:57 +01:00
|
|
|
autostart = mkOption {
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Whether to bring up this interface automatically during boot.";
|
2022-03-22 14:25:57 +01:00
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
2018-12-29 16:48:20 +01:00
|
|
|
dns = mkOption {
|
|
|
|
example = [ "192.168.2.2" ];
|
|
|
|
default = [];
|
|
|
|
type = with types; listOf str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "The IP addresses of DNS servers to configure.";
|
2018-12-29 16:48:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
privateKey = mkOption {
|
|
|
|
example = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=";
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2020-07-19 14:41:36 +02:00
|
|
|
Base64 private key generated by {command}`wg genkey`.
|
2018-12-29 16:48:20 +01:00
|
|
|
|
|
|
|
Warning: Consider using privateKeyFile instead if you do not
|
|
|
|
want to store the key in the world-readable Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
privateKeyFile = mkOption {
|
|
|
|
example = "/private/wireguard_key";
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2020-07-19 14:41:36 +02:00
|
|
|
Private key file as generated by {command}`wg genkey`.
|
2018-12-29 16:48:20 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
listenPort = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr int;
|
|
|
|
example = 51820;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
16-bit port for listening. Optional; if not specified,
|
|
|
|
automatically generated based on interface name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
preUp = mkOption {
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''"''${pkgs.iproute2}/bin/ip netns add foo"'';
|
2018-12-29 16:48:20 +01:00
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
Commands called at the start of the interface setup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
preDown = mkOption {
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''"''${pkgs.iproute2}/bin/ip netns del foo"'';
|
2018-12-29 16:48:20 +01:00
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
Command called before the interface is taken down.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
postUp = mkOption {
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''"''${pkgs.iproute2}/bin/ip netns add foo"'';
|
2018-12-29 16:48:20 +01:00
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
Commands called after the interface setup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
postDown = mkOption {
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''"''${pkgs.iproute2}/bin/ip netns del foo"'';
|
2018-12-29 16:48:20 +01:00
|
|
|
default = "";
|
|
|
|
type = with types; coercedTo (listOf str) (concatStringsSep "\n") lines;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
Command called after the interface is taken down.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
table = mkOption {
|
|
|
|
example = "main";
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
The kernel routing table to add this interface's
|
|
|
|
associated routes to. Setting this is useful for e.g. policy routing
|
2020-07-19 14:41:36 +02:00
|
|
|
("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".
|
2018-12-29 16:48:20 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mtu = mkOption {
|
|
|
|
example = 1248;
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr int;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2018-12-29 16:48:20 +01:00
|
|
|
If not specified, the MTU is automatically determined
|
|
|
|
from the endpoint addresses or the system default route, which is usually
|
|
|
|
a sane choice. However, to manually specify an MTU to override this
|
|
|
|
automatic discovery, this value may be specified explicitly.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
peers = mkOption {
|
|
|
|
default = [];
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Peers linked to the interface.";
|
2018-12-29 16:48:20 +01:00
|
|
|
type = with types; listOf (submodule peerOpts);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# peer options
|
|
|
|
|
|
|
|
peerOpts = {
|
|
|
|
options = {
|
|
|
|
publicKey = mkOption {
|
|
|
|
example = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=";
|
|
|
|
type = types.str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "The base64 public key to the peer.";
|
2018-12-29 16:48:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
presharedKey = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "rVXs/Ni9tu3oDBLS4hOyAUAa1qTWVA3loR8eL20os3I=";
|
|
|
|
type = with types; nullOr str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2020-07-20 00:14:44 +02:00
|
|
|
Base64 preshared key generated by {command}`wg genpsk`.
|
2020-07-19 14:41:36 +02:00
|
|
|
Optional, and may be omitted. This option adds an additional layer of
|
2018-12-29 16:48:20 +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;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2020-07-19 14:41:36 +02:00
|
|
|
File pointing to preshared key as generated by {command}`wg genpsk`.
|
|
|
|
Optional, and may be omitted. This option adds an additional layer of
|
2018-12-29 16:48:20 +01:00
|
|
|
symmetric-key cryptography to be mixed into the already existing
|
|
|
|
public-key cryptography, for post-quantum resistance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
allowedIPs = mkOption {
|
|
|
|
example = [ "10.192.122.3/32" "10.192.124.1/24" ];
|
|
|
|
type = with types; listOf str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''List of IP (v4 or v6) addresses with CIDR masks from
|
2018-12-29 16:48:20 +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;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''Endpoint IP or hostname of the peer, followed by a colon,
|
2018-12-29 16:48:20 +01:00
|
|
|
and then a port number of the peer.'';
|
|
|
|
};
|
|
|
|
|
|
|
|
persistentKeepalive = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr int;
|
|
|
|
example = 25;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''This is optional and is by default off, because most
|
2018-12-29 16:48:20 +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.'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
writeScriptFile = name: text: ((pkgs.writeShellScriptBin name text) + "/bin/${name}");
|
|
|
|
|
|
|
|
generateUnit = name: values:
|
2022-05-01 18:38:24 +02:00
|
|
|
assert assertMsg (values.configFile != null || ((values.privateKey != null) != (values.privateKeyFile != null))) "Only one of privateKey, configFile or privateKeyFile may be set";
|
2018-12-29 16:48:20 +01:00
|
|
|
let
|
|
|
|
preUpFile = if values.preUp != "" then writeScriptFile "preUp.sh" values.preUp else null;
|
|
|
|
postUp =
|
|
|
|
optional (values.privateKeyFile != null) "wg set ${name} private-key <(cat ${values.privateKeyFile})" ++
|
|
|
|
(concatMap (peer: optional (peer.presharedKeyFile != null) "wg set ${name} peer ${peer.publicKey} preshared-key <(cat ${peer.presharedKeyFile})") values.peers) ++
|
2022-05-05 10:02:15 +02:00
|
|
|
optional (values.postUp != "") values.postUp;
|
2018-12-29 16:48:20 +01:00
|
|
|
postUpFile = if postUp != [] then writeScriptFile "postUp.sh" (concatMapStringsSep "\n" (line: line) postUp) else null;
|
|
|
|
preDownFile = if values.preDown != "" then writeScriptFile "preDown.sh" values.preDown else null;
|
|
|
|
postDownFile = if values.postDown != "" then writeScriptFile "postDown.sh" values.postDown else null;
|
|
|
|
configDir = pkgs.writeTextFile {
|
|
|
|
name = "config-${name}";
|
|
|
|
executable = false;
|
|
|
|
destination = "/${name}.conf";
|
|
|
|
text =
|
|
|
|
''
|
|
|
|
[interface]
|
|
|
|
${concatMapStringsSep "\n" (address:
|
|
|
|
"Address = ${address}"
|
|
|
|
) values.address}
|
|
|
|
${concatMapStringsSep "\n" (dns:
|
|
|
|
"DNS = ${dns}"
|
|
|
|
) values.dns}
|
|
|
|
'' +
|
|
|
|
optionalString (values.table != null) "Table = ${values.table}\n" +
|
|
|
|
optionalString (values.mtu != null) "MTU = ${toString values.mtu}\n" +
|
|
|
|
optionalString (values.privateKey != null) "PrivateKey = ${values.privateKey}\n" +
|
|
|
|
optionalString (values.listenPort != null) "ListenPort = ${toString values.listenPort}\n" +
|
|
|
|
optionalString (preUpFile != null) "PreUp = ${preUpFile}\n" +
|
|
|
|
optionalString (postUpFile != null) "PostUp = ${postUpFile}\n" +
|
|
|
|
optionalString (preDownFile != null) "PreDown = ${preDownFile}\n" +
|
|
|
|
optionalString (postDownFile != null) "PostDown = ${postDownFile}\n" +
|
|
|
|
concatMapStringsSep "\n" (peer:
|
|
|
|
assert assertMsg (!((peer.presharedKeyFile != null) && (peer.presharedKey != null))) "Only one of presharedKey or presharedKeyFile may be set";
|
|
|
|
"[Peer]\n" +
|
|
|
|
"PublicKey = ${peer.publicKey}\n" +
|
|
|
|
optionalString (peer.presharedKey != null) "PresharedKey = ${peer.presharedKey}\n" +
|
|
|
|
optionalString (peer.endpoint != null) "Endpoint = ${peer.endpoint}\n" +
|
|
|
|
optionalString (peer.persistentKeepalive != null) "PersistentKeepalive = ${toString peer.persistentKeepalive}\n" +
|
|
|
|
optionalString (peer.allowedIPs != []) "AllowedIPs = ${concatStringsSep "," peer.allowedIPs}\n"
|
|
|
|
) values.peers;
|
|
|
|
};
|
2022-05-01 18:38:24 +02:00
|
|
|
configPath =
|
|
|
|
if values.configFile != null then
|
|
|
|
# This uses bind-mounted private tmp folder (/tmp/systemd-private-***)
|
|
|
|
"/tmp/${name}.conf"
|
|
|
|
else
|
|
|
|
"${configDir}/${name}.conf";
|
2018-12-29 16:48:20 +01:00
|
|
|
in
|
|
|
|
nameValuePair "wg-quick-${name}"
|
|
|
|
{
|
|
|
|
description = "wg-quick WireGuard Tunnel - ${name}";
|
|
|
|
requires = [ "network-online.target" ];
|
|
|
|
after = [ "network.target" "network-online.target" ];
|
2022-03-22 14:25:57 +01:00
|
|
|
wantedBy = optional values.autostart "multi-user.target";
|
2018-12-29 16:48:20 +01:00
|
|
|
environment.DEVICE = name;
|
2022-10-14 10:32:02 +02:00
|
|
|
path = [
|
|
|
|
pkgs.wireguard-tools
|
|
|
|
config.networking.firewall.package # iptables or nftables
|
|
|
|
config.networking.resolvconf.package # openresolv or systemd
|
|
|
|
];
|
2018-12-29 16:48:20 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
script = ''
|
2022-10-14 10:32:02 +02:00
|
|
|
${optionalString (!config.boot.isContainer) "${pkgs.kmod}/bin/modprobe wireguard"}
|
2022-05-01 18:38:24 +02:00
|
|
|
${optionalString (values.configFile != null) ''
|
|
|
|
cp ${values.configFile} ${configPath}
|
|
|
|
''}
|
2018-12-29 16:48:20 +01:00
|
|
|
wg-quick up ${configPath}
|
|
|
|
'';
|
|
|
|
|
2022-05-01 18:38:24 +02:00
|
|
|
serviceConfig = {
|
|
|
|
# Used to privately store renamed copies of external config files during activation
|
|
|
|
PrivateTmp = true;
|
|
|
|
};
|
|
|
|
|
2018-12-29 16:48:20 +01:00
|
|
|
preStop = ''
|
|
|
|
wg-quick down ${configPath}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
networking.wg-quick = {
|
|
|
|
interfaces = mkOption {
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Wireguard interfaces.";
|
2018-12-29 16:48:20 +01:00
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
wg0 = {
|
|
|
|
address = [ "192.168.20.4/24" ];
|
|
|
|
privateKey = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=";
|
|
|
|
peers = [
|
|
|
|
{ allowedIPs = [ "192.168.20.1/32" ];
|
|
|
|
publicKey = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=";
|
|
|
|
endpoint = "demo.wireguard.io:12913"; }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
type = with types; attrsOf (submodule interfaceOpts);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf (cfg.interfaces != {}) {
|
2020-04-03 11:16:35 +02:00
|
|
|
boot.extraModulePackages = optional (versionOlder kernel.kernel.version "5.6") kernel.wireguard;
|
2018-12-29 16:48:20 +01:00
|
|
|
environment.systemPackages = [ pkgs.wireguard-tools ];
|
|
|
|
systemd.services = mapAttrs' generateUnit cfg.interfaces;
|
2022-03-29 21:18:46 +02:00
|
|
|
|
|
|
|
# Prevent networkd from clearing the rules set by wg-quick when restarted (e.g. when waking up from suspend).
|
|
|
|
systemd.network.config.networkConfig.ManageForeignRoutingPolicyRules = mkDefault false;
|
|
|
|
|
|
|
|
# WireGuard interfaces should be ignored in determining whether the network is online.
|
|
|
|
systemd.network.wait-online.ignoredInterfaces = builtins.attrNames cfg.interfaces;
|
2018-12-29 16:48:20 +01:00
|
|
|
};
|
|
|
|
}
|