# Package registries Every user and organization has a package registry on this forge. It speaks the Cargo registry protocol (the sparse index), so `cargo` can publish to it, install from it and yank versions, and the OCI container protocol, so `docker` can push and pull images (see [Container images](#container-images)). Packages show on the owner's profile under the **Packages** tab, and on the package page with its README, versions and an install snippet. The registry of an owner is named after the owner. Below, `tahinli` is the owner and `git.tahinli.com` is the forge. ## Use a registry in Cargo Add the registry to `~/.cargo/config.toml` (or to a project's `.cargo/config.toml`): ```toml [registries.tahinli] index = "sparse+https://git.tahinli.com/api/packages/tahinli/cargo/" ``` Then add a crate from it: ```sh cargo add --registry tahinli my-crate ``` Or refer to it in `Cargo.toml`: ```toml [dependencies] my-crate = { version = "0.1", registry = "tahinli" } ``` Packages marked private need a token for every request to that registry, so Cargo asks for credentials when it reads the index (see below). ## Tokens Create a personal access token under **Settings, Tokens** and tick **Packages**. A token with the `packages` scope can read private packages in the registries of the owners it may act for, publish, yank and unyank. A token without it is treated as anonymous for public reads and is refused for writes. Log in once on your machine: ```sh cargo login --registry tahinli ``` Cargo asks for the token and stores it in `~/.cargo/credentials.toml`. Cargo sends the bare token in the `Authorization` header, which is what this forge expects. ## Publish Set the registry in the crate's `Cargo.toml` so that `cargo publish` does not go to crates.io: ```toml [package] name = "my-crate" version = "0.1.0" repository = "https://git.tahinli.com/tahinli/my-crate" # links the package to this repo publish = ["tahinli"] ``` Then publish: ```sh cargo publish --registry tahinli ``` Rules: - The crate name starts with a letter and holds letters, digits, `-` and `_`, at most 64 characters. Names are case-insensitive in the registry: `My_Crate` and `my_crate` are the same package. - The version must be valid semver, and it can be published only once. A second publish of the same version fails with `409 Conflict` and the message `crate version ... is already uploaded`. - A `.crate` file is at most 10 MiB. - Only the owner, the instance admin, or an owner of an organization may publish. Members of an organization without the owner role may read but not publish. - The package links to a repository when its `repository` URL points to a repo of the same owner on this forge. The package then shows on that repo's page. Links to other owners' repos are ignored. - The README is read from the `.crate` (`README*` at the top of the package directory), or from the `readme` field of the publish metadata when the file is missing. ## Yank ```sh cargo yank --registry tahinli --version 0.1.0 my-crate cargo yank --registry tahinli --version 0.1.0 --undo my-crate ``` A yanked version stays in the index and can still be downloaded, so existing `Cargo.lock` files keep working. Cargo stops choosing it for new builds. ## Continuous integration Create a token with the `packages` scope and store it as a secret of the CI system, for example `FORGE_PACKAGES_TOKEN`. Then pass it to Cargo through an environment variable, which Cargo reads for a registry named `tahinli`: ```yaml env: CARGO_REGISTRIES_TAHINLI_INDEX: sparse+https://git.tahinli.com/api/packages/tahinli/cargo/ CARGO_REGISTRIES_TAHINLI_TOKEN: ${{ secrets.FORGE_PACKAGES_TOKEN }} steps: - run: cargo publish --registry tahinli ``` Keep the token out of the repository. Use a dedicated token for CI, and revoke it when the job is no longer needed. ## Private packages A package is public unless it is marked private. Public packages install without a token. When an owner has any private package, the registry's `config.json` sets `auth-required`, and Cargo then sends the token for every request to that registry, so a token is needed for public packages too. ## Container images Every owner also has a container registry. It speaks the OCI Distribution protocol (v1.1) under `/v2/`, so `docker`, `podman` and other OCI clients push and pull images with no extra setup. Container images show in the owner's **Packages** tab with a `container` badge, next to the Cargo crates. ### Log in, push and pull Log in with your username and a personal access token that has the **packages** scope: ```sh docker login git.tahinli.com ``` Tag an image with the owner and a name below the owner, then push it: ```sh docker tag myapp:latest git.tahinli.com/tahinli/myapp:1.0.0 docker push git.tahinli.com/tahinli/myapp:1.0.0 ``` A name may have several parts, such as `tahinli/tools/cli`. Pull a public image with no login: ```sh docker pull git.tahinli.com/tahinli/myapp:1.0.0 ``` Pulls of a public image need no token. A private image needs a token with the packages scope, and a pull of a private image without one is answered with `401` and a `WWW-Authenticate` challenge. Pushing needs write access to the owner: the owner, or a member of the organization with write access. The web page of an image shows the pull command, the tags with their digest, size and date, and the delete control for tags. ### Token endpoint Clients that use the Bearer challenge get their token from `/v2/token`. The request uses HTTP Basic with the username and the token. The response is `{"token": ..., "access_token": ..., "expires_in": 300, ...}`, and the token is good for five minutes. Basic auth also works on each `/v2/` request directly, so simple clients need no token step. ### Deleting Deleting a tag, with the owner's delete control or `DELETE /v2/{name}/manifests/{tag}`, removes the tag. A manifest that no tag names any more, and that no index names, is removed as well. Deleting by digest removes the manifest and all its tags. Blobs that no image uses are then swept from the store. The sweep runs on the request that deleted something, under a lock, so the registry has no background timer. A push that is still in flight when a sweep runs can lose its blobs; the client then gets `MANIFEST_BLOB_UNKNOWN` and pushes again. ### Proxy in front of the forge Blobs are large, so the reverse proxy in front of the forge must accept request bodies of gigabytes and must not buffer request bodies. Route `/v2/` to the forge with a large body size limit and with request buffering turned off. A proxy that buffers streams every layer to disk before forwarding, and a small limit rejects the first layer. ### Limits and storage Blobs are stored once per digest under the blob store (`oci/`), however many images use them. Upload sessions are temporary files under the data directory and are removed when the upload completes. A session left behind by a crash is removed by the next upload, once it is older than a day. Manifests and tags are in the database. A manifest is at most 4 MiB, and a blob at most 10 GiB. Not supported yet: the referrers API (`/v2/{name}/referrers/{digest}`). Blobs can be mounted from one image into another, so a shared base layer is uploaded once. ## What is not supported yet - `cargo search` and the search endpoint. - Adding or removing owners with `cargo owner`. The owners list (`cargo owner --list`) works and shows the owner, or the owners of an organization. - Managing package visibility from the web page. ## Storage Each `.crate` file is stored with the other release and artifact blobs (see `[storage]` in [backup.md](backup.md)), under `packages/`. The metadata, the index lines, the checksums and the READMEs are in the database, so the index is served without reading any file. Container images are rows of the same packages table with the kind `oci`. Their tags, manifests and blob links are in their own tables, and their blob bytes are stored under `oci/`. ## Protocol endpoints All paths are under `/api/packages/{owner}/cargo/`: | Method | Path | Purpose | | --- | --- | --- | | GET | `config.json` | Registry config: `dl`, `api` and `auth-required`. | | GET | `1/{name}`, `2/{name}`, `{ab}/{cd}/{name}` | Sparse index file of a crate. | | GET | `download/{name}/{version}` | The `.crate` file. | | PUT | `api/v1/crates/new` | Publish. The body is the length-prefixed JSON metadata and `.crate`. | | DELETE | `api/v1/crates/{name}/{version}/yank` | Yank a version. | | PUT | `api/v1/crates/{name}/{version}/unyank` | Unyank a version. | | GET | `api/v1/crates/{name}/owners` | The people who may publish. | Errors use the body Cargo expects: `{"errors":[{"detail":"..."}]}`.