Rust Packages
Rust packages live in pkgs/<name>/default.nix using rustPlatform.buildRustPackage.
Minimal Example
{
lib,
rustPlatform,
fetchFromGitHub,
}:
rustPlatform.buildRustPackage (finalAttrs: {
pname = "example";
version = "1.0.0";
src = fetchFromGitHub {
owner = "example";
repo = "example";
tag = "v${finalAttrs.version}";
hash = "sha256-...";
};
cargoHash = "sha256-...";
meta = {
description = "Example Rust tool";
license = lib.licenses.mit;
mainProgram = "example";
};
})
Essential Attributes
Auto-Generated Tests
buildRustPackage automatically provides passthru.tests.build — a variant that re-runs with doCheck = true. User-supplied passthru.tests entries merge alongside it.
Native Dependencies
Rust crates that link C libraries need explicit inputs:
nativeBuildInputs = [ pkg-config ];
buildInputs = [ openssl zlib ];
Shell Completions
nativeBuildInputs = [ installShellFiles ];
postInstall = ''
installShellCompletion --cmd example \
--bash <($out/bin/example completions bash) \
--fish <($out/bin/example completions fish) \
--zsh <($out/bin/example completions zsh)
'';
Guard with build platform check when cross-compiling:
postInstall = lib.optionalString (stdenv.hostPlatform.emulatorAvailable buildPackages) ''
installShellCompletion ...
'';
Complete Example
{
lib,
rustPlatform,
fetchFromGitHub,
pkg-config,
erlang,
git,
}:
rustPlatform.buildRustPackage (finalAttrs: {
pname = "gleam";
version = "1.18.1";
src = fetchFromGitHub {
owner = "gleam-lang";
repo = "gleam";
tag = "v${finalAttrs.version}";
hash = "sha256-...";
};
cargoHash = "sha256-...";
nativeBuildInputs = [ pkg-config erlang ];
nativeCheckInputs = [ git ];
checkFlags = [
"--skip=tests::all_files_have_copyright_notice"
"--skip=tests::echo::"
];
meta = {
description = "Statically typed language for the Erlang VM";
homepage = "https://gleam.run/";
license = lib.licenses.asl20;
mainProgram = "gleam";
};
})