Skip to the content.

cargo

Point cargo at the proxy’s crates.io upstream by replacing the crates-io source. The examples use the upstream name crates; replace it with the name your operator gave the upstream (what you need).

   
Upstream type cargo
Package source sparse+https://proxy.example.com/v1/crates/ (a sparse registry)
Clients cargo
Tested cargo 1.99: in CI on every push and in the client lab.

Configure

Source replacement redirects every crates.io dependency to the proxy without touching a Cargo.toml. The trailing slash on the sparse+ URL is required.

In the user-level configuration, $CARGO_HOME/config.toml:

[source.crates-io]
replace-with = "proxy"

[source.proxy]
registry = "sparse+https://proxy.example.com/v1/crates/"

$CARGO_HOME is ~/.cargo unless the variable is set — and the official Rust container image sets it to /usr/local/cargo, so a ~/.cargo/config.toml written there is not read. A .cargo/config.toml in the project (or any parent directory) works the same and can be committed.

Without a file, as in a CI job, pass the two settings as --config flags:

cargo --config 'source.crates-io.replace-with="proxy"' \
      --config 'source.proxy.registry="sparse+https://proxy.example.com/v1/crates/"' \
      fetch

CARGO_SOURCE_… environment variables do not work. cargo maps most of its configuration to CARGO_* variables, but not the [source] table: with CARGO_SOURCE_CRATES_IO_REPLACE_WITH set, cargo still goes to crates.io.

Authenticate

cargo cannot use a proxy that requires a token. cargo presents a registry token only after the registry answered 401, and for crate downloads only when the registry’s config.json declares "auth-required": true, which the proxy’s does not. With a token configured ([registries.proxy] with a matching index), cargo reads the index and is then refused the download:

error: failed to download from `https://proxy.example.com/v1/crates/api/v1/crates/cfg-if/1.0.0/download`

Caused by:
  failed to get successful HTTP response from `https://proxy.example.com/v1/crates/api/v1/crates/cfg-if/1.0.0/download`, got 401

Where access control is on, the operator has to admit cargo without a token: an anonymous role scoped to the cargo upstream (how).

Trust a private CA

In $CARGO_HOME/config.toml:

[http]
cainfo = "/etc/ssl/corp-ca.pem"

or in the environment:

export CARGO_HTTP_CAINFO=/etc/ssl/corp-ca.pem

Check that it works

cargo fetch

prints Updating `proxy` index and Downloaded cfg-if v1.0.0 (registry `proxy`) — the replacement source by name. An error that mentions index.crates.io means the configuration was not read (see $CARGO_HOME above).

Good to know

On the proxy

upstreams:
  crates:
    type: cargo
    url: https://index.crates.io

The sparse index is served live; config.json is rewritten so .crate downloads come back through the proxy (which is why server.publicUrl must be set) and are cached immutably. Details: backends: cargo.