2023-01-20 06:57:49 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2023-01-19 23:55:56 +00:00
|
|
|
|
|
|
|
let
|
2023-01-20 07:22:34 +00:00
|
|
|
inherit (builtins) mapAttrsToList;
|
2023-01-20 05:10:51 +00:00
|
|
|
inherit (lib) 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-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;
|
|
|
|
};
|
|
|
|
sane.services.wg-home.role = mkOption {
|
|
|
|
type = types.enum [ "client" "server" ];
|
|
|
|
};
|
|
|
|
};
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 05:10:51 +00:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{
|
2023-01-20 06:57:49 +00:00
|
|
|
# generate a (deterministic) wireguard private key
|
|
|
|
sane.derived-secrets."/run/wg-home.priv" = {
|
|
|
|
len = 32;
|
|
|
|
encoding = "base64";
|
|
|
|
};
|
|
|
|
|
2023-01-20 05:10:51 +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 05:10:51 +00:00
|
|
|
# for convenience, have both the server and client use the same port for their wireguard connections.
|
|
|
|
networking.firewall.allowedUDPPorts = [ 51820 ];
|
|
|
|
networking.wireguard.interfaces.wg-home = {
|
|
|
|
listenPort = 51820;
|
2023-01-20 06:57:49 +00:00
|
|
|
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-20 05:10:51 +00:00
|
|
|
};
|
|
|
|
}
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 05:10:51 +00:00
|
|
|
{
|
|
|
|
networking.wireguard.interfaces.wg-home = lib.mkIf (cfg.role == "client") {
|
|
|
|
# client IP (TODO: make host-specific)
|
2023-01-20 05:38:14 +00:00
|
|
|
ips = [ "10.0.10.20/24" ];
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 05:10:51 +00:00
|
|
|
peers = [
|
|
|
|
{
|
|
|
|
# server pubkey
|
2023-01-20 07:22:34 +00:00
|
|
|
publicKey = server-cfg.pubkey;
|
2023-01-19 23:55:56 +00:00
|
|
|
|
2023-01-20 05:10:51 +00:00
|
|
|
# accept traffic from any IP addr on the other side of the tunnel
|
2023-01-20 05:38:14 +00:00
|
|
|
# allowedIPs = [ "0.0.0.0/0" ];
|
|
|
|
allowedIPs = [ "10.0.10.5/32" ];
|
2023-01-20 00:13:13 +00:00
|
|
|
|
2023-01-20 05:38:14 +00:00
|
|
|
# endpoint = "uninsane.org:51820";
|
|
|
|
endpoint = "97.126.41.123:51820";
|
2023-01-20 05:10:51 +00:00
|
|
|
|
|
|
|
# send keepalives every 25 seconds to keep NAT routes live
|
|
|
|
persistentKeepalive = 25;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
networking.wireguard.interfaces.wg-home = lib.mkIf (cfg.role == "server") {
|
|
|
|
ips = [
|
|
|
|
"10.0.10.5/24"
|
|
|
|
];
|
2023-01-20 07:22:34 +00:00
|
|
|
peers = mapAttrsToList
|
|
|
|
(name: hostcfg:
|
|
|
|
lib.mkIf (hostcfg.wg-home.ip or server-cfg.ip != server-cfg.ip) {
|
|
|
|
publicKey = hostcfg.wg-home.pubkey;
|
|
|
|
allowedIPs = [ "${hostcfg.wg-home.ip}/32" ];
|
|
|
|
}
|
|
|
|
)
|
|
|
|
config.sane.hosts.by-name;
|
2023-01-20 05:10:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|
2023-01-19 23:55:56 +00:00
|
|
|
}
|