# CLI

https://virtx.sh/docs/reference/cli/

Build and manage a server's images, and sign in, from a shell.

The `virtx` command does two things from a shell: it does to a server's images what `docker` does to its own, and it signs a person in to a server, for a client to send the session's token.

## Installing

The Python and Node.js packages carry it, so their package managers run it without installing anything:

```sh
uvx virtx images                 # pipx run virtx, without uv
npx -y @brekkylab/virtx images
```

To have `virtx` on `PATH`, install it with one of:

| With | Command |
| --- | --- |
| uv | `uv tool install virtx` |
| pipx | `pipx install virtx` |
| npm | `npm install -g @brekkylab/virtx` |
| Rust, from a clone of the [virtx repository](https://github.com/brekkylab/virtx) | `cargo install --path virtx-cli` |

Any of them will do: they all behave the same.

## Images

| Command | What it does |
| --- | --- |
| `virtx build -t NAME[:TAG] [PATH]` | Builds an image from the Dockerfile in `PATH`, `.` by default, or the one `-f` names. The log goes to stderr and `NAME:TAG sha256:…` to stdout. |
| `virtx images` | Lists images. |
| `virtx inspect IMAGE…` | Prints images as JSON, each by a ref (`name:tag`), image ID or digest. |
| `virtx tag SOURCE TARGET[:TAG]` | Points a ref at an image, moving the ref if it is in use. |
| `virtx rmi IMAGE…` | Untags refs, removing an image with its last ref, or removes images by image ID or digest with every ref on them. An image a sandbox or a build uses keeps its last ref. |
| `virtx pull NAME[:TAG]` | Pulls an image from a registry, tagged as it is named. |
| `virtx push NAME[:TAG]` | Pushes an image to the registry its ref names. |
| `virtx save -o FILE IMAGE` | Writes an image to an OCI archive. |
| `virtx load -i FILE -t NAME[:TAG]` | Tags the image in an OCI archive, uploading what the server lacks. |
| `virtx image prune` | Removes every image no ref points at and no sandbox uses. |

Each command is also `virtx image <command>`, with `ls` and `rm` for `images` and `rmi`. A registry that wants credentials gets them with `-u USER`, its password or access token read from stdin, or `--token-stdin` for a bearer token. Ctrl-C during a build, pull or push cancels the job.

```sh
virtx build -t jq:latest .
virtx tag jq:latest ghcr.io/me/jq:1
echo "$GHCR_TOKEN" | virtx push -u me ghcr.io/me/jq:1
virtx save -o jq.tar jq:latest
virtx rmi jq:latest
```

### Which server

Without options, the command starts `virtx-stdio` of its own version, fetched into the cache the first time, or the program `--server` (`$VIRTX_STDIO`) names. `--host URL` (`$VIRTX_HOST`) reaches a server over HTTP instead, with `$VIRTX_TOKEN` as its bearer token; the token comes from the environment alone, so it is never on a command line. `--project NAME` (`$VIRTX_PROJECT`) names the project the requests belong to, a project of the user's by its name or any by its id, sent as the `virtx-project` header; without it, the requests are in the user's own, `default`. `--host` and `--server` together are refused; otherwise a flag wins over the environment and a host over a program, so `--server` starts its program with `$VIRTX_HOST` set, and `$VIRTX_HOST` is reached with `$VIRTX_STDIO` set. See [Hello world](https://virtx.sh/docs/hello-world/#run-it) for the two servers.

## Signing in

These commands are for connecting to a `virtx-server-tcp` that you or your team run: they sign you in to the server's OpenID Connect provider, and a client sends the session's token with each request.
For a server of your own, see [Connecting to a server](https://virtx.sh/docs/guides/self-hosting/#connecting-to-a-server).
A local server, which `Client.spawn()` starts, needs no signing in.

| Command | What it does |
| --- | --- |
| `virtx login [URL]` | Signs in: it shows a code and a page to approve it at, and opens the page in a browser where there is one. |
| `virtx token` | Prints a token of the session, refreshed as needed; `--json` prints `{"token", "expires_in"}`, as the SDKs read it. |
| `virtx whoami` | Says who is signed in. |
| `virtx logout` | Signs out: the session is forgotten on this machine, and where it was signed in to is asked to end it. |

A client given a command for its token runs it each time it needs one: `uvx virtx token --json` (or `pipx run virtx token --json`, without uv) or `npx -y @brekkylab/virtx token --json` with nothing installed, or `virtx token --json` where an installed `virtx` is on `PATH`.  
On Windows, where npm installs a command as a `.cmd`, the program is `npx.cmd`, or `virtx.cmd` for one `npm install -g` installed.

A command is of the provider it names with `--issuer` and `--client-id`, or the one the server `login` is given names, and otherwise of what was signed in to last.

### A server's provider

`virtx login <the server's URL>` asks the server which provider to sign in to and as which client: the `issuer` and `client_id` of its `oidc`. `--issuer` (`$VIRTX_OIDC_ISSUER`) and `--client-id` (`$VIRTX_OIDC_CLIENT_ID`) name the two without asking a server, and `--client-secret` (`$VIRTX_OIDC_CLIENT_SECRET`) gives the secret of a provider that has one for its public clients.

`virtx token` prints the ID token, which the server checks against the provider's keys. `virtx logout` asks the provider to end the session, where the provider takes that.

The session is kept under `~/.config/virtx/`. The refresh token and the ID token last issued are kept in files. The files are named for the client and the provider: `virtx@idp.example.token` and `virtx@idp.example.id` for the `oidc` of the server in [Running your own server](https://virtx.sh/docs/guides/self-hosting/#configure). On Linux and macOS the directory is set to `0700`, the files are written `0600`, readable by their owner alone, and a file there that the directory's owner does not own is not read.
