From 9c0a794c2d5140afa3b391dfad34a860c262a3a2 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 20:13:45 +0000 Subject: [PATCH] run0: install via scripts/as-root, chandra-root for GUI apps as root, session refuses root - scripts/as-root runs a command as root with run0 (systemd PID 1 + run0 present), else sudo, doas or pkexec; resolves the program to an absolute path since run0 doesn't inherit PATH. The install-session recipes use it and pass install.py by absolute path. - session/chandra-root (installed to /usr/local/bin): run a graphical app as root inside Chandra, passing WAYLAND_DISPLAY (absolute) and the Wayland toolkit variables through run0 --setenv (or sudo/doas/pkexec env). - chandra-session: refuses to run as root (CHANDRA_ALLOW_ROOT=1 overrides), fills in HOME and XDG_RUNTIME_DIR when started without a login environment, logs when the logind session has no seat (run0 --user / su / ssh), and starts an installed polkit agent once the socket is up so run0/pkexec from the launcher can prompt (CHANDRA_POLKIT_AGENT picks one or 'none'). - install.py: checks for root before --remove too; installs chandra-root. - docs/run0.md: invocations for installing, starting from a TTY, apps as root, polkit agents. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hkq6Hq7AXG85g5HyJU --- README.md | 4 ++ docs/run0.md | 94 +++++++++++++++++++++++++++++++++++++++++ justfile | 10 ++--- scripts/as-root | 25 +++++++++++ session/chandra-root | 50 ++++++++++++++++++++++ session/chandra-session | 65 ++++++++++++++++++++++++++++ session/install.py | 14 ++++-- 7 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 docs/run0.md create mode 100755 scripts/as-root create mode 100755 session/chandra-root diff --git a/README.md b/README.md index 4adc9d2..965c0f7 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ scripts/build.sh clock-binary # builds just that component Screenshots: see the end of [docs/TOOLCHAIN.md](docs/TOOLCHAIN.md). +Installing the login session (`just install-session`) asks for root through run0, else sudo/doas/pkexec; +for starting the desktop from a TTY and running apps as root inside it (`chandra-root`), see +[docs/run0.md](docs/run0.md). + ## Layout ``` diff --git a/docs/run0.md b/docs/run0.md new file mode 100644 index 0000000..5c19f1a --- /dev/null +++ b/docs/run0.md @@ -0,0 +1,94 @@ +# 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-session # or install-session-dev / install-session-native / uninstall-session +``` + +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-session` 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-session`): 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-session`). +- **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/` 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** running in the session. +`chandra-session` starts the first one it finds once the compositor is up: polkit-gnome, +mate-polkit, polkit-kde, hyprpolkitagent, xfce-polkit or lxpolkit (install one of them). Set +`CHANDRA_POLKIT_AGENT=/path/to/agent` to choose, 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. diff --git a/justfile b/justfile index f9da3c6..42e21ff 100644 --- a/justfile +++ b/justfile @@ -96,23 +96,23 @@ run-drm-native: build-release modules # Install the Chandra login session (display-manager entry): the wasm compositor + its host (the MoonBit host). Asks for root once. install-session: compositor-wasm wasmhost modules - run0 python3 session/install.py + scripts/as-root python3 "{{justfile_directory()}}/session/install.py" # Dev install: symlink the session into this checkout, so after one root install `just compositor-wasm wasmhost modules` is all a rebuild needs. install-session-dev: compositor-wasm wasmhost modules - run0 python3 session/install.py --link + scripts/as-root python3 "{{justfile_directory()}}/session/install.py" --link # Install the login session with the Rust host (host-wasm) instead of the MoonBit one. install-session-rust: compositor-wasm host-wasm modules - run0 python3 session/install.py --rust-host + scripts/as-root python3 "{{justfile_directory()}}/session/install.py" --rust-host # Install the login session with the native compositor instead. install-session-native: build-release modules - run0 python3 session/install.py --native + scripts/as-root python3 "{{justfile_directory()}}/session/install.py" --native # Remove the login session again. uninstall-session: - run0 python3 session/install.py --remove + scripts/as-root python3 "{{justfile_directory()}}/session/install.py" --remove # Start a Wayland client inside the running compositor (default: foot). client *cmd="foot": diff --git a/scripts/as-root b/scripts/as-root new file mode 100755 index 0000000..1d554aa --- /dev/null +++ b/scripts/as-root @@ -0,0 +1,25 @@ +#!/bin/sh +# Run one command as root with whatever this system uses for that: run0 (systemd 256+), else +# sudo, doas, or pkexec. Used by `just install-session` and friends. +# +# scripts/as-root python3 session/install.py +# +# run0 starts the command as a transient systemd service: it does not inherit our environment +# (PATH included; only TERM and a few others get through) and it looks the program up in the +# service manager's own PATH. So the program is resolved to an absolute path here first, which +# also makes tools that are only on our PATH (pixi, ~/.moon/bin) work. The working directory is +# kept (run0 keeps it when the target user is root), so relative paths in the arguments are fine. +set -e +[ $# -gt 0 ] || { echo "usage: scripts/as-root COMMAND [ARG...]" >&2; exit 2; } +prog=$(command -v "$1") || { echo "as-root: $1: not found" >&2; exit 127; } +shift +[ "$(id -u)" = 0 ] && exec "$prog" "$@" +# CHANDRA_AS_ROOT picks the tool (run0, sudo, doas or pkexec) instead of the first one found. +for tool in ${CHANDRA_AS_ROOT:-run0 sudo doas pkexec}; do + command -v "$tool" >/dev/null 2>&1 || continue + # run0 needs systemd as PID 1 (and systemd 256+); on other inits fall through to sudo/doas. + [ "$tool" = run0 ] && [ ! -d /run/systemd/system ] && continue + exec "$tool" "$prog" "$@" +done +echo "as-root: none of run0, sudo, doas or pkexec is available; run this as root: $prog $*" >&2 +exit 1 diff --git a/session/chandra-root b/session/chandra-root new file mode 100755 index 0000000..42cc312 --- /dev/null +++ b/session/chandra-root @@ -0,0 +1,50 @@ +#!/bin/sh +# Run a graphical app as root inside Chandra, e.g. `chandra-root gparted`. +# +# `run0 gparted` alone opens no window: run0 runs the app as a systemd service with a clean +# environment, so the app has no WAYLAND_DISPLAY and can't find the compositor. This passes the +# compositor's socket (as an absolute path, since root's XDG_RUNTIME_DIR is not ours) and the +# "use Wayland" toolkit variables through run0 --setenv. Root can open the socket in our runtime +# directory; nothing else of ours (session bus, runtime dir, HOME) is handed to the root app. +# +# Started from a terminal, run0 asks for the password there. Started from the launcher or a +# shortcut, it needs a polkit authentication agent, which chandra-session starts when one is +# installed (polkit-gnome, mate-polkit, polkit-kde, lxpolkit, ...). Without run0 (systemd < 256, +# or not systemd) it uses sudo, doas or pkexec instead; CHANDRA_AS_ROOT picks one. +[ $# -gt 0 ] || { echo "usage: chandra-root APP [ARG...]" >&2; exit 2; } +prog=$(command -v "$1") || { echo "chandra-root: $1: not found" >&2; exit 127; } +shift + +rt=${XDG_RUNTIME_DIR:-/run/user/$(id -u)} +disp=${WAYLAND_DISPLAY:-wayland-moon-0} +case "$disp" in /*) ;; *) disp="$rt/$disp" ;; esac +[ -S "$disp" ] || { echo "chandra-root: no Wayland socket at $disp (is Chandra running?)" >&2; exit 1; } + +set -- WAYLAND_DISPLAY="$disp" XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP="${XDG_CURRENT_DESKTOP:-chandra}" \ + GDK_BACKEND=wayland QT_QPA_PLATFORM=wayland SDL_VIDEODRIVER=wayland MOZ_ENABLE_WAYLAND=1 \ + ELECTRON_OZONE_PLATFORM_HINT=wayland CLUTTER_BACKEND=wayland "$prog" "$@" +[ -n "$LANG" ] && set -- LANG="$LANG" "$@" + +if [ "$(id -u)" = 0 ]; then exec env "$@"; fi +for tool in ${CHANDRA_AS_ROOT:-run0 sudo doas pkexec}; do + command -v "$tool" >/dev/null 2>&1 || continue + case "$tool" in + run0) + [ -d /run/systemd/system ] || continue + # run0 --setenv=NAME=VALUE for each variable, then the program and its arguments. + n=$# + while [ "$n" -gt 0 ]; do + a=$1; shift; n=$((n - 1)) + case "$a" in + *=*) [ -z "$done_env" ] && { set -- "$@" "--setenv=$a"; continue; } ;; + esac + done_env=1 + set -- "$@" "$a" + done + exec run0 "$@" + ;; + *) exec "$tool" env "$@" ;; + esac +done +echo "chandra-root: none of run0, sudo, doas or pkexec is available" >&2 +exit 1 diff --git a/session/chandra-session b/session/chandra-session index b3a03f9..4115afa 100755 --- a/session/chandra-session +++ b/session/chandra-session @@ -5,6 +5,27 @@ # WebAssembly component, run by chandra-host; if only the native binary was installed # (session/install.py --native), it runs that instead. share=/usr/local/share/chandra + +# The desktop runs as the user who logged in, never as root: as root (say `run0 chandra-session` +# or `sudo chandra-session`) every app would run as root, its socket would live in root's runtime +# dir where your apps can't see it, and settings would land in /root. For one app that needs +# root, use `chandra-root APP` inside the session. CHANDRA_ALLOW_ROOT=1 overrides this. +if [ "$(id -u)" = 0 ] && [ -z "$CHANDRA_ALLOW_ROOT" ]; then + echo "chandra-session: refusing to run the desktop as root; start it as yourself" \ + "(use chandra-root APP for single apps that need root)" >&2 + exit 1 +fi + +# Started from a login without the usual session environment (run0 --user=..., su, a bare +# `env -i`): fill in HOME and the runtime directory from the system instead of falling back to +# /tmp, so the socket lands where apps and chandra-root look for it. +if [ -z "$HOME" ]; then + HOME=$(getent passwd "$(id -u)" | cut -d: -f6) + export HOME +fi +if [ -z "$XDG_RUNTIME_DIR" ] && [ -d "/run/user/$(id -u)" ] && [ -O "/run/user/$(id -u)" ]; then + export XDG_RUNTIME_DIR="/run/user/$(id -u)" +fi case "$0" in /*) self="$0" ;; *) self="$PWD/$0" ;; esac # for the dbus-run-session re-exec export XDG_SESSION_TYPE=wayland @@ -65,5 +86,49 @@ if command -v gnome-keyring-daemon >/dev/null 2>&1; then [ -n "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK fi +# A logind session without a seat (made by run0 --user=..., su - or ssh rather than by logging in +# on the VT) can't open the screen and input devices through logind; say so in the log, since +# the compositor's own error ("could not take control of the session") doesn't explain why. +if [ -n "$XDG_SESSION_ID" ] && command -v loginctl >/dev/null 2>&1; then + seat=$(loginctl show-session "$XDG_SESSION_ID" -p Seat --value 2>/dev/null) + if [ -z "$seat" ]; then + echo "chandra-session: login session $XDG_SESSION_ID has no seat (started by run0, su or" \ + "ssh?); log in on the VT itself, or DRM/input access through logind will fail" >>"$log" + fi +fi + +# A polkit authentication agent, so `chandra-root APP`, run0 and pkexec started from the launcher +# or a shortcut (no terminal to ask in) can show a password prompt. Uses the first one installed, +# once the compositor's socket is up (the agents are GTK/Qt apps that need it at startup). +# CHANDRA_POLKIT_AGENT names one explicitly, or "none" to start none (say, when a systemd user +# unit already starts one). +agent=${CHANDRA_POLKIT_AGENT:-} +if [ -z "$agent" ]; then + for a in \ + /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 \ + /usr/libexec/polkit-gnome-authentication-agent-1 \ + /usr/lib/mate-polkit/polkit-mate-authentication-agent-1 \ + /usr/libexec/polkit-mate-authentication-agent-1 \ + /usr/lib/x86_64-linux-gnu/polkit-mate/polkit-mate-authentication-agent-1 \ + /usr/lib/polkit-kde-authentication-agent-1 \ + /usr/libexec/polkit-kde-authentication-agent-1 \ + /usr/libexec/kf6/polkit-kde-authentication-agent-1 \ + /usr/lib/x86_64-linux-gnu/libexec/polkit-kde-authentication-agent-1 \ + /usr/lib/hyprpolkitagent/hyprpolkitagent \ + /usr/libexec/hyprpolkitagent \ + /usr/libexec/xfce-polkit \ + /usr/lib/xfce-polkit/xfce-polkit \ + "$(command -v lxpolkit 2>/dev/null)"; do + if [ -n "$a" ] && [ -x "$a" ]; then agent=$a; break; fi + done +fi +if [ -n "$agent" ] && [ "$agent" != none ]; then + ( + i=0 + while [ ! -S "$sock" ] && [ $i -lt 200 ]; do sleep 0.1; i=$((i + 1)); done + WAYLAND_DISPLAY="$sock" GDK_BACKEND=wayland exec "$agent" + ) >>"$log" 2>&1 & +fi + exec "$@" --drm --socket "$sock" \ --theme "$share/config/themes/moon-night.json" >>"$log" 2>&1 diff --git a/session/install.py b/session/install.py index 76348de..c15e432 100755 --- a/session/install.py +++ b/session/install.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 -"""Install (or remove) the Chandra login session. Run as root, once: +"""Install (or remove) the Chandra login session. Run as root, once (`just install-session` etc. do +this through scripts/as-root, which uses run0 and falls back to sudo, doas or pkexec): run0 python3 session/install.py # install: the compositor as a wasm component run0 python3 session/install.py --link # dev: symlink into this checkout (see below) @@ -7,6 +8,9 @@ run0 python3 session/install.py --rust-host # the wasm compositor on the Rust host (host-wasm) run0 python3 session/install.py --remove # uninstall +(`sudo python3 session/install.py` works the same. run0 keeps the working directory when the +target is root, so the relative path is fine from the checkout's root.) + With --link nothing is copied: the installed files are symlinks to the build outputs in this checkout, so after one root install, `just compositor-wasm wasmhost modules` is all it takes for the next login to run your latest build (no root, no reinstall). The checkout must stay where it is. @@ -16,6 +20,7 @@ Installs: /usr/local/share/chandra/compositor.wasm the compositor, as a WebAssembly component /usr/local/bin/chandra (--native) release native compositor binary /usr/local/bin/chandra-session session launcher (starts the host if installed) + /usr/local/bin/chandra-root run one graphical app as root in the session (run0) /usr/local/share/chandra/{config,wit,assets}/ files the compositor reads at runtime /usr/local/share/chandra/modules/*.wasm the panel's wasm modules (build them first: just modules) /usr/share/wayland-sessions/chandra.desktop display-manager entry @@ -37,9 +42,13 @@ targets = [ "/usr/local/bin/chandra", "/usr/local/bin/chandra-host", "/usr/local/bin/chandra-session", + "/usr/local/bin/chandra-root", "/usr/share/wayland-sessions/chandra.desktop", ] +if os.geteuid() != 0: + sys.exit("run as root: run0 python3 session/install.py (or sudo), or `just install-session`") + if "--remove" in sys.argv: for t in targets: if os.path.lexists(t): @@ -50,8 +59,6 @@ if "--remove" in sys.argv: print("removed", share) sys.exit(0) -if os.geteuid() != 0: - sys.exit("run as root: run0 python3 session/install.py") if native: if not os.path.exists(exe): sys.exit("missing %s - run `just build-release` first" % exe) @@ -94,6 +101,7 @@ os.makedirs(share, exist_ok=True) if not native: install(component, os.path.join(share, "compositor.wasm")) install(os.path.join(root, "session/chandra-session"), "/usr/local/bin/chandra-session") +install(os.path.join(root, "session/chandra-root"), "/usr/local/bin/chandra-root") # The display-manager entry stays a real file: some managers don't follow symlinks there. put(os.path.join(root, "session/chandra.desktop"), "/usr/share/wayland-sessions/chandra.desktop", 0o644) # Read at runtime relative to the working directory (the launcher cd's here): the Interface Explorer