2021-02-17 13:09:41 +01:00
|
|
|
# A simple, in-kernel VPN service
|
|
|
|
#
|
|
|
|
# Strongly inspired by [1].
|
|
|
|
# [1]: https://github.com/delroth/infra.delroth.net/blob/master/roles/wireguard-peer.nix
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.my.services.wireguard;
|
2021-09-25 14:41:43 +02:00
|
|
|
secrets = config.age.secrets;
|
2021-02-17 13:09:41 +01:00
|
|
|
hostName = config.networking.hostName;
|
|
|
|
|
2023-05-03 16:44:10 +02:00
|
|
|
peers = {
|
|
|
|
# "Server"
|
|
|
|
porthos = {
|
|
|
|
clientNum = 1;
|
|
|
|
publicKey = "PLdgsizztddri0LYtjuNHr5r2E8D+yI+gM8cm5WDfHQ=";
|
|
|
|
externalIp = "91.121.177.163";
|
|
|
|
};
|
2021-09-25 14:41:43 +02:00
|
|
|
|
2023-05-03 16:44:10 +02:00
|
|
|
# "Clients"
|
|
|
|
aramis = {
|
|
|
|
clientNum = 2;
|
|
|
|
publicKey = "QJSWIBS1mXTpxYybLlKu/Y5wy0GFbUfn4yPzpF1DZDc=";
|
|
|
|
};
|
2021-09-25 14:41:43 +02:00
|
|
|
|
2023-05-03 16:44:10 +02:00
|
|
|
richelieu = {
|
|
|
|
clientNum = 3;
|
|
|
|
publicKey = "w4IADAj2Tt7Qe95a0RxDv9ovg/Dr/f3q1LrVOPF48Rk=";
|
|
|
|
};
|
2021-10-10 17:16:21 +02:00
|
|
|
|
2023-05-03 16:44:10 +02:00
|
|
|
# Sarah's iPhone
|
|
|
|
milady = {
|
|
|
|
clientNum = 4;
|
|
|
|
publicKey = "3MKEu4F6o8kww54xeAao5Uet86fv8z/QsZ2L2mOzqDQ=";
|
2021-09-25 14:41:43 +02:00
|
|
|
};
|
2023-05-03 16:44:10 +02:00
|
|
|
};
|
2021-02-17 13:09:41 +01:00
|
|
|
thisPeer = peers."${hostName}";
|
2021-04-21 21:35:16 +02:00
|
|
|
thisPeerIsServer = thisPeer ? externalIp;
|
|
|
|
# Only connect to clients from server, and only connect to server from clients
|
|
|
|
otherPeers =
|
|
|
|
let
|
|
|
|
allOthers = lib.filterAttrs (name: _: name != hostName) peers;
|
|
|
|
shouldConnectToPeer = _: peer: thisPeerIsServer != (peer ? externalIp);
|
|
|
|
in
|
|
|
|
lib.filterAttrs shouldConnectToPeer allOthers;
|
2021-02-18 01:10:45 +01:00
|
|
|
|
2021-05-29 16:41:59 +02:00
|
|
|
extIface = config.my.hardware.networking.externalInterface;
|
2021-04-24 17:30:52 +02:00
|
|
|
|
|
|
|
mkInterface = clientAllowedIPs: {
|
|
|
|
listenPort = cfg.port;
|
|
|
|
address = with cfg.net; with lib; [
|
|
|
|
"${v4.subnet}.${toString thisPeer.clientNum}/${toString v4.mask}"
|
|
|
|
"${v6.subnet}::${toString thisPeer.clientNum}/${toHexString v6.mask}"
|
|
|
|
];
|
2023-05-03 16:44:10 +02:00
|
|
|
privateKeyFile = secrets."wireguard/private-key".path;
|
2021-04-24 17:30:52 +02:00
|
|
|
|
|
|
|
peers =
|
|
|
|
let
|
|
|
|
mkPeer = _: peer: lib.mkMerge [
|
|
|
|
{
|
|
|
|
inherit (peer) publicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
(lib.optionalAttrs thisPeerIsServer {
|
|
|
|
# Only forward from server to clients
|
|
|
|
allowedIPs = with cfg.net; [
|
|
|
|
"${v4.subnet}.${toString peer.clientNum}/32"
|
|
|
|
"${v6.subnet}::${toString peer.clientNum}/128"
|
|
|
|
];
|
|
|
|
})
|
|
|
|
|
|
|
|
(lib.optionalAttrs (!thisPeerIsServer) {
|
|
|
|
# Forward all traffic through wireguard to server
|
|
|
|
allowedIPs = clientAllowedIPs;
|
|
|
|
# Roaming clients need to keep NAT-ing active
|
|
|
|
persistentKeepalive = 10;
|
|
|
|
# We know that `peer` is a server, set up the endpoint
|
|
|
|
endpoint = "${peer.externalIp}:${toString cfg.port}";
|
|
|
|
})
|
|
|
|
];
|
|
|
|
in
|
|
|
|
lib.mapAttrsToList mkPeer otherPeers;
|
2021-04-24 19:22:40 +02:00
|
|
|
|
|
|
|
# Set up clients to use configured DNS servers
|
|
|
|
dns =
|
|
|
|
let
|
|
|
|
toInternalIps = peer: [
|
|
|
|
"${cfg.net.v4.subnet}.${toString peer.clientNum}"
|
|
|
|
"${cfg.net.v6.subnet}::${toString peer.clientNum}"
|
|
|
|
];
|
|
|
|
# We know that `otherPeers` is an attribute set of servers
|
|
|
|
internalIps = lib.flatten
|
|
|
|
(lib.mapAttrsToList (_: peer: toInternalIps peer) otherPeers);
|
|
|
|
internalServers = lib.optionals cfg.dns.useInternal internalIps;
|
|
|
|
in
|
|
|
|
lib.mkIf (!thisPeerIsServer)
|
|
|
|
(internalServers ++ cfg.dns.additionalServers);
|
2021-04-24 17:30:52 +02:00
|
|
|
};
|
2021-02-17 13:09:41 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.my.services.wireguard = with lib; {
|
|
|
|
enable = mkEnableOption "Wireguard VPN service";
|
|
|
|
|
2023-12-14 12:10:14 +01:00
|
|
|
simpleManagement = my.mkDisableOption "manage units without password prompts";
|
|
|
|
|
2021-04-23 20:29:06 +02:00
|
|
|
startAtBoot = mkEnableOption ''
|
|
|
|
Should the VPN service be started at boot. Must be true for the server to
|
|
|
|
work reliably.
|
|
|
|
'';
|
|
|
|
|
2021-02-17 13:09:41 +01:00
|
|
|
iface = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "wg";
|
|
|
|
example = "wg0";
|
|
|
|
description = "Name of the interface to configure";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 51820;
|
|
|
|
example = 55555;
|
|
|
|
description = "Port to configure for Wireguard";
|
|
|
|
};
|
|
|
|
|
2021-04-21 21:35:16 +02:00
|
|
|
dns = {
|
|
|
|
useInternal = my.mkDisableOption ''
|
|
|
|
Use internal DNS servers from wireguard 'server'
|
|
|
|
'';
|
|
|
|
|
|
|
|
additionalServers = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [
|
|
|
|
"1.0.0.1"
|
|
|
|
"1.1.1.1"
|
|
|
|
];
|
|
|
|
example = [
|
|
|
|
"8.8.4.4"
|
|
|
|
"8.8.8.8"
|
|
|
|
];
|
|
|
|
description = "Which DNS servers to use in addition to adblock ones";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-17 13:09:41 +01:00
|
|
|
net = {
|
2021-04-21 21:35:16 +02:00
|
|
|
# FIXME: use new ip library to handle this more cleanly
|
2021-02-17 13:09:41 +01:00
|
|
|
v4 = {
|
|
|
|
subnet = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "10.0.0";
|
|
|
|
example = "10.100.0";
|
|
|
|
description = "Which prefix to use for internal IPs";
|
|
|
|
};
|
|
|
|
mask = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 24;
|
|
|
|
example = 28;
|
|
|
|
description = "The CIDR mask to use on internal IPs";
|
|
|
|
};
|
|
|
|
};
|
2021-04-21 21:35:16 +02:00
|
|
|
# FIXME: extend library for IPv6
|
2021-02-17 13:09:41 +01:00
|
|
|
v6 = {
|
|
|
|
subnet = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "fd42:42:42";
|
|
|
|
example = "fdc9:281f:04d7:9ee9";
|
|
|
|
description = "Which prefix to use for internal IPs";
|
|
|
|
};
|
|
|
|
mask = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 64;
|
|
|
|
example = 68;
|
|
|
|
description = "The CIDR mask to use on internal IPs";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-04-24 17:30:52 +02:00
|
|
|
|
|
|
|
internal = {
|
|
|
|
enable = mkEnableOption ''
|
|
|
|
Additional interface which does not route WAN traffic, but gives access
|
|
|
|
to wireguard peers.
|
|
|
|
Is useful for accessing DNS and other internal services, without having
|
|
|
|
to route all traffic through wireguard.
|
|
|
|
Is automatically disabled on server, and enabled otherwise.
|
|
|
|
'' // {
|
|
|
|
default = !thisPeerIsServer;
|
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "lan";
|
|
|
|
example = "internal";
|
|
|
|
description = "Which name to use for this interface";
|
|
|
|
};
|
|
|
|
|
|
|
|
startAtBoot = my.mkDisableOption ''
|
|
|
|
Should the internal VPN service be started at boot.
|
|
|
|
'';
|
|
|
|
};
|
2021-02-17 13:09:41 +01:00
|
|
|
};
|
|
|
|
|
2021-04-23 22:31:10 +02:00
|
|
|
config = lib.mkIf cfg.enable (lib.mkMerge [
|
2021-04-24 17:30:52 +02:00
|
|
|
# Normal interface should route all traffic from client through server
|
2021-04-23 22:31:10 +02:00
|
|
|
{
|
2021-04-24 17:30:52 +02:00
|
|
|
networking.wg-quick.interfaces."${cfg.iface}" = mkInterface [
|
|
|
|
"0.0.0.0/0"
|
|
|
|
"::/0"
|
|
|
|
];
|
2021-04-23 22:31:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:30:52 +02:00
|
|
|
# Additional inteface is only used to get access to "LAN" from wireguard
|
|
|
|
(lib.mkIf cfg.internal.enable {
|
|
|
|
networking.wg-quick.interfaces."${cfg.internal.name}" = mkInterface [
|
|
|
|
"${cfg.net.v4.subnet}.0/${toString cfg.net.v4.mask}"
|
|
|
|
"${cfg.net.v6.subnet}::/${toString cfg.net.v6.mask}"
|
|
|
|
];
|
2021-04-23 22:31:10 +02:00
|
|
|
})
|
2021-02-17 13:09:41 +01:00
|
|
|
|
2021-04-23 22:31:10 +02:00
|
|
|
# Expose port
|
|
|
|
{
|
|
|
|
networking.firewall.allowedUDPPorts = [ cfg.port ];
|
|
|
|
}
|
2021-02-17 13:09:41 +01:00
|
|
|
|
2021-04-23 22:31:10 +02:00
|
|
|
# Allow NATing wireguard traffic on server
|
|
|
|
(lib.mkIf thisPeerIsServer {
|
|
|
|
networking.nat = {
|
|
|
|
enable = true;
|
|
|
|
externalInterface = extIface;
|
|
|
|
internalInterfaces = [ cfg.iface ];
|
|
|
|
};
|
|
|
|
})
|
2021-04-23 20:29:06 +02:00
|
|
|
|
2021-04-23 22:31:10 +02:00
|
|
|
# Set up forwarding to WAN
|
|
|
|
(lib.mkIf thisPeerIsServer {
|
|
|
|
networking.wg-quick.interfaces."${cfg.iface}" = {
|
|
|
|
postUp = with cfg.net; ''
|
|
|
|
${pkgs.iptables}/bin/iptables -A FORWARD -i ${cfg.iface} -j ACCEPT
|
2021-04-24 13:04:47 +02:00
|
|
|
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING \
|
|
|
|
-s ${v4.subnet}.${toString thisPeer.clientNum}/${toString v4.mask} \
|
|
|
|
-o ${extIface} -j MASQUERADE
|
2021-04-23 22:31:10 +02:00
|
|
|
${pkgs.iptables}/bin/ip6tables -A FORWARD -i ${cfg.iface} -j ACCEPT
|
2021-04-24 13:04:47 +02:00
|
|
|
${pkgs.iptables}/bin/ip6tables -t nat -A POSTROUTING \
|
|
|
|
-s ${v6.subnet}::${toString thisPeer.clientNum}/${toString v6.mask} \
|
|
|
|
-o ${extIface} -j MASQUERADE
|
2021-04-23 22:31:10 +02:00
|
|
|
'';
|
|
|
|
preDown = with cfg.net; ''
|
|
|
|
${pkgs.iptables}/bin/iptables -D FORWARD -i ${cfg.iface} -j ACCEPT
|
2021-04-24 13:04:47 +02:00
|
|
|
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING \
|
|
|
|
-s ${v4.subnet}.${toString thisPeer.clientNum}/${toString v4.mask} \
|
|
|
|
-o ${extIface} -j MASQUERADE
|
2021-04-23 22:31:10 +02:00
|
|
|
${pkgs.iptables}/bin/ip6tables -D FORWARD -i ${cfg.iface} -j ACCEPT
|
2021-04-24 13:04:47 +02:00
|
|
|
${pkgs.iptables}/bin/ip6tables -t nat -D POSTROUTING \
|
|
|
|
-s ${v6.subnet}::${toString thisPeer.clientNum}/${toString v6.mask} \
|
|
|
|
-o ${extIface} -j MASQUERADE
|
2021-04-23 22:31:10 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
# When not needed at boot, ensure that there are no reverse dependencies
|
|
|
|
(lib.mkIf (!cfg.startAtBoot) {
|
|
|
|
systemd.services."wg-quick-${cfg.iface}".wantedBy = lib.mkForce [ ];
|
|
|
|
})
|
2021-04-24 17:30:52 +02:00
|
|
|
|
|
|
|
# Same idea, for internal-only interface
|
|
|
|
(lib.mkIf (cfg.internal.enable && !cfg.internal.startAtBoot) {
|
|
|
|
systemd.services."wg-quick-${cfg.internal.name}".wantedBy = lib.mkForce [ ];
|
|
|
|
})
|
2023-12-12 15:09:10 +01:00
|
|
|
|
|
|
|
# Make systemd shut down one service when starting the other
|
|
|
|
(lib.mkIf (cfg.internal.enable) {
|
|
|
|
systemd.services."wg-quick-${cfg.iface}" = {
|
|
|
|
conflicts = [ "wg-quick-${cfg.internal.name}.service" ];
|
|
|
|
after = [ "wg-quick-${cfg.internal.name}.service" ];
|
|
|
|
};
|
|
|
|
systemd.services."wg-quick-${cfg.internal.name}" = {
|
|
|
|
conflicts = [ "wg-quick-${cfg.iface}.service" ];
|
|
|
|
after = [ "wg-quick-${cfg.iface}.service" ];
|
|
|
|
};
|
|
|
|
})
|
2023-12-14 12:10:14 +01:00
|
|
|
|
|
|
|
# Make it possible to manage those units without using passwords, for admins
|
|
|
|
(lib.mkIf cfg.simpleManagement {
|
|
|
|
environment.etc."polkit-1/rules.d/50-wg-quick.rules".text = ''
|
|
|
|
polkit.addRule(function(action, subject) {
|
|
|
|
if (action.id == "org.freedesktop.systemd1.manage-units") {
|
|
|
|
var unit = action.lookup("unit")
|
|
|
|
if (unit == "wg-quick-${cfg.iface}.service" || unit == "wg-quick-${cfg.internal.name}.service") {
|
|
|
|
var verb = action.lookup("verb");
|
|
|
|
if (verb == "start" || verb == "stop" || verb == "restart") {
|
|
|
|
if (subject.isInGroup("wheel")) {
|
|
|
|
return polkit.Result.YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
'';
|
|
|
|
})
|
2021-04-23 22:31:10 +02:00
|
|
|
]);
|
2021-02-17 13:09:41 +01:00
|
|
|
}
|