2025-08-17 20:37:46 +05:30
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib) mkOption types;
|
|
|
|
|
|
|
|
wgConfigType = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
interface = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "WireGuard interface name.";
|
|
|
|
};
|
|
|
|
serverAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description =
|
2025-09-30 21:10:33 +05:30
|
|
|
"IPv6 address/prefix to bind WireGuard (e.g., 2001:db8::1/64).";
|
2025-08-17 20:37:46 +05:30
|
|
|
};
|
|
|
|
serverPort = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
description = "Port for WireGuard.";
|
|
|
|
};
|
|
|
|
clientAddress = mkOption {
|
|
|
|
type = types.str;
|
2025-09-30 21:10:33 +05:30
|
|
|
description = "Peer IPv6 address/prefix (e.g., 2001:db8::2/128).";
|
2025-08-17 20:37:46 +05:30
|
|
|
};
|
|
|
|
clientSubnet = mkOption {
|
|
|
|
type = types.str;
|
2025-09-30 21:10:33 +05:30
|
|
|
description = "Peer IPv6 subnet (e.g., 2001:db8::/64).";
|
2025-08-17 20:37:46 +05:30
|
|
|
};
|
|
|
|
serverPrivateKeyFile = mkOption {
|
2025-09-30 21:10:33 +05:30
|
|
|
type = types.path;
|
|
|
|
description = "Path to server private key file.";
|
2025-08-17 20:37:46 +05:30
|
|
|
};
|
|
|
|
clientPublicKeyFile = mkOption {
|
2025-09-30 21:10:33 +05:30
|
|
|
type = types.path;
|
|
|
|
description = "Path to client public key file.";
|
2025-08-17 20:37:46 +05:30
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
interfaces = config.wireguard.interfaces;
|
|
|
|
|
|
|
|
extract = attr: map (x: x.${attr}) interfaces;
|
|
|
|
|
|
|
|
assertUnique = name: values: {
|
|
|
|
assertion = values == lib.unique values;
|
|
|
|
message =
|
|
|
|
"Duplicate values found for '${name}': ${builtins.toString values}";
|
|
|
|
};
|
|
|
|
|
2025-09-30 21:10:33 +05:30
|
|
|
fileAssertions = lib.flatten (map (cfg: [
|
2025-08-17 20:37:46 +05:30
|
|
|
{
|
2025-09-30 21:10:33 +05:30
|
|
|
assertion = cfg.serverPrivateKeyFile != null;
|
|
|
|
message = "serverPrivateKeyFile must be provided for interface ${cfg.interface}";
|
2025-08-17 20:37:46 +05:30
|
|
|
}
|
|
|
|
{
|
2025-09-30 21:10:33 +05:30
|
|
|
assertion = cfg.clientPublicKeyFile != null;
|
|
|
|
message = "clientPublicKeyFile must be provided for interface ${cfg.interface}";
|
2025-08-17 20:37:46 +05:30
|
|
|
}
|
|
|
|
]) interfaces);
|
|
|
|
|
|
|
|
uniquenessAssertions = [
|
|
|
|
(assertUnique "interface" (extract "interface"))
|
|
|
|
(assertUnique "serverAddress" (extract "serverAddress"))
|
|
|
|
(assertUnique "serverPort" (extract "serverPort"))
|
|
|
|
(assertUnique "clientAddress" (extract "clientAddress"))
|
|
|
|
(assertUnique "clientSubnet" (extract "clientSubnet"))
|
|
|
|
];
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.wireguard.interfaces = mkOption {
|
|
|
|
type = wgConfigType;
|
2025-09-30 21:10:33 +05:30
|
|
|
default = [];
|
2025-08-17 20:37:46 +05:30
|
|
|
description = "List of WireGuard interface configurations.";
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2025-09-30 21:10:33 +05:30
|
|
|
assertions = lib.mkAfter (fileAssertions ++ uniquenessAssertions);
|
2025-08-17 20:37:46 +05:30
|
|
|
|
2025-09-30 21:10:33 +05:30
|
|
|
systemd.network.enable = true;
|
2025-08-17 20:37:46 +05:30
|
|
|
|
|
|
|
systemd.network.netdevs = lib.mkMerge (map (cfg: {
|
|
|
|
"${cfg.interface}" = {
|
|
|
|
netdevConfig = {
|
|
|
|
Kind = "wireguard";
|
2025-09-30 21:10:33 +05:30
|
|
|
MTUBytes = "1412";
|
2025-08-17 20:37:46 +05:30
|
|
|
Name = cfg.interface;
|
|
|
|
};
|
|
|
|
wireguardConfig = {
|
2025-09-30 21:10:33 +05:30
|
|
|
PrivateKeyFile = cfg.serverPrivateKeyFile;
|
2025-08-17 20:37:46 +05:30
|
|
|
ListenPort = cfg.serverPort;
|
|
|
|
};
|
|
|
|
wireguardPeers = [{
|
2025-09-30 21:10:33 +05:30
|
|
|
PublicKeyFile = cfg.clientPublicKeyFile;
|
2025-08-17 20:37:46 +05:30
|
|
|
AllowedIPs = [ cfg.clientAddress cfg.clientSubnet ];
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
}) interfaces);
|
|
|
|
|
|
|
|
systemd.network.networks = lib.mkMerge (map (cfg: {
|
|
|
|
"${cfg.interface}" = {
|
|
|
|
matchConfig = { Name = cfg.interface; };
|
2025-09-30 21:10:33 +05:30
|
|
|
networkConfig = { IPv6Forwarding = "yes"; };
|
|
|
|
|
2025-08-17 20:37:46 +05:30
|
|
|
address = [ cfg.serverAddress ];
|
|
|
|
|
|
|
|
routes = [
|
|
|
|
{
|
|
|
|
Destination = cfg.clientSubnet;
|
|
|
|
Scope = "link";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}) interfaces);
|
2025-09-30 21:10:33 +05:30
|
|
|
|
|
|
|
# Automatically open firewall ports for WireGuard
|
|
|
|
networking.firewall.allowedUDPPorts = map (cfg: cfg.serverPort) interfaces;
|
2025-08-17 20:37:46 +05:30
|
|
|
};
|
|
|
|
}
|