# Running your own server

https://virtx.sh/docs/tutorials/self-hosting/

Connect to a team's virtx-server-tcp, and run one on your own machine, signing users in through your OpenID Connect provider.

`virtx-server-tcp` is the VirtX server for a team.
It runs continuously on a machine you control, and serves every user that your OpenID Connect (OIDC) provider signs in.
Each user works in their own projects and in projects others share with them; see [Managing projects](https://virtx.sh/docs/tutorials/projects/).

If you only need sandboxes for yourself on your own machine, you don't need this server: `Client.spawn()` starts a local server for your program. See [Quick start](https://virtx.sh/docs/hello-world/).

This page has two parts:

- [Connecting to a server](#connecting-to-a-server), for each user.
- [Running the server](#running-the-server), for the administrator.

## Connecting to a server

Your administrator gives you the server's URL. These examples use `https://virtx.example`.

> **Note:**
> If you can't sign in or connect, or the server rejects a request, contact the administrator.
> The administrator controls who can sign in, and can add you to projects.

1. Sign in once with the `virtx` command. `uvx` (or `pipx run`) and `npx` run it without installing it; to install it, see [Installing](https://virtx.sh/docs/reference/cli/#installing).
   The command asks the server which OIDC provider to use, then shows a code for you to approve in your browser:

   ```sh
   uvx virtx login https://virtx.example
   # or
   npx -y @brekkylab/virtx login https://virtx.example
   ```

2. In your program, connect with `CommandAuth`. The client runs `virtx token` to get a token, and runs it again whenever the token expires:

**Python**

```python
from virtx import Client, CommandAuth

client = Client.connect("https://virtx.example", CommandAuth("uvx", ("virtx", "token", "--json")))
```

**Node.js**

```js
const client = Client.connect('https://virtx.example', {
  type: 'command',
  program: 'npx',
  args: ['-y', '@brekkylab/virtx', 'token', '--json'],
})
```

**Rust**

```rust
use virtx::{Auth, Client};

let client = Client::connect(
    "https://virtx.example",
    Auth::Command { program: "uvx".into(), args: vec!["virtx".into(), "token".into(), "--json".into()] },
    None,
)?;
```

Other cases:

- **The provider gives its public clients a secret:** pass it to `virtx login` with `--client-secret`.
- **Your program gets tokens some other way:** pass the token directly with `BearerAuth` in Python, `{ type: 'bearer', token }` in Node.js, or `Auth::Bearer` in Rust. The client doesn't refresh a bearer token, so replace it with `set_auth` before it expires.
- **You are signed in to more than one provider:** tell `virtx token` which one to use: `uvx virtx token --json --issuer https://idp.example --client-id virtx`.

`uvx virtx whoami` shows who is signed in, and `uvx virtx logout` signs out. See [Signing in](https://virtx.sh/docs/reference/cli/#signing-in) in the CLI reference.

## Running the server

### Install

1. Check that the machine meets the [requirements](https://virtx.sh/docs/installation/#requirements) for running VMs.
2. Download `virtx-server-tcp-<os>-<arch>.tar.gz` from a [release](https://github.com/brekkylab/virtx/releases).
3. Unpack it into its own directory, and run `virtx-server-tcp` from that directory. The server loads the files it needs from the same directory.

### Configure

Write a `server.json`:

```json
{
  "listen": "127.0.0.1:8470",
  "home": "/var/lib/virtx",
  "oidc": { "issuer": "https://idp.example", "client_id": "virtx" }
}
```

Then start the server with it:

```sh
virtx-server-tcp serve --config server.json
```

| Field | What it sets |
| --- | --- |
| `listen` | The address and port the server listens on. |
| `home` | The directory where the server stores everything its users create, including sandboxes. When the server restarts with the same `home`, its sandboxes and their files are still there. Default: `$VIRTX_HOME`, or a `virtx` directory in the server user's cache directory. |
| `oidc` | The OpenID Connect provider users sign in with; see [Add users](#add-users). |
| `origins` | The web pages allowed to call the server from a browser, such as `["https://app.example"]`. Without it, browsers are refused; programs are not affected. |
| `private_registries` | Registries on the server's machine or its private network that image jobs may reach, such as `["registry.internal:5000"]`; see [Pull and push](https://virtx.sh/docs/tutorials/images/#pull-and-push). Without it, jobs reach only registries on public addresses. |
| `registry_mirror` | A mirror of Docker Hub, such as `"mirror.gcr.io"`, that pulls of Docker Hub's images without credentials try first, a build's base among them; see [Pull and push](https://virtx.sh/docs/tutorials/images/#pull-and-push). Without it, they go to Docker Hub. |
| `share_public_images` | `true` to share pulls without credentials between the server's projects; see [Share public images](#share-public-images). `false` unless set. |

`listen` and `oidc` are required. The server refuses to start if `server.json` contains a field it doesn't recognize.

Under `home`, each project's images, sandboxes and volumes are stored in `project/<id>/`.

> **Caution:**
> Sandboxes reach their volumes through virtio-fs shares. libkrun, the VM library VirtX uses, doesn't guarantee that a guest can't reach other directories on the same host filesystem. Put `home` on a dedicated filesystem that holds nothing else.

### Serve it over HTTPS

The server speaks plain HTTP. Put a reverse proxy that terminates TLS in front of it, as for any HTTP service:

- Clients send tokens to remote servers only over HTTPS, so users must reach the server at an `https://` URL.
- The proxy must pass WebSocket connections through, because [ports](https://virtx.sh/docs/tutorials/networks-ports/) are reached over WebSockets.
- If the proxy runs on the same machine, set `listen` to `127.0.0.1`. Listen on another address only when the proxy runs on another machine.

### Share public images

Each project keeps its own images, so when two projects pull the same public image, the server downloads it twice.

With `"share_public_images": true`, an anonymous pull (including a build's base image) first checks whether another project on the server already pulled the same image, as the registry serves it now. If so, the server reuses that copy, with the layers stored once on disk, and downloads nothing.
Pulls with credentials are never shared, and neither are images a project built or pushed.

> **Caution:**
> Sharing is off by default because it leaks information between projects. A pull that finds a shared image finishes much faster than one that downloads it, so anyone who can pull in one project can tell whether another project on the server pulled that image anonymously.

Turn it on only when the server's users may know this about each other, for example on a server for one person or one team. Leave it off when projects belong to people or organizations that must not learn anything about each other.

### Add users

The server has no user list of its own. Anyone your OIDC provider signs in to the client you configure is a user, and nobody else can get in.
To add or remove a user, allow or deny them access to that client in your provider. Allow only your team.

1. In your provider, register a public client that allows the device authorization flow.
2. Put the provider and the client in the `oidc` field of `server.json`:

| Field | What it sets |
| --- | --- |
| `issuer` | The provider's issuer URL. |
| `client_id` | The client you registered. |
| `username_claim` | The ID token claim used as the user's name on the server. Default: `email`, which is accepted only once the provider has verified it. |
| `jwks_uri` | The URL of the provider's signing keys, for a provider without a discovery document. Most providers don't need it. `virtx login` requires a discovery document, so with such a provider, users must get tokens another way and connect with a bearer token, as [Connecting to a server](#connecting-to-a-server) shows. |

Users add each other to projects by user name, so with the default claim, teammates add each other by email address.

3. Give your users the server's URL, and the client's secret if your provider issued one. [Connecting to a server](#connecting-to-a-server) shows what they do next.

## Troubleshooting

1. **`401`, `unauthorized`, `no token this server knows`**

   **Cause:** The token was not signed by `oidc.issuer` for its `client_id`, or it expired, or the provider has not verified the user's email.

   **Fix:** Sign in again with `virtx login <url>`. With the default `username_claim`, verify the email at the provider, or set another claim.

2. **`not signed in; run virtx login <the server's URL>`**

   **Cause:** `virtx token` or `virtx whoami` ran with no session.

   **Fix:** Run `virtx login <url>`.

3. **`… names no provider to sign in to`**

   **Cause:** The URL is not a `virtx-server-tcp` with `oidc` set.

   **Fix:** Check the URL, or pass `--issuer` and `--client-id`.

4. **The client refuses to send a token in the clear**

   **Cause:** The URL is `http://` to another machine.

   **Fix:** Put TLS in front of the server and use `https://`.

5. **`403`, from a page in a browser**

   **Cause:** The page's origin is not in `origins`.

   **Fix:** Add it to `origins`.

6. **The server won't start, naming an unknown field**

   **Cause:** A typo, or a field the server does not have, in `server.json`.

   **Fix:** Use only the fields listed above.

7. **`private_registries: "…": a registry is named host or host:port`**

   **Cause:** A scheme or a path in a registry, or in `registry_mirror`.

   **Fix:** Give only `host` or `host:port`.

## Next

- [Managing projects](https://virtx.sh/docs/tutorials/projects/): what your users do on the server, and how they share it.
- [CLI](https://virtx.sh/docs/reference/cli/): sign in, and manage the server's images from a shell.
