Cargo Registry

Cloudsmith provides public & private registries for Cargo (Rust).

For more information on Rust, please see:

  • Rust: The official website for Rust language
  • Cargo: The official documentation for Cargo - the Rust package manager
  • Crates Public Registry: The official Rust community's crate registry

Contextual Documentation

The examples in this document are generic. Cloudsmith provides contextual setup instructions within each repository, complete with copy and paste snippets (with your namespace/repo/rsa-key pre-configured).

In the following examples:

IdentifierDescription
OWNERYour Cloudsmith account name or organization name (namespace)
REPOSITORYYour Cloudsmith Repository name (also called "slug")
TOKENYour Cloudsmith Entitlement Token (see Entitlements for more details)
USERNAMEYour Cloudsmith username
PASSWORDYour Cloudsmith password
API-KEYYour Cloudsmith API Key
REGISTRY_NAMEA name for the Cargo registry
PACKAGE_NAMEThe name of your package
PACKAGE_VERSIONThe version number of your package
PROJECT_NAMEThe name of your Rust project

Upload a Package

Upload via Cargo Publish

A name and URL for the registry must be added to your .cargo/config file as follows:

Public Repositories

toml
[registries]
REGISTRY_NAME = { index = "https://dl.cloudsmith.io/public/OWNER/REPOSITORY/cargo/index.git" }

Private Repositories

Private Repositories

Private Cloudsmith repositories require authentication. You can choose between two types of authentication, Entitlement Token Authentication or HTTP Basic Authentication. The setup method will differ depending on what authentication type you choose to use.

Warning

Entitlement Tokens, User Credentials and API-Keys should be treated as secrets, and you should ensure that you do not commit them in configurations files along with source code, or expose them in any logs.

toml
# Entitlement Token Auth
[registries]
REGISTRY_NAME = { index = "https://dl.cloudsmith.io/TOKEN/OWNER/REPOSITORY/cargo/index.git" }
toml
# HTTP Basic Auth
[registries]
REGISTRY_NAME = { index = "https://dl.cloudsmith.io/basic/OWNER/REPOSITORY/cargo/index.git" }

If using HTTP basic authentication, you'll need to configure Git with credentials. Git's standard authentication mechanisms are used by Cargo and can be configured in the normal way. For example, you could use git's per-user credential store as follows:

shell
# HTTP Basic Auth (User & Pass)
git config --global credential.helper store
echo "https://USERNAME:PASSWORD@dl.cloudsmith.io" > ~/.git-credentials
shell
# HTTP Basic Auth (API-Key)
git config --global credential.helper store
echo "https://USERNAME:API-KEY@dl.cloudsmith.io" > ~/.git-credentials
shell
# HTTP Basic Auth (Token)
git config --global credential.helper store
echo "https://token:TOKEN@dl.cloudsmith.io" > ~/.git-credentials

For further details or other configuration options, see the Official Git Documentation.

In order to authenticate for publishing via cargo, you can either enter your credentials using the command:

shell
cargo login --registry REGISTRY_NAME

Or, add your credentials to your .cargo/credentials file:

toml
[registries.REGISTRY_NAME]
token = API-KEY

Publish

To publish a crate, you can do so from your project directory using cargo publish as follows:

shell
cargo publish --registry REGISTRY_NAME

If you haven't specified credentials using one of the methods above, you'll be asked to provide them using cargo login.

Instead of usign the previous authentication methods, you can also set the following environment variables to define your index and token:

  • CARGO_REGISTRIES_<REGISTRY_NAME>_INDEX
  • CARGO_REGISTRIES_<REGISTRY_NAME>_TOKEN

Remember to rename the variable with your registry name.

Upload via the Cloudsmith CLI

For full details of how to install and setup the Cloudsmith CLI, see Command Line Interface.

The command to upload a cargo crate via the Cloudsmith CLI is:

shell
cloudsmith push cargo OWNER/REPOSITORY PACKAGE_NAME.crate

Example:

