-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg-netns.nix
183 lines (167 loc) · 5.96 KB
/
wg-netns.nix
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# A NixOS service that runs Wireguard in a netns, and can be bound to by other
# services to isolate their networking.
#
# Currently only supports one Wireguard namespace, but nothing should prevent
# running multiple, it just needs some NixOS config refactoring work.
#
# WARNING: currently this leaks DNS! Services inside the netns can talk to nscd
# outside and perform DNS resolutions this way. For my current set of use cases
# this is not a problem.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.my.services.wg-netns;
privKey = pkgs.writeText "wireguard-priv-key" cfg.privateKey;
resolvconf = pkgs.writeText "wg-resolv.conf" "nameserver 8.8.8.8";
nsswitchconf = pkgs.writeText "wg-nsswitch.conf" "hosts: files dns";
in
{
options.my.services.wg-netns = with lib; {
enable = mkEnableOption "Wireguard netns container";
privateKey = mkOption {
type = types.str;
description = ''
Wireguard private key for this host.
'';
};
peerPublicKey = mkOption {
type = types.str;
description = ''
Wireguard public key of the peer host.
'';
};
endpointAddr = mkOption {
type = types.str;
description = ''
IP:port of the Wireguard endpoint to connect to.
'';
};
ip4 = mkOption {
type = types.str;
description = ''
Local IPv4 of this host on the Wireguard interface.
'';
};
isolateServices = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Names of systemd services to "patch" to force them to run inside the
Wireguard network namespace.
'';
};
forwardPorts = mkOption {
type = types.listOf types.port;
default = [ ];
description = ''
Port numbers that services listen on in the Wireguard netns and that
should be exposed (listening on ::1 only) in the outer netns.
'';
};
};
config = lib.mkIf cfg.enable {
boot.kernelModules = [ "wireguard" ];
systemd =
let
patchedServices = lib.genAttrs cfg.isolateServices (
svcname: {
bindsTo = [ "wireguard.service" ];
after = [ "wireguard.service" ];
unitConfig.JoinsNamespaceOf = "wireguard-netns.service";
serviceConfig = {
PrivateNetwork = true;
BindReadOnlyPaths = [
"${resolvconf}:/etc/resolv.conf"
"${nsswitchconf}:/etc/nsswitch.conf"
];
};
}
);
forwardSockets = builtins.listToAttrs (
map
(port: {
name = "wireguard-netns-forward-${toString port}";
value = {
wantedBy = [ "sockets.target" ];
socketConfig.ListenStream = port;
};
})
cfg.forwardPorts
);
forwardServices = builtins.listToAttrs (
map
(port: rec {
name = "wireguard-netns-forward-${toString port}";
value = {
requires = [ "${name}.socket" ];
after = [ "${name}.socket" ];
unitConfig.JoinsNamespaceOf = "wireguard-netns.service";
serviceConfig = {
PrivateNetwork = true;
ExecStart = "${pkgs.systemd}/lib/systemd/systemd-socket-proxyd 127.0.0.1:${toString port}";
};
};
})
cfg.forwardPorts
);
in
{
services =
patchedServices
// forwardServices
// {
wireguard-netns = {
description = "wireguard netns manager";
before = [ "network.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
PrivateNetwork = true;
PrivateMounts = false;
ExecStart = pkgs.writers.writeDash "wireguard-netns-up" ''
${pkgs.iproute2}/bin/ip netns add wireguard
${pkgs.util-linux}/bin/umount /var/run/netns/wireguard
${pkgs.util-linux}/bin/mount --bind /proc/self/ns/net /var/run/netns/wireguard
'';
ExecStop = pkgs.writers.writeDash "wireguard-netns-down" ''
${pkgs.iproute2}/bin/ip netns del wireguard
'';
};
};
wireguard = {
description = "wireguard VPN client";
bindsTo = [ "wireguard-netns.service" ];
requires = [ "network-online.target" ];
after = [ "wireguard-netns.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writers.writeDash "wireguard-up" ''
# Note: creating the iface in the outer netns means that wg will
# "remember" the packets need to go through the outer netns.
${pkgs.iproute2}/bin/ip link add wireguard type wireguard
${pkgs.wireguard-tools}/bin/wg set wireguard \
private-key ${privKey} \
peer '${cfg.peerPublicKey}' \
endpoint '${cfg.endpointAddr}' \
allowed-ips '0.0.0.0/0,::0/0'
${pkgs.iproute2}/bin/ip link set wireguard netns wireguard up
${pkgs.iproute2}/bin/ip -n wireguard addr add ${cfg.ip4}/32 dev wireguard
${pkgs.iproute2}/bin/ip -n wireguard route add default dev wireguard
${pkgs.iproute2}/bin/ip -n wireguard -6 route add default dev wireguard
'';
ExecStop = pkgs.writers.writeDash "wireguard-down" ''
${pkgs.iproute2}/bin/ip -n wireguard route del default dev wireguard
${pkgs.iproute2}/bin/ip -n wireguard link del wireguard
'';
};
};
};
sockets = forwardSockets;
};
};
}