2024-11-12 01:49:31 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
cfg,
|
|
|
|
...
|
2024-11-12 03:46:56 +01:00
|
|
|
}: let
|
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;
|
|
|
|
#filter out hosts that don't support IP{$version}
|
2024-11-13 23:17:28 +01:00
|
|
|
filteredHosts = lib.filterAttrs (_: host: host.${version} != null) wgEnabledHosts;
|
2024-11-13 18:43:06 +01:00
|
|
|
in
|
2024-11-13 22:40:30 +01:00
|
|
|
lib.mapAttrsToList (name: host: {
|
2024-11-13 18:43:06 +01:00
|
|
|
pubkey = host.wg.pubkey;
|
|
|
|
#if there is no public IP, make endpoint null so wgautomesh knows it unknown
|
2024-11-14 01:32:43 +01:00
|
|
|
endpoint =
|
|
|
|
if host.${version}.public == ""
|
|
|
|
then null
|
|
|
|
else "${host.${version}.public}:${toString host.wg.port}";
|
2024-11-13 18:43:06 +01:00
|
|
|
address = host.${version}.internal;
|
|
|
|
})
|
|
|
|
filteredHosts;
|
|
|
|
|
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
|
|
|
wireguardPort = currentHost.wg.port;
|
|
|
|
in {
|
2024-11-12 01:49:31 +01:00
|
|
|
opt.useIPv6 = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = "Whether to use IPv6. Defaults to true";
|
|
|
|
default = true;
|
|
|
|
};
|
2024-11-13 20:30:39 +01:00
|
|
|
config = {
|
|
|
|
networking.wireguard.interfaces.wg0 = {
|
|
|
|
ips =
|
2024-11-12 01:52:07 +01:00
|
|
|
if cfg.useIPv6
|
2024-11-13 20:30:39 +01:00
|
|
|
then ["${meshnetwork.wg_subnets.IPv6}"]
|
|
|
|
else ["${meshnetwork.wg_subnets.IPv4}"];
|
2024-11-13 21:08:27 +01:00
|
|
|
listenPort = wireguardPort;
|
2024-11-13 20:30:39 +01:00
|
|
|
privateKeyFile = "/var/lib/wireguard-keys/private";
|
|
|
|
mtu = 1420;
|
|
|
|
};
|
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 = {
|
2024-11-13 20:30:39 +01:00
|
|
|
interface = "wg0";
|
|
|
|
peers =
|
|
|
|
if cfg.useIPv6
|
|
|
|
then buildPeerlist "v6" meshnetwork.hosts
|
|
|
|
else buildPeerlist "v4" meshnetwork.hosts;
|
|
|
|
upnp_forward_external_port = wireguardPort;
|
|
|
|
};
|
2024-11-13 22:40:30 +01:00
|
|
|
gossipSecretFile = gossip_secret_path;
|
2024-11-14 01:32:43 +01:00
|
|
|
|
|
|
|
#DEBUG
|
|
|
|
logLevel = "trace";
|
2024-11-12 01:49:31 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|