For a team's private Cargo registry, how do you depend on it, publish to it, authenticate CI, and keep internal crates off crates.io?
answer
- [registries] in config
- registry key on the dependency
- package.publish as an allow-list
- credential providers, not bare tokens
basics
~20 sName the registry under [registries] in .cargo/config.toml, mark its dependencies registry = "internal", and set publish = ["internal"] to keep crates off crates.io. Tokens come from cargo login --registry internal or CARGO_REGISTRIES_INTERNAL_TOKEN, read by a credential provider.
solid answer
~40 sDeclare the registry in `.cargo/config.toml` as `[registries.internal]` with an `index` URL (a git URL, or `sparse+https://...` for the sparse protocol). A dependency opts in with `registry = "internal"`, and that includes a workspace path dependency, whose packaged form otherwise points at crates.io. `publish = ["internal"]` in `[package]` is an allow-list: a bare `cargo publish` goes to `internal`, and `--registry crates-io` is refused. Tokens come from `cargo login --registry internal` or `CARGO_REGISTRIES_INTERNAL_TOKEN`, read through credential providers. For a registry that requires authentication on every request, Cargo refuses to work until a provider is configured, such as `credential-provider = "cargo:token"` for CI or `cargo:macos-keychain` on a laptop. crates.io itself rejects packages that depend on other registries.
code
toml · 14 lines# .cargo/config.toml
[registries.internal]
index = "sparse+https://crates.internal.example/index/"
credential-provider = "cargo:token"
# quantile-sketch/Cargo.toml
[package]
name = "quantile-sketch"
version = "2.1.0"
edition = "2024"
publish = ["internal"]
[dependencies]
sketch-core = { path = "../sketch-core", version = "2.1.0", registry = "internal" }go deeper
Recall the three pieces: a [registries] entry in config, registry = "internal" on the dependency, and cargo publish --registry internal.
Explain package.publish as an allow-list, the CARGO_REGISTRIES_<NAME>_TOKEN naming rule, and where cargo login stores a token.
Diagnose the credential-provider error on an authenticated registry, the crates.io lookup for an unregistered path dependency, and token handling in CI logs.
Decide how tokens are issued, scoped and rotated for the registry, and make publish = ["internal"] a rule enforced on every internal manifest.
## Naming the registry Cargo knows only crates.io until you add a registry to a **config file** (`.cargo/config.toml` in the repository, or Cargo's home config): ```toml [registries.internal] index = "sparse+https://crates.internal.example/index/" credential-provider = "cargo:token" ``` The `index` is either a git repository URL or a **sparse** URL with the `sparse+` prefix; sparse fetches individual index files over HTTP instead of cloning the whole index. As with most config values, the environment can supply it instead: `CARGO_REGISTRIES_INTERNAL_INDEX`. `[registry] default = "internal"` makes it the default for commands such as `cargo publish`. ## Depending on internal crates A dependency comes from another registry only when it says so: ```toml [dependencies] quantile-sketch = { version = "2.0", registry = "internal" } ``` The same applies to a **path dependency inside a workspace**. When the crate is packaged, Cargo removes `path` and keeps `version`, and without a `registry` key the dependency points at crates.io. Packaging then searches the crates.io index for an internal crate and fails with "no matching package named `sketch-core` found", so write `{ path = "../sketch-core", version = "2.1.0", registry = "internal" }`. ## Keeping crates off crates.io `package.publish` controls where a package may go: | Value | Effect | |---|---| | omitted or `true` | any registry, crates.io by default | | `["internal"]` | only `internal`; a bare `cargo publish` picks it automatically | | `false` (same as `[]`) | publishing refused everywhere | With `publish = ["internal"]`, `cargo publish --registry crates-io` fails with "The registry `crates-io` is not listed in the `package.publish` value in Cargo.toml". This is the guard against a closed-source crate leaking by accident. There is a second guard on the other side: **crates.io does not accept packages that depend on crates from other registries**. ## Tokens and credential providers Cargo reads every token through a **credential provider**: - `cargo:token` stores tokens in plain text in `credentials.toml` and also reads `CARGO_REGISTRIES_<NAME>_TOKEN` (`CARGO_REGISTRY_TOKEN` for crates.io). If it is not in the provider list, those variables do nothing. - `cargo:macos-keychain`, `cargo:wincred` and `cargo:libsecret` keep tokens in the operating system's store. - `cargo:token-from-stdout <command>` runs a program that prints a token, for example one fetching it from a secrets manager. - A plugin executable can implement the credential-provider protocol. A provider is chosen per registry (`registries.internal.credential-provider`) or globally (`registry.global-credential-providers`, where later entries take precedence). The Cargo book recommends `["cargo:token", "cargo:libsecret", "cargo:macos-keychain", "cargo:wincred"]` globally. The trap is **authenticated registries**, whose `config.json` sets `auth-required` so that even index reads need a token. For them Cargo will not fall back silently to a plain-text file. With no provider configured, every command that touches the registry (`cargo publish`, `cargo login`, `cargo generate-lockfile`) stops with "authenticated registries require a credential-provider to be configured", even when the token variable is set. Public registries keep the historical behaviour: `cargo:token` is used if nothing is configured. Two more facts belong in a runbook: 1. `cargo login --registry internal` saves the token through the provider; `cargo logout --registry internal` removes it locally and says it **does not revoke** it on the server. 2. `cargo publish --token` still works but is soft-deprecated; prefer the environment variable or a provider so tokens do not land in shell history or CI logs. ## A CI setup that works - Commit `.cargo/config.toml` with `[registries.internal]`, its `index` and `credential-provider = "cargo:token"`. - Inject `CARGO_REGISTRIES_INTERNAL_TOKEN` from the CI secret store, scoped to the publish job. - Set `publish = ["internal"]` on every internal package, and `publish = false` on tools and services that are never published. ## Reading the common failures | Message | Usual cause | |---|---| | "authenticated registries require a credential-provider to be configured" | registry needs a token for every request and no provider is set | | "no token found for `internal`, please run `cargo login --registry internal`" | a provider is configured, but neither the credentials file nor the variable holds a token | | "The registry `crates-io` is not listed in the `package.publish` value" | the guard working as intended | | "no matching package named ... location searched: crates.io index" | an internal dependency without a `registry` key | The first two look alike but point at different fixes: one is configuration, the other is a missing secret.
- What does `publish = ["internal"]` change for a bare `cargo publish`?Cargo notes that it found `internal` as the only allowed registry and publishes there automatically. Any other target, including `--registry crates-io`, is refused before packaging, which is why the key works as a guard for closed-source code.
- Why does `cargo publish --workspace` search crates.io for an internal sibling crate?Because the sibling dependency has `path` and `version` but no `registry` key. Packaging removes `path`, so the dependency defaults to crates.io and resolution fails there. Adding `registry = "internal"` to the dependency keeps it pointing at the private registry.
- Where does `cargo login` put a token with the `cargo:token` provider?In `credentials.toml` in Cargo's home directory, under `[registries.internal]`, in plain text. An operating-system provider such as `cargo:macos-keychain` keeps it out of that file, which is why the Cargo book recommends one for laptops.
- Setting CARGO_REGISTRIES_INTERNAL_TOKEN is always enough to authenticate
- CARGO_REGISTRY_TOKEN works for any alternative registry
- A path dependency publishes to the same registry as its parent
- cargo logout revokes the token on the registry
- crates.io accepts a crate that depends on a private registry
- publish = false is needed to choose a private registry