Compare commits

...
Author SHA1 Message Date
Tom Bereknyei 1169b75e34 tests: add required file 2023-11-27 14:56:51 -05:00
Tom Bereknyei ba56f8b953 notes: document change in nix-shell behavior 2023-11-27 14:55:09 -05:00
Tom Bereknyei 9a4641146f tests: ensure nix-shell uses relative paths for expressions 2023-11-27 14:40:38 -05:00
Matthew Bauer 04023360ed 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
2023-11-27 14:40:38 -05:00
5 changed files with 33 additions and 2 deletions

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
synopsis: ensure nix-shell shebang uses relative path
prs: #5088
description: {
`nix-shell` shebangs use relative paths for files, but expressions were still evaluated using the current working directory. The new behavior is that evalutations are performed relative to the script.
}
+8
View File
@@ -0,0 +1,8 @@
# Release X.Y (202?-??-??)
- Fixed a bug where `nix-env --query` ignored `--drv-path` when `--json` was set.
- Introduced the store [`mounted-ssh-ng://`](@docroot@/command-ref/new-cli/nix3-help-stores.md).
This store allows full access to a Nix store on a remote machine and additionally requires that the store be mounted in the local filesystem.
- [`nix-shell`](../command-ref/nix-shell.md) shebangs now interpret "-E" with relative paths
+4 -2
View File
@@ -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)))));
}
}
+10
View File
@@ -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
+4
View File
@@ -0,0 +1,4 @@
#! @ENV_PROG@ nix-shell
#! nix-shell -E "with (import ./shell.nix {}); runCommand \"hi\" {buildInputs = [ruby];} \"\""
#! nix-shell --pure -i ruby
puts ARGV[1]