Configure Cargo to use the Varnish Virtual Registry

This tutorial points cargo at a Virtual Registry that caches crates.io.

Prerequisites

  • Rust and cargo are installed.
  • The Virtual Registry is installed.

Configure the registry

virtual_registry:
  registries:
  - name: cargo
    remotes:
    - url: https://index.crates.io

The examples below reach the registry as https://cargo.example.com, a host that the registry named cargo answers (see name).

The remote is the sparse index. Crates download from another host, static.crates.io, and the registry rewrites the download URL in the index’s config.json so that they come through it too. With deny_unknown_hosts set, that host has to be listed:

virtual_registry:
  redirects:
    deny_unknown_hosts: true
    extra_known_hosts:
    - static.crates.io

Package types describes how the index and the crates are cached.

Point cargo at the registry

Replace crates.io with the registry in $CARGO_HOME/config.toml, or in .cargo/config.toml inside a project:

[source.crates-io]
replace-with = "virtual-registry"

[source.virtual-registry]
registry = "sparse+https://cargo.example.com/"

CARGO_HOME is ~/.cargo unless it is set, and the rust container image sets it to /usr/local/cargo. Keep the sparse+ prefix. Without it, cargo reads the URL as a Git index.

Builds now resolve and download through the registry:

cargo fetch

®Varnish Software, Wallingatan 12, 111 60 Stockholm, Organization nr. 556805-6203