From b0a8a229830722bbefcd244270b29315a0823fe0 Mon Sep 17 00:00:00 2001 From: Alex David Date: Sat, 4 Oct 2025 15:10:08 -0700 Subject: [PATCH] Pull remaining default perms into base combinator `base-permissions.nix` was the source of truth for all of the base combinators, but it was not documented well anywhere. To make this easier and more obvious for users, this commit pulls all remaining custom permissions into a new `base` combinator which is included by default. This way there is no more undocumented permissions included by default, they are all documented in the `Default included combinators` section of the docs. This should also make it easier to override base permissions because users can now just specify `base` instead of all of the `unsafe-add-raw-args` calls unless they really want to change them. --- lib/base-permissions.nix | 12 +---- lib/combinators.nix | 1 + lib/combinators/base.nix | 50 +++++++++++++++++++ website/static-docs/advanced-configuration.md | 30 ++++++----- 4 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 lib/combinators/base.nix diff --git a/lib/base-permissions.nix b/lib/base-permissions.nix index b09cc9a..5d957e6 100644 --- a/lib/base-permissions.nix +++ b/lib/base-permissions.nix @@ -1,15 +1,5 @@ pkgs: combinators: with combinators; [ - (unsafe-add-raw-args "--proc /proc") - (unsafe-add-raw-args "--dev /dev") - (unsafe-add-raw-args "--tmpfs /tmp") - (unsafe-add-raw-args "--tmpfs ~") - (unsafe-add-raw-args "--clearenv") - (unsafe-add-raw-args "--die-with-parent") - (add-pkg-deps [ pkgs.coreutils ]) - (ro-bind "${pkgs.bash}/bin/sh" "/bin/sh") - (fwd-env "LANG") - (fwd-env "HOME") - (fwd-env "TERM") + base bind-nix-store-runtime-closure fake-passwd ] diff --git a/lib/combinators.nix b/lib/combinators.nix index a7d9b54..2553e7b 100644 --- a/lib/combinators.nix +++ b/lib/combinators.nix @@ -22,6 +22,7 @@ let add-path = ./combinators/add-path.nix; add-pkg-deps = ./combinators/add-pkg-deps.nix; add-runtime = ./combinators/add-runtime.nix; + base = ./combinators/base.nix; bind-nix-store-runtime-closure = ./combinators/bind-nix-store-runtime-closure.nix; bind-pkg = ./combinators/bind-pkg.nix; camera = ./combinators/camera.nix; diff --git a/lib/combinators/base.nix b/lib/combinators/base.nix new file mode 100644 index 0000000..83e552e --- /dev/null +++ b/lib/combinators/base.nix @@ -0,0 +1,50 @@ +{ combinators, pkgs, ... }: +let + inherit (combinators) + add-pkg-deps + ro-bind + compose + fwd-env + unsafe-add-raw-args + ; +in +{ + sig = "Permission"; + includedInBasePermissions = true; + doc = '' + This sets up a basic minimal set of permissions that a lot of software will + need. + + This combinator: + + * Sets up a fake proc at `/proc` (Using `--proc /proc` bwrap args) + * Sets up a fake dev at `/dev` (Using `--dev /dev` bwrap args) + * Sets up a tmpfs at `/tmp` (Using `--tmpfs /tmp` bwrap args) + * Sets up a tmpfs at your home (Using `--tmpfs ~` bwrap args) + * Ensures all processes in the jail are killed when the jail exits + (Using `--die-with-parent` bwrap arg) + * Adds coreutils to the package deps (`add-pkg-deps [ pkgs.coreutils ]`) + * Binds `/bin/sh` from `pkgs.bash` + (`ro-bind "''${pkgs.bash}/bin/sh" "/bin/sh"`) + * Clears the environment except `LANG`, `HOME` and `TERM` + + This is included in the base permissions by default so you shouldn't need + to include it unless you override base permissions. It is exposed as a + combinator (like the other default included combinators) so that you can + use it in a custom + [`basePermissions`](advanced-configuration.md#basepermissions). + ''; + impl = compose [ + (unsafe-add-raw-args "--proc /proc") + (unsafe-add-raw-args "--dev /dev") + (unsafe-add-raw-args "--tmpfs /tmp") + (unsafe-add-raw-args "--tmpfs ~") + (unsafe-add-raw-args "--die-with-parent") + (add-pkg-deps [ pkgs.coreutils ]) + (ro-bind "${pkgs.bash}/bin/sh" "/bin/sh") + (unsafe-add-raw-args "--clearenv") + (fwd-env "LANG") + (fwd-env "HOME") + (fwd-env "TERM") + ]; +} diff --git a/website/static-docs/advanced-configuration.md b/website/static-docs/advanced-configuration.md index 3dcaa7f..e11c92d 100644 --- a/website/static-docs/advanced-configuration.md +++ b/website/static-docs/advanced-configuration.md @@ -46,22 +46,28 @@ with enough permissions to have most software behave correctly. If you override this, you may want to take a look at the [default included combinators](combinators.md#default-included-combinators). -Example: +For example, here is the default set of base permissions: ```nix jail = jail-nix.lib.extend { inherit pkgs; - basePermissions = builtinCombinators: with builtinCombinators; [ - (unsafe-add-raw-args "--proc /proc") - (unsafe-add-raw-args "--dev /dev") - (unsafe-add-raw-args "--tmpfs /tmp") - (unsafe-add-raw-args "--tmpfs ~") - (unsafe-add-raw-args "--clearenv") - (unsafe-add-raw-args "--die-with-parent") + basePermissions = combinators: with combinators; [ + base + bind-nix-store-runtime-closure + fake-passwd + ]; +}; +``` + +However if you wanted to bind in your entire nix store rather than using the +bind-nix-store-runtime-closure combinator you could do that like so: + +```nix +jail = jail-nix.lib.extend { + inherit pkgs; + basePermissions = combinators: with combinators; [ + base (readonly "/nix/store") - (readonly "/bin/sh") - (fwd-env "LANG") - (fwd-env "HOME") - (fwd-env "TERM") + fake-passwd ]; }; ```