Skip to content

Caches

Gradient includes a own Nix binary cache. A cache stores build outputs so any machine with nix can substitute from it.

Projects subscribe to caches. Every build a project produces goes into each cache the project subscribed to.

flowchart LR
    project[Project] -- build outputs --> cache[(Cache)]
    cache -- pull-through --> upstream[(Upstream caches)]
    cache -- one URL, one key --> clients[nix clients]

Using a Cache

The cache page lists two values needed for Nix configuration: the substituter URL and the public key. Public caches accept any client. Private caches require credentials. See Authentication for details.

Each cache sends a priority to nix. The default value is 10, and lower numbers take precedence. A cache can also send a separate priority to machines on the local network. Those machines then prefer the Gradient cache over remote caches.

Signing Key

Caches sign their store paths with a single key. To create a new key pair, set Rotate Key in the cache configuration. New key pairs require manageCacheKeys permission and fail for caches from declarative state.

Key rotations reuse the key name, so trusted-public-keys can have one entry per cache. Store paths carry both the previous key signature and the new key signature. Both signatures appear in the .narinfo response. Clients that trust only the old key keep working until the next rotation. No timers end that grace period. Add the new public key to all clients before the next rotation.

The current public key and its start date appear on the Settings page. Retired keys appear with their dates.

Upstream Types

Find upstream caches under Settings -> Upstream Caches on the cache page.

Type Upstream Cache Modes
Internal Another cache on the same Gradient instance Read & Write, Read Only, Write Only
Gradient Proto A cache on another Gradient instance, reached over that instance's cache protocol Read & Write, Read Only, Write Only
HTTP Any Nix binary cache, e.g. cache.nixos.org Read Only

The modes control traffic direction:

  • Read & Write: pull through and push results upstream.
  • Read Only: pull through only.
  • Write Only: push only.

Caches declared in services.gradient.state accept Internal and HTTP upstream caches (external in Nix). Configure Gradient Proto upstream caches in the UI.

Add an upstream cache through the UI or API. Set the URL to a public address. Gradient rejects localhost, loopback, private, link-local, and addresses for cloud metadata at that URL. Declare the upstream cache in services.gradient.state to use the cache on an internal network.

Use Deactivate to disable an upstream cache while keeping the entry. Use Activate to switch it back on. Inactive upstream caches keep their settings. Gradient never queries an inactive upstream cache. Paths then come from the remaining upstream caches. Declared caches also accept Deactivate and Activate. The next server start will restore the declared active value.

Use Test on an HTTP upstream cache to fetch its nix-cache-info over HTTP/1.1 and over HTTP/2. Both results appear in a notification.

Some HTTP upstream caches break HTTP/2 transfers. Enable HTTP/1.1 only in the add or edit form for such an upstream cache. Declared caches take the value from http1_only. These upstream caches show an HTTP/1.1 badge.

Upstream Credentials

An HTTP upstream cache holds at most one stored credential. Stored credentials always stay encrypted at rest.

Scheme Field Declared field
basic user, password basic_auth_user, basic_auth_password_file
bearer token bearer_token_file
{
  "type": "http",
  "display_name": "Company cache",
  "url": "https://cache.example.com",
  "public_key": "cache.example.com-1:<public key>",
  "credential": { "scheme": "bearer", "token": "<token>" }
}
  • Add and edit requests accept a credential in the credential field.
  • Edits that use "credential": { "scheme": "none" } delete stored credentials.
  • Declared caches get credentials from fields in upstream_caches. The server reads the files again on each state apply.
  • Credentials are write-only. API responses contain only credential_scheme: basic, bearer, or null.
  • Credentials need a https:// URL.
  • URL changes to another scheme, host, or port need the same edit to resend the credentials or remove them.
  • Upstream URLs don't allow a username, password, or query. Add, edit, and state apply reject such URLs.
  • Workers never receive an upstream cache with credentials. Only the server can query such an upstream cache, for example pull-through and Test.

Previous releases stored the username and password in the URL. The server changes these stored URLs once at startup.

  • The username and password disappear from HTTP URLs. Both move to encrypted basic credentials.
  • Moved http:// URLs keep working. The server logs contain one warning.
  • Stored URLs that contain a query go inactive.
  • Upstream caches with stored credentials and a username in the URL become inactive.
  • Gradient Proto URLs that contain a user name or password become inactive.
  • Warnings in the server log show only the upstream cache ID.
  • An upstream cache with this URL can't become active. Correct the URL first.

Pull-Through

Pull-through lets a cache serve paths it does not store. On a request for a missing path, the cache fetched the copy from an upstream cache, re-signed it with its own key, and sent it to the client. Clients therefore needed only one URL and one key, no matter where a path came from.

To disable pull-through, toggle Pull-Through under Settings on the cache page. A cache with pull-through off served only the paths it held. Builds could still substitute from the upstream caches.

Substitution

Gradient will decide per derivation whether a build is needed at all, before building anything.

  1. Outputs present in a cache on the instance require no work.
  2. For outputs absent on the instance, check the upstream caches of the subscribed caches.
  3. Builds with every needed output found count as substituted. Workers download those outputs. Builds and downloads below the derivation never happen.
  4. Needed outputs cover every output of an entry point. They also cover each output a wanted build above the derivation can read. No cache copy and no reader leave an output out of the list. One case: the dev output of a nix copy upload.
  5. Derivations with a missing needed output land on a worker for building. Their inputs pass through the same check. A left-out output read by a later build can turn the derivation into a real build.

Lookups only cover derivations an evaluation actually needs. Unresponsive upstream caches pause for a minute instead of slowing every lookup.

Sharing

Projects subscribe to a cache for pushing outputs there and substituting from there. Subscribing needs rights on both sides. Subscriptions without cache rights turn into a request. A cache admin can approve the request under Subscriptions on the cache page.

Roles

Role Access
Admin Full control: settings, members, roles and subscriptions
Write Read access plus path uploads
View Cache visibility plus path downloads

Members and Roles explains how to build custom roles from individual permissions.