-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixjail.nix
442 lines (392 loc) · 15.8 KB
/
nixjail.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# NixJail --- Bwrap wrapper for nixpkgs
# Copyright (C) 2023 Shiryel <contact@shiryel.com>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <https://www.gnu.org/licenses/>.
{ config, lib, pkgs, ... }@inputs:
with builtins;
let
my_lib = lib // (import ./libs.nix inputs);
cfg = config.nixjail;
in
with my_lib;
{
options.nixjail =
let
bind_from_to = with types; {
options = {
from = mkOption { type = str; };
to = mkOption { type = str; };
};
};
shared_options = with types; {
install = mkOption {
default = true;
type = bool;
description = mdDoc "Add package to `environment.systemPackages`";
};
post_exec = mkOption {
default = ''"$@"'';
type = str;
description = mdDoc "arguments to pass to the packages";
};
pre_exec = mkOption {
default = "";
type = str;
description = mdDoc "commands before the exec";
};
#
# BWRAP
#
autoBindHome = mkOption {
default = true;
type = bool;
description = mdDoc "Automatically creates a home directory on `home_dir_root`";
};
dri = mkOption {
default = false;
type = bool;
description = mdDoc "If `true` add `--dev-bind-try /dev/dri /dev/dri`";
};
dev = mkOption {
default = false;
type = bool;
description = mdDoc "If `true` add `--dev-bind-try /dev /dev`";
};
xdg = mkOption {
default = false;
type = oneOf [ bool (enum [ "ro" ]) ];
description = mdDoc "If `true` add `--bind-try $XDG_RUNTIME_DIR $XDG_RUNTIME_DIR`";
};
net = mkOption {
default = false;
type = bool;
description = mdDoc "If `true` add `--share-net`";
};
tmp = mkOption {
default = false;
type = bool;
description = mdDoc "If `true` add `--bind-try /tmp /tmp`";
};
ipc = mkOption {
default = false;
type = bool;
description = mdDoc "Share IPC";
};
trim_etc = mkOption {
default = true;
type = bool;
description = mdDoc "Only ro-bind the essential on /etc";
};
cacert = mkOption {
default = null;
type = nullOr package;
description = mdDoc "replace cacert package. (requires trim_etc = true)";
};
resolv = mkOption {
default = null;
type = nullOr str;
description = mdDoc "replace /etc/resolv.conf. (requires trim_etc = true)";
};
rwBinds = mkOption {
default = [ ];
type = listOf (oneOf [ str (submodule bind_from_to) ]);
description = mdDoc "Adds `--bind-try $(readlink -mn $${cfg.from} $${cfg.to})`";
};
roBinds = mkOption {
default = [ ];
type = listOf (oneOf [ str (submodule bind_from_to) ]);
description = mdDoc "Adds `--ro-bind-try $(readlink -mn $${cfg.from} $${cfg.to})`";
};
defaultBinds = mkOption {
default = true;
type = bool;
description = mdDoc ''
Adds the following read-only binds:
"$HOME/.config/mimeapps.list"
"$HOME/.local/share/applications/mimeapps.list"
"$HOME/.config/dconf"
"$HOME/.config/gtk-3.0/settings.ini"
"$HOME/.config/gtk-4.0/settings.ini"
"$HOME/.gtkrc-2.0"
'';
};
unshareAll = mkOption {
default = true;
type = bool;
description = mdDoc "If `false` removes `--unshare-all`, not recommended!";
};
#
# DBUS PROXY
#
dbusProxy =
let
# Descriptions from: https://github.com/flatpak/xdg-dbus-proxy/blob/main/xdg-dbus-proxy.xml
# The policy for the filtering consists of a mapping from well-known names to a policy that is either SEE, TALK or OWN.
# The default initial policy is that the the user is only allowed to TALK to the bus itself (org.freedesktop.DBus, or
# no destination specified), and TALK to its own unique ID. All other clients are invisible.
shared_dbus_options = {
sees = mkOption {
default = [ ];
type = listOf str;
description = ''
- The name/ID is visible in the ListNames reply
- The name/ID is visible in the ListActivatableNames repl
- You can call GetNameOwner on the name
- You can call NameHasOwner on the name
- You see NameOwnerChanged signals on the name
- You see NameOwnerChanged signals on the ID when the client disconnects
- You can call the GetXXX methods on the name/ID to get e.g. the peer pid
- You get AccessDenied rather than NameHasNoOwner when sending messages to the name/ID
'';
};
talks = mkOption {
default = [ ];
type = listOf str;
description = ''
- You can send any method calls and signals to the name/ID
- You will receive broadcast signals from the name/ID (if you have a match rule for them)
- You can call StartServiceByName on the name
'';
};
owns = mkOption {
default = [ ];
type = listOf str;
description = ''
- You are allowed to call RequestName/ReleaseName/ListQueuedOwners on the name
'';
};
calls = mkOption {
default = [ ];
type = listOf str;
description = ''
In addition to the basic SEE/TALK/OWN policy, it is possible to specify more complicated rules about what method calls can be made on and what broadcast signals can be received from well-known names. A rule can restrict the allowed calls/signals to a specific object path or a subtree of object paths, and it can restrict the allowed interface down to an individual method or signal name.
Rules are specified with the --call and --broadcast options. The RULE in these options determines what interfaces, methods and object paths are allowed. It must be of the form [METHOD][@PATH], where METHOD can be either '*' or a D-Bus interface, possible with a '.*' suffix, or a fully-qualified method name, and PATH is a D-Bus object path, possible with a '/*' suffix.
'';
};
broadcasts = mkOption {
default = [ ];
type = listOf str;
description = ''
In addition to the basic SEE/TALK/OWN policy, it is possible to specify more complicated rules about what method calls can be made on and what broadcast signals can be received from well-known names. A rule can restrict the allowed calls/signals to a specific object path or a subtree of object paths, and it can restrict the allowed interface down to an individual method or signal name.
Rules are specified with the --call and --broadcast options. The RULE in these options determines what interfaces, methods and object paths are allowed. It must be of the form [METHOD][@PATH], where METHOD can be either '*' or a D-Bus interface, possible with a '.*' suffix, or a fully-qualified method name, and PATH is a D-Bus object path, possible with a '/*' suffix.
'';
};
};
in
{
enable = mkOption {
default = false;
type = bool;
description = "Enables xdg-dbus-proxy";
};
debug = mkOption {
default = false;
type = bool;
description = "Enables xdg-dbus-proxy logs";
};
user = shared_dbus_options;
system = shared_dbus_options;
};
#
# EXTRA
#
keepSession = mkOption {
default = false;
type = bool;
description = mdDoc ''
Fixes "cannot set terminal process group (-1)" by adding `--new-session`
but is not recommended because of a security issue with TIOCSTI [1]
[1] - https://wiki.archlinux.org/title/Bubblewrap#New_session
'';
};
ldCache = mkOption {
default = false;
type = bool;
description = mdDoc "Add ld.so.conf and ld.so.cache symlinks (both 32 and 64 bit glibcs)";
};
extraConfig = mkOption {
default = [ ];
type = listOf str;
description = mdDoc "Extra configs for `bwrap`";
};
};
in
{
pkgs = mkOption {
default = pkgs;
internal = true;
};
fhs = {
defaultHomeDirRoot = with types; mkOption {
default = "$HOME/nixjail";
type = str;
description = mdDoc "Default root dir, used by `homeDirRoot`";
};
profiles = with types; mkOption {
default = [ ];
description = mdDoc "Configure profiles for the `packages` list, using the further options to configure them with bwrap";
type = listOf (submodule {
options = {
homeDirRoot = mkOption {
default = cfg.fhs.defaultHomeDirRoot;
type = str;
description = mdDoc "Root dir for the `autoBindHome`";
};
name = mkOption {
default = null;
type = str;
description = mdDoc "Name of the FHS";
};
runScript = mkOption {
default = "$TERM";
type = str;
description = mdDoc "Script to run when starting FHS";
};
profile = mkOption {
default = "";
type = str;
description = mdDoc "Script to run when configuring FHS";
};
targetPkgs = mkOption {
default = pkgs: [ ];
type = functionTo (listOf package);
description = mdDoc "Packages that will only be installed once-matching the host's architecture (64bit on x86_64 and 32bit on x86)";
};
multiPkgs = mkOption {
default = pkgs: [ ];
type = functionTo (listOf package);
description = mdDoc ''
Packages installed once on x86 systems and twice on x86_64 systems.
On x86 they are merged with packages from targetPkgs.
On x86_64 they are added to targetPkgs and in addition their 32bit versions are also installed.
The final directory structure looks as follows:
/lib32 will include 32bit libraries from multiPkgs
/lib64 will include 64bit libraries from multiPkgs and targetPkgs /lib will link to /lib32
'';
};
} // shared_options;
});
};
};
bwrap = {
defaultHomeDirRoot = with types; mkOption {
default = "$HOME/nixjail";
type = str;
description = mdDoc "Default root dir, used by `homeDirRoot`";
};
profiles = with types; mkOption {
default = [ ];
description = mdDoc "Configure profiles for the `packages` list, using the further options to configure them with bwrap";
type = listOf (submodule {
options = {
homeDirRoot = mkOption {
default = cfg.bwrap.defaultHomeDirRoot;
type = str;
description = mdDoc "Root dir for the `autoBindHome`";
};
packages = mkOption {
default = final: prev: { };
type = mkOptionType {
name = "nixpkgs-overlay";
description = "nixpkgs overlay";
check = lib.isFunction;
merge = lib.mergeOneOption;
};
description = mdDoc "Packages to be wrapped with bwrap using the configs on the profile";
};
symlinkJoin = mkOption {
default = true;
type = bool;
description = mdDoc "If `false` it disables the merge of the generated bwrapped package with the original content (like desktop entries, libs and man pages)";
};
removeDesktopItems = mkOption {
default = false;
type = bool;
description = mdDoc "Removes all desktop items from derivation, requires `symlinkJoin = false` to work";
};
} // shared_options;
});
};
};
};
config = {
environment.systemPackages =
let
bwrap_packages = pipe cfg.bwrap.profiles [
# map by profile
(map ({ install, packages, ... }@profile:
# list of packages names for the `environment.systemPackages` attr
(if install then
pipe (packages cfg.pkgs cfg.pkgs) [
# we need to use cfg.pkgs."${package_name}" because _package would be the
# version BEFORE the overlay IF the package_name is not the same attr as _package
(mapAttrsToList (package_name: _package: cfg.pkgs."${package_name}"))
]
else
[ ]))
)
lists.flatten
];
fhs_packages = pipe cfg.fhs.profiles [
# map by profile
(map ({ install, name, ... }@profile:
# list of packages names for the `environment.systemPackages` attr
(if install then
[ cfg.pkgs.${name} ]
else
[ ]))
)
lists.flatten
];
in
assert isList bwrap_packages;
assert isList fhs_packages;
bwrap_packages ++ fhs_packages;
# FIXME:
# if nixpkgs /pkgs/top-level/aliases.nix has an alias: X = Y;
# and the user create an overlay like: { Y = X; }
# them we will have an infinite recursion on the `checkInPkgs` function
nixpkgs.overlays =
let
bwrap_overlays = pipe cfg.bwrap.profiles [
# map by profile
(map ({ packages, ... }@profile:
# list of functions for the `nixpkgs.overlays` attr
(final: prev: pipe (packages final prev) [
(mapAttrsToList (package_name: package: {
name = package_name;
value = bwrapIt ((removeAttrs profile [ "install" "packages" ]) // { inherit package; name = package_name; });
}))
listToAttrs
]))
)
lists.flatten
];
fhs_overlays = final: prev: pipe cfg.fhs.profiles [
# map by profile
(map ({ name, ... }@profile:
{
name = name;
value = (fhsIt (removeAttrs profile [ "install" ]));
})
)
listToAttrs
];
in
assert isList bwrap_overlays;
assert isFunction fhs_overlays;
# order matters
[ fhs_overlays ] ++ bwrap_overlays;
};
}