2023-01-20 06:57:49 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2023-01-19 23:55:56 +00:00
|
|
|
|
|
|
|
let
|
2023-01-20 10:20:33 +00:00
|
|
|
inherit (builtins) filter map;
|
|
|
|
inherit (lib) concatMap mapAttrsToList mkIf mkMerge mkOption optionalAttrs types;
|
2023-01-20 00:13:13 +00:00
|
|
|
cfg = config.sane.services.wg-home;
|
2023-01-20 07:22:34 +00:00
|
|
|
server-cfg = config.sane.hosts.by-name."servo".wg-home;
|
2023-01-20 10:20:33 +00:00
|
|
|
mkPeer = { ips, pubkey, endpoint }: {
|
|
|
|
publicKey = pubkey;
|
|
|
|
allowedIPs = map (k: "${k}/32") ips;
|
2023-01-20 10:34:30 +00:00
|
|
|
} // (optionalAttrs (endpoint != null) {
|
|
|
|
inherit endpoint;
|
2023-01-20 10:20:33 +00:00
|
|
|
# send keepalives every 25 seconds to keep NAT routes live.
|
|
|
|
# only need to do this from client -> server though, i think.
|
2023-01-20 10:34:30 +00:00
|
|
|
persistentKeepalive = 25;
|
|
|
|
# allows wireguard to notice DNS/hostname changes, with this much effective TTL.
|
|
|
|
dynamicEndpointRefreshSeconds = 600;
|
|
|
|
});
|
2023-01-20 10:20:33 +00:00
|
|
|
# make separate peers to route each given host
|
|
|
|
mkClientPeers = hosts: map (p: mkPeer {
|
|
|
|
inherit (p) pubkey endpoint;
|
|
|
|
ips = [ p.ip ];
|
|
|
|
}) hosts;
|
|
|
|
# make a single peer which routes all the given hosts
|
|
|
|
mkServerPeer = hosts: mkPeer {
|
|
|
|
inherit (server-cfg) pubkey endpoint;
|
|
|
|
ips = map (h: h.ip) hosts;
|
|
|
|
};
|
2023-01-19 23:55:56 +00:00
|
|
|
in
|
|
|
|
{
|
2023-01-20 00:13:13 +00:00
|
|
|
options = {
|
|
|
|
sane.services.wg-home.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2023-05-26 23:28:30 +00:00
|
|
|
sane.services.wg-home.enableWan = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "whether to make this port visible on the WAN";
|
|
|
|
};
|
2023-01-20 07:42:31 +00:00
|
|
|
sane.services.wg-home.ip = mkOption {
|
|
|
|
type = types.str;
|
2023-01-20 00:13:13 +00:00
|
|
|
};
|
|
|
|
};
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 07:42:31 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
# generate a (deterministic) wireguard private key
|
|
|
|
sane.derived-secrets."/run/wg-home.priv" = {
|
|
|
|
len = 32;
|
|
|
|
encoding = "base64";
|
|
|
|
};
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 07:42:31 +00:00
|
|
|
# wireguard VPN which allows everything on my domain to speak to each other even when
|
|
|
|
# not behind a shared LAN.
|
|
|
|
# this config defines both the endpoint (server) and client configs
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 07:42:31 +00:00
|
|
|
# for convenience, have both the server and client use the same port for their wireguard connections.
|
2023-05-31 04:25:39 +00:00
|
|
|
sane.ports.ports."51820" = {
|
|
|
|
protocol = [ "udp" ];
|
|
|
|
visibleTo.lan = true;
|
|
|
|
visibleTo.wan = cfg.enableWan;
|
|
|
|
description = "colin-wireguard";
|
|
|
|
};
|
2023-01-20 07:42:31 +00:00
|
|
|
networking.wireguard.interfaces.wg-home = {
|
|
|
|
listenPort = 51820;
|
|
|
|
privateKeyFile = "/run/wg-home.priv";
|
|
|
|
preSetup =
|
|
|
|
let
|
|
|
|
gen-key = config.sane.fs."/run/wg-home.priv".unit;
|
|
|
|
in
|
|
|
|
"${pkgs.systemd}/bin/systemctl start '${gen-key}'";
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 07:42:31 +00:00
|
|
|
ips = [
|
|
|
|
"${cfg.ip}/24"
|
|
|
|
];
|
2023-01-20 00:13:13 +00:00
|
|
|
|
2023-01-20 10:20:33 +00:00
|
|
|
peers =
|
|
|
|
let
|
|
|
|
all-peers = mapAttrsToList (_: hostcfg: hostcfg.wg-home) config.sane.hosts.by-name;
|
|
|
|
peer-list = filter (p: p.ip != null && p.ip != cfg.ip && p.pubkey != null) all-peers;
|
|
|
|
in
|
|
|
|
if cfg.ip == server-cfg.ip then
|
|
|
|
# if we're the server, then we maintain the entire client list
|
|
|
|
mkClientPeers peer-list
|
|
|
|
else
|
|
|
|
# but if we're a client, we maintain a single peer -- the server -- which does the actual routing
|
|
|
|
[ (mkServerPeer peer-list) ];
|
2023-01-20 07:42:31 +00:00
|
|
|
};
|
|
|
|
};
|
2023-01-19 23:55:56 +00:00
|
|
|
}
|