From 04023360edbeb3477286783b3101a5b53f78021b Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Tue, 3 Aug 2021 15:25:15 -0500 Subject: [PATCH 1/3] Evaluate nix-shell -i args relative to script When writing a shebang script, you expect your path to be relative to the script, not the cwd. We previously handled this correctly for relative file paths, but not for expressions. This handles both -p & -E args. My understanding is this should be what we want in any cases I can think of - people run scripts from many different working directories. @edolstra is there any reason to handle -p args differently in this case? Fixes #4232 --- src/nix-build/nix-build.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 75ce12a8c3e..4120ca3cf82 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -298,7 +298,9 @@ static void main_nix_build(int argc, char * * argv) else for (auto i : left) { if (fromArgs) - exprs.push_back(state->parseExprFromString(std::move(i), state->rootPath(CanonPath::fromCwd()))); + exprs.push_back(state->parseExprFromString( + std::move(i), + state->rootPath(CanonPath::fromCwd(inShebang ? dirOf(script) : ".")))); else { auto absolute = i; try { @@ -311,7 +313,7 @@ static void main_nix_build(int argc, char * * argv) /* If we're in a #! script, interpret filenames relative to the script. */ exprs.push_back(state->parseExprFromFile(resolveExprPath(state->checkSourcePath(lookupFileArg(*state, - inShebang && !packages ? absPath(i, absPath(dirOf(script))) : i))))); + inShebang ? absPath(i, absPath(dirOf(script))) : i))))); } } From 9a4641146f79d631eb0825c7313dd335715de0d6 Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Sat, 25 Nov 2023 19:06:45 -0500 Subject: [PATCH 2/3] tests: ensure nix-shell uses relative paths for expressions --- tests/functional/nix-shell.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/functional/nix-shell.sh b/tests/functional/nix-shell.sh index 13403fadb82..702d3a6b566 100644 --- a/tests/functional/nix-shell.sh +++ b/tests/functional/nix-shell.sh @@ -59,6 +59,16 @@ chmod a+rx $TEST_ROOT/shell.shebang.sh output=$($TEST_ROOT/shell.shebang.sh abc def) [ "$output" = "foo bar abc def" ] +# Test nix-shell shebang mode with an alternate working directory +sed -e "s|@ENV_PROG@|$(type -P env)|" shell.shebang.expr > $TEST_ROOT/shell.shebang.expr +chmod a+rx $TEST_ROOT/shell.shebang.expr +# Should fail due to expressions using relative path +! $TEST_ROOT/shell.shebang.expr bar +cp shell.nix config.nix $TEST_ROOT +# Should succeed +output=$($TEST_ROOT/shell.shebang.expr bar) +[ "$output" = '-e load(ARGV.shift) -- '"$TEST_ROOT"'/shell.shebang.expr bar' ] + # Test nix-shell shebang mode again with metacharacters in the filename. # First word of filename is chosen to not match any file in the test root. sed -e "s|@ENV_PROG@|$(type -P env)|" shell.shebang.sh > $TEST_ROOT/spaced\ \\\'\"shell.shebang.sh From f66f498bd43efaa6883f12ca5988a282eef09697 Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Sat, 25 Nov 2023 19:07:29 -0500 Subject: [PATCH 3/3] notes: document change in nix-shell behavior --- doc/manual/rl-next/shebang-relative.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 doc/manual/rl-next/shebang-relative.md diff --git a/doc/manual/rl-next/shebang-relative.md b/doc/manual/rl-next/shebang-relative.md new file mode 100644 index 00000000000..dbda0db4c88 --- /dev/null +++ b/doc/manual/rl-next/shebang-relative.md @@ -0,0 +1,8 @@ +synopsis: ensure nix-shell shebang uses relative path +prs: #5088 +description: { + +`nix-shell` shebangs use the script file's relative location to resolve relative paths to files passed as command line arguments, but expression arguments were still evaluated using the current working directory as a base path. +The new behavior is that evalutations are performed relative to the script. + +}