Skip to content

Commit

Permalink
Evaluate nix-shell -i args relative to script
Browse files Browse the repository at this point in the history
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 NixOS#4232
  • Loading branch information
matthewbauer authored and tomberek committed Nov 27, 2023
1 parent f018048 commit 0402336
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/nix-build/nix-build.cc
Expand Up @@ -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 {
Expand All @@ -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)))));
}
}

Expand Down

0 comments on commit 0402336

Please sign in to comment.