From 3c0a3b807b0ee8f71adf7ee9d061d58fd64deddf Mon Sep 17 00:00:00 2001 From: Alex David Date: Sat, 17 May 2025 11:19:23 -0700 Subject: [PATCH] Allow sharing namespaces other than net --- combinators.nix | 4 +++- lib.nix | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/combinators.nix b/combinators.nix index 3dfbe4d..dcf794c 100644 --- a/combinators.nix +++ b/combinators.nix @@ -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\"")) diff --git a/lib.nix b/lib.nix index 315501f..f5fe1d1 100644 --- a/lib.nix +++ b/lib.nix @@ -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: