Skip to content
Public template

About

Rust flake template with crane, rust-overlay, clippy, rustfmt, and pre-commit hooks.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

Rust development template

A Nix flake for a Rust crate. purr builds the flake outputs from the files in develop/.

What you get

  • The latest stable Rust from rust-overlay, with rust-src
  • A package built with crane
  • A dev shell with the toolchain, pkg-config, openssl and the lint tools
  • Pre-commit hooks from git-hooks.nix: nixfmt, deadnix, statix, cargo-check, clippy, rustfmt
  • nixfmt-tree as the flake formatter

Start a project

nix flake init -t "github:nixcafe/develop-templates#rust" --refresh
git init
git add -A

To use a shorter registry name:

nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#rust
git init
git add -A

If you use cattery-modules, the beans registry entry already exists.

Nix only sees files that git tracks, so add new files before you build. The first nix command writes flake.lock. Commit it.

You can also create a GitHub repository from this one:

gh repo create my-rust-project --template nixcafe-develop/rust --clone

Commands

nix develop                             # enter the dev shell and install the git hooks
nix fmt                                 # format the Nix files
nix flake check                         # run the hooks on all files
nix build                               # build the crate and run its tests
nix run                                 # run the binary

With direnv, run direnv allow once and the shell loads when you enter the directory.

Inside the shell, use cargo as usual. cargo fmt formats the Rust code.

Layout

flake.nix                                  inputs and the purr call
Cargo.toml
Cargo.lock
statix.toml
src/main.rs
develop/
  overlays/rust-toolchain/default.nix      toolchain, available as pkgs.rustToolchain
  packages/default/default.nix             crane build
  shells/default/default.nix               dev shell
  checks/git-hooks/default.nix             hooks

Customize

Toolchain

Edit develop/overlays/rust-toolchain/default.nix. The package, the shell and the hooks all use it.

# a fixed release
final.rust-bin.stable."1.90.0".default.override { extensions = [ "rust-src" ]; }

# the latest nightly
final.rust-bin.selectLatestNightlyWith (
  toolchain: toolchain.default.override { extensions = [ "rust-src" ]; }
)

# whatever rust-toolchain.toml says
final.rust-bin.fromRustupToolchainFile ../../../rust-toolchain.toml

Keep rust-src in the toolchain, because RUST_SRC_PATH points to it. With rust-toolchain.toml, list it under components.

To move to a newer stable release, run nix flake update rust-overlay.

Native libraries

Add them to nativeBuildInputs or buildInputs in develop/packages/default/default.nix. The dev shell and the cargo hooks pick them up.

Extra source files

The build only copies Cargo and Rust sources. If the crate reads other files, for example through include_str!, widen src in the package. The crane docs explain source filtering.

Rename the crate

Change name in Cargo.toml, run cargo build to update Cargo.lock, and commit both.

Hooks

Edit develop/checks/git-hooks/default.nix. The hook list shows what is available. statix.toml holds the statix settings.

More outputs

Add develop/packages/<name>/default.nix, develop/shells/<name>/default.nix or develop/checks/<name>/default.nix. purr picks them up without changes to flake.nix.

About

Rust flake template with crane, rust-overlay, clippy, rustfmt, and pre-commit hooks.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages

Generated from nixcafe-develop/nix