shell
cloudsmith push cargo org/repo your-package.crate

Upload via Cloudsmith Website

Please see Upload a Package for details of how to upload via the Cloudsmith web app.

Download a Package

Configure a new Upstream

Cloudsmith supports https://index.crates.io as an upstream. This allows you to proxy and cache external Rust crates that are not part of your repository.

To enable the upstream, browse to your repository overview page and click in the Upstreams tab. Then, click on + Add Upstream Proxy and then choose the Cargo Upstream.

In the Upstream creation menu, define a name your Upstream and in the Proxy URL field insert https://index.crates.io/. Click on + Create Upstream Proxy and the upstream should become immediately available.

Registry Setup

It is easy to add a Cloudsmith-based Cargo registry.

Cargo Sparse Registry

Cargo Sparse Registries are a new addition to Cargo as of v1.68.0, and are the recommended way to interact with your Cloudsmith Repositories - they offer significant performance advantages over the old Git-based registries, such as reducing the bandwidth used and improving dependency resolution times.

First, the name and config for the registry must be added to your .cargo/config.toml or .cargo/config file as follows:

Cargo >= v1.74 (HTTP Sparse Registry)

toml
[registries.OWNER-REPOSITORY]
index = "sparse+https://cargo.cloudsmith.io/OWNER/REPOSITORY/"
token = "Token API-KEY"
credential-provider = "cargo:token"
toml
[registries.OWNER-REPOSITORY]
index = "sparse+https://cargo.cloudsmith.io/OWNER/REPOSITORY/"
token = "Token TOKEN"
credential-provider = "cargo:token"

config.toml

Once you’ve configured your credentials in the config.toml file, there’s no need to run cargo login. Cargo will automatically authenticate using the token from the configuration.

Cargo < v1.74 (HTTP Sparse Registry)

If you are using Cargo version < 1.74, the only way to authenticate with a private sparse registry is using Cloudsmith's URL-based authentication.

toml
[registries.OWNER-REPOSITORY]
index = "sparse+https://dl.cloudsmith.io/TOKEN/OWNER/REPOSITORY/cargo/"

Cargo < v1.68 (Legacy Git Registry)

toml
[registries]
OWNER-REPOSITORY = { index = "https://dl.cloudsmith.io/basic/OWNER/REPOSITORY/cargo/index.git" }
toml
[registries]
OWNER-REPOSITORY = { index = "https://dl.cloudsmith.io/TOKEN/OWNER/REPOSITORY/cargo/index.git" }

Registry Authentication

You must configure Git with the proper credentials to clone the registry when using HTTP basic authentication. Git's standard authentication mechanisms are used by Cargo and can be configured normally. For example, you could use git's per-user credential store like so:

shell
git config --global credential.helper store
echo "https://USERNAME:API-KEY@dl.cloudsmith.io" > ~/.git-credentials

When using URL-based authentication, no further configuration is required, you're all set up and ready to go.

Install a Package

Once you have configured a registry using one of the methods described above, a crate can then depend on a crate from your registry by specifying the registry key and a value of the registry's name in that dependency's entry in Cargo.toml:

toml
[package]
name = "PROJECT_NAME"
version = "0.1.0"
[dependencies]
PACKAGE_NAME = { version = "PACKAGE_VERSION", registry = "REGISTRY_NAME" }

You can also install a crate directly by specifying the registry on the command line:

shell
cargo install PACKAGE_NAME --registry REGISTRY_NAME`

Security Scanning

Supported. Please see our Security Scanning documentation for further information.

Key Signing Support

Not Supported.

Known Limitations

The cargo search command is not supported.

Troubleshooting

Q. It's not working...
Cargo's cache can become corrupted. If this happens, you can often fix the issue by manually deleting the cache directories. The folders to remove are:

  • ~/.cargo/registry
  • ~/.cargo/git

If you previously set an override for $CARGO_HOME, the cache may be in a non-standard location.

Still Need Help?

Contact us here. We're always happy to help!