nix-super/doc/manual/rl-next/shebang-relative.md
2024-07-11 13:52:03 +02:00

1.9 KiB

synopsis prs issues
`nix-shell` shebang uses relative path
5088
11058
4232

Relative path literals in nix-shell shebang scripts' options are now resolved relative to the script's location. Previously they were resolved relative to the current working directory.

For example, consider the following script in ~/myproject/say-hi:

#!/usr/bin/env nix-shell
#!nix-shell --expr 'import ./shell.nix'
#!nix-shell --arg toolset './greeting-tools.nix'
#!nix-shell -i bash
hello

Older versions of nix-shell would resolve shell.nix relative to the current working directory; home in this example:

[hostname:~]$ ./myproject/say-hi
error:
       … while calling the 'import' builtin
         at «string»:1:2:
            1| (import ./shell.nix)
             |  ^

       error: path '/home/user/shell.nix' does not exist

Since this release, nix-shell resolves shell.nix relative to the script's location, and ~/myproject/shell.nix is used.

$ ./myproject/say-hi
Hello, world!

Opt-out

This is technically a breaking change, so we have added an option so you can adapt independently of your Nix update. The old behavior can be opted into by setting the option nix-shell-shebang-arguments-relative-to-script to false. This option will be removed in a future release.

nix command shebang

The experimental nix command shebang already behaves in this script-relative manner.

Example:

#!/usr/bin/env nix
#!nix develop
#!nix --expr ``import ./shell.nix``
#!nix -c bash
hello