Skip to content

Run a Program from the CLI

A flake program from the working tree, started on an exec worker and streamed back to the local terminal.

Requirements:

1. Allow Exec Jobs in Build Requests

Programs from gradient run arrive as build requests on the server. Exec jobs in build requests need the project setting below.

Open the project Settings, tick Allow Exec Jobs in Build-Requests and save.

services.gradient.state.projects.my-project.allow_exec_in_build_requests = true;
  • The setting covers every exec job in build requests, also exec apps from gradient build.
  • The instance and worker switches still apply, see Turn On Exec Jobs.

2. Start the Program

gradient run .#packages.x86_64-linux.hello
Attribute Program
Exec app with passthru.gradient As declared, see Declare an Exec App
Other apps with type = "app" The declared program, like nix run
devShells.* Interactive bash inside the shell environment, like nix develop
Other derivations bin/<name> of the package, with meta.mainProgram, pname or the package name as <name>
  • Commands without an attribute run apps.<system>.default.
  • Bare .#name values map to apps.<system>.name.
  • A job can retry once unless apps declare their own retries.
  • Build logs of the program stay visible until the job start.
  • Status and wait reason lines from the CLI print each time these change during a job wait. These lines include worker features that are missing.
  • Jobs canceled before start get a reason line and a nonzero exit code from the CLI.

3. Pick a Mode

Mode Use Behavior
Interactive A single matched job and a local terminal The local terminal attaches to the program terminal. The program waits for this attach
Log --no-shell, more matched jobs, or no terminal The CLI follows the job logs. Lines get the attribute name in front when more jobs match
  • Exit codes of the CLI equal the exit code of the program. Jobs in log mode end with the first non-zero code.
  • Interactive mode forwards every key to the program, Ctrl-C included.
  • Typing ~. at the start of a line can abort the job.
  • Ctrl-C in log mode can abort every unfinished job.

4. Reattach

Lost connections end with Connection closed unexpectedly. Use gradient run --attach <job-id>. Jobs keep going until their timeout.

gradient run --attach <job-id>
  • Reattached terminals continue at the current output. Job logs keep the output from the gap.

5. Set Resources

Flag Job field
--cpus <N> cpus
--memory <MB> memoryMb
--network network = true
--labels a,b labels
--timeout <D> timeout and maxSilent, in seconds or like 30m
  • Flags override the same fields of an exec app. Worker limits still apply.
  • Interactive jobs get ssh = true. Only workers with terminals can take them. Jobs in log mode keep the ssh value of the app or the worker.

6. Forward Ports

gradient run -L 8888 .#notebook # (1)!
gradient run --attach <job-id> -L 9000:8888 # (2)!
  1. Local port 8888 -> guest port 8888.
  2. Local port 9000 -> guest port 8888 on the running job.

  3. New jobs add each guest port to ports and get ssh = true.

  4. Running jobs accept forwards only to ports in their own ports.
  5. Forwards need exactly one matching job.
  6. Forwards stay open until CLI shutdown. Skip SSH setup.

Verify Deployment

  • The output of gradient run .#packages.x86_64-linux.hello is Hello, world! together with a zero exit code.
  • Refused jobs show the message Exec jobs don't run in build requests for this project. with the name of the setting.

Next Steps