Skip to content

Add a Remote Worker

A remote worker is an extra machine that can evaluate and build for a project. It can run alongside the local worker or take over from it.

Requirements:

  • A Mac with nix-darwin installed, or a NixOS machine with network access to the Gradient server
  • A project with a cache subscription, see First Project

1. Register the Worker

Open Settings -> Workers -> Register Worker in the project. Enter a name. Select capabilities. The server can generate a worker ID and token.

Both items appear once on the result screen - the NixOS configuration for the worker machine and the peers line. Save the peers line as a secret on the worker machine. Store the peers line in `/run/secrets/gradient-worker-peers`, for example.
gradient worker register --display-name builder-1

The same NixOS configuration and peers line come from the output of this command. Team admins register team workers with gradient team worker register <TEAM> --display-name builder-1.

Configure the server with:

services.gradient.state = {
  projects.acme.id = "<project uuid>"; # (1)!
  workers.builder-1 = {
    worker_id = "<worker UUID, for example from cat /proc/sys/kernel/random/uuid>";
    projects = [ "acme" ];
    token_file = "/run/secrets/builder-1-token"; # (2)!
  };
};
  1. The peers file for the worker references this ID. Add the project to the peers file before the server's first start.
  2. Generate the token with openssl rand -base64 48. Put the same token in the worker's peers file.

On the worker machine, write the peers file entry as <project uuid>:<token>.

2. Open the Server to Workers

Remote workers use a WebSocket at /proto to reach the server. The bundled nginx forwards /proto only when proto.public is enabled.

# configuration.nix on the server
services.gradient.proto.public = true;

Without this setting, the local worker can still connect on the loopback address.

3. Configure the Machine

Paste the NixOS configuration from step 1. The configuration looks like this.

# configuration.nix on the worker machine
{
  imports = [ inputs.gradient.nixosModules.default ];

  services.gradient.worker = {
    enable = true;
    id = "<worker id from step 1>";
    serverUrl = "wss://gradient.example.com/proto";
    peersFile = "/run/secrets/gradient-worker-peers"; # (1)!
    build.metrics = true; # (2)!
  };
}
  1. This file holds one line per project on this worker. See Peers File.
  2. Optional. With this enabled, the worker records memory and CPU usage for each build. The scheduler can use those numbers to place heavy builds on machines with matching capacity.
# darwin-configuration.nix on the worker machine
{
  imports = [ inputs.gradient.darwinModules.default ];

  services.gradient.worker = {
    enable = true;
    id = "<worker id from step 1>";
    serverUrl = "wss://gradient.example.com/proto";
    peersFile = "/var/lib/gradient-worker/peers"; # (1)!
  };
}
  1. Give the _gradient-worker user read access to the file. The owner = "_gradient-worker" setting in sops-nix or agenix can assign the owner. The worker can open the file directly. No systemd credentials exist on macOS.

Verify Deployment

  • Check Settings -> Workers. The worker appears as connected, together with the detected systems and features.
  • The next project evaluation places builds on the new worker.
  • The connection to the server is visible in /var/log/gradient-worker.log on macOS.

One Machine, Many Projects

Each registration in the UI or CLI creates a new worker ID. To use one machine for several projects, register a team worker and grant the team with its workers to each project. Alternatively, declare the worker with several projects in the server state configuration and give each project its own line in the peers file.

macOS Workers

The darwinModules.default module can run the worker as a launchd daemon org.nixos.gradient-worker. The macOS module and the NixOS module differ.

  • User: _gradient-worker, with UID and GID 536. Set users.users._gradient-worker.uid and users.groups._gradient-worker.gid to different values if another account can use 536.
  • Logs: /var/log/gradient-worker.log.
  • Restart: sudo launchctl kickstart -k system/org.nixos.gradient-worker.
  • System: aarch64-darwin only. Support for Intel Mac (x86_64-darwin) is absent. nixpkgs deprecated this platform.
  • Linux only: exec jobs (capabilities.exec), build.metrics and the reverse proxy options.
  • Nix settings: the module can add _gradient-worker to trusted-users and enable the nix-command, flakes and ca-derivations features. Add both to the Nix settings manually for nix.enable = false, for example Determinate Nix.

Peers File

The worker authenticates with one line per project.

# /run/secrets/gradient-worker-peers
<project uuid>:<token>
<other project uuid>:<other token>

A single *:<token> line can cover every project with the same token instead.

One line per project *:<token>
Setup One new line per project One line. Every project must register the worker with the same pre-generated token
Leaked token One project exposed Every project of the worker exposed
Revoking Per project Everywhere at once
Fit Workers shared between teams A worker owned by one team, serving all of that team's projects

Prefer one line per project. Reserve * for workers where a single owner holds every project.

A team worker needs one line for its team, <team id>:<token>, or *:<token>.

Next Steps