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

nix: Multiline shebang not working #280033

Open
tobiasBora opened this issue Jan 10, 2024 · 6 comments · Fixed by #280092
Open

nix: Multiline shebang not working #280033

tobiasBora opened this issue Jan 10, 2024 · 6 comments · Fixed by #280092
Labels
9.needs: package (update) This needs a package to be updated 9.needs: upstream fix This PR needs upstream to change something

Comments

@tobiasBora
Copy link
Contributor

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 2caf4ef

Add a 👍 reaction to issues you find important.

@tobiasBora tobiasBora added the 0.kind: bug Something is broken label Jan 10, 2024
@nixos-discourse
Copy link

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
Copy link
Contributor

This only works with nix 2.19 and up.

@eclairevoyant eclairevoyant added 9.needs: documentation 9.needs: package (update) This needs a package to be updated and removed 0.kind: bug Something is broken labels Jan 10, 2024
@tobiasBora
Copy link
Contributor Author

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
Copy link
Contributor

Reopening since the nixVersions.stable update was reverted.

@NasaGeek NasaGeek reopened this Feb 21, 2024
@eclairevoyant
Copy link
Contributor

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.

@eclairevoyant eclairevoyant added 9.needs: upstream fix This PR needs upstream to change something and removed 9.needs: documentation labels Jun 10, 2024
@phanirithvij
Copy link
Member

#335342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
9.needs: package (update) This needs a package to be updated 9.needs: upstream fix This PR needs upstream to change something
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants