Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;
  featuresFile = ./rust-features.toml;  # Optional
};

Project Structure

my-project/
├── Cargo.toml
├── Cargo.lock
├── rust-deps.toml        # Generated from Cargo.lock
├── rust-features.toml    # Manual feature overrides
└── 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",
]

Feature Overrides

Use rust-features.toml for manual feature control:

[overrides]
serde = ["derive", "std"]
tokio = ["full"]

Auto-Sync

The cargo command is wrapped to auto-sync:

cargo add serde  # Triggers sync