Files
chandra/docs/run0.md
Claude 80af76fb15
CI / test (push) Canceled after 21m14s
Password dialog rebuilt: a separate polkit agent, one state machine, explicit errors
The first version ran polkit registration, polkit-agent-helper-1, the PAM
conversation and the drawing inside the compositor's frame loop, with
overlapping state in two layers. A helper that quit before asking was taken
for a wrong password and restarted in a loop that wiped the field (typing
seemed dead), a refused registration quietly started a third-party agent,
and the dialog and the Face unlock window each grabbed the keyboard.

Now:
- chandra-polkit-agent (compositor/authagent, /usr/local/lib/chandra) does
  polkit and PAM. Each request runs one state machine (polkit/machine.mbt:
  starting, asking, checking, failed, done). A check that quits or can't
  start is Failed with the reason and waits for Try again; only a wrong
  password starts the next check by itself. It logs each step to stderr
  (the session log), never the typed text.
- The compositor starts it on a socket pair and only draws what it says
  (host/auth.mbt, auth_draw.mbt; line protocol in polkit/wire.mbt). One
  input owner (grab_input): the dialog, else the Face unlock window.
- No silent fallback: if the agent can't run, polkit refuses it, polkit
  doesn't answer in 10 s, or it dies, a notice on screen says why (a crash
  after it ran restarts it, up to 3 times, and says so). The third-party
  agent runs only when the Settings switch is off.
- chandra-ctl auth-demo [password|face|face-ok|die|slow|users] shows the
  real dialog with a pretend check (password "demo"), no root needed;
  chandra-ctl auth-status says whether the agent is registered.
- just install builds and installs the agent (just auth-agent).

Tests: polkit_test (link, machine, view, helper Gone) and auth_test end to
end through handle_input: the agent loop in-process against a mock polkit
on a private dbus-daemon plus a mock helper, and the real agent program
spawned by the compositor (demo, and registered with the mock polkit).
Typing on a real xkb keymap, wrong password, cancel, withdrawal, helper
quitting early or missing, polkit refusing, agent not starting or crashing,
demo modes, Face unlock Apply. 170/170 host and 20/20 polkit tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgkRPrAzXECBUGmNEE
2026-11-26 05:09:49 +00:00

99 lines
5.2 KiB
Markdown

# Chandra and run0
[run0](https://www.freedesktop.org/software/systemd/man/latest/run0.html) (systemd 256+) is
systemd's replacement for sudo. It is not setuid: it asks the service manager to start the
command as a transient service, authenticated by polkit. Two differences from sudo matter here:
- **Clean environment.** The command does not inherit your environment: no `PATH` additions
(pixi, `~/.moon/bin`), no `WAYLAND_DISPLAY`, `XDG_RUNTIME_DIR`, `DBUS_SESSION_BUS_ADDRESS` or
your `HOME`. Pass what you need with `--setenv=NAME=VALUE` (or `--setenv=NAME` to copy yours).
- **Its own login session.** The command runs in a new PAM/logind session that has no seat and no
VT, even with `--user=you`.
Everything below works with sudo, doas or pkexec too; run0 is only preferred when it exists.
## Installing the login session
```bash
just install # flags: --link, --native, --pam, --no-face; and `just uninstall`
```
These go through `scripts/as-root`, which uses run0 if systemd is PID 1 and run0 is installed,
else sudo, doas or pkexec (`CHANDRA_AS_ROOT=sudo just install` picks one). It resolves
the program to an absolute path first, because run0 would otherwise look it up in the service
manager's `PATH`, and passes the installer by absolute path, so the working directory doesn't
matter. By hand, from the checkout's root:
```bash
run0 python3 session/install.py # run0 keeps the working directory when the target is root
```
Don't run `just` itself under run0 (`run0 just install`): the recipes build as you first,
and under run0 `just` finds neither pixi nor moon, and would leave root-owned build outputs.
## Starting the desktop
Start it **as yourself, never as root**. `run0 chandra-session` (or `sudo chandra-session`) would
run the compositor and every app as root, put the socket in root's runtime directory where your
apps don't look, and write settings under `/root`; `chandra-session` refuses (override with
`CHANDRA_ALLOW_ROOT=1` if you really mean it).
- **Display manager:** pick "Chandra" in the session menu (installed by `just install`).
- **From a text console:** log in on the VT as yourself and run `chandra-session` (installed), or
`just run-drm` (from the checkout). logind gives that login session the seat, which is what lets
the compositor open the screen and input devices without being root.
`run0 --user=you chandra-session` from a root shell does **not** work with logind: run0's session
has no seat, so the compositor can't take the screen and input devices. `chandra-session` notes
this in `~/.local/state/chandra/session.log` when it sees a seatless session. (With seatd instead
of logind, it can work if you are in seatd's group; there you'd pass the environment yourself:
`run0 --user=you --setenv=XDG_RUNTIME_DIR=/run/user/$(id -u you) chandra-session`.)
When started without the usual login environment, `chandra-session` fills in `HOME` from the
password database and uses `/run/user/<uid>` as `XDG_RUNTIME_DIR` when it exists and is yours,
instead of falling back to `/tmp`.
## Running apps as root inside Chandra
Apps launched from Chandra get `WAYLAND_DISPLAY` as an absolute socket path. Under run0 they lose
it, so `run0 gparted` opens no window. Use:
```bash
chandra-root gparted # installed with the session
```
It runs the app through run0 with `--setenv` for `WAYLAND_DISPLAY` (absolute: root's runtime
directory is not yours, but root can open your socket) and the "use Wayland" toolkit variables
(`GDK_BACKEND`, `QT_QPA_PLATFORM`, ...), and falls back to `sudo env ...`, `doas env ...` or
`pkexec env ...`. By hand it is:
```bash
run0 --setenv=WAYLAND_DISPLAY --setenv=GDK_BACKEND=wayland --setenv=QT_QPA_PLATFORM=wayland gparted
```
Your session bus is deliberately not passed: the root app gets none, which is what you want: a root
process on your session bus could be steered by anything else running as you. Apps that write to
`XDG_RUNTIME_DIR` get root's own.
Command-line tools need nothing special: `run0 apt upgrade` in a terminal inside Chandra asks for
the password in the terminal, as usual.
### Password prompts without a terminal
Started from a terminal, run0 asks there. Started from the launcher or a keyboard shortcut there is
no terminal, so run0 and pkexec need a **polkit authentication agent** in the session. Chandra is
its own: the compositor starts chandra-polkit-agent and asks in a dialog it draws itself, showing
the command run0 will run (see [auth-dialog.md](auth-dialog.md); `chandra-ctl auth-demo` shows it
without root). If it can't start, a notice on screen says why. Turn off Settings → Session →
"Chandra password dialog" to use a third-party agent instead; `chandra-session` finds the first one
installed (polkit-gnome, mate-polkit, polkit-kde, hyprpolkitagent, xfce-polkit or lxpolkit) and
the compositor starts it then (only then). Set
`CHANDRA_POLKIT_AGENT=/path/to/agent` to always use that agent, or `CHANDRA_POLKIT_AGENT=none` if
a systemd user unit already starts one. Its output goes to the session log.
## Power actions
Suspend, restart and power off (Settings → Power, idle suspend) call `systemctl` (else `loginctl`)
as you; logind authorizes them through polkit for the active local session, so neither run0 nor
sudo is involved.