forked from subspacecommunity/subspace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
283 lines (252 loc) · 9.68 KB
/
Copy pathflake.nix
File metadata and controls
283 lines (252 loc) · 9.68 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
description = "A fork of the simple WireGuard VPN server GUI community maintained ";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
# Don't update this this input, as newer versions of wg-bond are no longer compatible with subspace
wg-bond.url = "github:cab404/wg-bond/v0.2.0";
};
outputs = { self, nixpkgs, flake-utils, wg-bond }: (flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; overlays = [ self.overlay ]; };
in
{
packages.subspace = pkgs.subspace;
packages.wireguard-tools = pkgs.wireguard-tools;
packages.wg-bond = with pkgs; rustPlatform.buildRustPackage {
pname = "wg-bond";
version = "0.2.0";
src = wg-bond;
cargoHash = "sha256-Itw3fnKfUW+67KKB2Y7tutGBTm3E8mGNhBL4MOGEn9o=";
nativeBuildInputs = [ makeWrapper ];
postInstall = ''
wrapProgram $out/bin/wg-bond --set PATH ${lib.makeBinPath [ wireguard-tools ]}
'';
};
defaultPackage = self.packages.${system}.subspace;
devShell = pkgs.mkShell {
buildInputs = with pkgs; [ self.packages.${system}.wireguard-tools self.packages.${system}.wg-bond go go-bindata ];
};
})) // {
overlay = final: prev: {
wireguard-tools = prev.wireguard-tools.overrideDerivation (super: {
patches = super.patches ++ [
./wg-quick-no-uid.patch
];
});
subspace =
let
goPackagePath = "github.com/subspacecommunity/subspace";
version = "1.5.0";
in
final.buildGoPackage {
inherit goPackagePath version;
src = nixpkgs.lib.cleanSource ./.;
name = "subspace";
goDeps = ./deps.nix;
nativeBuildInputs = with final; [ go-bindata which diffutils ];
buildPhase = ''
runHook preBuild
cd go/src/${goPackagePath}
export CGO_ENABLED=0
rm -rf subspace
go-bindata -o cmd/subspace/bindata.go --prefix "web/" --pkg main web/...
go build -v --compiler gc --ldflags "-extldflags -static -s -w -X main.version=${version}" -o subspace ./cmd/subspace
runHook postBuild
'';
installPhase = ''
install -Dm777 subspace $out/bin/subspace
mkdir -p $out/libexec
cp -r web $out/libexec/web
'';
};
};
nixosModule = { pkgs, lib, config, ... }:
with lib;
let
cfg = config.services.subspace;
in
{
options.services.subspace = {
enable = mkEnableOption "subspace";
package = mkOption {
description = "A package from which to take subspace";
default = self.defaultPackage.${pkgs.system};
type = types.package;
};
privateKeyFile = mkOption {
description = "Path to Wireguard private key";
default = "/secrets/subspace.private";
type = types.str;
};
user = mkOption {
description = "User account under which Subspace runs.";
default = "subspace";
type = types.str;
};
group = mkOption {
description = "Group account under which Subspace runs.";
default = "subspace";
type = types.str;
};
httpHost = mkOption {
description = "The host to listen on and set cookies for";
default = "localhost";
type = types.str;
};
backlink = mkOption {
description = "The page to set the home button to";
default = "/";
type = types.str;
};
dataDir = mkOption {
description = "Path to data folder";
default = "/var/lib/subspace";
type = types.str;
};
debug = mkOption {
description = "Place subspace into debug mode for verbose log output";
default = false;
type = types.bool;
};
httpInsecure = mkOption {
description = "enable session cookies for http and remove redirect to https";
default = false;
type = types.bool;
};
letsencrypt = mkOption {
description = "Whether or not to use a LetsEncrypt certificate";
default = true;
type = types.bool;
};
httpAddr = mkOption {
description = "HTTP Listen address";
default = ":3331";
type = types.str;
};
params = mkOption {
description = "Parameters for Subspace binary";
default = "";
type = types.str;
};
proxyPort = mkOption {
description = "Port for managed WireGuard interface";
default = "53222";
type = types.str;
};
subnet = mkOption {
description = "Subnet to be used by Subspace VPN";
default = "10.0.0.0/24";
type = types.str;
};
};
config = mkIf cfg.enable {
users.users = optionalAttrs (cfg.user == "subspace") ({
subspace = {
isSystemUser = true;
group = cfg.group;
# uid = config.ids.uids.subspace;
description = "Subspace WireGuard GUI user";
home = cfg.dataDir;
};
});
users.groups = optionalAttrs (cfg.group == "subspace") ({
subspace = {
# gid = config.ids.gids.subspace;
};
});
systemd.tmpfiles.rules = [ "d ${cfg.dataDir} 0750 ${cfg.user} ${cfg.group}" ];
systemd.services.subspace = rec {
description = "A simple WireGuard VPN server GUI";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
startLimitBurst = mkDefault 5;
startLimitIntervalSec = mkDefault 300;
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Restart = mkDefault "on-failure";
RestartSec = mkDefault 10;
CapabilityBoundingSet = "CAP_NET_ADMIN";
AmbientCapabilities = "CAP_NET_ADMIN";
ReadWritePaths = [ "${cfg.dataDir}" ];
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_NETLINK"
];
RestrictNamespaces = "yes";
DeviceAllow = "no";
KeyringMode = "private";
NoNewPrivileges = "yes";
NotifyAccess = "none";
PrivateDevices = "yes";
PrivateMounts = "yes";
PrivateTmp = "yes";
ProtectClock = "yes";
ProtectControlGroups = "yes";
ProtectHome = "yes";
ProtectKernelLogs = "yes";
ProtectKernelModules = "yes";
ProtectKernelTunables = "yes";
ProtectProc = "invisible";
ProtectSystem = "strict";
RestrictSUIDSGID = "yes";
SystemCallArchitectures = "native";
SystemCallFilter = [
"~@clock"
"~@debug"
"~@module"
"~@mount"
"~@raw-io"
"~@reboot"
"~@swap"
# "~@privileged"
"~@resources"
"~@cpu-emulation"
"~@obsolete"
];
RestrictRealtime = "yes";
Delegate = "no";
LockPersonality = "yes";
MemoryDenyWriteExecute = "yes";
RemoveIPC = "yes";
UMask = "0027";
ProtectHostname = "yes";
ProcSubset = "pid";
WorkingDirectory = "${cfg.package}/libexec";
};
path = with pkgs; [ self.packages.${system}.wg-bond self.packages.${system}.wireguard-tools iptables bash gawk ];
preStart = ''
if [[ ! -f ${cfg.dataDir}/wireguard/wg-bond.json ]]; then
mkdir -p ${cfg.dataDir}/wireguard/
mkdir -p ${cfg.dataDir}/wireguard/clients
mkdir -p ${cfg.dataDir}/wireguard/peers
wg-bond -c ${cfg.dataDir}/wireguard/wg-bond.json init subspace --network "${cfg.subnet}"
wg-bond -c ${cfg.dataDir}/wireguard/wg-bond.json add subspace-root --endpoint ${cfg.httpHost}:${cfg.proxyPort} --center --gateway --masquerade eth0
fi
if [[ ! -d ${cfg.dataDir}/wireguard/clients ]]; then mkdir -p ${cfg.dataDir}/wireguard/clients; fi
if [[ ! -d ${cfg.dataDir}/wireguard/peers ]]; then mkdir -p ${cfg.dataDir}/wireguard/peers; fi
wg-bond -c ${cfg.dataDir}/wireguard/wg-bond.json conf subspace-root > ${cfg.dataDir}/wireguard/subspace.conf
wg-quick up ${cfg.dataDir}/wireguard/subspace.conf
chmod -R u+rwX,g+rX,o-rwx ${cfg.dataDir}
chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
'';
postStop = ''
wg-quick down ${cfg.dataDir}/wireguard/subspace.conf
'';
script = ''
${cfg.package}/bin/subspace \
--http-host="${cfg.httpHost}" \
--backlink="${cfg.backlink}" \
--datadir="${cfg.dataDir}" \
--debug="${if cfg.debug then "true" else "false"}" \
--http-addr="${cfg.httpAddr}" \
--http-insecure="${if cfg.httpInsecure then "true" else "false"}" \
--letsencrypt="${if cfg.letsencrypt then "true" else "false"}" \
${cfg.params}
'';
};
};
};
};
}