nix: Add a demo

A setup for trying the broker by hand, laid out like a deployment.
`devenv up` runs the broker from the checkout with its web app, Pocket
ID for approvers to sign in with, and a box VM for the SSH targets; a
setup task creates the approver, their group and the broker's OIDC
client through Pocket ID's API. `demo` boots the agents' VM on the
terminal, which reaches the broker only over vsock.

Approvers use `slop-approve` in the devenv shell, or the web app after
signing in with a link from `demo-sign-in`, so passkey approvals can
be tried without deploying anything.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jackos1998andClaude Opus 5.5 committed 2026-11-23 00:39:36 +00:00
1 parent 076f4959d9
commit 313c028ad9
6 files changed
+382 -2

No files matched your search

+185 -2
View File
@@ -1,20 +1,203 @@
# The development environment: `devenv shell`, or direnv with `.envrc`. The package, modules and
# checks are in `flake.nix`.
{ pkgs, ... }:
#
# `devenv up` runs the demo's host side (see `docs/demo.md`): the broker from this checkout, with
# its web app, Pocket ID for approvers to sign in with, and the box VM its SSH targets reach.
{ pkgs, lib, config, ... }:
let
demo = import ./nix/demo/settings.nix;
state = "${config.devenv.state}/slop-demo";
# Sockets, whose paths must be short
run = "${config.devenv.runtime}/slop-demo";
issuer = "http://localhost:${toString demo.ports.pocketId}";
appUrl = "http://localhost:${toString demo.ports.web}";
# `@group@` becomes the user's primary group, whose members may approve
brokerConfig = (pkgs.formats.toml { }).generate "slop-demo.toml" {
requesters.agent = { vsock_cid = demo.agentCid; policy = "agent"; };
policies.agent.ops = {
query.action = "auto";
ssh = {
action = "ask";
leases = [ "5m" "1h" ];
rules = [
{ match.target = "router"; action = "ask"; confirm = "passkey"; }
{ match.target = "vault"; action = "deny"; }
];
};
};
vsock_port = 6262;
approve_socket = "${run}/approve.sock";
approver_group = "@group@";
audit_log = "${state}/audit.jsonl";
ssh = {
ssh = "${pkgs.openssh}/bin/ssh";
key = "${state}/broker-key";
known_hosts = "${state}/known_hosts";
targets = lib.genAttrs [ "web" "router" "vault" ] (_: {
hostname = "localhost";
user = "box";
ssh_opts = [ "-p" (toString demo.ports.boxSsh) ];
});
};
web = {
listen = "127.0.0.1:${toString demo.ports.web}";
url = appUrl;
inherit issuer;
client_id = "slop-approve";
client_secret = "${state}/oidc-secret";
group = "slop-approvers";
state_dir = "${state}/web";
};
};
# Pocket ID's admin API, with the key `devenv up` made
pocketIdApi = ''
api() {
local method=$1 path=$2
shift 2
curl -sS --fail-with-body -X "$method" "${issuer}/api/$path" \
-H "X-API-Key: $(sed -n 's/^STATIC_API_KEY=//p' ${state}/pocket-id.env)" -H 'Content-Type: application/json' "$@"
}
'';
in
{
languages.rust.enable = true;
# `pkg-config` with OpenSSL for Web Push's payload encryption and SQLite for the web app's
# sessions, `git` for the builder's tests
packages = with pkgs; [ pkg-config openssl sqlite git nixpkgs-fmt ];
packages = with pkgs; [ pkg-config openssl sqlite git nixpkgs-fmt curl jq ];
# For `slop-approve` in this shell
env.SLOP_APPROVE = "unix:${run}/approve.sock";
scripts = {
slop-approve = {
description = "Answer the demo's requests (built from this checkout)";
exec = ''exec cargo run -q --manifest-path "$DEVENV_ROOT/Cargo.toml" --bin slop-broker -- approve "$@"'';
};
demo = {
description = "Boot the demo's agents' VM on this terminal; needs `devenv up` running";
exec = ''
if [ ! -S ${run}/approve.sock ]; then
echo "The broker isn't running: start \`devenv up\` first." >&2
exit 1
fi
exec nix run "$DEVENV_ROOT#demo" -- "$@"
'';
};
demo-box = {
description = "SSH into the demo's box VM as `box`, with the broker's key";
exec = ''
exec ${pkgs.openssh}/bin/ssh -F none -i ${state}/broker-key -o IdentitiesOnly=yes \
-o UserKnownHostsFile=${state}/known_hosts -o StrictHostKeyChecking=accept-new \
-p ${toString demo.ports.boxSsh} box@localhost "$@"
'';
};
demo-sign-in = {
description = "Print a one-time link that signs in to the demo's Pocket ID as the approver";
exec = ''
${pocketIdApi}
user=$(api GET 'users?search=demo' | jq -r '.data[] | select(.username == "demo") | .id')
echo "${issuer}/lc/$(api POST "users/$user/one-time-access-token" -d '{"ttl":"1h"}' | jq -r .token)"
'';
};
demo-reset = {
description = "Delete the demo's state (Pocket ID's users and passkeys, keys, audit log)";
exec = ''rm -rf ${state}'';
};
vm-test = {
description = "Run the NixOS VM test of the broker module (needs KVM)";
exec = ''nix build -L --no-link "$DEVENV_ROOT#checks.x86_64-linux.vm" "$@"'';
};
};
# The demo's host side; not for `devenv test`
tasks = lib.mkIf (!config.devenv.isTesting) {
"slop-demo:keys" = {
description = "Make the demo's secrets and SSH keys";
exec = ''
set -eu
umask 077
mkdir -p ${state}/box ${state}/pocket-id ${state}/web ${run}
if [ ! -e ${state}/pocket-id.env ]; then
key() { ${pkgs.openssl}/bin/openssl rand -hex 32; }
printf 'ENCRYPTION_KEY=%s\nSTATIC_API_KEY=%s\n' "$(key)" "$(key)" > ${state}/pocket-id.env
fi
# The box VM gets the broker's public key and its own host key through its shared directory
[ -e ${state}/broker-key ] || ${pkgs.openssh}/bin/ssh-keygen -q -t ed25519 -N "" -C slop-demo -f ${state}/broker-key
cp ${state}/broker-key.pub ${state}/box/broker.pub
[ -e ${state}/box/host-key ] || ${pkgs.openssh}/bin/ssh-keygen -q -t ed25519 -N "" -C slop-demo-box -f ${state}/box/host-key
'';
};
# Once: the approver `demo` in the group `slop-approvers`, and the broker's OIDC client
"slop-demo:pocket-id" = {
description = "Set up the demo's approver and OIDC client in Pocket ID";
after = [ "devenv:processes:pocket-id" ];
status = "test -s ${state}/oidc-secret";
exec = ''
set -eu
umask 077
${pocketIdApi}
user=$(api POST users -d '{"username":"demo","email":"demo@example.org","firstName":"Demo","lastName":"Approver","displayName":"Demo Approver","isAdmin":false}' | jq -r .id)
group=$(api POST user-groups -d '{"name":"slop-approvers","friendlyName":"slop approvers"}' | jq -r .id)
api PUT "user-groups/$group/users" -d "$(jq -n --arg u "$user" '{userIds: [$u]}')" >/dev/null
api POST oidc/clients -d '{"id":"slop-approve","name":"slop-broker demo","callbackURLs":["${appUrl}/auth/callback"],"isPublic":false,"pkceEnabled":true,"isGroupRestricted":true}' >/dev/null
api PUT oidc/clients/slop-approve/allowed-user-groups -d "$(jq -n --arg g "$group" '{userGroupIds: [$g]}')" >/dev/null
api POST oidc/clients/slop-approve/secrets | jq -j .secret > ${state}/oidc-secret.new
mv ${state}/oidc-secret.new ${state}/oidc-secret
'';
};
};
processes = lib.mkIf (!config.devenv.isTesting) {
pocket-id = {
exec = ''
set -a
. ${state}/pocket-id.env
exec ${pkgs.pocket-id}/bin/pocket-id
'';
cwd = "${state}/pocket-id";
env = {
APP_URL = issuer;
HOST = "127.0.0.1";
PORT = toString demo.ports.pocketId;
ANALYTICS_DISABLED = "true";
};
after = [ "slop-demo:keys" ];
ready = {
http.get = { port = demo.ports.pocketId; path = "/.well-known/openid-configuration"; };
period = 1;
};
};
slop-broker = {
exec = ''
sed "s/@group@/$(id -gn)/" ${brokerConfig} > ${state}/broker.toml
exec cargo run -q --bin slop-broker -- serve --config ${state}/broker.toml
'';
cwd = config.devenv.root;
after = [ "slop-demo:pocket-id" ];
ready = {
exec = "test -S ${run}/approve.sock";
period = 1;
};
};
box = {
exec = ''SHARED_DIR=${state}/box exec nix run "$DEVENV_ROOT#demo-box"'';
after = [ "slop-demo:keys" ];
};
};
# `devenv test`
enterTest = ''
cargo clippy --all-targets -- -D warnings
+75
View File
@@ -0,0 +1,75 @@
# Demo
A setup for trying the broker by hand, laid out like a real deployment: the broker runs on the host,
the agents are in a VM that reaches it only over vsock, and its SSH targets are another VM. Paseo
and real coding agents are out of scope: you type the agent's `slop` commands yourself.
It needs the devenv shell (direnv with `.envrc`, which provides [devenv](https://devenv.sh)) and
KVM, with `/dev/vhost-vsock` usable by your user. In one terminal:
```sh
devenv up # the host side; `devenv up -d` runs it in the background
```
and in another, in the devenv shell:
```sh
demo # the agents' VM, on this terminal
```
## What's in it
| Part | Where | Runs |
|---|---|---|
| Broker, from this checkout (`cargo run`) | Host | `devenv up` (process `slop-broker`) |
| Web app | <http://localhost:8095> | Part of the broker |
| [Pocket ID](https://pocket-id.org), for approvers to sign in | <http://localhost:1411> | `devenv up` (process `pocket-id`) |
| Box VM ([`box.nix`](../nix/demo/box.nix)): `sshd` for the targets | `localhost:2222`, as `box` | `devenv up` (process `box`) |
| Agents' VM ([`agent.nix`](../nix/demo/agent.nix)), signed in as `agent` | Your terminal | `demo` |
The ports are on the host's loopback, so they must be free there; they and the agents' VM's vsock
CID are in [`settings.nix`](../nix/demo/settings.nix). The broker's configuration is in
[`devenv.nix`](../devenv.nix):
- **Requester:** `agent`, the agents' VM's vsock CID. The VMs have no network beyond their
forwarded ports, and only vsock links the agents' VM to the host.
- **Approvers:** members of your primary group, through `slop-approve` in the devenv shell, and the
Pocket ID user `demo` (in the group `slop-approvers`) through the web app.
- **Targets:** `web`, `router` and `vault` all reach the box VM.
- **Policy:**
- The box queries (`slop status`, `slop logs`, `slop failed`) run without asking.
- `slop ssh web` asks, and offers leases of 5 minutes or an hour.
- `slop ssh router` asks and needs a passkey (`confirm = "passkey"`), so only the web app can
approve it.
- `slop ssh vault` is denied.
## Approving
- **Terminal:** `slop-approve` in the devenv shell lists pending requests; answer one with
`slop-approve <id> once|<lease>|deny`.
- **Web app:** `demo-sign-in` prints a one-time link that signs in to Pocket ID as `demo`. Add a
passkey when Pocket ID asks, then sign in to <http://localhost:8095>; the passkey also approves
`router` requests. Web Push works when the broker can reach the browser's push service.
A request blocks until it's answered, and `slop ssh` forwards stdin. To keep the agent's shell, run
requests in the background with stdin closed:
```sh
slop ssh web -- uptime </dev/null &
```
## The box VM
`demo-box` in the devenv shell logs in to the box VM as `box`, with the broker's key, to see what
approved commands did; `demo-box <command>` runs one command there. Its console output is in
`devenv processes logs box`.
## State
The VMs keep nothing: `poweroff` (or Ctrl-A X) stops the agents' VM, and each boot starts from
scratch. The host side keeps its state in `.devenv/state/slop-demo` between runs: Pocket ID's
database (so passkeys survive), the keys, the audit log and the web app's sessions. `demo-reset`
deletes it while `devenv up` isn't running; the next `devenv up` sets Pocket ID up again.
If the broker doesn't start, read `devenv processes logs slop-broker`, and the setup's output in
`devenv up`.
+3
View File
@@ -21,6 +21,9 @@
packages = forSystems (pkgs: rec {
slop-broker = pkgs.callPackage ./nix/package.nix { };
default = slop-broker;
# The demo's VMs (see `docs/demo.md`)
demo = import ./nix/demo/agent.nix { inherit pkgs slop-broker; };
demo-box = import ./nix/demo/box.nix { inherit pkgs; };
});
overlays.default = final: _prev: {
+65
View File
@@ -0,0 +1,65 @@
# The demo's agents' VM: an `agent` user with `slop`, which reaches the broker on the host over
# vsock, like a real agents' VM. `devenv up` runs the broker; `demo` boots this VM on the terminal.
{ pkgs, slop-broker }:
let
inherit (pkgs) lib;
settings = import ./settings.nix;
help = pkgs.writeShellScriptBin "demo-help" ''
cat <<'EOF'
slop-broker demo. You're `agent` on the agents' VM; the broker runs on the host, and the
targets `web`, `router` and `vault` are all the box VM.
slop status web sshd.service runs without asking (policy: auto)
slop logs web sshd.service -n 20
slop ssh web -- uptime asks an approver; leases of 5m or 1h are offered
slop ssh router -- uptime asks too, and approving needs a passkey in the web app
slop ssh vault -- true denied by policy
slop requests your pending requests and leases
slop guide the guide agents get
Approve on the host: `slop-approve` in the devenv shell, or the web app at
http://localhost:${toString settings.ports.web} (`demo-sign-in` prints a sign-in link).
A request blocks until it's answered. To keep this shell, run it in the background with stdin
closed (`slop ssh` forwards stdin): `slop ssh web -- uptime </dev/null &`. `poweroff` (or
Ctrl-A X) stops the VM; nothing in it is kept.
EOF
'';
in
(pkgs.nixos ({ modulesPath, ... }: {
imports = [ "${modulesPath}/virtualisation/qemu-vm.nix" ];
system.stateVersion = lib.trivial.release;
networking.hostName = "slop-demo-agent";
virtualisation = {
graphics = false;
diskImage = null;
memorySize = 1024;
# Only vsock reaches the host
restrictNetwork = true;
qemu.options = [ "-device vhost-vsock-pci,guest-cid=${toString settings.agentCid}" ];
};
users.users.agent.isNormalUser = true;
services.getty.autologinUser = "agent";
# So `poweroff` works from the serial console
security.polkit = {
enable = true;
extraConfig = ''
polkit.addRule(function(action, subject) {
if (action.id.indexOf("org.freedesktop.login1.power-off") == 0 && subject.user == "agent") {
return polkit.Result.YES;
}
});
'';
};
environment.systemPackages = [ slop-broker help ];
programs.bash.loginShellInit = ''
if [ "$USER" = agent ]; then demo-help; fi
'';
})).vm
+47
View File
@@ -0,0 +1,47 @@
# The box the demo's SSH targets reach: `sshd` with a `box` account for the broker's key. `devenv up`
# runs it with `SHARED_DIR` holding the broker's public key and the box's host key, which it keeps
# between runs so the broker's `known_hosts` stays valid.
{ pkgs }:
let
inherit (pkgs) lib;
settings = import ./settings.nix;
hostKey = "/etc/ssh/ssh_host_ed25519_key";
in
(pkgs.nixos ({ config, modulesPath, ... }: {
imports = [ "${modulesPath}/virtualisation/qemu-vm.nix" ];
system.stateVersion = lib.trivial.release;
networking.hostName = "slop-demo-box";
virtualisation = {
graphics = false;
diskImage = null;
memorySize = 512;
restrictNetwork = true;
forwardPorts = [{
from = "host";
host = { port = settings.ports.boxSsh; address = "127.0.0.1"; };
guest.port = 22;
}];
};
users.users.box.isNormalUser = true;
services.openssh = {
enable = true;
hostKeys = [{ path = hostKey; type = "ed25519"; }];
};
# The shared directory is mounted at `/tmp/shared`
systemd.services.slop-demo-keys = {
wantedBy = [ "multi-user.target" ];
before = [ "sshd.service" "sshd-keygen.service" ];
requiredBy = [ "sshd.service" ];
unitConfig.RequiresMountsFor = "/tmp/shared";
serviceConfig = { Type = "oneshot"; RemainAfterExit = true; };
script = ''
install -m 600 /tmp/shared/host-key ${hostKey}
install -d -m 700 -o box -g users ${config.users.users.box.home}/.ssh
install -m 600 -o box -g users /tmp/shared/broker.pub ${config.users.users.box.home}/.ssh/authorized_keys
'';
};
})).vm
+7
View File
@@ -0,0 +1,7 @@
# Shared by the demo's VMs and its host side in `devenv.nix`
{
# The agents' VM's vsock CID: anything above 2 that no other VM on the host uses
agentCid = 3626;
# On the host's loopback
ports = { pocketId = 1411; web = 8095; boxSsh = 2222; };
}