blob: c758174b4517dcd294ee2ed26daa634b82f7ffc1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
{
config,
lib,
hostname,
masterDomain,
...
}:
let
cfg = config.os.srv.wireguard;
in
{
options.os.srv.wireguard = {
enable = lib.mkEnableOption "enables wireguard vpn";
role = lib.mkOption {
type = lib.types.enum [
"server"
"client"
];
default = "client";
description = "where the machine is accepting connections or connecting";
};
server = {
externalInterface = lib.mkOption {
type = lib.types.str;
default = "eth0";
description = "The public WAN interface of the server";
};
publicKey = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "The public key of your primary WireGuard server node.";
};
peers = lib.mkOption {
type = lib.types.listOf (
lib.types.submodule {
options = {
name = lib.mkOption { type = lib.types.str; };
publicKey = lib.mkOption { type = lib.types.str; };
};
}
);
default = [ ];
description = "List of client peers authorized to connect to this server";
};
};
client = {
index = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "The assigned host index number for the client IP address";
};
routeAllTraffic = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Routes 100% of your internet traffic through the server when active";
};
};
};
config = lib.mkIf cfg.enable (
lib.mkMerge [
{
assertions = [
{
assertion = config.os.srv.sops.enable;
message = "required for wg private key";
}
];
sops.secrets."wg_private_key/${hostname}" = {
owner = "root";
group = "root";
mode = "0600";
};
}
(lib.mkIf (cfg.role == "server") {
assertions = [
{
assertion = config.os.srv.firewall.enable;
message = "required for opening ports and passthrough";
}
];
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
networking.firewall.allowedUDPPorts = [ 51280 ];
networking.nftables = {
tables.wg-nat = {
family = "inet";
content = ''
chain forward {
type filter hook forward priority 0; policy accept;
iifname "wg0" accept
oifname "wg0" accept
}
chain postrouting {
type nat hook postrouting priority 100; policy accept;
oifname "${cfg.server.externalInterface}" masquerade
}
'';
};
};
networking.wireguard.interfaces.wg0 = {
ips = [ "10.3.0.1/24" ];
listenPort = 51280;
privateKeyFile = config.sops.secrets."wg_private_key/${hostname}".path;
peers = lib.imap1 (i: peer: {
publicKey = peer.publicKey;
allowedIPs = [ "10.3.0.${toString (i + 1)}/32" ];
persistentKeepalive = 25;
}) cfg.server.peers;
};
})
(lib.mkIf (cfg.role == "client") {
assertions = [
{
assertion = cfg.client.index != null;
message = "WireGuard client role requires a valid 'client.index' integer designation.";
}
];
networking.nameservers = [
config.os.core.network.ips.vm2-gateway
"9.9.9.9"
];
networking.wireguard.interfaces.wg0 = {
ips = [ "10.3.0.${toString cfg.client.index + 1}/24" ];
privateKeyFile = config.sops.secrets."wg_private_key/${hostname}".path;
peers = [
{
publicKey = cfg.server.publicKey;
endpoint = "${masterDomain}:51280";
persistentKeepalive = 25;
allowedIPs = if cfg.client.routeAllTraffic then [ "0.0.0.0/0" ] else [ "10.255.0.0/16" ];
}
];
};
})
]
);
}
|