Declarative State¶
Gradient supports declaring State in Nix under services.gradient.state. Each startup applies this state to the server. Entities that come from Nix appear as read-only in the UI, so the Nix configuration acts as the single source of truth.
flowchart LR
nix[NixOS configuration] -- validated at build time --> server[Gradient]
server -- export --> nix
Declarable Entities¶
| Entity | Content |
|---|---|
| Users | Accounts with a password or OIDC-only login, plus superusers |
| Projects | Member lists, an SSH key, workers, and cache subscriptions |
| Tasks | Repository, wildcard, triggers, actions, and concurrency settings |
| Connections to Git Hosts | Connections to a Git host for incoming events and outgoing status updates |
| Caches | Signing key, upstream caches, and members |
| Roles | Custom roles for projects and caches |
| Teams | Members, OIDC and SCIM groups, and grants on projects and caches |
| API Keys | Scoped keys, with the token hash read from a secret file |
| Workers | Workers at the project level and the team level |
UI-Managed and Nix-Managed¶
| Created in the UI | Declared in Nix | |
|---|---|---|
| Editing | Through the UI and the API | Through Nix alone. The UI shows the controls but keeps them disabled |
| Removal | Via the delete button | By dropping the entry from Nix, after which the next start deletes it |
| Validation | At save time | During the NixOS configuration build |
| Activate / Deactivate | Through the UI and the API | Through the UI as well, for caches, upstream caches and workers. The declared value comes back on the next start |
Both kinds coexist. A declared project can contain tasks created in the UI.
Validation¶
State declarations can be checked while the NixOS configuration builds. Enable this through services.gradient.state.validate, which is on by default. The check catches unknown users or projects, duplicate ids, and broken references. Errors surface at build time rather than at server start.
Secrets¶
The Nix store never uses secret material. Each *_file option points to a path on the server, commonly managed with sops-nix or agenix. The server loads these files at startup.
| Option | Content | Generate |
|---|---|---|
users.<name>.password_file |
Argon2id hash of the password | gradient hash > alice-password |
projects.<name>.private_key_file |
SSH private key for cloning | ssh-keygen -t ed25519 -N "" -C gradient-acme -f acme-ssh-key |
projects.<name>.git_https_credentials.*.password_file |
Password or access token for Git over HTTPS | From the Git host |
caches.<name>.signing_key_file |
Nix signing key, base64 only | nix-store --generate-binary-cache-key main main-key main-key.pub, then sed -i 's/^[^:]*://' main-key |
api_keys.<name>.key_file |
SHA-256 hex digest of the token, without GRAD |
See API Key Files |
workers.<name>.token_file |
Worker registration token | openssl rand -base64 48 |
git_host_connections.<name>.secret_file |
Webhook secret shared with the Git host | openssl rand -hex 32 |
git_host_connections.<name>.access_token_file |
Git host access token | From the Git host |
- Run
gradient hashto enter the password twice and get the hash. Piping a password through<<<puts a trailing newline into the hash, and later sign-ins then fail. - Upload
acme-ssh-key.pubto the Git host as the deploy key. nix-storewrites the key asmain:<key>. Store only the key in the file, without themain:prefix. Gradient can derive the public key itself.- Users without
password_filecan sign in only through an external login provider such as OIDC. Servers link that account to the provider's ID on first sign-in only when the provider reportsemailas a verified email address. Servers skip a declared user when another method (registration, SCIM, external sign-in) created an account with that username or email address. They log an error. They skip the memberships for that user too. Setexisting_account_idto intentionally adopt such an account. Servers remove the password, sessions, undeclared API keys, external ID links, and SCIM external ID of that account with this setting.
API Key Files¶
The server stores only a digest of each API key. Put that digest in key_file. Clients send the token with a GRAD prefix.
Keys with project = null can also list cache_permissions such as readStore. Those keys then receive these permissions on all caches where their owners are members. Examples include fetching from a private cache through the netrc file.
The command outputs two values: the API token for clients (Authorization: Bearer GRAD...) and the key_file digest for the key_file.
Without the CLI
TOKEN=$(openssl rand -hex 32)
printf %s "$TOKEN" | sha256sum | cut -d' ' -f1 > ci-key # (1)!
echo "GRAD$TOKEN"
printf %sexcludes the trailing newline from the digest.
Rename¶
State entries match database rows by name. Entries with a changed name and no id delete the old entity and create a new one. Caches lose all stored paths in that case.
Projects, caches and tasks accept an id with the UUID of an existing entity. Find that UUID in the danger zone of the settings page. That UUID is in the GET /api/v1/admin/state export. Use an id with a changed name to rename the entity in place.
services.gradient.state.caches.some-cache = {
id = "bae05ff2-30a1-40b3-993a-7e16d4fcd83c"; # (1)!
created_by = "alice";
signing_key_file = "/run/secrets/main-cache-key";
};
-
The UUID of a cache that previously had the name
main. -
A new UUID from
uuidgencan only match new entities. A new UUID and a newnametogether create a new entity and delete the old entity. - The cache URL and the signing key name for the cache change with the cache name. Update the substituter URL and
trusted-public-keysin Nix clients. - Tasks can't move to another project.
- Rename controls for entities that the UI created sit in the Danger Zone on the settings page.
Removal¶
The option services.gradient.state.delete controls deletion of users, projects and caches that no longer appear in the config. That option is on by default. Disable that option and these entities remain in the database, editable in the UI. Deleted users become inactive and lose superuser status. Always delete API keys removed from the config.
Export¶
A GET /api/v1/admin/state request returns the live configuration in the services.gradient.state format. This output can serve as a starting point for a Nix setup built through the UI.
Related¶
- State reference: all options, each with examples
- Projects and Tasks: definitions of these entities