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:
- An exec worker, see Set Up an Exec Worker
- The Gradient CLI with a login, see CLI
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.
- 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¶
| 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
.#namevalues map toapps.<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-Cincluded. - Typing
~.at the start of a line can abort the job. Ctrl-Cin 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.
- 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 thesshvalue of the app or the worker.
6. Forward Ports¶
- Local port 8888 -> guest port 8888.
-
Local port 9000 -> guest port 8888 on the running job.
-
New jobs add each guest port to
portsand getssh = true. - Running jobs accept forwards only to ports in their own
ports. - Forwards need exactly one matching job.
- Forwards stay open until CLI shutdown. Skip SSH setup.
Verify Deployment¶
- The output of
gradient run .#packages.x86_64-linux.helloisHello, 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¶
- Declare an Exec App: fixed hardware requests, secrets and images
- Open a Terminal: browser terminals and SSH logins
- Turn On Exec Jobs: every switch between a repository and a worker
- CLI: all flags of
gradient run