blob: 0351d81d555926a55cb31968410ad854147e30f9 (
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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.openvpn;
makeOpenVPNJob =
cfg: name:
let
path = makeBinPath (getAttr "openvpn-${name}" config.systemd.services).path;
upScript = ''
export PATH=${path}
# For convenience in client scripts, extract the remote domain
# name and name server.
for var in ''${!foreign_option_*}; do
x=(''${!var})
if [ "''${x[0]}" = dhcp-option ]; then
if [ "''${x[1]}" = DOMAIN ]; then domain="''${x[2]}"
elif [ "''${x[1]}" = DNS ]; then nameserver="''${x[2]}"
fi
fi
done
${cfg.up}
${optionalString cfg.updateResolvConf "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
'';
downScript = ''
export PATH=${path}
${optionalString cfg.updateResolvConf "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
${cfg.down}
'';
configFile = pkgs.writeText "openvpn-config-${name}" ''
errors-to-stderr
${optionalString (cfg.up != "" || cfg.down != "" || cfg.updateResolvConf) "script-security 2"}
${cfg.config}
${optionalString (
cfg.up != "" || cfg.updateResolvConf
) "up ${pkgs.writeShellScript "openvpn-${name}-up" upScript}"}
${optionalString (
cfg.down != "" || cfg.updateResolvConf
) "down ${pkgs.writeShellScript "openvpn-${name}-down" downScript}"}
${optionalString (cfg.authUserPass != null) (
if isAttrs cfg.authUserPass then
"auth-user-pass ${pkgs.writeText "openvpn-credentials-${name}" ''
${cfg.authUserPass.username}
${cfg.authUserPass.password}
''}"
else
"auth-user-pass ${cfg.authUserPass}"
)}
'';
in
{
description = "OpenVPN instance ‘${name}’";
wantedBy = optional cfg.autoStart "multi-user.target";
after = [ "network.target" ];
path = [
pkgs.iptables
pkgs.iproute2
pkgs.net-tools
];
serviceConfig.ExecStart = "@${config.services.openvpn.package}/sbin/openvpn openvpn --suppress-timestamps --config ${configFile}";
serviceConfig.Restart = "always";
serviceConfig.Type = "notify";
};
restartService = optionalAttrs cfg.restartAfterSleep {
openvpn-restart = {
wantedBy = [ "sleep.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart =
let
unitNames = map (n: "openvpn-${n}.service") (builtins.attrNames cfg.servers);
in
"systemctl try-restart ${lib.escapeShellArgs unitNames}";
};
description = "Restart system OpenVPN connections when returning from sleep";
};
};
in
{
imports = [
(mkRemovedOptionModule [ "services" "openvpn" "enable" ] "")
];
###### interface
options = {
services.openvpn.package = lib.mkPackageOption pkgs "openvpn" { };
services.openvpn.servers = mkOption {
default = { };
example = literalExpression ''
{
server = {
config = '''
# Simplest server configuration: https://community.openvpn.net/openvpn/wiki/StaticKeyMiniHowto
# server :
dev tun
ifconfig 10.8.0.1 10.8.0.2
secret /root/static.key
''';
up = "ip route add ...";
down = "ip route del ...";
};
client = {
config = '''
client
remote vpn.example.org
dev tun
proto tcp-client
port 8080
ca /root/.vpn/ca.crt
cert /root/.vpn/alice.crt
key /root/.vpn/alice.key
''';
up = "echo nameserver $nameserver | ''${pkgs.openresolv}/sbin/resolvconf -m 0 -a $dev";
down = "''${pkgs.openresolv}/sbin/resolvconf -d $dev";
};
}
'';
description = ''
Each attribute of this option defines a systemd service that
runs an OpenVPN instance. These can be OpenVPN servers or
clients. The name of each systemd service is
`openvpn-«name».service`,
where «name» is the corresponding
attribute name.
'';
type =
with types;
attrsOf (submodule {
options = {
config = mkOption {
type = types.lines;
description = ''
Configuration of this OpenVPN instance. See
{manpage}`openvpn(8)`
for details.
To import an external config file, use the following definition:
`config = "config /path/to/config.ovpn"`
'';
};
up = mkOption {
default = "";
type = types.lines;
description = ''
Shell commands executed when the instance is starting.
'';
};
down = mkOption {
default = "";
type = types.lines;
description = ''
Shell commands executed when the instance is shutting down.
'';
};
autoStart = mkOption {
default = true;
type = types.bool;
description = "Whether this OpenVPN instance should be started automatically.";
};
updateResolvConf = mkOption {
default = false;
type = types.bool;
description = ''
Use the script from the update-resolv-conf package to automatically
update resolv.conf with the DNS information provided by openvpn. The
script will be run after the "up" commands and before the "down" commands.
'';
};
authUserPass = mkOption {
default = null;
description = ''
This option can be used to store the username / password credentials
with the "auth-user-pass" authentication method.
You can either provide an attribute set of `username` and `password`,
or the path to a file containing the credentials on two lines.
WARNING: If you use an attribute set, this option will put the credentials WORLD-READABLE into the Nix store!
'';
type = types.nullOr (
types.oneOf [
types.singleLineStr
(types.submodule {
options = {
username = mkOption {
description = "The username to store inside the credentials file.";
type = types.str;
};
password = mkOption {
description = "The password to store inside the credentials file.";
type = types.str;
};
};
})
]
);
};
};
});
};
services.openvpn.restartAfterSleep = mkOption {
default = true;
type = types.bool;
description = "Whether OpenVPN client should be restarted after sleep.";
};
};
###### implementation
config = mkIf (cfg.servers != { }) {
systemd.services =
(listToAttrs (
mapAttrsToList (
name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)
) cfg.servers
))
// restartService;
environment.systemPackages = [ cfg.package ];
boot.kernelModules = [ "tun" ];
};
}
|