As I’ve only started using nix / nixos relatively recently, I usually use flakes and new-style nix
commands. I swear I’ve used the nix-shell shebang trick before, but for some reason can’t get it to work today – keep getting an error about it expecting default.nix
and not finding it.
After reading https://nixos.wiki/wiki/Nix-shell_shebang
$ nix-shell -p nix-info --run "nix-info -m"
- system: `"x86_64-linux"`
- host os: `Linux 5.15.85, NixOS, 22.11 (Raccoon), 22.11.20221224.dac57a4`
- multi-user?: `yes`
- sandbox: `yes`
- version: `nix-env (Nix) 2.11.1`
- channels(root): `"nixos-22.11"`
- channels(n8henrie): `"nixpkgs-22.11"`
- nixpkgs: `/home/n8henrie/.nix-defexpr/channels/nixpkgs`
$ cat <<'EOF' > foo.sh
#! /usr/bin/env nix-shell
#! nix-shell -i bash
printf "hello world\n"
EOF
$ chmod +x ./foo.sh
$ ./foo.sh
error: getting status of '/tmp/tmp.bIWspg0FPp/default.nix': No such file or directory
$ $PWD/foo.sh
error: getting status of '/tmp/tmp.bIWspg0FPp/default.nix': No such file or directory
$ bash foo.sh
hello world
Seeing same behavior on MacOS (aarch64) and NixOS (x86_64).
This thread is a little hard to follow but looks related: . / c u r s e d . n i x - #7 by deliciouslytyped, which is why I tried the absolute path (with $PWD
).
Why am I not able to run ./foo.sh
here?