Nix users, you can fearlessly start using Rust scripts already

14 Likes

macOS is not happy with it:

$ ./cargo-nix-script.rs
error: '"github:nix-community/fenix?rev=092bd452904e749efa39907aa4a20a42678ac31e' is not a valid URL

Looks like the # before minimal.toolchain interpreted as a comment marker and everything behind it is stripped. Damn.

2 Likes

try to use ' instead of " for quoting, or try to use \# instead of # :woman_shrugging:

Nope. The shebang parser seems to be very primitive in Darwin. As seen above it takes the first quote (") as part of the argument. So really it just strips of everything after the second # and then splits by space I guess.

Regardless, it’s a cool little demonstration of Nix and its capabilities for this.

I’ve done similar things before

#!/usr/bin/env nix-shell
//! ```cargo
//! [dependencies]
//! time = "0.1.25"
//! ```
/*
#!nix-shell -i rust-script -p rustc -p rust-script -p cargo
*/
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
#!/usr/bin/env nix-shell
#![allow()] /*
#!nix-shell -i bash -p rustc
rsfile="$(readlink -f $0)"
binfile="/tmp/$(basename "$rsfile").bin"
rustc "$rsfile" -o "$binfile" --edition=2021 && exec "$binfile" $@ || exit $?
*/
fn main() {
    for argument in std::env::args().skip(1) {
        println!("{}", argument);
    };
    println!("{}", std::env::var("HOME").expect(""));
}
// vim: ft=rust

Any weird syntaxes are workarounds which I needed in 2022

@jer these only use nix-shell as the shebang so they should work on darwin

Just tried this. Truly amazing. First run had some delay, of course, while the rust nightly was installed. Subsequent runs, even after altering the script, are very fast.