Fix issue 11892

It seems that I copied the expression for baseDir thoughtlessly and
did not come back to it.

- `baseDir` was only used in the `fromArgs` branch.
- `fromArgs` is true when `packages` is true.

(cherry picked from commit c4b95dbdd1)
This commit is contained in:
Robert Hensing 2024-11-18 16:45:18 +01:00 committed by Mergify
parent 8e8a572d37
commit 403f388fdb
3 changed files with 40 additions and 5 deletions

View File

@ -340,13 +340,15 @@ static void main_nix_build(int argc, char * * argv)
exprs = {state->parseStdin()}; exprs = {state->parseStdin()};
else else
for (auto i : remainingArgs) { for (auto i : remainingArgs) {
auto baseDir = inShebang && !packages ? absPath(dirOf(script)) : i; if (fromArgs) {
auto shebangBaseDir = absPath(dirOf(script));
if (fromArgs)
exprs.push_back(state->parseExprFromString( exprs.push_back(state->parseExprFromString(
std::move(i), std::move(i),
(inShebang && compatibilitySettings.nixShellShebangArgumentsRelativeToScript) ? lookupFileArg(*state, baseDir) : state->rootPath(".") (inShebang && compatibilitySettings.nixShellShebangArgumentsRelativeToScript)
? lookupFileArg(*state, shebangBaseDir)
: state->rootPath(".")
)); ));
}
else { else {
auto absolute = i; auto absolute = i;
try { try {

View File

@ -167,6 +167,35 @@ EOF
chmod a+x $TEST_ROOT/marco/polo/default.nix chmod a+x $TEST_ROOT/marco/polo/default.nix
(cd $TEST_ROOT/marco && ./polo/default.nix | grepQuiet "Polo") (cd $TEST_ROOT/marco && ./polo/default.nix | grepQuiet "Polo")
# https://github.com/NixOS/nix/issues/11892
mkdir $TEST_ROOT/issue-11892
cat >$TEST_ROOT/issue-11892/shebangscript <<EOF
#!$(type -P env) nix-shell
#! nix-shell -I nixpkgs=$shellDotNix
#! nix-shell -p 'callPackage (import ./my_package.nix) {}'
#! nix-shell -i bash
set -euxo pipefail
my_package
EOF
cat >$TEST_ROOT/issue-11892/my_package.nix <<EOF
{ stdenv, shell, ... }:
stdenv.mkDerivation {
name = "my_package";
buildCommand = ''
mkdir -p \$out/bin
( echo "#!\${shell}"
echo "echo 'ok' 'baz11892'"
) > \$out/bin/my_package
cat \$out/bin/my_package
chmod a+x \$out/bin/my_package
'';
}
EOF
chmod a+x $TEST_ROOT/issue-11892/shebangscript
$TEST_ROOT/issue-11892/shebangscript \
| tee /dev/stderr \
| grepQuiet "ok baz11892"
##################### #####################
# Flake equivalents # # Flake equivalents #

View File

@ -37,7 +37,7 @@ let pkgs = rec {
mkdir -p $out mkdir -p $out
ln -s ${setupSh} $out/setup ln -s ${setupSh} $out/setup
''; '';
}; } // { inherit mkDerivation; };
shellDrv = mkDerivation { shellDrv = mkDerivation {
name = "shellDrv"; name = "shellDrv";
@ -94,5 +94,9 @@ let pkgs = rec {
chmod a+rx $out/bin/ruby chmod a+rx $out/bin/ruby
''; '';
inherit (cfg) shell;
callPackage = f: args: f (pkgs // args);
inherit pkgs; inherit pkgs;
}; in pkgs }; in pkgs