Edit

Share via


Use Cargo packages from Crates.io

Azure DevOps Services | Azure DevOps Server | Azure DevOps Server 2022

Azure Artifacts upstream sources enable developers to consume packages from public registries like Crates.io and nuget.org. This article will guide you through setting up your project and using the command line to consume Crates from Crates.io.

Prerequisites

Product Requirements
Azure DevOps - An Azure DevOps organization.
- An Azure DevOps project.
- Download and install rustup. You'll need Cargo version 1.74.0 or later.

Create a feed

Azure Artifacts recommends having a dedicated feed for consuming crates from crates.io and a separate feed exclusively for publishing internal crates. If you already have an Azure Artifacts feed, skip to the next section. Otherwise, create a new one as follows:

  1. Sign in to your Azure DevOps organization, then navigate to your project.

  2. Select Artifacts, and then select Create Feed.

  3. Provide a Name for your feed, select the Visibility to define who can view your packages, and choose the feed Scope. Check the Include packages from common public sources checkbox to enable upstream sources on your feed.

  4. Select Create when you're done.

    A screenshot displaying how to create a feed to consume crates from Crates.io upstream.

Connect to your feed

Now that you have a feed, you need to set up your config.toml file, configure a credential provider, then log in to the registry to authenticate with your feed.

Project setup

  1. Sign in to your Azure DevOps organization, then navigate to your project.

  2. Select Artifacts, then select your feed from the dropdown menu.

  3. Select Connect to feed, then select Cargo from the left navigation pane.

  4. Copy the snippet from the Project setup section and paste it into your .cargo/config.toml file in your source repository. It should look similar to the following:

    • Project-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      
    • Organization-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      
  1. Sign in to your Azure DevOps collection, then navigate to your project.

  2. Select Artifacts, then select your feed from the dropdown menu.

  3. Select Connect to feed, then select Cargo from the left navigation pane.

  4. Copy the snippet from the Project setup section and paste it into your .cargo/config.toml file in your source repository. It should look similar to:

    • Project-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<COLLECTION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      
    • Collection-scoped feed:

      [registries]
      <FEED_NAME> = { index = "sparse+https://pkgs.dev.azure.com/<COLLECTION_NAME>/_packaging/<FEED_NAME>/Cargo/index/" }
      
      [source.crates-io]
      replace-with = "<FEED_NAME>"
      

Configure a credential provider

To use Cargo with Azure Artifacts, you need to set up a credential provider. The following settings will set a default credential helper for the user:

Paste the following snippet in your %USERPROFILE%.cargo\config.toml:

[registry]
global-credential-providers = ["cargo:token", "cargo:wincred"]

Log in to your registry

  1. Create a Personal access token with Packaging > Read & write scopes to authenticate with your feed.

  2. Run the following command to log in to your registry. Replace <FEED_NAME> with your feed name, and when promoted, paste the personal access token you created earlier.

    "Basic " + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("PAT:" + (Read-Host -MaskInput "Enter PAT"))) | cargo login --registry <FEED_NAME>
    

Save packages from Crates.io

Now that your project is set up, the credential provider is configured, and you're authenticated with your feed, you can begin consuming packages from upstream sources. Azure Artifacts automatically saves a copy of any package you install from upstream to your feed.

The following example demonstrates how to consume the reqwest crate, an HTTP client library used for making HTTP requests:

  1. Add the crate to your Cargo.toml by running the following command in your project directory:

    cargo add reqwest
    
  2. Run the following command to build your project and consume your crate:

    cargo build
    

Once the package is installed, a copy will be saved to your feed. You can verify this by navigating to your feed in Azure DevOps. The package should be available in your feed, as shown below:

A screenshot displaying the reqwest crate listed in an Azure Artifacts feed after being saved from Crates.io.

Note

To save packages from upstreams, you must have the Feed and Upstream Reader (Collaborator) role or higher. See Feed roles and permissions for more details.