blob: ede6958089452bc51b3e3158df2ba51981db0497 (
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
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.nordvpn;
defaultUser = "nordvpn";
defaultGroup = "nordvpn";
nordvpn =
let
cli = cfg.package.cli.overrideAttrs (old: {
preBuild =
let
extraPreBuild = lib.optionalString (cfg.group != defaultGroup) ''
substituteInPlace internal/permissions.go \
--replace-fail \
'string{"nordvpn"}' \
'string{"${cfg.group}"}'
substituteInPlace internal/constants.go \
--replace-fail \
'NordvpnGroup = "nordvpn"' \
'NordvpnGroup = "${cfg.group}"'
'';
in
extraPreBuild + (old.preBuild or "");
# postFixup wraps nordvpnd so that it can find binaries that it calls.
# here, instead, use systemd to update the path to those binaries.
postFixup = "";
});
in
pkgs.symlinkJoin {
inherit (cfg.package) pname version meta;
paths = [
cli
cfg.package.gui
];
};
in
{
options.services.nordvpn = {
enable = lib.mkEnableOption "Enable NordVPN";
package = lib.mkPackageOption pkgs "nordvpn" { };
user = lib.mkOption {
type = lib.types.str;
default = defaultUser;
description = ''
The User that owns the `nordvpnd` systemd process.
If overriding the default, a user with the same name must exist.
'';
};
group = lib.mkOption {
type = lib.types.str;
default = defaultGroup;
description = ''
The Group that owns the `nordvpnd` systemd process.
If overriding the default, a group with the same name must exist.
'';
};
};
config = lib.mkIf cfg.enable {
# create the default user if that's the one used
users.users = lib.mkIf (cfg.user == defaultUser) {
${defaultUser} = {
description = "User that runs `nordvpnd`.";
group = cfg.group;
isSystemUser = true;
};
};
# create the default group if that's the one used
users.groups = lib.mkIf (cfg.group == defaultGroup) {
${defaultGroup} = { };
};
# nordvpnd uses resolved to configure dns
services.resolved.enable = true;
# policy that allows nordvpnd to configure dns
security.polkit = {
enable = true;
extraConfig = ''
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.resolve1.set-dns-servers"
&& subject.isInGroup("${cfg.group}")) {
return polkit.Result.YES;
}
});
'';
};
environment.systemPackages = [
nordvpn
];
systemd.services.nordvpnd = {
after = [ "network-online.target" ];
description = "NordVPN daemon.";
path = (
with pkgs;
[
e2fsprogs
iproute2
libxslt
nftables
procps
wireguard-tools
]
++ [ nordvpn ]
);
serviceConfig = {
# nordvpnd needs CAP_NET_ADMIN to configure network interfaces
AmbientCapabilities = "CAP_NET_ADMIN";
CapabilityBoundingSet = "CAP_NET_ADMIN";
ExecStart = lib.getExe' nordvpn "nordvpnd";
Group = cfg.group;
KillMode = "process";
NonBlocking = true;
Requires = "nordvpnd.socket";
Restart = "on-failure";
RestartSec = 5;
RuntimeDirectory = "nordvpn";
RuntimeDirectoryMode = "0750";
StateDirectory = "nordvpn";
StateDirectoryMode = "0750";
User = cfg.user;
};
wantedBy = [ "default.target" ];
wants = [ "network-online.target" ];
};
systemd.sockets.nordvpnd = {
description = "NordVPN Daemon Socket";
listenStreams = [ "/run/nordvpn/nordvpnd.sock" ];
partOf = [ "nordvpnd.service" ];
socketConfig = {
DirectoryMode = "0750";
NoDelay = true;
SocketGroup = cfg.group;
SocketMode = "0770";
SocketUser = cfg.user;
};
wantedBy = [ "sockets.target" ];
};
systemd.user.services.norduserd = {
after = [ "network-online.target" ];
description = "NordUserD Service";
serviceConfig = {
ExecStart = lib.getExe' nordvpn "norduserd";
NonBlocking = true;
Restart = "on-failure";
RestartSec = 5;
};
wantedBy = [ "graphical-session.target" ];
wants = [ "network-online.target" ];
};
};
meta = {
doc = ./nordvpn.md;
maintainers = with lib.maintainers; [
different-error
novalkun
];
};
}
|