Shebang and NIX_PATH

I’m using the form NIX_PATH=nixpkgs=http://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz <nix command> lately (see Anti pattern: nix-channel command · Issue #16 · NixOS/nix.dev · GitHub)

Now when it comes to shebang usage there seems to be an edge:

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p hello
print "hello"
#!nix-shell -p python2 -i python2
print "world"

Leaves me

error: undefined variable ‘print’ at /home/573/shebang-nix-xp/nix-shell-node.sh:3:1

Whereas

#!/usr/bin/env NIX_PATH=nixpkgs=http://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz nix-shell
#!nix-shell -i bash -p hello
print "hello"
#!nix-shell -p python2 -i python2
print "world"

Just times out.

$ cat ~/hmm
Thu Apr 15 2021 19:22:06 --> 
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p hello
print "hello"
#!nix-shell -p python2 -i python2
print "world"
Thu Apr 15 2021 19:22:06 (9.860ms)

$ ~/hmm
Thu Apr 15 2021 19:20:15 --> 
warning: warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
hello
world
Thu Apr 15 2021 19:20:17 (2.4s)

$ cat ~/hmm2
Thu Apr 15 2021 19:22:18 --> 
#!/usr/bin/env NIX_PATH=nixpkgs=http://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz nix-shell
#!nix-shell -i bash -p hello
print "hello"
#!nix-shell -p python2 -i python2
print "world"
Thu Apr 15 2021 19:22:18 (10.40ms)

