Cargo Nix Plugin banner
anthropics anthropics

Cargo Nix Plugin

Development community

Description

A Nix plugin that resolves Cargo workspaces natively and allows for crate level builds

Installation

This entry records only its repository, not the path inside it, so there is no exact command to give. Open the source below and copy the folder into ~/.claude/skills/, or the file into ~/.claude/agents/.

README

cargo-nix-plugin

A Nix plugin that resolves Cargo workspaces natively, replacing the generated `Cargo.nix` file from crate2nix with a single `builtins.resolveCargoWorkspace` primop.

What It Does

  • Reads Cargo.lock and the sparse registry index directly, so there is no cargo binary at eval time (you can also feed it pre-generated cargo metadata JSON)
  • Evaluates cfg() target expressions for your platform during resolution
  • Produces an attrset that plugs into buildRustCrate
  • No more crate2nix generate step or checked-in 50K-100K line Cargo.nix

Install

Add the plugin to your Nix configuration:

# nix.conf or via --option — point at the directory so the right
# extension (.so/.dylib) is picked up automatically
plugin-files = /path/to/cargo-nix-plugin/lib/nix/plugins

Or use the flake output:

{
  inputs.cargo-nix-plugin.url = "github:anthropics/cargo-nix-plugin";
}

Usage

Default (lockfile resolve)

Point at your workspace root:

cargoNix = cargo-nix-plugin.lib {
  inherit pkgs;
  src = ./.;  # must contain Cargo.toml + Cargo.lock
};

The plugin reads `Cargo.lock` plus the sparse registry index directly — no `cargo` binary, no crate sources at eval time. On first use it fetches each crate's index entry (a few hundred bytes) into `$CARGO_HOME` and reuses it on later runs.

If you already redirect cargo to a mirror (`CARGO_REGISTRIES_CRATES_IO_INDEX` or `[source.crates-io] replace-with` in `.cargo/config.toml`), the resolver picks that up too:

# .cargo/config.toml — used by both cargo and the plugin
[source.crates-io]
replace-with = "mirror"
[source.mirror]
registry = "sparse+https://artifactory.example/api/cargo/crates/index/"

If every index lookup fails (e.g. egress to `index.crates.io` is blocked and no mirror is configured), evaluation fails rather than silently producing derivations with missing features.

Explicit metadata

You can also generate cargo's resolution up front an