Skip to content

Feature request: Strip "Nix-style consecutive shebangs" or introduce limited # line comments #146138

@dpc

Description

@dpc

I'm trying to write a script that uses nix hashbang, to import Rust toolchain, and then use cargo-script from it, but Rust side is very picky about the hashbangs:

> cat cargo-script-test.rs
#!/usr/bin/env nix
#!nix shell github:nix-community/fenix?rev=80dbdab137f2809e3c823ed027e1665ce2502d74#minimal.toolchain -c cargo -q -Zscript
---
[dependencies]
duct = "*"
snafu = "*"
---

use duct::cmd;
use snafu::Whatever;
use snafu::ResultExt;

fn main() -> Result<(), Whatever> {
    println!("Hello from cargo-script");
    cmd!("echo", "hi").run().whatever_context("Failed to run command")?;
    Ok(())
}

> ./cargo-script-test.rs
error: expected `[`, found `nix`
 --> cargo-script-test.rs:2:3
  |
2 | #!nix shell github:nix-community/fenix?rev=80dbdab137f2809e3c823ed027e1665ce2502d74#minimal.toolchain -c cargo -q -Zscript
  |   ^^^ expected `[`
  |
  = note: the token sequence `#!` here looks like the start of a shebang interpreter directive but it is not
  = help: if you meant this to be a shebang interpreter directive, move it to the very start of the file

> cargo --version -v
cargo 1.91.0-nightly (a6c58d430 2025-08-26)
release: 1.91.0-nightly
commit-hash: a6c58d43051d01d83f55a3e61ef5f5b2b0dd6bd9
commit-date: 2025-08-26
host: x86_64-unknown-linux-gnu
libgit2: 1.9.1 (sys:0.20.2 vendored)
libcurl: 8.14.1-DEV (sys:0.4.82+curl-8.14.1 vendored ssl:OpenSSL/3.5.0)
ssl: OpenSSL 3.5.0 8 Apr 2025
os: NixOS 25.5.0 [64-bit]

Notably moving one line fixes it:

> cat cargo-script-test.rs
#!/usr/bin/env nix
---
#!nix shell github:nix-community/fenix?rev=80dbdab137f2809e3c823ed027e1665ce2502d74#minimal.toolchain -c cargo -q -Zscript
[dependencies]
duct = "*"
snafu = "*"
---

use duct::cmd;
use snafu::Whatever;
use snafu::ResultExt;


fn main() -> Result<(), Whatever> {
    println!("Hello from cargo-script");
    cmd!("echo", "hi").run().whatever_context("Failed to run command")?;
    Ok(())
}
> ./cargo-script-test.rs
Hello from cargo-script
hi
>

But this is only because nix seems much more liberal and persistent at looking for its own directive, and this might not work with other tooling like this.

It seems to me that it would be preferable if cargo-script accepted and ignored any amount of leading newlines and lines starting with a hash, permitting shenanigans like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-grammarArea: The grammar of RustC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language teamneeds-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions