Use packages from Crates.io
Azure DevOps Services | 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.
This article will guide you through how to:
- Create an Azure Artifacts feed
- Connect to your feed
- Consume crates from upstream
Prerequisites
An Azure DevOps organization. Create an organization if you haven't already.
An Azure DevOps project. Create a project if you don't have one.
Download and install rustup.
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.
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Artifacts, and then select Create Feed.
Enter a Name for your feed, define its Visibility, and then select your Scope. Make sure to check the Upstream sources checkbox to include packages from public registries.
Select Create when you're done.
Connect to your feed
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Artifacts, and then select your feed from the dropdown menu.
Select Connect to feed, and then select Cargo from the left navigation pane.
If this is the first time using Cargo with Azure Artifacts, make sure you have installed rustup.
Add the provided snippet in the Project setup section to your .cargo/config.toml file in your source repository:
- 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>"
Configure a credential provider
To use Cargo with Azure Artifacts, you need to set up a credential provider. The provided settings will configure a default credential helper for the current user:
Paste the following snippet in your %USERPROFILE%.cargo\config.toml:
[registry]
global-credential-providers = ["cargo:token", "cargo:wincred"]
Log in to your registry
Create a Personal access token with Packaging > Read & write scopes to authenticate with your feed.
Run the following command to log in to your registry. Replace the placeholder with your feed's name, and paste the personal access token you created in the previous step when prompted:
"Basic " + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("PAT:" + (Read-Host -MaskInput "Enter PAT"))) | cargo login --registry <FEED_NAME>
Save packages from Crates.io
Note
To save packages from upstreams, you must have the Feed and Upstream Reader (Collaborator) role or higher. See Manage Permissions for more details.
Now that we have set up our project, configured a credential provider, and logged into our feed, we can begin consuming packages from upstream. Azure Artifacts saves a copy of any package you install from upstream to your feed.
In this example, we consume the serde
crate, a serialization/deserialization framework:
Run the following command in your project directory to add the crate to your cargo.toml:
cargo add serde
Run the following command to build your project and consume your crate:
cargo build
Once your package is installed, a copy will be saved to your feed. Navigate to your feed to verify its presence. Your package should be available in your feed, as shown below: