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.
This commit is contained in:
Alex David 2026-02-26 12:32:34 -08:00
commit 42b355c38c
5 changed files with 45 additions and 5 deletions

View file

@ -4,5 +4,12 @@
doc = '' doc = ''
Prepends the passed string to `$PATH`. 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;
};
};
} }

View file

@ -7,5 +7,18 @@ in
doc = '' doc = ''
Adds the packages' `bin` directory to `$PATH`. 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);
}
)
]
);
} }

View file

@ -50,7 +50,6 @@ in
let let
runtimeClosure = pkgs.writeText "${state.name}-runtime-closure" '' runtimeClosure = pkgs.writeText "${state.name}-runtime-closure" ''
${pkgs.lib.concatStringsSep "\n" state.additionalRuntimeClosures} ${pkgs.lib.concatStringsSep "\n" state.additionalRuntimeClosures}
${pkgs.lib.concatStringsSep "\n" state.path}
${state.argv} ${state.argv}
${state.entry} ${state.entry}
''; '';

View file

@ -32,7 +32,6 @@ let
name = name; name = name;
cmd = lib.getExe bubblewrapPackage; cmd = lib.getExe bubblewrapPackage;
entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe; entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe;
path = [ ];
argv = "\"$@\""; argv = "\"$@\"";
runtime = ""; runtime = "";
newSession = true; newSession = true;
@ -74,7 +73,6 @@ let
] ]
s 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.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) (s: if s.dieWithParent then unsafe-add-raw-args "--die-with-parent" s else s)
( (

View file

@ -69,6 +69,29 @@ spec = parallel $ inTestM $ do
|] |]
`shouldOutput` "secret\n" `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 it "correctly escapes variables" $ do
withEnv "MY_ENV_VAR" "$(echo foo)" $ do withEnv "MY_ENV_VAR" "$(echo foo)" $ do
[i| [i|