//python/uv:repositories.bzl

Repository rules and macros to set up uv in a WORKSPACE file.

uv_register_toolchains(name='uv', manifests=None, version=None, configure=None, netrc=None, auth_patterns=None)

Create the uv toolchain repositories and register them in a WORKSPACE file.

This is the WORKSPACE equivalent of the uv module extension. It creates one download repository per uv version and platform, and the toolchain hub repository name with a toolchain for each of them, which it registers as @{name}//:all; lock can then be used, and uv can be run directly via bazel run @{name}//:uv -- <args>. See the WORKSPACE setup guide for usage examples.

Platforms without an archive in a manifest get no toolchain. The configure entries are dicts with the attributes of uv.configure; refer to the uv.configure documentation for how entries are applied.

Note

A Python toolchain must be registered as well, e.g. via python_register_toolchains, because lock uses the Python exec tools toolchain to run uv with a hermetic interpreter.

Note

compatible_with and target_settings are written into the hub repository’s BUILD file as-is, so plain strings resolve relative to @{name}; pass Label objects or @//-qualified strings for targets in your own repository.

Added in version 2.5.0.

Args:
  • name – (str) (default “uv”)

    the name of the toolchain hub repository. The download repositories are named {name}_{version}_{platform} with . and - replaced by _.

  • manifests – (dict[str, dict] | None) (default None)

    the download urls and sha256 of the archives, keyed by version and then by platform ({version: {platform: {"urls": [...], "sha256": "..."}}}), as written to dist_manifest.bzl by uv_toolchains_manifest, which binds its own when uv_register_toolchains is loaded from there.

  • version – (str | None) (default None)

    the uv version for configure entries that do not name one. Defaults to the first version of manifests, or to the version the uv extension uses by default when there are none.

  • configure – (list[dict] | None) (default None)

    the entries to apply, each a dict with a subset of the uv.configure attributes: version, platform, compatible_with, target_settings, urls, sha256, netrc and auth_patterns. Defaults to one empty entry per version of manifests, i.e. every version with the default platforms, or to a single entry for version.

  • netrc – (str | None) (default None)

    the default location of the .netrc file to use for authenticating the archive downloads.

  • auth_patterns – (dict[str, str] | None) (default None)

    the default dict mapping host names to custom authorization patterns for the archive downloads, as understood by http_archive.

repo rule uv_toolchains_manifest(name, auth_patterns={}, base_url='https://github.com/astral-sh/uv/releases/download', manifest_filename='dist-manifest.json', netrc='', versions=['0.13.5'])

Fetch the release manifests of uv versions in a WORKSPACE file.

The repository downloads the manifest of every version in versions and writes dist_manifest.bzl, which has the download URLs and checksums of every archive in them as DIST_MANIFESTS, and uv_register_toolchains bound to them; load that to create and register the toolchains:

load("@rules_python//python/uv:repositories.bzl", "uv_toolchains_manifest")

uv_toolchains_manifest(
    name = "uv_manifest",
    versions = ["0.13.5"],
)

load("@uv_manifest//:dist_manifest.bzl", "uv_register_toolchains")

uv_register_toolchains(name = "uv")

The manifests are read as-is: which platforms get a toolchain, and from which archives, is decided by uv_register_toolchains. Versions released from different places can be fetched with one repository each and combined with the manifests argument of uv_register_toolchains.

Added in version 2.5.0.

Attributes:
  • name – (Name)

    A unique name for this repository.

    mandatory

  • auth_patterns – (dict[str, str]) (default {})

    An optional dict mapping host names to custom authorization patterns.

    If a URL’s host name is present in this dict the value will be used as a pattern when generating the authorization header for the http request. This enables the use of custom authorization schemes used in a lot of common cloud storage providers.

    The pattern currently supports 2 tokens: <login> and <password>, which are replaced with their equivalent value in the netrc file for the same host name. After formatting, the result is set as the value for the Authorization field of the HTTP request.

    Example attribute and netrc for a http download to an oauth2 enabled API using a bearer token:

    auth_patterns = {
        "storage.cloudprovider.com": "Bearer <password>"
    }
    

    netrc:

    machine storage.cloudprovider.com
            password RANDOM-TOKEN
    

    The final HTTP request would have the following header:

    Authorization: Bearer RANDOM-TOKEN
    

    optional

  • base_url – (str) (default “https://github.com/astral-sh/uv/releases/download”)

    The base URL of the uv releases; the manifest of a version is downloaded from {base_url}/{version}/{manifest_filename}. Defaults to the GitHub releases of uv.

    optional

  • manifest_filename – (str) (default “dist-manifest.json”)

    The release manifest file name.

    optional

  • netrc – (str) (default “”)

    Location of the .netrc file to use for authentication

    optional

  • versions – (list[str]) (default [“0.13.5”])

    The uv versions to fetch the manifests of. Defaults to the version the uv extension uses by default.

    optional