Skip to content

Using Rust with Nix Flakes

When starting a new Rust project, there are a few simple ways to get off the ground with Nix Flakes.

Option 1: Crane

Start by initializing the Flake template, replacing any occurrences of my-crate in the Flake with the actual name of your crate.

nix flake init -t github:ipetkov/crane#quick-start
nix flake init -t github:ipetkov/crane#quick-start

Finally, run either nix develop or direnv allow and you should be ready to start coding in Rust! Note, there are additional templates for other build types as well.

Option 2: cargo2nix

Allows you to use Cargo.toml to manage Nix dependencies via a Cargo.nix file.

# Use nix to get cargo2nix & rust toolchain on your path
nix develop github:cargo2nix/cargo2nix#bootstrap

# In directory with Cargo.toml & Cargo.lock files (cargo generate-lockfile)
cargo2nix

# Or skip the shell and run it directly
nix run github:cargo2nix/cargo2nix

# You'll need this in version control
git add Cargo.nix
# Use nix to get cargo2nix & rust toolchain on your path
nix develop github:cargo2nix/cargo2nix#bootstrap

# In directory with Cargo.toml & Cargo.lock files (cargo generate-lockfile)
cargo2nix

# Or skip the shell and run it directly
nix run github:cargo2nix/cargo2nix

# You'll need this in version control
git add Cargo.nix

We recommend using the Crane option since as of writing cargo2Nix doesn't appear to be fully up-to-date