Allow sharing namespaces other than net

This commit is contained in:
Alex David 2025-05-17 11:19:23 -07:00
commit 3c0a3b807b
2 changed files with 13 additions and 2 deletions

View file

@ -13,6 +13,7 @@ in rec {
add-pkg-deps = pkgs: compose (builtins.map (pkg: add-path "${pkg}/bin") pkgs);
no-new-session = state: state // { new-session = false; };
set-env = name: value: state: state // { env = state.env // { ${name} = escape value; }; };
share-ns = namespace: state: state // { namespaces = state.namespaces // { ${namespace} = true; }; };
set-hostname = hostname: state: state // { inherit hostname; };
@ -62,6 +63,7 @@ in rec {
];
network = state: compose [
time-zone
(share-ns "net")
(readonly "/etc/hosts")
(readonly "/etc/nsswitch.conf")
(readonly "/etc/resolv.conf")
@ -69,7 +71,7 @@ in rec {
(readonly "/etc/static/nsswitch.conf")
(readonly "/etc/static/ssl")
(write-text "/etc/hostname" "${state.hostname}\n")
(unsafe-add-raw-args "--share-net --hostname ${escape state.hostname}")
(unsafe-add-raw-args "--hostname ${escape state.hostname}")
] state;
dbus-unsafe = compose [
(readonly (noescape "\"$XDG_RUNTIME_DIR/bus\""))

11
lib.nix
View file

@ -23,6 +23,7 @@
new-session = true;
hostname = "jail";
env = {};
namespaces = {};
};
in lib.pipe initial-state (
# apply pre-user combinators
@ -33,7 +34,6 @@ in lib.pipe initial-state (
(unsafe-add-raw-args "--tmpfs ~")
(unsafe-add-raw-args "--clearenv")
(unsafe-add-raw-args "--die-with-parent")
(unsafe-add-raw-args "--unshare-all")
(readonly "/nix/store")
(readonly "/bin/sh")
(fwd-env "LANG")
@ -58,6 +58,15 @@ in lib.pipe initial-state (
# apply post-user combinators
++ (with combinators; [
(s:
# See `--unshare-*` from man bwrap
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: