config.toml, Source Replacement & Vendoring
Cargo.toml describes a package. .cargo/config.toml describes how and where to build it: which registries exist, where crates come from, which linker to use, shortcut commands, environment variables. This lesson uses it for the big job it was designed for: building without the internet, from mirrors and vendored sources.
Where Cargo looks
Cargo merges every config.toml from the current directory up to the root, then $CARGO_HOME/config.toml (~/.cargo/config.toml). Closer files win for single values; arrays are merged. On top of that:
- environment variables:
CARGO_BUILD_JOBS=1,CARGO_NET_OFFLINE=true,CARGO_TARGET_<TRIPLE>_LINKER=...(every key has an env form: upper case,.and-become_); --config KEY=VALUEor--config path/to/file.tomlon the command line, which wins over everything.
| Section | Example use |
|---|---|
[alias] |
ci = "clippy --all-targets --locked -- -D warnings" → cargo ci |
[build] |
target-dir, jobs, rustflags, default target |
[target.<triple>] |
linker, runner, rustflags per target (Cross-Compiling) |
[env] |
environment variables for build scripts and rustc |
[registries.<name>], [registry] |
alternative registries (Publishing) |
[source.<name>] |
source replacement: mirrors and vendoring |
[net] |
offline, retry, git-fetch-with-cli |
[http] |
proxy, timeouts, CA certificates |
Source replacement: what this course's machine does
Every session in this course runs without network, yet cargo add serde works. Here's why:
cat ~/.cargo/config.toml# Offline Cargo setup for build-rust sessions (installed as ~/.cargo/config.toml).
# crates.io is replaced by a local registry seeded at image build time (/opt/crates): normal `serde = "1"`
# dependencies resolve and lock exactly as against crates.io, but only the seeded versions exist.
[source.crates-io]
replace-with = "offline-mirror"
[source.offline-mirror]
local-registry = "/opt/crates"
# A writable alternative registry for `cargo publish --registry local` (start it with `crate-registry start`).
[registries.local]
index = "sparse+http://127.0.0.1:8282/index/"
[source.crates-io] replace-with = "offline-mirror" tells Cargo: whenever a dependency comes from crates.io, fetch it from offline-mirror instead, a local registry (a directory of .crate files plus an index). Lockfiles still say registry+https://github.com/rust-lang/crates.io-index, because the identity of the source hasn't changed, only where its files come from. That's what makes source replacement safe: switch the mirror off and the same Cargo.lock works against the real crates.io. Companies use the same mechanism to point Cargo at an internal crates.io proxy (Artifactory, Nexus, a sparse mirror) with registry = "sparse+https://..." instead of local-registry.
Source kinds you can replace with: registry (another index), local-registry (directory of .crate files), directory (unpacked sources, what cargo vendor produces).
Aliases and [env]
cargo new app --vcs none -q
cd app
mkdir -p .cargo
cat > .cargo/config.toml <<'T'
[alias]
lint = "clippy --all-targets -- -D warnings"
r = "run --quiet"
[env]
APP_GREETING = "configured in .cargo/config.toml"
[build]
target-dir = "out"
T
echo 'fn main() { println!("{}", option_env!("APP_GREETING").unwrap_or("not set")); }' > src/main.rs
cargo r
cargo lint 2>&1
lsconfigured in .cargo/config.toml
Checking app v0.1.0 (/home/learner/app)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
Cargo.lock
Cargo.toml
out
src
target-dir = "out" moved the build output, which is how CI setups share one target directory between several checkouts. [env] values reach rustc and build scripts but not the program at runtime, so the code reads it with option_env! (compile time).
Note:
cargo config get(print the merged configuration) is still unstable and only works on nightly; on stable, read the files.
Vendoring: dependencies inside the repository
cargo vendor copies the source of every dependency in Cargo.lock into a folder (default vendor/) and prints the config needed to use it. The resulting project builds with no registry at all: on an air-gapped machine, in a hermetic build system, or years later when you need to rebuild an old release exactly.
On this machine crates.io is already replaced by a mirror, so the command needs --respect-source-config to read from the mirror. (Without the flag, cargo vendor ignores source replacement and tries to reach index.crates.io, which fails offline.)
cargo new app --vcs none -q
cd app
cargo add anyhow regex -q
cargo vendor --respect-source-config vendor > vendor-config.toml 2> vendor.log
grep Vendoring vendor.log | sed 's/ (.*) to / -> /'
ls vendor
cat vendor-config.toml
mkdir -p .cargo && cat vendor-config.toml > .cargo/config.toml
echo "--- a vendored crate keeps a checksum file"
head -c 120 vendor/anyhow/.cargo-checksum.json; echo
echo "--- build with an EMPTY Cargo home: no mirror, no cache, no network"
mkdir empty-home
CARGO_HOME=$PWD/empty-home cargo build --offline 2>&1 Vendoring aho-corasick v1.1.5 -> vendor/aho-corasick
Vendoring anyhow v1.0.104 -> vendor/anyhow
Vendoring memchr v2.8.3 -> vendor/memchr
Vendoring regex v1.13.1 -> vendor/regex
Vendoring regex-automata v0.4.18 -> vendor/regex-automata
Vendoring regex-syntax v0.8.11 -> vendor/regex-syntax
aho-corasick
anyhow
memchr
regex
regex-automata
regex-syntax
[source.crates-io]
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "vendor"
--- a vendored crate keeps a checksum file
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not prote
--- build with an EMPTY Cargo home: no mirror, no cache, no network
Compiling memchr v2.8.3
Compiling aho-corasick v1.1.5
Compiling anyhow v1.0.104
Compiling regex-syntax v0.8.11
Compiling regex-automata v0.4.18
Compiling regex v1.13.1
Compiling app v0.1.0 (/home/learner/app)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.95s
The last build had no ~/.cargo/config.toml, no registry cache and no network, and still compiled, entirely from vendor/. Notice there are no Unpacking lines: directory sources are used in place.
Details to know:
.cargo-checksum.jsonholds a SHA-256 for every file. Editing a vendored file breaks the build (the listed checksum … has changed) on purpose: patch with[patch]instead.- Re-run
cargo vendorafter everyCargo.lockchange; CI can check withcargo build --locked --offlinethatvendor/is complete. - Vendoring makes the repository bigger (often hundreds of MB with Windows-only crates);
--versioned-dirsand tools likecargo-vendor-filtererhelp. - A workspace is vendored as a whole from its root;
cargo vendor -s other/Cargo.tomladds more manifests.
Without vendoring: cargo fetch and --offline
If you only need to survive a flaky network, you don't have to vendor. cargo fetch downloads everything the lockfile needs into ~/.cargo/registry; after that, cargo build --offline (or CARGO_NET_OFFLINE=true) never touches the network. Docker builds use the same trick: copy Cargo.toml + Cargo.lock, run cargo fetch in its own layer, then copy the sources.
Test yourself
What does [source.crates-io] replace-with = "mirror" change in Cargo.lock?
- Every source URL is rewritten to the mirror
- Nothing: the lockfile still names crates.io; only where files are fetched from changes
- The checksums are removed
- Cargo.lock is no longer used
On a machine where crates.io is already replaced by a mirror, why does cargo vendor need --respect-source-config?
- To vendor dev-dependencies
- Without it, cargo vendor ignores the replacement and tries to fetch from crates.io itself
- To write the config snippet
- It's required for workspaces
You need to fix a bug in a vendored crate. What's the right way?
- Edit vendor/<crate>/src directly
- Use [patch] with a local copy (or upstream the fix), then re-vendor
- Delete .cargo-checksum.json
- Use --offline