cargo-script is an in-progress feature of cargo, that allows writing "shell-like" scripts using Rust and run them as such.

It's already implemented and available in the Rust nightly toolchain. For time being, this makes it a bit inconvenient to use as it requires sourcing the nightly toolchain, passing extra flags, and since it's an unstable feature any script written in it could potentially break in the future.

Unless you are a Nix user. Nix users can easily do otherwise difficult things. In this case use arbitrary versions of software and make it work like it is stable (enough) already.

Update: Since 23.11 Nix has even better support for being used as shell interpreter. See:

Here is the example of nix+cargo-script to showcase it:

#!/usr/bin/env -S nix shell "github:nix-community/fenix?rev=092bd452904e749efa39907aa4a20a42678ac31e#minimal.toolchain" -c cargo -q -Zscript
clap = { version = "4.2", features = ["derive"] }

use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,

fn main() {
    let args = Args::parse();
    println!("{:?}", args);

If you store it as e.g. you should be able to execute it with:

> ./ --config somefile
Args { config: Some("somefile") } 

Screenshot of executing the above script

The Rust toolchain is provided by the Fenix Nix Flake and locked to the specific commit version which means it will not break even if the upstream implementation of cargo-script changes.

I hope this help you rewrite some shell scripts in Rust and provide feedback to the cargo developers about this feature.