Search code examples
nixshebangnix-shell

Nix shell #! to refer to to shell.nix in a parent directory


I start almost all my scripts lately with

#!/usr/bin/env nix-shell
#! nix-shell -i bash
set -e

But this requires that the default.nix/shell.nix be in the same directory as the script which is not always the case. Is there a way to tell nix-shell to look at all parent directories for a *.nix file?


Solution

  • The items after #! nix-shell are basically regular nix-shell arguments, so you can add the file name like this.

    #!/usr/bin/env nix-shell
    #! nix-shell -i bash ../shell.nix
    set -e
    
    

    Path resolution happens relative to the script file. When you use a directory path, it will look for default.nix, not shell.nix inside the directory.