Rust
The languages.rust
module provides comprehensive support for Rust development, offering flexible toolchain management through two distinct approaches.
Getting started
To enable Rust support in your devenv.nix
:
This will provide a complete Rust development environment with rustc
, cargo
, clippy
, rustfmt
, and rust-analyzer
.
Toolchain management
devenv supports two approaches for managing Rust toolchains:
1. nixpkgs channel (default)
The nixpkgs
channel is easy to set up and uses the Rust version currently available in your nixpkgs revision. However, it's limited to the version in nixpkgs.
2. rust-overlay channels
For more control over versions and features, use the stable
, beta
, or nightly
channels powered by rust-overlay:
- ✅ Access to any Rust version
- ✅ Support for cross-compilation targets
- ✅ Rustup-like channel selection
- ✅ Custom profiles (minimal, default, complete)
Examples
Basic setup with latest stable
Nightly Rust with extra components
{
languages.rust = {
enable = true;
channel = "nightly";
components = [ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" "miri" ];
};
}
Cross-compilation setup
{
languages.rust = {
enable = true;
channel = "stable";
targets = [ "wasm32-unknown-unknown" "aarch64-unknown-linux-gnu" ];
};
}
Minimal installation
{
languages.rust = {
enable = true;
channel = "stable";
profile = "minimal";
components = [ "rustc" "cargo" ];
};
}
Using rust-toolchain.toml
If your project uses a rust-toolchain.toml
file, you can leverage it with rust-overlay:
{ config, ... }:
{
languages.rust = {
enable = true;
channel = "stable";
toolchain = config.languages.rust.rustBin.fromRustupToolchainFile ./rust-toolchain.toml;
};
}
Example rust-toolchain.toml
:
[toolchain]
channel = "1.81.0"
components = ["rustfmt", "rust-analyzer"]
targets = ["wasm32-unknown-unknown"]
profile = "default"
Integration with other tools
Git hooks
Rust tools integrate seamlessly with git hooks:
{
languages.rust.enable = true;
git-hooks.hooks = {
rustfmt.enable = true;
clippy.enable = true;
};
}
Options
languages.rust.enable
Whether to enable tools for Rust development.
Type: boolean
Default:
false
Example:
true
languages.rust.channel
The rustup toolchain to install.
nixpkgs
is a special channel.
It will use whichever version is currently available in nixpkgs.
Type: one of “nixpkgs”, “stable”, “beta”, “nightly”
Default:
"nixpkgs"
languages.rust.components
List of Rustup components
to install. Defaults to those available in nixpkgs
.
Type: list of string
Default:
[ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" ]
languages.rust.mold.enable
Enable mold as the linker.
Enabled by default on x86_64 Linux machines when no cross-compilation targets are specified.
Type: boolean
Default:
pkgs.stdenv.isLinux && pkgs.stdenv.isx86_64 && languages.rust.targets == [ ]
languages.rust.profile
The rustup toolchain profile to use.
Only used when languages.rust.channel is NOT set to nixpkgs
.
Type: one of “default”, “minimal”, “complete”
Default:
"default"
languages.rust.rustBin
Initialized rust-overlay library.
Only available when channel
is not set to nixpkgs
.
Type: null or anything (read only)
Default:
null
languages.rust.rustflags
Extra flags to pass to the Rust compiler.
Type: string
Default:
""
languages.rust.targets
List of extra targets to install. Defaults to the native target.
Type: list of string
Default:
[ ]
languages.rust.toolchain
The Rust toolchain to use.
When the channel is set to nixpkgs
, the toolchain is created by symlinking the individual components from languages.rust.components
.
For other channels, the toolchain is created using rust-overlay with the specified version, profile, and components.
Type: package
languages.rust.version
The version of rust to use.
Examples: latest
,1.81.0
, 2021-01-01
.
Only used when languages.rust.channel is NOT set to nixpkgs
.
Type: string
Default:
"latest"