From 42b355c38ca63dab4904acc5c0d95f17954a8c9b Mon Sep 17 00:00:00 2001 From: Alex David Date: Thu, 26 Feb 2026 12:32:34 -0800 Subject: [PATCH] Fix `fwd-env "PATH"` not working Jail.nix does not expose `$PATH` by default because the automatic runtime-closure binding often means your `$PATH` won't work anyway. However, for some jails, you may want to disable the runtime-closure binding and just bind your whole nix store. In those cases, it would be nice if `fwd-env "PATH"` worked correctly. --- lib/combinators/add-path.nix | 9 +++++++- lib/combinators/add-pkg-deps.nix | 15 +++++++++++- .../bind-nix-store-runtime-closure.nix | 1 - lib/jail.nix | 2 -- tests/spec/CombinatorsSpec.hs | 23 +++++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/combinators/add-path.nix b/lib/combinators/add-path.nix index c0d211d..65a31f2 100644 --- a/lib/combinators/add-path.nix +++ b/lib/combinators/add-path.nix @@ -4,5 +4,12 @@ doc = '' Prepends the passed string to `$PATH`. ''; - impl = path: state: state // { path = [ path ] ++ state.path; }; + impl = + path: state: + state + // { + env = state.env // { + PATH = if state.env ? PATH && state.env.PATH != "" then "${path}:${state.env.PATH}" else path; + }; + }; } diff --git a/lib/combinators/add-pkg-deps.nix b/lib/combinators/add-pkg-deps.nix index 644872c..4fd6875 100644 --- a/lib/combinators/add-pkg-deps.nix +++ b/lib/combinators/add-pkg-deps.nix @@ -7,5 +7,18 @@ in doc = '' Adds the packages' `bin` directory to `$PATH`. ''; - impl = pkgs: compose (builtins.map (pkg: add-path "${lib.getBin pkg}/bin") pkgs); + impl = + pkgsToAdd: + compose ( + builtins.map (pkg: add-path "${lib.getBin pkg}/bin") pkgsToAdd + ++ [ + ( + state: + state + // { + additionalRuntimeClosures = state.additionalRuntimeClosures ++ (map toString pkgsToAdd); + } + ) + ] + ); } diff --git a/lib/combinators/bind-nix-store-runtime-closure.nix b/lib/combinators/bind-nix-store-runtime-closure.nix index 121b39e..33f488a 100644 --- a/lib/combinators/bind-nix-store-runtime-closure.nix +++ b/lib/combinators/bind-nix-store-runtime-closure.nix @@ -50,7 +50,6 @@ in let runtimeClosure = pkgs.writeText "${state.name}-runtime-closure" '' ${pkgs.lib.concatStringsSep "\n" state.additionalRuntimeClosures} - ${pkgs.lib.concatStringsSep "\n" state.path} ${state.argv} ${state.entry} ''; diff --git a/lib/jail.nix b/lib/jail.nix index 1ff43a2..84edc45 100644 --- a/lib/jail.nix +++ b/lib/jail.nix @@ -32,7 +32,6 @@ let name = name; cmd = lib.getExe bubblewrapPackage; entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe; - path = [ ]; argv = "\"$@\""; runtime = ""; newSession = true; @@ -74,7 +73,6 @@ let ] s ) - (s: if builtins.length s.path > 0 then set-env "PATH" (lib.concatStringsSep ":" s.path) s else s) (s: if s.newSession then unsafe-add-raw-args "--new-session" s else s) (s: if s.dieWithParent then unsafe-add-raw-args "--die-with-parent" s else s) ( diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 6428cd3..830f796 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -69,6 +69,29 @@ spec = parallel $ inTestM $ do |] `shouldOutput` "secret\n" + it "allows forwarding $PATH and extending it with add-pkg-deps" $ do + helloPath <- evalNixExpr [i| "${pkgs.hello}/bin" |] + coreutilsPath <- evalNixExpr [i| "${pkgs.coreutils}/bin" |] + withEnv "PATH" "foo:bar" $ do + [i| + jail "test" (sh ''echo "''${PATH-unset}"'') (c: [ + (c.fwd-env "PATH") + (c.add-pkg-deps [ pkgs.hello ]) + ]) + |] + `shouldOutput` (helloPath <> ":" <> coreutilsPath <> ":foo:bar\n") + + it "overrides $PATH to whatever is forwarded if specified after add-pkg-deps" $ do + coreutilsPath <- evalNixExpr [i| "${pkgs.coreutils}/bin" |] + withEnv "PATH" "foo:bar" $ do + [i| + jail "test" (sh ''echo "''${PATH-unset}"'') (c: [ + (c.add-pkg-deps [ pkgs.hello ]) + (c.fwd-env "PATH") + ]) + |] + `shouldOutput` (coreutilsPath <> ":foo:bar\n") + it "correctly escapes variables" $ do withEnv "MY_ENV_VAR" "$(echo foo)" $ do [i|