Open fmnxl opened 10 months ago
@fmnxl (also cc @rockofox), and anyone interested: this issue keeps bugging me, probably for the same reason as you, nix-shell
(so we can use zx
in a "fully contained" way).
Given both #715 and #716 got closed without this being addressed, using "two shebangs" still results in a SyntaxError
.
As I managed to find a different not-as-portable approach to overcome this limitation (which makes it a single shebang line), I'm sharing it here. Afaict, it requires two things:
nix shell
(and not nix-shell
), i.e. the nix-command experimental feature turned on;env
with the -S
flag:
-S, --split-string=S process and split S into separate arguments;
used to pass multiple arguments on shebang lines
coreutils
8.30 (July 2018);If you can live with those two requirements, this is the shebang line:
#!/usr/bin/env -S nix shell nixpkgs#zx --command zx
Please let me know if this works for you, and if you manage to find a way to simplify this.
(given the current files-without-extension temporary files behavior, I advise you to still use the .mjs
extension for now, unfortunately.)
Actually shbang supported via node. Zx not handles shbang.
@antonmedv I guess the reasoning behind this issue being a feature request was because it was possible for zx
to be hackishly modified to support them (as implemented in #716).
@fmnxl @rockofox I just accidentally discovered that nix-shell
parses nix-shell
option hashbangs anywhere in the file.
This was discussed in https://github.com/NixOS/nix/issues/2570 and finally documented in https://github.com/NixOS/nix/pull/11202 (so the first reference to this in documentation appears in the 2.24 docs (last paragraph)).
As such, @antonmedv, I believe this issue can be closed because, as in the unofficial NixOS wiki Lua example, we can resort to JavaScript's block comments ("multi-line") to make this work with nix-shell
:
#! /usr/bin/env nix-shell
/*
#! nix-shell -i zx -p zx
*/
$.verbose = true;
await Promise.all([
$`sleep 1; echo 1`,
$`sleep 2; echo 2`,
$`sleep 3; echo 3`,
]);
Should we add some notes about this behavior to documentation? A nix-guide of docs website?
@jlbribeiro maybe you can write a short .md file?
Expected Behavior
Nix allows the use of shebang to define a custom interpreter, for example:
zx should ignore multiple shebang lines instead only the first one.
Actual Behavior
SyntaxError: Invalid or unexpected token
Steps to Reproduce the Problem
zx script.mjs
Specifications