Rust Support
Turnkey provides Rust support with automatic dependency management.
Setup
Add to toolchain.toml:
[toolchains]
rust = {}
cargo = {}
rustdeps-gen = {}
Enable Rust dependencies in flake.nix:
turnkey.toolchains.buck2.rust = {
enable = true;
depsFile = ./rust-deps.toml;
};
Project Structure
my-project/
├── Cargo.toml
├── Cargo.lock
├── rust-deps.toml # Generated by tk sync, from cargo
└── rust/
└── mycrate/
├── src/
│ └── lib.rs
└── rules.star
Build Rules
In rules.star:
load("@prelude//rust:rust.bzl", "rust_library", "rust_binary")
rust_library(
name = "mycrate",
srcs = glob(["src/**/*.rs"]),
deps = ["rustdeps//serde:serde"],
)
External Dependencies
Reference crates via the rustdeps cell:
deps = [
"rustdeps//serde:serde",
"rustdeps//tokio:tokio",
]
Features and Dependencies
Each vendored crate is built with the features and dependencies cargo
resolves for it. tk sync records, for every crate, its package slice in
rust-deps.toml: the features cargo tree reports for cargo test --workspace, and its normal dependencies resolved to exact versions, each
with the platforms it applies on (one cargo tree run per platform the
project builds for). Each crate's own rules.star is generated from its slice
alone, so a change to one crate rebuilds only that crate and its dependents.
Declare what you need in Cargo.toml as you would for Cargo; there is
nothing to repeat for Buck2, and no way to override Cargo's result: ask for a
feature in the member's Cargo.toml.
tk syncrunscargowith--locked, soCargo.lockmust matchCargo.toml. After editing a manifest by hand, run a cargo command (ortw cargo …) that updates the lock. The first run downloads the crates into~/.cargo/registry.- Every workspace member's
Cargo.tomlis a source ofrust-deps.toml, so a features-only edit in a member regenerates it. - A crate no configured platform builds (Windows-only, wasm, a build
dependency) has an empty slice: its
rules.starhas no features or dependencies, and nothing configures it.
The rustdeps cell is a real directory, .turnkey/rustdeps, kept in line
with the Nix-built cell index by tk materialize; see
Managing Dependencies.
Auto-Sync
The cargo command is wrapped to auto-sync:
cargo add serde # Triggers sync