2021-02-20 19:56:33 +01:00
|
|
|
{ lib, config, pkgs, ... }:
|
2020-12-05 14:39:36 +01:00
|
|
|
let
|
2021-02-20 19:03:40 +01:00
|
|
|
serverHostName = "vueko";
|
|
|
|
peers = {
|
|
|
|
sayuri = {
|
|
|
|
address = "10.80.0.5";
|
|
|
|
publicKey = "t7hpd2yZupAKHxYerHtXnlPRUjV1aGbrrzjYakKdOwE=";
|
|
|
|
};
|
|
|
|
vueko = {
|
|
|
|
address = "10.80.0.6";
|
|
|
|
publicKey = "JbOfL4FxPCzJOjI8AGklPHY2FniCXq0QwOa08gjSyns=";
|
|
|
|
};
|
|
|
|
fuuko = {
|
|
|
|
address = "10.80.0.7";
|
|
|
|
publicKey = "VXic8mhaJBSl6yFkx0Cu6JI8tqqjjM3UbW7x+05pV0M=";
|
|
|
|
};
|
2021-09-30 07:32:03 +02:00
|
|
|
mayushii = {
|
|
|
|
address = "10.80.0.9";
|
|
|
|
publicKey = "nnLdgywXmDg8HWH6I0G28Z2zb4OmmyFDpnvvEBzKJTg=";
|
|
|
|
};
|
2022-03-23 15:03:08 +01:00
|
|
|
renge = {
|
|
|
|
address = "10.80.0.11";
|
|
|
|
publicKey = "RlLs/uiWb9qaBU2iDgRag7Q+FFaR7oHI3yOPLZPKgmA=";
|
|
|
|
};
|
2022-06-09 17:38:24 +02:00
|
|
|
nunotaba = {
|
|
|
|
address = "10.80.0.4";
|
|
|
|
publicKey = "LscDAJR0IjOzNuwX3geYgcvxyvaNhAOc/ojgvGyunT8=";
|
|
|
|
};
|
2021-02-20 19:03:40 +01:00
|
|
|
};
|
|
|
|
|
2020-12-05 14:39:36 +01:00
|
|
|
cfg = config.sbruder.wireguard.home;
|
2021-02-20 19:03:40 +01:00
|
|
|
enableServer = config.networking.hostName == serverHostName;
|
2020-12-05 14:39:36 +01:00
|
|
|
in
|
2020-08-22 17:44:39 +02:00
|
|
|
{
|
2020-12-05 14:39:36 +01:00
|
|
|
options = {
|
|
|
|
sbruder.wireguard.home = {
|
|
|
|
enable = lib.mkEnableOption "WireGuard tunnel wg-home";
|
|
|
|
address = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-02-20 19:03:40 +01:00
|
|
|
visible = false;
|
|
|
|
readOnly = true;
|
2020-12-05 14:39:36 +01:00
|
|
|
};
|
2020-08-22 17:44:39 +02:00
|
|
|
};
|
|
|
|
};
|
2020-12-05 14:39:36 +01:00
|
|
|
|
2021-01-06 13:09:29 +01:00
|
|
|
config = lib.mkIf cfg.enable {
|
2021-03-01 15:27:18 +01:00
|
|
|
sops.secrets.wg-home-private-key = {
|
2021-05-01 13:16:14 +02:00
|
|
|
sopsFile = ./../../machines + "/${config.networking.hostName}/secrets.yaml";
|
2021-03-01 15:27:18 +01:00
|
|
|
};
|
2021-01-06 13:09:29 +01:00
|
|
|
|
2021-02-20 19:03:40 +01:00
|
|
|
sbruder.wireguard.home.address = peers."${config.networking.hostName}".address;
|
|
|
|
|
2021-01-06 13:09:29 +01:00
|
|
|
networking.wireguard.interfaces.wg-home = {
|
2021-03-01 15:27:18 +01:00
|
|
|
privateKeyFile = config.sops.secrets.wg-home-private-key.path;
|
2021-01-06 13:09:29 +01:00
|
|
|
ips = [ "${cfg.address}/24" ];
|
2021-10-05 21:31:41 +02:00
|
|
|
listenPort = if enableServer then 51820 else null;
|
2021-02-20 19:03:40 +01:00
|
|
|
peers =
|
2021-10-05 21:31:41 +02:00
|
|
|
if enableServer
|
|
|
|
then
|
|
|
|
map
|
|
|
|
(peerConfig: with peerConfig; {
|
|
|
|
allowedIPs = [ "${address}/32" ];
|
|
|
|
inherit publicKey;
|
|
|
|
})
|
|
|
|
(lib.attrValues
|
|
|
|
(lib.filterAttrs
|
|
|
|
(n: v: n != config.networking.hostName)
|
|
|
|
peers))
|
|
|
|
else [
|
2021-02-20 19:03:40 +01:00
|
|
|
{
|
|
|
|
allowedIPs = [ "10.80.0.0/24" ];
|
|
|
|
publicKey = peers."${serverHostName}".publicKey;
|
2022-10-15 23:28:33 +02:00
|
|
|
#endpoint = "${serverHostName}.sbruder.de:51820"; # not possible because sadly not all devices have IPv6 connectivity
|
|
|
|
endpoint = "195.201.139.15:51820";
|
2021-02-20 19:03:40 +01:00
|
|
|
persistentKeepalive = 25;
|
2021-10-05 21:31:41 +02:00
|
|
|
}
|
|
|
|
];
|
2021-01-06 13:09:29 +01:00
|
|
|
};
|
2021-01-18 00:05:18 +01:00
|
|
|
|
2021-02-20 19:03:40 +01:00
|
|
|
networking.firewall = {
|
|
|
|
trustedInterfaces = [ "wg-home" ];
|
2021-10-05 21:31:41 +02:00
|
|
|
allowedUDPPorts = lib.optionals enableServer [
|
|
|
|
51820
|
|
|
|
53
|
|
|
|
];
|
2021-02-20 19:03:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
boot.kernel.sysctl = lib.optionalAttrs enableServer {
|
|
|
|
"net.ipv4.ip_forward" = 1;
|
|
|
|
};
|
2021-02-20 19:56:33 +01:00
|
|
|
|
|
|
|
services.bind = lib.mkIf enableServer {
|
|
|
|
enable = true;
|
|
|
|
zones = lib.singleton {
|
|
|
|
name = "vpn.sbruder.de";
|
2021-05-28 14:24:50 +02:00
|
|
|
master = true;
|
2021-02-20 19:56:33 +01:00
|
|
|
file =
|
|
|
|
let
|
|
|
|
# !!! very hacky
|
|
|
|
hexStringToInt = hex: (builtins.fromTOML "int = 0x${hex}").int;
|
|
|
|
|
|
|
|
peerRecords = lib.concatStrings
|
|
|
|
(lib.mapAttrsToList
|
|
|
|
(peer: peerConfig: ''
|
|
|
|
${peer} IN A ${peerConfig.address}
|
|
|
|
'')
|
|
|
|
peers);
|
|
|
|
|
|
|
|
peerRecordsHash = builtins.hashString "sha256" peerRecords;
|
|
|
|
serial = hexStringToInt (lib.substring 0 8 peerRecordsHash);
|
|
|
|
in
|
|
|
|
pkgs.writeText "vpn.sbruder.de.zone" (''
|
|
|
|
$TTL 3600
|
|
|
|
@ IN SOA ${serverHostName}.sbruder.de. hostmaster.sbruder.de. ${toString serial} 28800 3600 604800 3600
|
|
|
|
@ IN NS ${serverHostName}.sbruder.de.
|
|
|
|
'' + peerRecords);
|
|
|
|
};
|
|
|
|
};
|
2020-12-05 14:39:36 +01:00
|
|
|
};
|
2020-08-22 17:44:39 +02:00
|
|
|
}
|