jail.nix/lib.nix

87 lines
2.8 KiB
Nix
Raw Normal View History

2025-04-27 12:07:03 -07:00
{ pkgs, name, exe, getOpts }: let
inherit (pkgs) lib;
helpers = rec {
dataDir = "~/.local/share/jail.nix";
dataDirSubPath = subPath: "${dataDir}/${combinators.escape subPath}";
2025-05-11 14:51:48 -07:00
};
combinators = lib.mapAttrs
(_: v: v.impl combinators)
(import ./combinators.nix { inherit pkgs lib helpers; });
2025-04-27 12:07:03 -07:00
2025-05-11 15:21:02 -07:00
initial-state = {
2025-06-07 15:19:45 -07:00
name = name;
2025-04-27 12:07:03 -07:00
cmd = "${lib.getExe pkgs.bubblewrap}";
2025-06-07 15:19:45 -07:00
entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe;
2025-04-27 12:07:03 -07:00
path = "${pkgs.coreutils}/bin";
argv = "\"$@\"";
runtime = "";
new-session = true;
hostname = "jail";
env = {};
namespaces = {};
2025-05-11 15:21:02 -07:00
};
in lib.pipe initial-state (
# apply pre-user combinators
(with combinators; [
2025-04-27 12:07:03 -07:00
(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")
(add-runtime ''
2025-06-07 15:14:29 -07:00
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")
2025-05-11 15:21:02 -07:00
])
2025-04-27 12:07:03 -07:00
2025-05-11 15:21:02 -07:00
# apply user combinators
++ getOpts combinators
# 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
)
2025-04-27 12:07:03 -07:00
(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
2025-05-11 15:21:02 -07:00
++ [
(state: ''
RUNTIME_ARGS=()
${state.runtime}
2025-06-07 15:19:45 -07:00
exec ${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
2025-05-11 15:21:02 -07:00
'')
(text: pkgs.writeShellApplication { inherit name text; })
2025-04-27 12:07:03 -07:00
2025-05-11 15:21:02 -07:00
# forward man pages
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
]
)