Split combinators into files
The `lib/combinators.nix` file was getting quite long and some ideas I have for future combinators may even require ancillary files. To help scale these changes, this commit breaks each combinator into its own file under the `lib/combinators` directory.
This commit is contained in:
parent
8fa6d132e1
commit
6181276ae1
54 changed files with 1531 additions and 1260 deletions
32
lib/combinators/add-cleanup.nix
Normal file
32
lib/combinators/add-cleanup.nix
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{ helpers, ... }:
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Adds arbitrary logic to run when the jail exits.
|
||||
|
||||
This is designed to be an easy way to register cleanup actions for things
|
||||
created in [add-runtime](#add-runtime). These scripts run in the same
|
||||
scope as `add-runtime` so any shell variables defined there will be in
|
||||
scope.
|
||||
|
||||
The cleanup actions may run even if the runtime doesn't— for example if a
|
||||
previous runtime exits non-zero the jail will exit prematurely, but the
|
||||
cleanup actions will still run.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
compose [
|
||||
(add-runtime ${"''"}
|
||||
TMP_FILE=$(mktemp)
|
||||
do-something "$TMP_FILE"
|
||||
${"''"})
|
||||
(add-cleanup ${"''"}
|
||||
if [ -e "${"''"}''${TMP_FILE-}" ]; then
|
||||
rm "$TMP_FILE"
|
||||
fi
|
||||
${"''"})
|
||||
]
|
||||
```
|
||||
'';
|
||||
impl = helpers.pushState "cleanup";
|
||||
}
|
||||
8
lib/combinators/add-path.nix
Normal file
8
lib/combinators/add-path.nix
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{ ... }:
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Prepends the passed string to `$PATH`.
|
||||
'';
|
||||
impl = path: state: state // { path = [ path ] ++ state.path; };
|
||||
}
|
||||
11
lib/combinators/add-pkg-deps.nix
Normal file
11
lib/combinators/add-pkg-deps.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{ combinators, lib, ... }:
|
||||
let
|
||||
inherit (combinators) compose add-path;
|
||||
in
|
||||
{
|
||||
sig = "[Package] -> Permission";
|
||||
doc = ''
|
||||
Adds the packages' `bin` directory to `$PATH`.
|
||||
'';
|
||||
impl = pkgs: compose (builtins.map (pkg: add-path "${lib.getBin pkg}/bin") pkgs);
|
||||
}
|
||||
29
lib/combinators/add-runtime.nix
Normal file
29
lib/combinators/add-runtime.nix
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{ ... }:
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Adds arbitrary logic to run at runtime, before the jail starts.
|
||||
|
||||
You can push additional bubblewrap arguments by appending the bash
|
||||
array `$RUNTIME_ARGS`. This allows you to modify the bubblewrap flags
|
||||
to be dependent on runtime conditions.
|
||||
|
||||
Note that anything added here is *not* run inside the jail. To run
|
||||
arbitrary things at runtime inside the jail see
|
||||
[wrap-entry](#wrap-entry).
|
||||
|
||||
Example:
|
||||
```nix
|
||||
add-runtime ${"''"}
|
||||
# binds /foo only if /bar exists on the host
|
||||
if [ -e /bar ]; then
|
||||
RUNTIME_ARGS+=(--bind /foo /foo)
|
||||
fi
|
||||
${"''"}
|
||||
```
|
||||
|
||||
If you create any resources in add-runtime that you want to automatically
|
||||
clean up when the jail exits use [add-cleanup](#add-cleanup).
|
||||
'';
|
||||
impl = runtime: state: state // { runtime = "${state.runtime}\n${runtime}\n"; };
|
||||
}
|
||||
78
lib/combinators/bind-nix-store-runtime-closure.nix
Normal file
78
lib/combinators/bind-nix-store-runtime-closure.nix
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
{ combinators, pkgs, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
compose
|
||||
defer
|
||||
include-once
|
||||
unsafe-add-raw-args
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
includedInBasePermissions = true;
|
||||
doc = ''
|
||||
Binds all `/nix/store` paths in the runtime closure of the jailed
|
||||
application.
|
||||
|
||||
If you don't have any sensitive nix store paths, you may consider just
|
||||
bind mounting `/nix/store` inside the jail instead.
|
||||
|
||||
For example, the jail defined by
|
||||
```nix
|
||||
# Note that this combinator is included in base permissions, so it does
|
||||
# not need to be provided:
|
||||
let listNixStore = pkgs.writeShellScriptBin "list-nix-store" "ls -l /nix/store";
|
||||
in jail "list-nix-store" listNixStore [];
|
||||
```
|
||||
will print something like
|
||||
```
|
||||
total 52
|
||||
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 2dx846w0q80307z72r9jxai4xlj9ghb2-list-nix-store
|
||||
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 3mi59bgj22xx29dyss7jhmx3sgznd85m-acl-2.3.2
|
||||
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 6hqzbvz50bm87hcj4qfn51gh7arxj8a6-gcc-14.2.1.20250322-libgcc
|
||||
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 6nkqdqzpa75514lhglgnjs5k4dklw4sb-libidn2-2.3.8
|
||||
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 7c0v0kbrrdc2cqgisi78jdqxn73n3401-gcc-14.2.1.20250322-lib
|
||||
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 87fck6hm17chxjq7badb11mq036zbyv9-coreutils-9.7
|
||||
dr-xr-xr-x 5 65534 65534 4096 Jan 1 1970 8syylmkvnn7lg2nar9fddpp5izb4gh56-attr-2.5.2
|
||||
dr-xr-xr-x 6 65534 65534 4096 Jan 1 1970 cg9s562sa33k78m63njfn1rw47dp9z0i-glibc-2.40-66
|
||||
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 nzg6zqsijbv7yc95wlfcdswx6bg69srq-gmp-with-cxx-6.3.0
|
||||
-r--r--r-- 1 65534 65534 145 Jan 1 1970 vylji4sibxm3mr3hync4zcmpmgbv09az-list-nix-store-runtime-closure
|
||||
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 xy4jjgw87sbgwylm5kn047d9gkbhsr9x-bash-5.2p37
|
||||
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 yypqcvqhnv8y4zpicgxdigp3giq81gzb-libunistring-1.3
|
||||
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 za53jjhjl1xajv3y1zpjvr9mh4w0c1ay-xgcc-14.2.1.20250322-libgcc
|
||||
```
|
||||
rather than your entire nix store.
|
||||
'';
|
||||
impl = include-once "bind-nix-store-runtime-closure" (
|
||||
defer (
|
||||
state:
|
||||
let
|
||||
runtimeClosure = pkgs.writeText "${state.name}-runtime-closure" ''
|
||||
${pkgs.lib.concatStringsSep "\n" state.additionalRuntimeClosures}
|
||||
${pkgs.lib.concatStringsSep "\n" state.path}
|
||||
${state.argv}
|
||||
${state.entry}
|
||||
'';
|
||||
bindArgs =
|
||||
pkgs.runCommand "${state.name}-runtime-closure-bind-args"
|
||||
{
|
||||
__structuredAttrs = true;
|
||||
exportReferencesGraph.runtime = runtimeClosure;
|
||||
nativeBuildInputs = [ pkgs.jq ];
|
||||
}
|
||||
''
|
||||
while read -r DEP; do
|
||||
printf '%s\0' --ro-bind "$DEP" "$DEP" >> $out
|
||||
done <<< "$(jq -r '.runtime | map(.path) | sort | .[]' "$NIX_ATTRS_JSON_FILE")"
|
||||
'';
|
||||
in
|
||||
compose [
|
||||
# Use `--args` and a file descriptor here in case the runtime
|
||||
# closure is huge, to avoid running into argv size limitations
|
||||
(add-runtime "exec {RUNTIME_CLOSURE_BIND_ARGS_FD}<${bindArgs}")
|
||||
(unsafe-add-raw-args "--args \"$RUNTIME_CLOSURE_BIND_ARGS_FD\"")
|
||||
] state
|
||||
)
|
||||
);
|
||||
}
|
||||
21
lib/combinators/bind-pkg.nix
Normal file
21
lib/combinators/bind-pkg.nix
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{ combinators, helpers, ... }:
|
||||
let
|
||||
inherit (combinators) compose ro-bind;
|
||||
in
|
||||
{
|
||||
sig = "String -> Package -> Permission";
|
||||
doc = ''
|
||||
Bind mounts the passed derivation at a specified location.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
bind-pkg "/foo" (pkgs.writeText "foo" "bar")
|
||||
```
|
||||
'';
|
||||
impl =
|
||||
path: pkg:
|
||||
compose [
|
||||
(ro-bind (toString pkg) path)
|
||||
(helpers.pushState "additionalRuntimeClosures" pkg)
|
||||
];
|
||||
}
|
||||
16
lib/combinators/camera.nix
Normal file
16
lib/combinators/camera.nix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) add-runtime include-once;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Allows access to webcams and other V4L2 video devices at `/dev/video*`.
|
||||
'';
|
||||
impl = include-once "camera" (add-runtime ''
|
||||
for v in /dev/video*; do
|
||||
[ -e "$v" ] || continue
|
||||
RUNTIME_ARGS+=(--dev-bind "$v" "$v")
|
||||
done
|
||||
'');
|
||||
}
|
||||
26
lib/combinators/compose.nix
Normal file
26
lib/combinators/compose.nix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{ lib, ... }:
|
||||
{
|
||||
sig = "[Permission] -> Permission";
|
||||
doc = ''
|
||||
Allows combinator composition.
|
||||
|
||||
`compose [ a b c ]` combines `a`, `b`, and `c`.
|
||||
|
||||
This is useful when writing your own combinators, for example when using
|
||||
`jail-nix.lib.extend`:
|
||||
|
||||
```nix
|
||||
jail-nix.lib.extend {
|
||||
inherit pkgs;
|
||||
additionalCombinators = combinators: with combinators; {
|
||||
mycombinator = compose [
|
||||
(readonly "/foo")
|
||||
(readonly "/bar")
|
||||
gpu
|
||||
];
|
||||
};
|
||||
}
|
||||
```
|
||||
'';
|
||||
impl = lib.flip lib.pipe;
|
||||
}
|
||||
125
lib/combinators/dbus.nix
Normal file
125
lib/combinators/dbus.nix
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
{
|
||||
combinators,
|
||||
helpers,
|
||||
jail,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-cleanup
|
||||
add-runtime
|
||||
compose
|
||||
defer
|
||||
include-once
|
||||
noescape
|
||||
readwrite
|
||||
set-env
|
||||
unsafe-dbus
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "{ own? :: [String], talk? :: [String], see? :: [String], call? :: [String], broadcast? :: [String] } -> Permission";
|
||||
doc = ''
|
||||
Grants access to dbus, using
|
||||
[`xdg-dbus-proxy`](https://github.com/flatpak/xdg-dbus-proxy) (inside a
|
||||
jail itself) to filter messages that can be sent/received.
|
||||
|
||||
All of the args in the passed attrset turn into arguments for
|
||||
`xdg-dbus-proxy`. They are all optional.
|
||||
|
||||
Example:
|
||||
```
|
||||
dbus {
|
||||
talk = [
|
||||
"ca.desrt.dconf"
|
||||
"org.a11y.Bus"
|
||||
"org.freedesktop.DBus"
|
||||
"org.freedesktop.portal.*"
|
||||
"org.gtk.vfs"
|
||||
"org.gtk.vfs.*"
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
Multiple calls to `dbus` will only run a single xdg-dbus-proxy with a
|
||||
union of all passed own/talk/see/call/broadcast permissions. This makes
|
||||
it possible to write wrapper combinators for specific dbus permissions
|
||||
and use them in conjunction with each other.
|
||||
'';
|
||||
impl =
|
||||
{
|
||||
own ? [ ],
|
||||
talk ? [ ],
|
||||
see ? [ ],
|
||||
call ? [ ],
|
||||
broadcast ? [ ],
|
||||
}:
|
||||
compose [
|
||||
# Just set the options on state so dbus can be called multiple times
|
||||
(helpers.pushState "dbusPermissions" {
|
||||
inherit
|
||||
own
|
||||
talk
|
||||
see
|
||||
call
|
||||
broadcast
|
||||
;
|
||||
})
|
||||
# Then add deferred runtime logic to spin up xdg-dbus-proxy:
|
||||
(include-once "dbus" (
|
||||
defer (
|
||||
state:
|
||||
let
|
||||
proxy = jail "xdg-dbus-proxy" pkgs.xdg-dbus-proxy [
|
||||
unsafe-dbus
|
||||
(readwrite (noescape "\"$PROXIED_DBUS_SOCKET_DIR\""))
|
||||
];
|
||||
|
||||
getFlags =
|
||||
type:
|
||||
lib.pipe state.dbusPermissions [
|
||||
(map (p: p.${type}))
|
||||
lib.flatten
|
||||
lib.unique
|
||||
(map (id: "--${type}=${lib.escapeShellArg id}"))
|
||||
];
|
||||
|
||||
args =
|
||||
[ "--filter" ]
|
||||
++ getFlags "own"
|
||||
++ getFlags "talk"
|
||||
++ getFlags "see"
|
||||
++ getFlags "call"
|
||||
++ getFlags "broadcast";
|
||||
in
|
||||
compose [
|
||||
(add-runtime ''
|
||||
PROXIED_DBUS_SOCKET_DIR=$(mktemp -d)
|
||||
export PROXIED_DBUS_SOCKET_DIR
|
||||
PROXIED_DBUS_SOCKET="$PROXIED_DBUS_SOCKET_DIR/socket"
|
||||
mkfifo "$PROXIED_DBUS_SOCKET_DIR/ready"
|
||||
exec {XDG_DBUS_PROXY_READY_FD}<>"$PROXIED_DBUS_SOCKET_DIR/ready"
|
||||
${lib.getExe proxy} \
|
||||
"$DBUS_SESSION_BUS_ADDRESS" \
|
||||
"$PROXIED_DBUS_SOCKET" \
|
||||
--fd="$XDG_DBUS_PROXY_READY_FD" \
|
||||
${lib.concatStringsSep " " args} \
|
||||
&
|
||||
PROXY_PID=$!
|
||||
IFS= read -rn1 -u "$XDG_DBUS_PROXY_READY_FD"
|
||||
'')
|
||||
(add-cleanup ''
|
||||
kill "$PROXY_PID"
|
||||
if [ -e "''${PROXIED_DBUS_SOCKET_DIR-}" ]; then
|
||||
rm -rf "$PROXIED_DBUS_SOCKET_DIR"
|
||||
fi
|
||||
'')
|
||||
(readwrite (noescape "\"$PROXIED_DBUS_SOCKET_DIR\""))
|
||||
(set-env "DBUS_SESSION_BUS_ADDRESS" (noescape "\"unix:path=$PROXIED_DBUS_SOCKET\""))
|
||||
] state
|
||||
)
|
||||
))
|
||||
];
|
||||
}
|
||||
38
lib/combinators/defer.nix
Normal file
38
lib/combinators/defer.nix
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
{ helpers, ... }:
|
||||
{
|
||||
sig = "Permission -> Permission";
|
||||
doc = ''
|
||||
Runs the passed permission after all other permissions.
|
||||
|
||||
A `Permission` is just a `State -> State` function. For many
|
||||
permissions order does not matter since they touch different parts of
|
||||
state, or they add bubblewrap args that don't affect each other.
|
||||
|
||||
However, some more complicated permissions may need to operate on the
|
||||
final state. By calling `defer`, this permission will be applied after
|
||||
all other base and per-jail permissions.
|
||||
|
||||
This is useful when writing your own combinators, specifically ones
|
||||
that operate on state modified by other permissions. This is especially
|
||||
useful if your combinator is called in `basePermissions` since those
|
||||
are applied before any of the per-jail permissions are.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
let
|
||||
print-hostname = state:
|
||||
jail.combinators.add-runtime "echo 'Hostname is: ''${state.hostname}'"
|
||||
state;
|
||||
in jail "test" pkgs.hello (combinators: with combinators; [
|
||||
# This will print 'Hostname is: jail':
|
||||
print-hostname
|
||||
|
||||
# This will print 'Hostname is: foo'
|
||||
(defer print-hostname)
|
||||
|
||||
(set-hostname "foo")
|
||||
])
|
||||
```
|
||||
'';
|
||||
impl = helpers.pushState "deferredPermissions";
|
||||
}
|
||||
25
lib/combinators/escape.nix
Normal file
25
lib/combinators/escape.nix
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{ helpers, ... }:
|
||||
{
|
||||
sig = "String -> String";
|
||||
doc = ''
|
||||
Shell escapes the passed string.
|
||||
|
||||
Use [noescape](#noescape) to prevent escaping.
|
||||
|
||||
`escape` and `noescape` don't return `Permission`s, but they are useful
|
||||
helpers to expose when defining jails and writing custom combinators, so
|
||||
they are exposed with the rest of the combinators for convenience.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
jail-nix.lib.extend {
|
||||
inherit pkgs;
|
||||
additionalCombinators = combinators: with combinators; {
|
||||
# a combinator that binds the passed path to /foo
|
||||
my-combinator = path: unsafe-add-raw-args "--bind ''${escape path} /foo"
|
||||
};
|
||||
}
|
||||
```
|
||||
'';
|
||||
impl = helpers.escape;
|
||||
}
|
||||
42
lib/combinators/fake-passwd.nix
Normal file
42
lib/combinators/fake-passwd.nix
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
{
|
||||
combinators,
|
||||
helpers,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
compose
|
||||
noescape
|
||||
ro-bind
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
includedInBasePermissions = true;
|
||||
doc = ''
|
||||
Generates and mounts fake `/etc/passwd` and `/etc/group` files in the jail.
|
||||
|
||||
The fake `/etc/passwd` and `/etc/group` files contains a root user, and
|
||||
forward the calling user's user id, username, group id and group name.
|
||||
|
||||
If you do not want to hide the users and groups that exist on your system,
|
||||
you may consider just bind mounting `/etc/passwd` and `/etc/group` inside
|
||||
the jail instead.
|
||||
'';
|
||||
impl = compose [
|
||||
(add-runtime ''
|
||||
if [ ! -e ${helpers.dataDirSubPath "passwd"} ] || [ ! -e ${helpers.dataDirSubPath "group"} ]; then
|
||||
NOLOGIN=${pkgs.shadow}/bin/nologin
|
||||
mkdir -p ${helpers.dataDir}
|
||||
echo "root:x:0:0:System administrator:/root:$NOLOGIN" > ${helpers.dataDirSubPath "passwd"}
|
||||
echo "$(id -un):x:$(id -u):$(id -g)::$HOME:$NOLOGIN" >> ${helpers.dataDirSubPath "passwd"}
|
||||
echo "root:x:0:" > ${helpers.dataDirSubPath "group"}
|
||||
echo "$(id -gn):x:$(id -g):" >> ${helpers.dataDirSubPath "group"}
|
||||
fi
|
||||
'')
|
||||
(ro-bind (noescape (helpers.dataDirSubPath "passwd")) "/etc/passwd")
|
||||
(ro-bind (noescape (helpers.dataDirSubPath "group")) "/etc/group")
|
||||
];
|
||||
}
|
||||
17
lib/combinators/fwd-env.nix
Normal file
17
lib/combinators/fwd-env.nix
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{ combinators, helpers, ... }:
|
||||
let
|
||||
inherit (combinators) set-env;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Forwards the specified environment variable to the underlying process.
|
||||
|
||||
If the env var is not set when the jailed application is run, it will
|
||||
exit non-zero.
|
||||
|
||||
If you want to be tolerant of the environment being unset, use
|
||||
[try-fwd-env](#try-fwd-env) instead.
|
||||
'';
|
||||
impl = name: set-env name (helpers.noescape "\"\$${name}\"");
|
||||
}
|
||||
23
lib/combinators/gpu.nix
Normal file
23
lib/combinators/gpu.nix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
include-once
|
||||
noescape
|
||||
readonly
|
||||
runtime-deep-ro-bind
|
||||
unsafe-add-raw-args
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes the gpu to jailed application.
|
||||
'';
|
||||
impl = include-once "gpu" (compose [
|
||||
(runtime-deep-ro-bind (noescape "/run/opengl-driver"))
|
||||
(runtime-deep-ro-bind (noescape "/run/opengl-driver-32"))
|
||||
(readonly (noescape "/sys"))
|
||||
(unsafe-add-raw-args "--dev-bind /dev/dri /dev/dri")
|
||||
]);
|
||||
}
|
||||
43
lib/combinators/gui.nix
Normal file
43
lib/combinators/gui.nix
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
compose
|
||||
fwd-env
|
||||
noescape
|
||||
pipewire
|
||||
pulse
|
||||
readonly
|
||||
readonly-paths-from-var
|
||||
runtime-deep-ro-bind
|
||||
try-fwd-env
|
||||
wayland
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes everything required to get graphical applications to work.
|
||||
|
||||
This composes [pulse](#pulse), [pipewire](#pipewire),
|
||||
[wayland](#wayland), and forwards/binds a few other paths to get fonts
|
||||
and cursor to render correctly.
|
||||
'';
|
||||
impl = compose [
|
||||
(add-runtime "mkdir -p ~/.config/dconf")
|
||||
pulse
|
||||
pipewire
|
||||
wayland
|
||||
(runtime-deep-ro-bind (noescape "/etc/fonts"))
|
||||
(readonly (noescape "~/.config/dconf"))
|
||||
(fwd-env "XDG_RUNTIME_DIR")
|
||||
(fwd-env "XDG_DATA_DIRS")
|
||||
(readonly-paths-from-var "XDG_DATA_DIRS" ":")
|
||||
|
||||
# Cursor
|
||||
(try-fwd-env "XCURSOR_THEME")
|
||||
(try-fwd-env "XCURSOR_PATH")
|
||||
(try-fwd-env "XCURSOR_SIZE")
|
||||
(readonly-paths-from-var "XCURSOR_PATH" " ")
|
||||
];
|
||||
}
|
||||
44
lib/combinators/include-once.nix
Normal file
44
lib/combinators/include-once.nix
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
{ helpers, lib, ... }:
|
||||
{
|
||||
sig = "String -> Permission -> Permission";
|
||||
doc = ''
|
||||
Only run the passed permission if include-once hasn't been previously
|
||||
called with the specified key.
|
||||
|
||||
This is useful when writing your own combinators.
|
||||
|
||||
```nix
|
||||
let
|
||||
jail = jail-nix.lib.extend {
|
||||
inherit pkgs;
|
||||
additionalCombinators = combinators: with combinators; {
|
||||
# foo isn't `include-once` so each call to it adds a new echo
|
||||
foo = add-runtime "echo foo";
|
||||
# bar will only be included once, no matter how many times it is called
|
||||
bar = include-once "bar" (add-runtime "echo bar");
|
||||
};
|
||||
};
|
||||
in
|
||||
# Prints:
|
||||
# foo
|
||||
# foo
|
||||
# foo
|
||||
# bar
|
||||
# Hello, world!
|
||||
jail "test" pkgs.hello (c: with c; [
|
||||
foo
|
||||
foo
|
||||
foo
|
||||
bar
|
||||
bar
|
||||
bar
|
||||
])
|
||||
```
|
||||
'';
|
||||
impl =
|
||||
key: combinator: state:
|
||||
if lib.elem key state.includedOnce then
|
||||
state
|
||||
else
|
||||
combinator (helpers.pushState "includedOnce" key state);
|
||||
}
|
||||
101
lib/combinators/jail-to-host-channel.nix
Normal file
101
lib/combinators/jail-to-host-channel.nix
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
{ combinators, pkgs, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-cleanup
|
||||
add-pkg-deps
|
||||
add-runtime
|
||||
compose
|
||||
include-once
|
||||
noescape
|
||||
ro-bind
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Combinator";
|
||||
doc = ''
|
||||
Allows programs in the jail to execute and pass messages to a
|
||||
specific handler that runs outside of the jail.
|
||||
|
||||
The first parameter specifies a name of a program that is exposed
|
||||
to the jail that, when called, sends its first argument to the
|
||||
script passed to the second parameter. The script runs *outside*
|
||||
of the jail. Any stdout generated by the script is relayed back
|
||||
as stdout from the program in the jail.
|
||||
|
||||
Current limitations:
|
||||
|
||||
* Only a single argument is supported. For more arguments you
|
||||
will need to use an encoding like JSON.
|
||||
* Only stdout is relayed back to the jail, stderr will be
|
||||
visible in your terminal but the jail won't be able to read
|
||||
it.
|
||||
* The first parameter must be a valid POSIX variable name.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
jail-to-host-channel "getHostFileSize" ${"''"}
|
||||
# This runs *outside* of the jail
|
||||
if [ -f "$1" ]; then
|
||||
wc -c < "$1"
|
||||
else
|
||||
echo "$1 is not a file on the host"
|
||||
fi
|
||||
${"''"}
|
||||
```
|
||||
|
||||
This exposes a program inside the jail called `getHostFileSize`
|
||||
that returns the size of the file passed in without needing to
|
||||
give the jail read access to any files.
|
||||
'';
|
||||
impl =
|
||||
jailTxName: hostRxHandler:
|
||||
assert pkgs.lib.isValidPosixName jailTxName;
|
||||
let
|
||||
varPrefix = "J2H_CHAN_${pkgs.lib.toUpper jailTxName}";
|
||||
in
|
||||
include-once "jail-to-host-channel-${jailTxName}" (compose [
|
||||
(add-runtime ''
|
||||
# Set up jail-to-host channel "${jailTxName}"
|
||||
${varPrefix}_TMP=$(mktemp -d)
|
||||
mkfifo "''$${varPrefix}_TMP/fifo"
|
||||
(
|
||||
while true; do
|
||||
MSG=$(<"''$${varPrefix}_TMP/fifo")
|
||||
${
|
||||
pkgs.lib.getExe (
|
||||
pkgs.writeShellApplication {
|
||||
name = "${jailTxName}-host-rx-handler";
|
||||
text = hostRxHandler;
|
||||
}
|
||||
)
|
||||
} "$MSG" > "''$${varPrefix}_TMP/fifo" || break
|
||||
done
|
||||
) &
|
||||
${varPrefix}_RX_PID=$!
|
||||
'')
|
||||
(add-cleanup ''
|
||||
kill "''$${varPrefix}_RX_PID"
|
||||
rm -rf "''$${varPrefix}_TMP"
|
||||
'')
|
||||
(ro-bind (noescape "\"\$${varPrefix}_TMP/fifo\"") "/run/j2hc-${jailTxName}")
|
||||
(add-pkg-deps [
|
||||
(pkgs.writeShellApplication {
|
||||
name = jailTxName;
|
||||
runtimeInputs = [ pkgs.util-linux ];
|
||||
text = ''
|
||||
flock /run/j2hc.lock ${
|
||||
pkgs.lib.getExe (
|
||||
pkgs.writeShellApplication {
|
||||
name = "${jailTxName}-locked";
|
||||
text = ''
|
||||
echo "$1" > /run/j2hc-${jailTxName}
|
||||
echo "$(< /run/j2hc-${jailTxName})"
|
||||
'';
|
||||
}
|
||||
)
|
||||
} "$1"
|
||||
'';
|
||||
})
|
||||
])
|
||||
]);
|
||||
}
|
||||
14
lib/combinators/mount-cwd.nix
Normal file
14
lib/combinators/mount-cwd.nix
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
include-once
|
||||
unsafe-add-raw-args
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Bind mounts the runtime working directory as read-write.
|
||||
'';
|
||||
impl = include-once "mount-cwd" (unsafe-add-raw-args "--bind \"$PWD\" \"$PWD\"");
|
||||
}
|
||||
37
lib/combinators/network.nix
Normal file
37
lib/combinators/network.nix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
escape
|
||||
include-once
|
||||
runtime-deep-ro-bind
|
||||
share-ns
|
||||
time-zone
|
||||
unsafe-add-raw-args
|
||||
write-text
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Grants network access to the jail.
|
||||
|
||||
This also exposes everything required to allow TLS connections.
|
||||
|
||||
You can set your desired hostname with [set-hostname](#set-hostname). The
|
||||
default is `jail`.
|
||||
'';
|
||||
impl = include-once "network" (
|
||||
state:
|
||||
compose [
|
||||
time-zone
|
||||
(share-ns "net")
|
||||
(runtime-deep-ro-bind "/etc/hosts")
|
||||
(runtime-deep-ro-bind "/etc/nsswitch.conf")
|
||||
(runtime-deep-ro-bind "/etc/resolv.conf")
|
||||
(runtime-deep-ro-bind "/etc/ssl")
|
||||
(write-text "/etc/hostname" "${state.hostname}\n")
|
||||
(unsafe-add-raw-args "--hostname ${escape state.hostname}")
|
||||
] state
|
||||
);
|
||||
}
|
||||
14
lib/combinators/no-new-session.nix
Normal file
14
lib/combinators/no-new-session.nix
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{ ... }:
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Disables `--new-session`
|
||||
|
||||
By default, jail-nix includes the `--new-session` bwrap flag. Doing this
|
||||
prevents a jailed application from being able to feed keyboard input to
|
||||
the terminal, however this may break some TUI applications.
|
||||
|
||||
See BWRAP(1) for more information and security implications.
|
||||
'';
|
||||
impl = state: state // { newSession = false; };
|
||||
}
|
||||
29
lib/combinators/noescape.nix
Normal file
29
lib/combinators/noescape.nix
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{ helpers, ... }:
|
||||
{
|
||||
sig = "String -> NoEscapedString";
|
||||
doc = ''
|
||||
Prevent the passed string from being automatically shell escaped.
|
||||
|
||||
`escape` and `noescape` don't return `Permission`s, but they are useful
|
||||
helpers to expose when defining jails and writing custom combinators, so
|
||||
they are exposed with the rest of the combinators for convenience.
|
||||
|
||||
It is the caller's responsibility to ensure anything passed to this is
|
||||
correctly escaped.
|
||||
|
||||
```nix
|
||||
# Probably doesn't do what you intended since "~/foo" is shell escaped:
|
||||
(readonly "~/foo")
|
||||
|
||||
# This properly makes $HOME/foo readonly in the jail:
|
||||
(readonly (noescape "~/foo"))
|
||||
|
||||
# Binds the path specified by the runtime $FOO variable as read only.
|
||||
#
|
||||
# Note that we must properly quote this to ensure bash correctly keeps it
|
||||
# as a single argument, even if it contains spaces:
|
||||
(readonly (noescape "\"$FOO\""))
|
||||
```
|
||||
'';
|
||||
impl = helpers.noescape;
|
||||
}
|
||||
23
lib/combinators/open-urls-in-browser.nix
Normal file
23
lib/combinators/open-urls-in-browser.nix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
jail-to-host-channel
|
||||
set-env
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Allows access to open URLs in `$BROWSER`.
|
||||
|
||||
This works by creating a pipe that is mounted into the jail that forwards
|
||||
all URLs to the `$BROWSER` outside of the jail. This way the jailed
|
||||
program can launch your browser, even if it has a subset of the
|
||||
permissions your browser has.
|
||||
'';
|
||||
impl = compose [
|
||||
(jail-to-host-channel "browserchannel" ''"$BROWSER" "$1"'')
|
||||
(set-env "BROWSER" "browserchannel")
|
||||
];
|
||||
}
|
||||
27
lib/combinators/persist-home.nix
Normal file
27
lib/combinators/persist-home.nix
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{ combinators, helpers, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
compose
|
||||
noescape
|
||||
rw-bind
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Persists the home directory across all jails with the specified name.
|
||||
|
||||
This is useful for a lot of software that may want to write arbitrary
|
||||
things into your home directory and expect to read them back in a future
|
||||
invocation.
|
||||
|
||||
The home directory is persisted in `~/.local/share/jail.nix/home/<name>`.
|
||||
'';
|
||||
impl =
|
||||
name:
|
||||
compose [
|
||||
(add-runtime "mkdir -p ${helpers.dataDirSubPath "home/${name}"}")
|
||||
(rw-bind (noescape (helpers.dataDirSubPath "home/${name}")) (noescape "~"))
|
||||
];
|
||||
}
|
||||
20
lib/combinators/pipewire.nix
Normal file
20
lib/combinators/pipewire.nix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
fwd-env
|
||||
include-once
|
||||
unsafe-add-raw-args
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes pipewire to the jailed application.
|
||||
'';
|
||||
impl = include-once "pipewire" (compose [
|
||||
(fwd-env "XDG_RUNTIME_DIR")
|
||||
(unsafe-add-raw-args "--bind-try \"$XDG_RUNTIME_DIR/pipewire-0\" \"$XDG_RUNTIME_DIR/pipewire-0\"")
|
||||
(unsafe-add-raw-args "--bind-try /run/pipewire /run/pipewire")
|
||||
]);
|
||||
}
|
||||
22
lib/combinators/pulse.nix
Normal file
22
lib/combinators/pulse.nix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
fwd-env
|
||||
include-once
|
||||
try-fwd-env
|
||||
unsafe-add-raw-args
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes pulseaudio to the jailed application.
|
||||
'';
|
||||
impl = include-once "pulse" (compose [
|
||||
(fwd-env "XDG_RUNTIME_DIR")
|
||||
(try-fwd-env "PULSE_SERVER")
|
||||
(unsafe-add-raw-args "--bind-try /run/pulse /run/pulse")
|
||||
(unsafe-add-raw-args "--bind-try \"$XDG_RUNTIME_DIR/pulse\" \"$XDG_RUNTIME_DIR/pulse\"")
|
||||
]);
|
||||
}
|
||||
41
lib/combinators/readonly-paths-from-var.nix
Normal file
41
lib/combinators/readonly-paths-from-var.nix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
{ combinators, lib, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
include-once
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
This binds multiple paths as read-only specified by a single runtime
|
||||
environment variable.
|
||||
|
||||
The first argument to this combinator is the runtime environment variable
|
||||
that contains a list of paths to be bound. The second argument is a
|
||||
deliminator to split the paths (Typically either `" "` or `":"`).
|
||||
|
||||
This is useful for variables like `XDG_DATA_DIRS`, `GTK_PATH`,
|
||||
`XCURSOR_PATH`, etc.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
compose [
|
||||
(readonly-paths-from-var "XDG_DATA_DIRS" ":")
|
||||
(readonly-paths-from-var "XCURSOR_PATH" " ")
|
||||
]
|
||||
```
|
||||
'';
|
||||
impl =
|
||||
var: separator:
|
||||
assert lib.isValidPosixName var;
|
||||
include-once "readonly-paths-from-var-${var}" (add-runtime ''
|
||||
IFS=${lib.escapeShellArg separator} read -ra DIRS <<< "''${${var}-}"
|
||||
for DIR in "''${DIRS[@]}"; do
|
||||
if [ -e "$DIR" ]; then
|
||||
P="$(realpath "$DIR")"
|
||||
RUNTIME_ARGS+=(--ro-bind "$P" "$P")
|
||||
fi
|
||||
done
|
||||
'');
|
||||
}
|
||||
22
lib/combinators/readonly-runtime-args.nix
Normal file
22
lib/combinators/readonly-runtime-args.nix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
include-once
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Binds any valid paths passed in as arguments to the jailed program at
|
||||
runtime as read-only.
|
||||
'';
|
||||
impl = include-once "readonly-runtime-args" (add-runtime ''
|
||||
for MAYBE_PATH in "$@"; do
|
||||
if [ -e "$MAYBE_PATH" ]; then
|
||||
P="$(realpath "$MAYBE_PATH")"
|
||||
RUNTIME_ARGS+=(--ro-bind "$P" "$P")
|
||||
fi
|
||||
done
|
||||
'');
|
||||
}
|
||||
14
lib/combinators/readonly.nix
Normal file
14
lib/combinators/readonly.nix
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) ro-bind;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path in the jail as read-only.
|
||||
|
||||
This will error if the file does not exist. If you want to be tolerant of
|
||||
missing files, see [try-readonly](#try-readonly).
|
||||
'';
|
||||
impl = path: ro-bind path path;
|
||||
}
|
||||
22
lib/combinators/readwrite-runtime-args.nix
Normal file
22
lib/combinators/readwrite-runtime-args.nix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
add-runtime
|
||||
include-once
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Binds any valid paths passed in as arguments to the jailed program at
|
||||
runtime as read-write.
|
||||
'';
|
||||
impl = include-once "readwrite-runtime-args" (add-runtime ''
|
||||
for MAYBE_PATH in "$@"; do
|
||||
if [ -e "$MAYBE_PATH" ]; then
|
||||
P="$(realpath "$MAYBE_PATH")"
|
||||
RUNTIME_ARGS+=(--bind "$P" "$P")
|
||||
fi
|
||||
done
|
||||
'');
|
||||
}
|
||||
14
lib/combinators/readwrite.nix
Normal file
14
lib/combinators/readwrite.nix
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) rw-bind;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path in the jail as read-write.
|
||||
|
||||
This will error if the file does not exist. If you want to be tolerant of
|
||||
missing files, see [try-readwrite](#try-readwrite).
|
||||
'';
|
||||
impl = path: rw-bind path path;
|
||||
}
|
||||
20
lib/combinators/ro-bind.nix
Normal file
20
lib/combinators/ro-bind.nix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) escape unsafe-add-raw-args;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path on the host to a path in the jail as read-only.
|
||||
|
||||
This will error if the host file does not exist. If you want to be
|
||||
tolerant of missing files, see [try-ro-bind](#try-ro-bind).
|
||||
|
||||
Example:
|
||||
```nix
|
||||
# Binds /foo on the host to /bar in the jail
|
||||
ro-bind "/foo" "/bar"
|
||||
```
|
||||
'';
|
||||
impl = from: to: unsafe-add-raw-args "--ro-bind ${escape from} ${escape to}";
|
||||
}
|
||||
70
lib/combinators/runtime-deep-ro-bind.nix
Normal file
70
lib/combinators/runtime-deep-ro-bind.nix
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
{
|
||||
combinators,
|
||||
helpers,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (combinators) add-runtime compose include-once;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
This is an internal (for now) combinator to bind a path at runtime in a
|
||||
way.
|
||||
|
||||
This is marked as internal which means it isn't publicly documented on
|
||||
the website since this will likely change in the future. Feel free to
|
||||
use, but a future update may break this combinator.
|
||||
'';
|
||||
internal = true;
|
||||
impl =
|
||||
let
|
||||
bindRuntimeHelperFunction = include-once "bindRuntimeHelperFunction" (add-runtime ''
|
||||
function bindNixStoreClosure {
|
||||
local NIX_STORE_PATH
|
||||
NIX_STORE_PATH="$1"
|
||||
while read -r P; do
|
||||
RUNTIME_ARGS+=(--ro-bind "$P" "$P")
|
||||
done <<< "$(${pkgs.nix}/bin/nix-store --query --requisites "$NIX_STORE_PATH")"
|
||||
}
|
||||
|
||||
function bindRuntime {
|
||||
local MAX_DEPTH
|
||||
local PATH_TO_BIND
|
||||
MAX_DEPTH="$1"
|
||||
PATH_TO_BIND="$2"
|
||||
if ((MAX_DEPTH <= 0)); then
|
||||
echo "jail.nix: bindRuntime hit max depth" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! [ -e "$PATH_TO_BIND" ]; then
|
||||
return
|
||||
fi
|
||||
case $(stat -c '%F' "$PATH_TO_BIND") in
|
||||
'regular file'|socket)
|
||||
RUNTIME_ARGS+=(--ro-bind "$PATH_TO_BIND" "$PATH_TO_BIND")
|
||||
;;
|
||||
directory)
|
||||
for DIR_ENTRY in "$PATH_TO_BIND"/*; do
|
||||
[ -e "$DIR_ENTRY" ] || continue
|
||||
bindRuntime "$((MAX_DEPTH - 1))" "$DIR_ENTRY"
|
||||
done
|
||||
;;
|
||||
'symbolic link')
|
||||
LINK_PATH=$(realpath "$PATH_TO_BIND")
|
||||
RUNTIME_ARGS+=(--symlink "$LINK_PATH" "$PATH_TO_BIND")
|
||||
if [[ "$LINK_PATH" == /nix/store/* ]]; then
|
||||
bindNixStoreClosure "$LINK_PATH"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
'');
|
||||
in
|
||||
path:
|
||||
compose [
|
||||
bindRuntimeHelperFunction
|
||||
(add-runtime "bindRuntime 5 ${helpers.escape path}")
|
||||
];
|
||||
}
|
||||
20
lib/combinators/rw-bind.nix
Normal file
20
lib/combinators/rw-bind.nix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) escape unsafe-add-raw-args;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path on the host to a path in the jail as read-write.
|
||||
|
||||
This will error if the host file does not exist. If you want to be
|
||||
tolerant of missing files, see [try-rw-bind](#try-rw-bind).
|
||||
|
||||
Example:
|
||||
```nix
|
||||
# Binds /foo on the host to /bar in the jail
|
||||
rw-bind "/foo" "/bar"
|
||||
```
|
||||
'';
|
||||
impl = from: to: unsafe-add-raw-args "--bind ${escape from} ${escape to}";
|
||||
}
|
||||
13
lib/combinators/set-argv.nix
Normal file
13
lib/combinators/set-argv.nix
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
{ helpers, ... }:
|
||||
{
|
||||
sig = "[String] -> Permission";
|
||||
doc = ''
|
||||
Overrides the current argv that is passed to the jailed executable.
|
||||
|
||||
By default argv is set to `noescape "$@"` which will forward whatever
|
||||
arguments are provided to the wrapper script at runtime. Calling this
|
||||
will override the current value.
|
||||
'';
|
||||
impl =
|
||||
argv: state: state // { argv = builtins.concatStringsSep " " (builtins.map helpers.escape argv); };
|
||||
}
|
||||
17
lib/combinators/set-env.nix
Normal file
17
lib/combinators/set-env.nix
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{ helpers, ... }:
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
Sets the specified environment variable in the jail.
|
||||
|
||||
This will throw if the variable name is not a valid posix variable name.
|
||||
'';
|
||||
impl =
|
||||
name: value: state:
|
||||
state
|
||||
// {
|
||||
env = state.env // {
|
||||
${name} = helpers.escape value;
|
||||
};
|
||||
};
|
||||
}
|
||||
18
lib/combinators/set-hostname.nix
Normal file
18
lib/combinators/set-hostname.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{ ... }:
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Sets the hostname to use for the `network` combinator.
|
||||
|
||||
Must be specified before `network`.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
[
|
||||
(set-hostname "foo")
|
||||
network
|
||||
]
|
||||
```
|
||||
'';
|
||||
impl = hostname: state: state // { inherit hostname; };
|
||||
}
|
||||
21
lib/combinators/share-ns.nix
Normal file
21
lib/combinators/share-ns.nix
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{ ... }:
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Removes the call to `--unshare-` for the provided namespace.
|
||||
|
||||
By default, jail-nix unshares all namespaces, calling `share-ns "pid"`
|
||||
will remove the `--unshare-pid` flag from bwrap which will allow this
|
||||
process to share the same pid namespace as the host.
|
||||
|
||||
See BWRAP(1) for more information.
|
||||
'';
|
||||
impl =
|
||||
namespace: state:
|
||||
state
|
||||
// {
|
||||
namespaces = state.namespaces // {
|
||||
${namespace} = true;
|
||||
};
|
||||
};
|
||||
}
|
||||
18
lib/combinators/time-zone.nix
Normal file
18
lib/combinators/time-zone.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
include-once
|
||||
unsafe-add-raw-args
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes your timezone.
|
||||
'';
|
||||
impl = include-once "time-zone" (compose [
|
||||
(unsafe-add-raw-args "--ro-bind \"$(realpath /etc/localtime)\" \"$(readlink /etc/localtime)\"")
|
||||
(unsafe-add-raw-args "--symlink \"$(readlink /etc/localtime)\" /etc/localtime")
|
||||
]);
|
||||
}
|
||||
11
lib/combinators/tmpfs.nix
Normal file
11
lib/combinators/tmpfs.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{ combinators, helpers, ... }:
|
||||
let
|
||||
inherit (combinators) unsafe-add-raw-args;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Mounts a new tmpfs at the specified location.
|
||||
'';
|
||||
impl = path: unsafe-add-raw-args "--tmpfs ${helpers.escape path}";
|
||||
}
|
||||
11
lib/combinators/try-fwd-env.nix
Normal file
11
lib/combinators/try-fwd-env.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{ combinators, helpers, ... }:
|
||||
let
|
||||
inherit (combinators) set-env;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Forwards the specified environment variable to the underlying process (if set).
|
||||
'';
|
||||
impl = name: set-env name (helpers.noescape "\"\${${name}-}\"");
|
||||
}
|
||||
11
lib/combinators/try-readonly.nix
Normal file
11
lib/combinators/try-readonly.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) try-ro-bind;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path in the jail as read-only if it exists.
|
||||
'';
|
||||
impl = path: try-ro-bind path path;
|
||||
}
|
||||
11
lib/combinators/try-readwrite.nix
Normal file
11
lib/combinators/try-readwrite.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) try-rw-bind;
|
||||
in
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path in the jail as read-write if it exists.
|
||||
'';
|
||||
impl = path: try-rw-bind path path;
|
||||
}
|
||||
18
lib/combinators/try-ro-bind.nix
Normal file
18
lib/combinators/try-ro-bind.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) escape unsafe-add-raw-args;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path on the host to a path in the jail as read-only
|
||||
if it exists.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
# Binds /foo on the host to /bar in the jail if /foo exists
|
||||
try-ro-bind "/foo" "/bar"
|
||||
```
|
||||
'';
|
||||
impl = from: to: unsafe-add-raw-args "--ro-bind-try ${escape from} ${escape to}";
|
||||
}
|
||||
18
lib/combinators/try-rw-bind.nix
Normal file
18
lib/combinators/try-rw-bind.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators) escape unsafe-add-raw-args;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
Binds the specified path on the host to a path in the jail as read-write
|
||||
if it exists.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
# Binds /foo on the host to /bar in the jail if /foo exists
|
||||
try-rw-bind "/foo" "/bar"
|
||||
```
|
||||
'';
|
||||
impl = from: to: unsafe-add-raw-args "--bind-try ${escape from} ${escape to}";
|
||||
}
|
||||
11
lib/combinators/unsafe-add-raw-args.nix
Normal file
11
lib/combinators/unsafe-add-raw-args.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{ ... }:
|
||||
{
|
||||
sig = "String -> Permission";
|
||||
doc = ''
|
||||
Adds the raw string passed into it into the call to bubblewrap.
|
||||
|
||||
Nothing is escaped, it is the caller's responsibility to ensure
|
||||
everything is properly escaped.
|
||||
'';
|
||||
impl = args: state: state // { cmd = "${state.cmd} ${args}"; };
|
||||
}
|
||||
23
lib/combinators/unsafe-dbus.nix
Normal file
23
lib/combinators/unsafe-dbus.nix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
noescape
|
||||
readonly
|
||||
set-env
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes D-Bus to the jailed program.
|
||||
|
||||
This does no message filtering so it is marked as unsafe. If you want
|
||||
more control over the messages that can be sent/received, consider using
|
||||
the [dbus](#dbus) combinator instead.
|
||||
'';
|
||||
impl = compose [
|
||||
(readonly (noescape "\"$XDG_RUNTIME_DIR/bus\""))
|
||||
(set-env "DBUS_SESSION_BUS_ADDRESS" (noescape "\"$DBUS_SESSION_BUS_ADDRESS\""))
|
||||
];
|
||||
}
|
||||
24
lib/combinators/unsafe-x11.nix
Normal file
24
lib/combinators/unsafe-x11.nix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
fwd-env
|
||||
readwrite
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes X11 to the jailed application.
|
||||
|
||||
Note that applications may be able to break out of the jail because X11
|
||||
is not designed to be a security boundary.
|
||||
|
||||
For a safer alternative, consider using the [xwayland](#xwayland)
|
||||
combinator inside of a wayland compositor.
|
||||
'';
|
||||
impl = compose [
|
||||
(fwd-env "DISPLAY")
|
||||
(readwrite "/tmp/.X11-unix")
|
||||
];
|
||||
}
|
||||
21
lib/combinators/wayland.nix
Normal file
21
lib/combinators/wayland.nix
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{ combinators, ... }:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
fwd-env
|
||||
noescape
|
||||
readonly
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Exposes your wayland compositor to the jail.
|
||||
'';
|
||||
impl = compose [
|
||||
(fwd-env "WAYLAND_DISPLAY")
|
||||
(fwd-env "XDG_RUNTIME_DIR")
|
||||
(fwd-env "XDG_SESSION_TYPE")
|
||||
(readonly (noescape "\"$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY\""))
|
||||
];
|
||||
}
|
||||
32
lib/combinators/wrap-entry.nix
Normal file
32
lib/combinators/wrap-entry.nix
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{ lib, pkgs, ... }:
|
||||
{
|
||||
sig = "(String -> String) -> Permission";
|
||||
doc = ''
|
||||
Wraps the binary to be jailed in a bash script that will be the new
|
||||
entrypoint to the jail.
|
||||
|
||||
This similar in spirit to the [add-runtime combinator](#add-runtime),
|
||||
except that this runs *inside* the jail, while `add-runtime` runs before
|
||||
the jail starts.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
wrap-entry (entry: ${"''"}
|
||||
echo 'Inside the jail!'
|
||||
''${entry}
|
||||
echo 'Cleaning up...'
|
||||
${"''"})
|
||||
```
|
||||
'';
|
||||
impl =
|
||||
getWrapper: state:
|
||||
state
|
||||
// {
|
||||
entry = lib.getExe (
|
||||
pkgs.writeShellApplication {
|
||||
name = "${state.name}-jail-wrapper";
|
||||
text = getWrapper state.entry;
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
26
lib/combinators/write-text.nix
Normal file
26
lib/combinators/write-text.nix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
combinators,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (combinators) bind-pkg escape;
|
||||
in
|
||||
{
|
||||
sig = "String -> String -> Permission";
|
||||
doc = ''
|
||||
Bind mounts a read-only text file at a path.
|
||||
|
||||
Example:
|
||||
```nix
|
||||
# This will create a text file in the jail at `/hello.txt`
|
||||
write-text "/hello.txt" "Hello, world!"
|
||||
```
|
||||
'';
|
||||
impl =
|
||||
path: contents:
|
||||
bind-pkg path (
|
||||
pkgs.writeText "jail-write-text-${lib.strings.sanitizeDerivationName (escape path)}" contents
|
||||
);
|
||||
}
|
||||
41
lib/combinators/xwayland.nix
Normal file
41
lib/combinators/xwayland.nix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
combinators,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (combinators)
|
||||
compose
|
||||
include-once
|
||||
set-env
|
||||
wayland
|
||||
wrap-entry
|
||||
;
|
||||
in
|
||||
{
|
||||
sig = "Permission";
|
||||
doc = ''
|
||||
Safely allow X11 apps to render to a wayland compositor.
|
||||
|
||||
This combinator runs
|
||||
[xwayland-satellite](https://github.com/Supreeeme/xwayland-satellite)
|
||||
*inside the jail* and only exposes [wayland combinator](#wayland).
|
||||
|
||||
This has the advantage of not allowing multiple jailed X11 applicaitons
|
||||
to see each other since each jailed applicaiton gets its own
|
||||
xwayland-satelite server.
|
||||
|
||||
However, doing it this way does mean that every jailed applicaiton you
|
||||
run with this combinator will spin up its own personal xwayland-satelite
|
||||
server, which will consume more resources than having a global one.
|
||||
'';
|
||||
impl = include-once "xwayland" (compose [
|
||||
wayland
|
||||
(set-env "DISPLAY" ":42")
|
||||
(wrap-entry (entry: ''
|
||||
${lib.getExe pkgs.xwayland-satellite} :42 &
|
||||
${entry}
|
||||
''))
|
||||
]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue