NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.23k stars 14.22k forks source link

nix: Multiline shebang not working #280033

Open tobiasBora opened 10 months ago

tobiasBora commented 10 months ago

Describe the bug

The documentation here https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html?highlight=shebang#shebang-interpreter mentions that one can create an executable file:

#!/usr/bin/env nix
#! nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash

hello | cowsay

and run it, but I get an error:

$ chmod +x test
$./test
error: './test' is not a recognised command
Try 'nix --help' for more information.

I found a solution using the -S option of env like:

#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash

hello | cowsay

This works, but I don't see how to run this way more complex stuff. For instance, the documentation mentions that we can run:

#!/usr/bin/env nix
//! ```cargo
//! [dependencies]
//! time = "0.1.25"
//! ```
/*
#!nix shell nixpkgs#rustc nixpkgs#rust-script nixpkgs#cargo --command rust-script
*/
fn main() {
    for argument in std::env::args().skip(1) {
        println!("{}", argument);
    };
    println!("{}", std::env::var("HOME").expect(""));
    println!("{}", time::now().rfc822z());
}
// vim: ft=rust

but I get the same error.

Steps To Reproduce

Steps to reproduce the behavior: just described in previous section.

Notify maintainers

Metadata

I'm running NixOs 2caf4ef5005ecc68141ecb4aac271079f7371c44

Add a :+1: reaction to issues you find important.

nixos-discourse commented 10 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-shell-shebang-flakes-haskell/38064/4

eclairevoyant commented 10 months ago

This only works with nix 2.19 and up.

tobiasBora commented 10 months ago

Ok thanks, I tried to run the latest nix version via nix shell, but it was of course only in the current shell, while I guess nix-env nix would use the system nix. But I have 2 questions:

  1. would it be possible to mention the -S syntax in the doc that works for any version of nix?
  2. is the construction
    //! ```cargo
    //! [dependencies]
    //! time = "0.1.25"
    //! ```

    documented somewhere?

NasaGeek commented 9 months ago

Reopening since the nixVersions.stable update was reverted.

eclairevoyant commented 5 months ago

On second review, I don't know that we can do much in nixpkgs. Upgrading nix is dependent on nix upstream providing a version of nix > 2.19 that also does not cause regressions especially for our nixos tests. And the documentation is also out of our control.

phanirithvij commented 2 months ago

https://github.com/NixOS/nixpkgs/pull/335342