$ ~/hmm2
Thu Apr 15 2021 19:22:21 --> 
these 68 paths will be fetched (165.37 MiB download, 821.08 MiB unpacked):
  /nix/store/0s0wn8si3vj2cb69j4r7p417p3f4mn3y-gdbm-1.19
  /nix/store/1979wn81f2b2k50wsjma06zmz3ym24pk-compiler-rt-7.1.0
  /nix/store/1hc9m668n3n2crmr27xf6b348hvadb0h-xz-5.2.5-bin
  /nix/store/1x2jf0pq7iwycm431mh5jyw25gbnry4d-python-2.7.18
  /nix/store/23d5j06wg1mjr22mqs8dzrv0fangn4gh-libc++abi-7.1.0
  /nix/store/2hpm6bgri7xm3dwazvv7cd524alsclh6-ICU-66108
  /nix/store/2ka3b20v2z6hlkkx9mgfs4bc5j7l5fhb-ncurses-6.2-dev
  /nix/store/2pb1r2zgw6yiabv4v3d4j66d3vwgdd5q-clang-wrapper-7.1.0
  /nix/store/2vn9prydyzs09rpidmfczcz412psi14r-gzip-1.10
  /nix/store/2vv81cb5s6ghmacprin1v69nc9i6jdah-adv_cmds-119-locale
  /nix/store/2wlc7dca8yq1f1vd263vc962i94vr7ds-bzip2-1.0.6.0.2
  /nix/store/36vx88jh1p0g22d79026q1xs5mx4h1jg-clang-7.1.0
  /nix/store/38svcfl8irq5h4nmn0dmwb1xjj18f6kz-zlib-1.2.11-dev
  /nix/store/3k78brl74f3dlfk2w01gfrg72qk2z8kz-patch-2.7.6
  /nix/store/3sd24513q39dwwfmjcscibbsg6vqn8d7-nghttp2-1.41.0-lib
  /nix/store/3wfmp788xq5l85fhd1w2mqdk63v88rir-expand-response-params
  /nix/store/60lpzrs6snb7y86cdnrpkkp5js9832h5-pcre-8.44
  /nix/store/63c9xywhyd6y64qdqq64154baxdjra7d-bash-interactive-5.1-p4-doc
  /nix/store/67cniwzcc39ivi7mvg1z4hn157vzggdq-ed-1.17
  /nix/store/6g10kmv1lslnj2y2j2zg44kadyq29fi4-xz-5.2.5
  /nix/store/7kvzy5y0rsyrjvpabxqn5xqwhdpa4z15-openssl-1.1.1k
  /nix/store/7w17rr43cpj30aqm34hf6w56gmdgh7l8-brotli-1.0.9-lib
  /nix/store/89m6xcj0lacy82x6hljw7akd5j2nn15b-gawk-5.1.0
  /nix/store/8r9q8q3fwz5sfx6r91dxqkrkxf1xdylj-bzip2-1.0.6.0.2-bin
  /nix/store/97klhm3jmfrgqj0y9zizg6sz63jzghii-libiconv-50
  /nix/store/9avmapy5hckam47jvk29cpncpzh03nlr-bash-interactive-5.1-p4-info
  /nix/store/9zzhfjqr42n72104kkzp7shk8167n40f-libtapi-1100.0.11
  /nix/store/afxy30qf656pifidw60m92cjxc4x4kcr-llvm-7.1.0-lib
  /nix/store/aq29m68q91p19hbhjjs4kgkmrkm94vvh-ncurses-6.2-man
  /nix/store/asdxdp81zc6b04glgh4mjsi6zk84slbd-libxml2-2.9.10
  /nix/store/c3mf5lw7w572a78mxkjpxfp00ga8l74n-cctools-port-949.0.1
  /nix/store/cgq2s1g9rfp667silvf2yhf6z1bvvh9k-gnutar-1.34
  /nix/store/ci3fv2b5iwhfjvchhxykwdj3j0xf2a8i-bash-interactive-5.1-p4
  /nix/store/ckq4ayswv8sx4wkynvw96kfsv8bmlhqa-libffi-3.3
  /nix/store/csmak3l2a1nvg0fvfmdryarzw603qym1-gnumake-4.3
  /nix/store/cxajvhkqavq928qnwq9i2rrkkp4khnls-curl-7.74.0
  /nix/store/dwa5nj68hzds2hrr56nrdxldqqf3aarv-Security-55471.14.18
  /nix/store/f1yjw78ph4wlbfmmzxvpz1csmcv3pirg-sqlite-3.35.2
  /nix/store/fi4rdjbzyql2cdqjnqlfzxlq3rv9nz6g-bash-interactive-5.1-p4-dev
  /nix/store/flb3vjwx123c35bg9jqp8wzqpl1bfviw-llvm-7.1.0
  /nix/store/gyds873ndvy0jgb6513ipfigmz0f41ah-gnused-4.8
  /nix/store/gz0jj1sd5ca3xzn23wbds7ly1yinp14p-Libsystem-1238.60.2
  /nix/store/iyn1xm2s77zkkkz2m6hlak0xnhpjwa06-findutils-4.7.0
  /nix/store/jqgwdsz7dv3s2ssf64n6fvv6fz2khfgh-cctools-binutils-darwin-wrapper-949.0.1
  /nix/store/k9zyw5957mj5grp4kqw80c1cisk44sd7-stdenv-darwin
  /nix/store/kavf7an3ahc0x3kr9ikvfi6iflx1fs46-binutils-2.35.1
  /nix/store/kky2fwi8vdff1cgrzxa3njhgva9qbs77-gnugrep-3.6
  /nix/store/kmhmlpdw3q47c5nf0pwy6rbyx1544v81-clang-7.1.0-lib
  /nix/store/kqnbhv4cii17dankky0hl1vk4hmdxd1c-diffutils-3.7
  /nix/store/ksj2qdgizcx07yx3g7wsbkwq7mr4r4rm-gettext-0.21
  /nix/store/lwm9srcffhwrv8p4gdg59pg14iklq1fm-gettext-0.21
  /nix/store/lybawc3ak35rzm14i0wkhq2fpq6gfmmw-coreutils-8.32
  /nix/store/lyn5yccl16ay86gjhcp45psgqw9z4kz0-swift-corefoundation
  /nix/store/m55xy2fhs4i1d675bs5xgrycw464rff7-gmp-6.2.1
  /nix/store/msx7spf3f67b6rnpwir882455j4raa8k-ncurses-6.2
  /nix/store/p5hamql6dcy2hh1jd568z6skf9kzv40h-compiler-rt-7.1.0-dev
  /nix/store/pfzrja93kp8dy269bjil62g5lqc04c2y-cctools-binutils-darwin-949.0.1
  /nix/store/ra8yvijdfjcs5f66b99gdjn86gparrbz-bash-4.4-p23
  /nix/store/rziilbcgxdyav69p4bjqqaij1wc3j4c9-zlib-1.2.11
  /nix/store/srj51l9wl32r1b67m136lwblhb1g6msl-libssh2-1.9.0
  /nix/store/ss9cbfshzkmw6vbq75pf6p35l3xldnb4-readline-8.0p4
  /nix/store/v0qc7d5713af0nndgibcn4kvyvbbzxq0-hello-2.10
  /nix/store/vhvr94a8s5k9hhhlz3x9m21fz16g9pwa-libkrb5-1.18
  /nix/store/vi2h0cvq2ic5b48y0cs0wq074zgis95j-bash-interactive-5.1-p4-man
  /nix/store/w1r72lzjl1h5j0rhsxk8czjk8r4q1r7r-readline-6.3p08
  /nix/store/wmdkisbdf4ih0yla1kzaj769ri372vrw-openssl-1.1.1k
  /nix/store/xaaa71l0hxk4x8dggl251gi612rb5qsn-libc++-7.1.0
  /nix/store/xjlix2qgqq0g3l1zbjbwjlg649bv4kvc-configd-453.19
copying path '/nix/store/ra8yvijdfjcs5f66b99gdjn86gparrbz-bash-4.4-p23' from 'https://cache.nixos.org'...
copying path '/nix/store/63c9xywhyd6y64qdqq64154baxdjra7d-bash-interactive-5.1-p4-doc' from 'https://cache.nixos.org'...
copying path '/nix/store/9avmapy5hckam47jvk29cpncpzh03nlr-bash-interactive-5.1-p4-info' from 'https://cache.nixos.org'...
copying path '/nix/store/vi2h0cvq2ic5b48y0cs0wq074zgis95j-bash-interactive-5.1-p4-man' from 'https://cache.nixos.org'...
copying path '/nix/store/gz0jj1sd5ca3xzn23wbds7ly1yinp14p-Libsystem-1238.60.2' from 'https://cache.nixos.org'...
copying path '/nix/store/dwa5nj68hzds2hrr56nrdxldqqf3aarv-Security-55471.14.18' from 'https://cache.nixos.org'...
copying path '/nix/store/2vv81cb5s6ghmacprin1v69nc9i6jdah-adv_cmds-119-locale' from 'https://cache.nixos.org'...
copying path '/nix/store/7w17rr43cpj30aqm34hf6w56gmdgh7l8-brotli-1.0.9-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/2wlc7dca8yq1f1vd263vc962i94vr7ds-bzip2-1.0.6.0.2' from 'https://cache.nixos.org'...
copying path '/nix/store/67cniwzcc39ivi7mvg1z4hn157vzggdq-ed-1.17' from 'https://cache.nixos.org'...
copying path '/nix/store/8r9q8q3fwz5sfx6r91dxqkrkxf1xdylj-bzip2-1.0.6.0.2-bin' from 'https://cache.nixos.org'...
copying path '/nix/store/3wfmp788xq5l85fhd1w2mqdk63v88rir-expand-response-params' from 'https://cache.nixos.org'...
copying path '/nix/store/89m6xcj0lacy82x6hljw7akd5j2nn15b-gawk-5.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/csmak3l2a1nvg0fvfmdryarzw603qym1-gnumake-4.3' from 'https://cache.nixos.org'...
copying path '/nix/store/gyds873ndvy0jgb6513ipfigmz0f41ah-gnused-4.8' from 'https://cache.nixos.org'...
copying path '/nix/store/2vn9prydyzs09rpidmfczcz412psi14r-gzip-1.10' from 'https://cache.nixos.org'...
copying path '/nix/store/23d5j06wg1mjr22mqs8dzrv0fangn4gh-libc++abi-7.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/ckq4ayswv8sx4wkynvw96kfsv8bmlhqa-libffi-3.3' from 'https://cache.nixos.org'...
copying path '/nix/store/97klhm3jmfrgqj0y9zizg6sz63jzghii-libiconv-50' from 'https://cache.nixos.org'...
copying path '/nix/store/msx7spf3f67b6rnpwir882455j4raa8k-ncurses-6.2' from 'https://cache.nixos.org'...
copying path '/nix/store/aq29m68q91p19hbhjjs4kgkmrkm94vvh-ncurses-6.2-man' from 'https://cache.nixos.org'...
copying path '/nix/store/xaaa71l0hxk4x8dggl251gi612rb5qsn-libc++-7.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/3sd24513q39dwwfmjcscibbsg6vqn8d7-nghttp2-1.41.0-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/7kvzy5y0rsyrjvpabxqn5xqwhdpa4z15-openssl-1.1.1k' from 'https://cache.nixos.org'...
copying path '/nix/store/3k78brl74f3dlfk2w01gfrg72qk2z8kz-patch-2.7.6' from 'https://cache.nixos.org'...
copying path '/nix/store/vhvr94a8s5k9hhhlz3x9m21fz16g9pwa-libkrb5-1.18' from 'https://cache.nixos.org'...
copying path '/nix/store/60lpzrs6snb7y86cdnrpkkp5js9832h5-pcre-8.44' from 'https://cache.nixos.org'...
copying path '/nix/store/2hpm6bgri7xm3dwazvv7cd524alsclh6-ICU-66108' from 'https://cache.nixos.org'...
copying path '/nix/store/1979wn81f2b2k50wsjma06zmz3ym24pk-compiler-rt-7.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/lwm9srcffhwrv8p4gdg59pg14iklq1fm-gettext-0.21' from 'https://cache.nixos.org'...
copying path '/nix/store/m55xy2fhs4i1d675bs5xgrycw464rff7-gmp-6.2.1' from 'https://cache.nixos.org'...
copying path '/nix/store/kky2fwi8vdff1cgrzxa3njhgva9qbs77-gnugrep-3.6' from 'https://cache.nixos.org'...
copying path '/nix/store/p5hamql6dcy2hh1jd568z6skf9kzv40h-compiler-rt-7.1.0-dev' from 'https://cache.nixos.org'...
copying path '/nix/store/9zzhfjqr42n72104kkzp7shk8167n40f-libtapi-1100.0.11' from 'https://cache.nixos.org'...
copying path '/nix/store/lybawc3ak35rzm14i0wkhq2fpq6gfmmw-coreutils-8.32' from 'https://cache.nixos.org'...
copying path '/nix/store/2ka3b20v2z6hlkkx9mgfs4bc5j7l5fhb-ncurses-6.2-dev' from 'https://cache.nixos.org'...
copying path '/nix/store/kqnbhv4cii17dankky0hl1vk4hmdxd1c-diffutils-3.7' from 'https://cache.nixos.org'...
copying path '/nix/store/iyn1xm2s77zkkkz2m6hlak0xnhpjwa06-findutils-4.7.0' from 'https://cache.nixos.org'...
copying path '/nix/store/6g10kmv1lslnj2y2j2zg44kadyq29fi4-xz-5.2.5' from 'https://cache.nixos.org'...
copying path '/nix/store/rziilbcgxdyav69p4bjqqaij1wc3j4c9-zlib-1.2.11' from 'https://cache.nixos.org'...
copying path '/nix/store/1hc9m668n3n2crmr27xf6b348hvadb0h-xz-5.2.5-bin' from 'https://cache.nixos.org'...
copying path '/nix/store/srj51l9wl32r1b67m136lwblhb1g6msl-libssh2-1.9.0' from 'https://cache.nixos.org'...
copying path '/nix/store/kavf7an3ahc0x3kr9ikvfi6iflx1fs46-binutils-2.35.1' from 'https://cache.nixos.org'...
copying path '/nix/store/cgq2s1g9rfp667silvf2yhf6z1bvvh9k-gnutar-1.34' from 'https://cache.nixos.org'...
copying path '/nix/store/cxajvhkqavq928qnwq9i2rrkkp4khnls-curl-7.74.0' from 'https://cache.nixos.org'...
copying path '/nix/store/asdxdp81zc6b04glgh4mjsi6zk84slbd-libxml2-2.9.10' from 'https://cache.nixos.org'...
copying path '/nix/store/38svcfl8irq5h4nmn0dmwb1xjj18f6kz-zlib-1.2.11-dev' from 'https://cache.nixos.org'...
copying path '/nix/store/afxy30qf656pifidw60m92cjxc4x4kcr-llvm-7.1.0-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/c3mf5lw7w572a78mxkjpxfp00ga8l74n-cctools-port-949.0.1' from 'https://cache.nixos.org'...
copying path '/nix/store/lyn5yccl16ay86gjhcp45psgqw9z4kz0-swift-corefoundation' from 'https://cache.nixos.org'...
copying path '/nix/store/xjlix2qgqq0g3l1zbjbwjlg649bv4kvc-configd-453.19' from 'https://cache.nixos.org'...
copying path '/nix/store/0s0wn8si3vj2cb69j4r7p417p3f4mn3y-gdbm-1.19' from 'https://cache.nixos.org'...
copying path '/nix/store/ksj2qdgizcx07yx3g7wsbkwq7mr4r4rm-gettext-0.21' from 'https://cache.nixos.org'...
copying path '/nix/store/v0qc7d5713af0nndgibcn4kvyvbbzxq0-hello-2.10' from 'https://cache.nixos.org'...
copying path '/nix/store/wmdkisbdf4ih0yla1kzaj769ri372vrw-openssl-1.1.1k' from 'https://cache.nixos.org'...
copying path '/nix/store/w1r72lzjl1h5j0rhsxk8czjk8r4q1r7r-readline-6.3p08' from 'https://cache.nixos.org'...
copying path '/nix/store/ss9cbfshzkmw6vbq75pf6p35l3xldnb4-readline-8.0p4' from 'https://cache.nixos.org'...
copying path '/nix/store/f1yjw78ph4wlbfmmzxvpz1csmcv3pirg-sqlite-3.35.2' from 'https://cache.nixos.org'...
copying path '/nix/store/ci3fv2b5iwhfjvchhxykwdj3j0xf2a8i-bash-interactive-5.1-p4' from 'https://cache.nixos.org'...
copying path '/nix/store/1x2jf0pq7iwycm431mh5jyw25gbnry4d-python-2.7.18' from 'https://cache.nixos.org'...
copying path '/nix/store/fi4rdjbzyql2cdqjnqlfzxlq3rv9nz6g-bash-interactive-5.1-p4-dev' from 'https://cache.nixos.org'...
copying path '/nix/store/kmhmlpdw3q47c5nf0pwy6rbyx1544v81-clang-7.1.0-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/flb3vjwx123c35bg9jqp8wzqpl1bfviw-llvm-7.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/36vx88jh1p0g22d79026q1xs5mx4h1jg-clang-7.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/pfzrja93kp8dy269bjil62g5lqc04c2y-cctools-binutils-darwin-949.0.1' from 'https://cache.nixos.org'...
copying path '/nix/store/jqgwdsz7dv3s2ssf64n6fvv6fz2khfgh-cctools-binutils-darwin-wrapper-949.0.1' from 'https://cache.nixos.org'...
copying path '/nix/store/2pb1r2zgw6yiabv4v3d4j66d3vwgdd5q-clang-wrapper-7.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/k9zyw5957mj5grp4kqw80c1cisk44sd7-stdenv-darwin' from 'https://cache.nixos.org'...
hello
world
Thu Apr 15 2021 19:23:49 (1m28s)

To be more specific the first draft of my shebang script (~/hmm) yields

error: file ‘nixpkgs’ was not found in the Nix search path (add it using $NIX_PATH or -I), at (string):1:13

when executed as is (just ~/hmm).

The inserted second line as follows finally makes it work for me:

#!/usr/bin/env nix-shell
#!nix-shell -I nixpkgs=http://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz
#!nix-shell -i bash -p hello
print "hello"
#!nix-shell -p python2 -i python2
print "world"

Still IDK the reasons why the #!/usr/bin/env NIX_PATH... nix-shell or even #!/usr/bin/env -S NIX_PATH... nix-shell forms don’t work here.

what is the purpose of multiple shebangs in a single fille?

See NIX_PATH and polyglot shebang as well.