Make combinators available at jail.combinators

Instead of requiring the third argument to be a function that passes in
combinators, this exposes all combinators on `jail.combinators` and
allows the third argument to just be a list (or even null!). This may be
more ergonomic in some contexts (like nixpkgs overlays).

This isn't a breaking change, the third argument can still be a function
and will receive the combinators if it is.
This commit is contained in:
Alex David 2025-07-11 11:44:12 -07:00
commit c5ba72dc5a
4 changed files with 110 additions and 98 deletions

179
lib.nix
View file

@ -1,93 +1,110 @@
{ pkgs, name, exe, applyCombinators }: let
{ pkgs
, additionalCombinators ? _: {}
}: let
inherit (pkgs) lib;
helpers = rec {
dataDir = "~/.local/share/jail.nix";
dataDirSubPath = subPath: "${dataDir}/${combinators.escape subPath}";
dataDirSubPath = subPath: "${dataDir}/${builtinCombinators.escape subPath}";
};
combinators = import ./combinators.nix { inherit pkgs lib helpers; };
builtinCombinators = import ./combinators.nix { inherit pkgs lib helpers; };
initial-state = {
name = name;
cmd = "${lib.getExe pkgs.bubblewrap}";
entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe;
path = "${pkgs.coreutils}/bin";
argv = "\"$@\"";
runtime = "";
new-session = true;
hostname = "jail";
env = {};
namespaces = {};
included-once = []; # See include-once combinator
cleanup = []; # See cleanup combinator
};
in lib.pipe initial-state (
# apply pre-user 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")
(readonly "/nix/store")
(readonly "/bin/sh")
(fwd-env "LANG")
(fwd-env "HOME")
(fwd-env "TERM")
allCombinators = builtinCombinators // additionalCombinators builtinCombinators;
(add-runtime ''
if [ ! -e ${helpers.dataDirSubPath "passwd"} ] || [ ! -e ${helpers.dataDirSubPath "group"} ]; then
mkdir -p ${helpers.dataDir}
echo "root:x:0:0:System administrator:/root:$(which nologin)" > ${helpers.dataDirSubPath "passwd"}
echo "$(id -un):x:$(id -u):$(id -g)::$HOME:$(which 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")
])
in {
combinators = allCombinators;
# apply user combinators
++ applyCombinators combinators
__functor = _: name: exe: combinatorsToApply: let
initialState = {
name = name;
cmd = "${lib.getExe pkgs.bubblewrap}";
entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe;
path = "${pkgs.coreutils}/bin";
argv = "\"$@\"";
runtime = "";
new-session = true;
hostname = "jail";
env = {};
namespaces = {};
included-once = []; # See include-once combinator
cleanup = []; # See cleanup combinator
};
# apply post-user combinators
++ (with combinators; [
(s:
# See `--unshare-*` in BWRAP(1)
lib.pipe ["user" "ipc" "pid" "net" "uts" "cgroup"] [
(lib.filter (ns: !(s.namespaces.${ns} or false)))
(map (ns: "--unshare-${ns}"))
(builtins.concatStringsSep " ")
unsafe-add-raw-args
] s
)
(s: set-env "PATH" s.path s)
(s: if s.new-session then unsafe-add-raw-args "--new-session" s else s)
(s: lib.foldr (envVar:
assert pkgs.lib.isValidPosixName envVar;
unsafe-add-raw-args "--setenv ${envVar} ${s.env.${envVar}}"
) s (builtins.attrNames s.env))
])
userCombinators =
let t = builtins.typeOf combinatorsToApply; in
if t == "lambda" then combinatorsToApply allCombinators else
if t == "list" then combinatorsToApply else
if t == "null" then []
else throw "Unknown combinator type ${t}. Must be a function, list, or null";
# build jailed app from state
++ [
(state: ''
RUNTIME_ARGS=()
${if builtins.length state.cleanup > 0 then ''
function cleanup {
${lib.concatStringsSep "\n" state.cleanup}
}
trap cleanup EXIT
'' else ""}
${state.runtime}
${if builtins.length state.cleanup > 0 then "" else "exec "}${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
'')
(text: pkgs.writeShellApplication { inherit name text; })
in lib.pipe initialState (
# apply pre-user combinators
(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")
(readonly "/nix/store")
(readonly "/bin/sh")
(fwd-env "LANG")
(fwd-env "HOME")
(fwd-env "TERM")
# forward man pages
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
]
)
(add-runtime ''
if [ ! -e ${helpers.dataDirSubPath "passwd"} ] || [ ! -e ${helpers.dataDirSubPath "group"} ]; then
mkdir -p ${helpers.dataDir}
echo "root:x:0:0:System administrator:/root:$(which nologin)" > ${helpers.dataDirSubPath "passwd"}
echo "$(id -un):x:$(id -u):$(id -g)::$HOME:$(which 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")
])
# apply user combinators
++ userCombinators
# apply post-user combinators
++ (with builtinCombinators; [
(s:
# See `--unshare-*` in BWRAP(1)
lib.pipe ["user" "ipc" "pid" "net" "uts" "cgroup"] [
(lib.filter (ns: !(s.namespaces.${ns} or false)))
(map (ns: "--unshare-${ns}"))
(builtins.concatStringsSep " ")
unsafe-add-raw-args
] s
)
(s: set-env "PATH" s.path s)
(s: if s.new-session then unsafe-add-raw-args "--new-session" s else s)
(s: lib.foldr (envVar:
assert pkgs.lib.isValidPosixName envVar;
unsafe-add-raw-args "--setenv ${envVar} ${s.env.${envVar}}"
) s (builtins.attrNames s.env))
])
# build jailed app from state
++ [
(state: ''
RUNTIME_ARGS=()
${if builtins.length state.cleanup > 0 then ''
function cleanup {
${lib.concatStringsSep "\n" state.cleanup}
}
trap cleanup EXIT
'' else ""}
${state.runtime}
${if builtins.length state.cleanup > 0 then "" else "exec "}${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
'')
(text: pkgs.writeShellApplication { inherit name text; })
# forward man pages
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
]
);
}