2024-11-12 01:49:31 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
cfg,
|
|
|
|
...
|
2024-11-12 03:46:56 +01:00
|
|
|
}: let
|
2024-11-30 01:50:21 +01:00
|
|
|
buildInputs = [pkgs.wgautomesh];
|
|
|
|
|
2024-11-13 20:27:18 +01:00
|
|
|
prefix = "lyn";
|
2024-11-13 05:15:14 +01:00
|
|
|
|
2024-11-13 22:40:30 +01:00
|
|
|
# decrypt gossip secret
|
|
|
|
# change this to comply with you secret management
|
|
|
|
gossip_secret_path = config.sops.secrets."all/meshnetwork/gossip_secret".path;
|
|
|
|
|
2024-11-13 18:43:06 +01:00
|
|
|
# function to make a peerlist suitable for wgautomesh
|
|
|
|
buildPeerlist = version: hosts: let
|
|
|
|
#filter out hosts that have wg.enabled set to false
|
|
|
|
wgEnabledHosts = lib.filterAttrs (_: host: host.wg.enabled or false) hosts;
|
|
|
|
in
|
2024-11-13 22:40:30 +01:00
|
|
|
lib.mapAttrsToList (name: host: {
|
2025-01-14 18:56:49 +01:00
|
|
|
interface =
|
|
|
|
if version == "IPv6"
|
|
|
|
then "wg1"
|
|
|
|
else "wg0";
|
2024-11-13 18:43:06 +01:00
|
|
|
pubkey = host.wg.pubkey;
|
2024-11-30 01:50:21 +01:00
|
|
|
#if there is no public IP, make endpoint null so wgautomesh knows it unknown. Else format it to a SocketAddr
|
2025-01-14 18:56:49 +01:00
|
|
|
endpoint = host.${version}.public;
|
|
|
|
port =
|
|
|
|
if version == "IPv6"
|
|
|
|
then host.wg.port_v6
|
|
|
|
else host.wg.port_v4;
|
2024-11-13 18:43:06 +01:00
|
|
|
address = host.${version}.internal;
|
|
|
|
})
|
2025-01-14 18:56:49 +01:00
|
|
|
wgEnabledHosts;
|
2024-11-13 18:43:06 +01:00
|
|
|
|
2024-11-12 03:46:56 +01:00
|
|
|
# helper vars to prettify
|
2024-11-13 05:15:14 +01:00
|
|
|
meshnetwork = config.${prefix}.network;
|
2024-11-13 18:43:06 +01:00
|
|
|
currentHost = meshnetwork.hosts.${config.networking.hostName};
|
2024-11-12 03:46:56 +01:00
|
|
|
in {
|
2024-11-30 01:50:21 +01:00
|
|
|
opt = {
|
2025-01-14 21:43:47 +01:00
|
|
|
enable_upnp_portforward = lib.mkOption {
|
2024-11-30 01:50:21 +01:00
|
|
|
type = lib.types.bool;
|
|
|
|
description = "Whether to allow the wireguard port in the gateway using UPnP IGD. Necessary on some firewalls, might spam unnecessary debug messages on environments without IGD gateways.";
|
|
|
|
default = false;
|
|
|
|
};
|
2025-01-14 21:43:47 +01:00
|
|
|
enable_lan_discovery = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = "Try to discover mesh devices on the same local network.";
|
|
|
|
default = true;
|
|
|
|
};
|
2024-11-12 01:49:31 +01:00
|
|
|
};
|
2025-01-14 18:56:49 +01:00
|
|
|
config = rec {
|
2024-11-14 03:49:23 +01:00
|
|
|
networking.firewall = {
|
2024-11-30 01:50:21 +01:00
|
|
|
allowedUDPPorts =
|
|
|
|
[
|
2025-01-14 18:56:49 +01:00
|
|
|
currentHost.wg.port_v4
|
|
|
|
currentHost.wg.port_v6
|
2024-11-30 01:50:21 +01:00
|
|
|
]
|
|
|
|
# UPnP broadcast responses
|
|
|
|
++ (
|
2025-01-14 21:43:47 +01:00
|
|
|
if cfg.enable_upnp_portforward
|
2024-11-30 01:50:21 +01:00
|
|
|
then [1900]
|
|
|
|
else []
|
|
|
|
);
|
2024-11-14 03:49:23 +01:00
|
|
|
};
|
2024-11-14 01:48:38 +01:00
|
|
|
|
2024-11-13 20:30:39 +01:00
|
|
|
networking.wireguard.interfaces.wg0 = {
|
2025-01-14 18:56:49 +01:00
|
|
|
ips = ["${currentHost.IPv4.internal}/24"];
|
|
|
|
listenPort = currentHost.wg.port_v4;
|
|
|
|
privateKeyFile = "/var/lib/wireguard-keys/private";
|
|
|
|
mtu = 1280;
|
|
|
|
};
|
|
|
|
networking.wireguard.interfaces.wg1 = {
|
|
|
|
ips = ["${currentHost.IPv6.internal}/64"];
|
|
|
|
listenPort = currentHost.wg.port_v6;
|
2024-11-13 20:30:39 +01:00
|
|
|
privateKeyFile = "/var/lib/wireguard-keys/private";
|
2024-11-30 01:50:21 +01:00
|
|
|
mtu = 1280;
|
2024-11-13 20:30:39 +01:00
|
|
|
};
|
2024-11-14 01:32:43 +01:00
|
|
|
|
2024-11-13 20:30:39 +01:00
|
|
|
services.wgautomesh = {
|
|
|
|
enable = true;
|
2024-11-13 21:01:13 +01:00
|
|
|
settings = {
|
2025-01-14 18:56:49 +01:00
|
|
|
interfaces =
|
2025-01-14 21:43:47 +01:00
|
|
|
if cfg.enable_upnp_portforward
|
2025-01-14 18:56:49 +01:00
|
|
|
then [
|
|
|
|
{
|
|
|
|
name = "wg0";
|
|
|
|
upnp_forward_external_port = config.networking.wireguard.interfaces.wg0.listenPort;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "wg1";
|
|
|
|
upnp_forward_external_port = config.networking.wireguard.interfaces.wg1.listenPort;
|
|
|
|
}
|
|
|
|
]
|
2025-01-14 21:43:47 +01:00
|
|
|
else [];
|
2025-01-14 18:56:49 +01:00
|
|
|
peers = buildPeerlist "IPv6" meshnetwork.hosts ++ buildPeerlist "IPv4" meshnetwork.hosts;
|
2025-01-14 21:43:47 +01:00
|
|
|
lan_discovery = cfg.enable_lan_discovery;
|
2024-11-13 20:30:39 +01:00
|
|
|
};
|
2024-11-13 22:40:30 +01:00
|
|
|
gossipSecretFile = gossip_secret_path;
|
2024-11-12 01:49:31 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|