Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple lines of hashbangs #715

Open
fmnxl opened this issue Jan 15, 2024 · 5 comments
Open

Allow multiple lines of hashbangs #715

fmnxl opened this issue Jan 15, 2024 · 5 comments

Comments

@fmnxl
Copy link

fmnxl commented Jan 15, 2024

Expected Behavior

Nix allows the use of shebang to define a custom interpreter, for example:

#! /usr/bin/env nix-shell
#! nix-shell -i zx -p zx

await $`echo 1`

zx should ignore multiple shebang lines instead only the first one.

Actual Behavior

SyntaxError: Invalid or unexpected token

Steps to Reproduce the Problem

  1. (setup) Install nix
  2. Add the above shebang lines to the start of the script (script.mjs)
  3. Try to run the script with zx script.mjs

Specifications

  • Version: 7.1.1
  • Platform: macOS
@jlbribeiro
Copy link

jlbribeiro commented Aug 17, 2024

@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
    

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.)

@antonmedv antonmedv reopened this Aug 17, 2024
@antonmedv
Copy link
Collaborator

Actually shbang supported via node. Zx not handles shbang.

@antongolub
Copy link
Collaborator

@jlbribeiro
Copy link

jlbribeiro commented Sep 13, 2024

@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 NixOS/nix#2570 and finally documented in NixOS/nix#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`,
]);

@antonmedv
Copy link
Collaborator

antonmedv commented Sep 14, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants