From a1cea0e3be39d3f23fe61125f5e1c95af3c77720 Mon Sep 17 00:00:00 2001 From: pascalyazid Date: Fri, 27 Nov 2026 18:55:33 +0100 Subject: [PATCH] feat(greeter): add themed greetd login screen - Add an AstalGreet login UI with PAM messages, reboot and shutdown. - Run it under cage via start-greeter.sh and start a uwsm-managed Hyprland session. - Include install script, polkit rule, fallback theme and README. --- greeter/README.md | 120 +++++++++++++ greeter/greeter.tsx | 371 +++++++++++++++++++++++++++++++++++++++ greeter/install.sh | 146 +++++++++++++++ greeter/start-greeter.sh | 21 +++ greeter/style.scss | 101 +++++++++++ 5 files changed, 759 insertions(+) create mode 100644 greeter/README.md create mode 100644 greeter/greeter.tsx create mode 100755 greeter/install.sh create mode 100755 greeter/start-greeter.sh create mode 100644 greeter/style.scss diff --git a/greeter/README.md b/greeter/README.md new file mode 100644 index 0000000..b7c48a5 --- /dev/null +++ b/greeter/README.md @@ -0,0 +1,120 @@ +# AGS greeter for greetd + +A small AGS (GTK4) login UI that reuses this shell's pywal theme, wired to +greetd through [AstalGreet](https://aylur.github.io/libastal/greet/). + +## How it fits together + +``` +greetd ──exec──> start-greeter.sh ──> cage ──> dbus-run-session ──> ags run greeter.tsx + ^ │ + └──────────────── AstalGreet.Greeter (GREETD_SOCK) <─────────────────────┘ +``` + +AstalGreet is only the greetd IPC client; a compositor still has to host the +GTK window. Cage is used because it is a tiny kiosk compositor. Cage does not +support `wlr-layer-shell`, so the greeter is a plain `Gtk.ApplicationWindow` +(which cage maximizes) instead of an `Astal.Window`. + +## Requirements + +- `greetd` (already installed) +- `libastal-greetd` (already installed, provides `AstalGreet-0.1`) +- `ags` (already installed) +- `cage` — `pacman -S cage` +- `dart-sass` (already installed, used to compile the theme) +- a `greeter` user in the `video` group (already present) + +## Install + +```sh +sudo pacman -S cage +sudo ./greeter/install.sh +``` + +The script: + +- copies `greeter.tsx`, `style.scss`, and `start-greeter.sh` to + `/etc/greetd/ags` (the `greeter` user cannot read `/home/pascal`, which is + `0700`) +- creates `/var/lib/greetd/{run,.cache}` owned by `greeter` +- compiles a fallback theme and seeds the current pywal theme/wallpaper +- installs `/etc/polkit-1/rules.d/49-greetd-greeter.rules` so the `greeter` + user can reboot/power off from the login screen +- writes `/etc/greetd/config.toml` (backup at `config.toml.bak`) + +**Keep a TTY available (Ctrl+Alt+F2)** before restarting greetd so you can +recover if the greeter fails. + +```sh +sudo systemctl restart greetd +``` + +## Theming + +`theme.ts` now calls `exportGreeterTheme()` on startup and after every +`setTheme()`: + +- `greeter/style.scss` is compiled with the current `wal` palette to + `/var/tmp/ags-greeter/greeter.css` +- `~/.cache/current_wallpaper.jpg` is copied to + `/var/tmp/ags-greeter/wallpaper.jpg` + +`/var/tmp` is used because it survives reboots and is readable by the +`greeter` user. `/tmp` is empty at boot (the greeter runs before login) and +`~/.cache/wal` is unreadable by `greeter`. + +If the exported files do not exist yet, the greeter uses +`/etc/greetd/ags/fallback.css`, then an inline fallback. + +## Preview + +In a running Hyprland session: + +```sh +ags run ./greeter/greeter.tsx +``` + +This shows the real UI, but pressing "Log in" reports that the greeter must be +started by greetd (there is no `GREETD_SOCK` in your session). + +## Login messages and power buttons + +PAM info/error messages are shown in the card. greetd often reports only a +result code (for example `authenticate: AUTH_ERR`), so the greeter maps those +to readable text such as "Incorrect password. Please try again." + +The card also has reboot and shut-down buttons. They call +`/usr/bin/systemctl`; the polkit rule installed by `install.sh` allows the +`greeter` user to perform those two actions without an authentication agent. + +## Debugging + +`greetd` prefixes its command with `exec`, so the chain lives in +`start-greeter.sh`. Its output (cage/AGS) is logged to: + +```sh +cat /tmp/greeter.log +``` + +If greetd hits `start-limit-hit`, reset it after fixing: + +```sh +sudo systemctl reset-failed greetd +sudo systemctl start greetd +``` + +## Notes / limitations + +- `cage -m last` (in `start-greeter.sh`) puts the greeter on one monitor only. + Use `-m extend` to span all monitors, or switch to a layer-shell compositor + (sway/Hyprland) if you want per-monitor greeter windows. +- After a successful login the greeter starts + `/usr/bin/uwsm start -e -D Hyprland hyprland.desktop` (the same command as + `/usr/share/wayland-sessions/hyprland-uwsm.desktop`). No greetd-specific + uwsm configuration is needed; `~/.config/uwsm/env*` is still read from your + home when uwsm starts the user session. If you stop using uwsm, replace + `SESSION_CMD` in `greeter.tsx` with `["/usr/bin/start-hyprland"]`. +- The username field defaults to the user recorded in + `/etc/greetd/ags/default-user`; it can be edited before logging in. +- The greeter does not implement reboot/shutdown buttons yet. diff --git a/greeter/greeter.tsx b/greeter/greeter.tsx new file mode 100644 index 0000000..3f5d15d --- /dev/null +++ b/greeter/greeter.tsx @@ -0,0 +1,371 @@ +import app from "ags/gtk4/app" +import { Gtk } from "ags/gtk4" +import Greet from "gi://AstalGreet?version=0.1" +import GLib from "gi://GLib?version=2.0" +import Gio from "gi://Gio?version=2.0" +import { execAsync } from "ags/process" + +// Filled in by greeter/install.sh. Falls back to the local user for preview. +const DEFAULT_USER_FILE = "/etc/greetd/ags/default-user" + +// Matches /usr/share/wayland-sessions/hyprland-uwsm.desktop. +// If you ever stop using uwsm, replace this with ["/usr/bin/start-hyprland"]. +const SESSION_CMD = [ + "/usr/bin/uwsm", + "start", + "-e", + "-D", + "Hyprland", + "hyprland.desktop", +] +const SESSION_ENV = [ + "XDG_SESSION_TYPE=wayland", + "XDG_CURRENT_DESKTOP=Hyprland", + "XDG_SESSION_DESKTOP=Hyprland", +] + +// Written by exportGreeterTheme() in theme.ts whenever the shell theme changes. +const THEME_CSS = "/var/tmp/ags-greeter/greeter.css" +const FALLBACK_CSS = "/etc/greetd/ags/fallback.css" +const WALLPAPER = "/var/tmp/ags-greeter/wallpaper.jpg" + +const INLINE_CSS = ` +window.Greeter { background-color: #1e1e1e; color: #d4d4d4; } +window.Greeter .scrim { background-color: rgba(30, 30, 30, 0.45); padding: 32px; } +window.Greeter .login-card { + background-color: rgba(30, 30, 30, 0.9); + border: 1px solid rgba(212, 212, 212, 0.15); + border-radius: 16px; + padding: 32px; + min-width: 320px; +} +window.Greeter .greeting { font-size: 2em; font-weight: bold; } +window.Greeter .subtitle { opacity: 0.65; margin-bottom: 8px; } +window.Greeter entry { + background-color: rgba(212, 212, 212, 0.08); + border: 1px solid rgba(212, 212, 212, 0.15); + border-radius: 8px; + padding: 10px 12px; + color: #d4d4d4; +} +window.Greeter button.login-btn { + background-color: #6a9955; + color: #1e1e1e; + border: none; + border-radius: 8px; + padding: 10px; + font-weight: bold; +} +window.Greeter .messages { min-height: 1.2em; } +window.Greeter .message { font-size: 0.9em; opacity: 0.85; } +window.Greeter .message.error { color: #f44747; } +window.Greeter .message.info { color: #569cd6; } +window.Greeter .power-row { margin-top: 4px; } +window.Greeter button.power-action { + background-color: rgba(212, 212, 212, 0.06); + border: 1px solid rgba(212, 212, 212, 0.1); + border-radius: 8px; + padding: 8px; + min-width: 36px; + min-height: 36px; +} +window.Greeter button.power-action:hover { + background-color: rgba(212, 212, 212, 0.12); +} +window.Greeter button.power-action image { -gtk-icon-size: 18px; } +` + +// greetd only reports PAM result codes in the error description, e.g. +// "authenticate: AUTH_ERR". Map them to something human readable. +const AUTH_ERROR_MESSAGES: Record = { + AUTH_ERR: "Incorrect password. Please try again.", + MAXTRIES: "Too many failed attempts.", + CRED_EXPIRED: "Your credentials have expired.", + ACCT_EXPIRED: "Your account has expired.", + CRED_INSUFFICIENT: "Insufficient credentials.", + USER_UNKNOWN: "Unknown username.", + PERM_DENIED: "Permission denied.", + SERVICE_ERR: "Authentication service error.", + ABORT: "Authentication aborted.", +} + +function errorDescription(error: any): string { + try { + if (typeof error?.get_description === "function") + return error.get_description() ?? "" + return typeof error?.description === "string" ? error.description : "" + } catch { + return "" + } +} + +function errorType(error: any): number | null { + try { + if (typeof error?.get_error_type === "function") + return error.get_error_type() + return typeof error?.error_type === "number" ? error.error_type : null + } catch { + return null + } +} + +function humanizeError(error: any): string { + const description = errorDescription(error) + const code = description.match(/\b([A-Z][A-Z_]+)\b\s*$/)?.[1] + + if (code && AUTH_ERROR_MESSAGES[code]) return AUTH_ERROR_MESSAGES[code] + + // Only show the raw description when it is not a cryptic PAM result code. + if (description && !/^[a-z_]+: [A-Z_]+$/.test(description)) return description + + // AstalGreet.ErrorType.AUTH_ERROR is 0 + if (errorType(error) === 0) return "Authentication failed. Please try again." + + return description || "Login failed" +} + +function readTextFile(path: string): string | null { + try { + const [ok, contents] = GLib.file_get_contents(path) + if (!ok) return null + let text = "" + for (const byte of contents) text += String.fromCharCode(byte) + return text.trim() + } catch { + return null + } +} + +function defaultUser() { + return readTextFile(DEFAULT_USER_FILE) || "pascal" +} + +function themeCss() { + if (GLib.file_test(THEME_CSS, GLib.FileTest.EXISTS)) return THEME_CSS + if (GLib.file_test(FALLBACK_CSS, GLib.FileTest.EXISTS)) return FALLBACK_CSS + return INLINE_CSS +} + +function Greeter() { + const user = defaultUser() + const underGreetd = GLib.getenv("GREETD_SOCK") !== null + const greeter = new Greet.Greeter() + + let userEntry: Gtk.Entry | null = null + let passEntry: Gtk.Entry | null = null + let messageList: Gtk.Box | null = null + let loginButton: Gtk.Button | null = null + let busy = false + let lastPamError = "" + + const messageWidgets: Gtk.Label[] = [] + + const appendMessage = (text: string, kind: "error" | "info" = "info") => { + if (!messageList || !text) return + + const label = new Gtk.Label({ label: text, wrap: true, xalign: 0 }) + label.set_css_classes(["message", kind]) + messageList.append(label) + messageWidgets.push(label) + + while (messageWidgets.length > 4) { + const old = messageWidgets.shift() + if (old) messageList.remove(old) + } + } + + const clearMessages = () => { + if (!messageList) return + for (const label of messageWidgets) messageList.remove(label) + messageWidgets.length = 0 + } + + const reset = () => { + busy = false + if (loginButton) loginButton.sensitive = true + if (passEntry) { + passEntry.set_text("") + passEntry.grab_focus() + } + } + + const runPower = (action: "reboot" | "poweroff") => { + if (!underGreetd) { + appendMessage("This greeter must be started by greetd", "error") + return + } + + appendMessage(action === "reboot" ? "Rebooting…" : "Shutting down…") + execAsync(["/usr/bin/systemctl", action]).catch((error: unknown) => { + appendMessage(`Failed to ${action}: ${`${error}`.trim()}`, "error") + }) + } + + const login = () => { + if (busy) return + + if (!underGreetd) { + appendMessage("This greeter must be started by greetd", "error") + return + } + + const username = userEntry?.get_text().trim() || user + if (!passEntry?.get_text()) { + appendMessage("Enter your password", "error") + passEntry?.grab_focus() + return + } + + busy = true + if (loginButton) loginButton.sensitive = false + lastPamError = "" + clearMessages() + greeter.create_session(username) + } + + greeter.connect("visible-request", (_src: unknown, _message: string) => { + lastPamError = "" + greeter.post_auth(userEntry?.get_text().trim() || user) + }) + + greeter.connect("secret-request", (_src: unknown, _message: string) => { + lastPamError = "" + greeter.post_auth(passEntry?.get_text() ?? "") + }) + + greeter.connect("info-message", (_src: unknown, message: string) => { + appendMessage(message) + }) + + greeter.connect("error-message", (_src: unknown, message: string) => { + lastPamError = message + appendMessage(message, "error") + }) + + greeter.connect("cancelled", (_src: unknown, error: any) => { + if (!lastPamError) appendMessage(humanizeError(error), "error") + lastPamError = "" + reset() + }) + + greeter.connect("authenticated", () => { + greeter.start_session( + SESSION_CMD, + SESSION_ENV, + (_src: unknown, res: any) => { + try { + greeter.start_session_finish(res) + } catch (error: any) { + appendMessage(error?.message ?? `${error}`, "error") + reset() + return + } + + // greetd starts the session once the greeter process exits. + app.quit() + }, + ) + }) + + return ( + { + window.present() + GLib.idle_add(GLib.PRIORITY_DEFAULT, () => { + passEntry?.grab_focus() + return GLib.SOURCE_REMOVE + }) + }} + > + { + const content = overlay.get_child() + const picture = new Gtk.Picture() + picture.set_content_fit(Gtk.ContentFit.COVER) + picture.set_hexpand(true) + picture.set_vexpand(true) + if (GLib.file_test(WALLPAPER, GLib.FileTest.EXISTS)) { + picture.set_file(Gio.File.new_for_path(WALLPAPER)) + } + overlay.set_child(picture) + if (content) overlay.add_overlay(content) + }} + > + + + + + + ) +} + +app.start({ + css: themeCss(), + main() { + Greeter() + }, +}) diff --git a/greeter/install.sh b/greeter/install.sh new file mode 100755 index 0000000..a7153d6 --- /dev/null +++ b/greeter/install.sh @@ -0,0 +1,146 @@ +#!/bin/sh +# Installs the AGS greeter for greetd. +# +# Usage: sudo ./greeter/install.sh +# +# This replaces /etc/greetd/config.toml (a .bak copy is kept) and copies the +# greeter into /etc/greetd/ags so the `greeter` user can read it. +set -eu + +if [ "$(id -u)" -ne 0 ]; then + echo "Run this script with sudo." >&2 + exit 1 +fi + +SRC=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +TARGET_USER=${SUDO_USER:-pascal} + +for bin in cage sass dbus-run-session; do + if ! command -v "$bin" >/dev/null 2>&1; then + echo "Missing dependency: $bin" >&2 + case "$bin" in + cage) echo "Install it with: pacman -S cage" >&2 ;; + sass) echo "Install it with: pacman -S dart-sass" >&2 ;; + esac + exit 1 + fi +done + +if ! id greeter >/dev/null 2>&1; then + echo "The 'greeter' user does not exist. Create it with:" >&2 + echo " useradd -M -G video greeter" >&2 + exit 1 +fi + +install -d -m 755 /etc/greetd/ags +install -d -o greeter -g greeter -m 700 \ + /var/lib/greetd /var/lib/greetd/run /var/lib/greetd/.cache + +install -m 644 -o root -g root "$SRC/greeter.tsx" /etc/greetd/ags/greeter.tsx +install -m 644 -o root -g root "$SRC/style.scss" /etc/greetd/ags/style.scss +install -m 755 -o root -g root "$SRC/start-greeter.sh" /etc/greetd/ags/start-greeter.sh +rm -f /etc/greetd/ags/*.map +printf '%s\n' "$TARGET_USER" > /etc/greetd/ags/default-user +chmod 644 /etc/greetd/ags/default-user + +# Compile a fallback theme in case the shell has never exported one. +# /tmp is sticky and fs.protected_regular is enabled, so root cannot +# overwrite an existing user-owned file there. Remove it first and give it +# back to the user at the end so theme switching keeps working. +rm -f /tmp/ags-colors.scss +cat > /tmp/ags-colors.scss <<'EOF' +$wallpaper: ""; +$background: #1e1e1e; +$foreground: #d4d4d4; +$cursor: #d4d4d4; +$color0: #1e1e1e; +$color1: #f44747; +$color2: #6a9955; +$color3: #d7ba7d; +$color4: #569cd6; +$color5: #c586c0; +$color6: #4ec9b0; +$color7: #d4d4d4; +$color8: #808080; +$color9: #f44747; +$color10: #6a9955; +$color11: #d7ba7d; +$color12: #569cd6; +$color13: #c586c0; +$color14: #4ec9b0; +$color15: #d4d4d4; +EOF +sass --no-source-map /etc/greetd/ags/style.scss /etc/greetd/ags/fallback.css +chmod 644 /etc/greetd/ags/fallback.css + +# Seed the greeter theme from the user's current pywal state. +USER_HOME=$(getent passwd "$TARGET_USER" | cut -d: -f6 || true) +if [ -n "${USER_HOME:-}" ] && [ -f "$USER_HOME/.cache/wal/colors.scss" ]; then + install -d -m 755 -o "$TARGET_USER" -g "$TARGET_USER" /var/tmp/ags-greeter + rm -f /var/tmp/ags-greeter/greeter.css \ + /var/tmp/ags-greeter/greeter.css.map /var/tmp/ags-greeter/wallpaper.jpg + cp "$USER_HOME/.cache/wal/colors.scss" /tmp/ags-colors.scss + sass --no-source-map /etc/greetd/ags/style.scss \ + /var/tmp/ags-greeter/greeter.css + if [ -f "$USER_HOME/.cache/current_wallpaper.jpg" ]; then + cp "$USER_HOME/.cache/current_wallpaper.jpg" \ + /var/tmp/ags-greeter/wallpaper.jpg + fi + chown "$TARGET_USER:$TARGET_USER" \ + /var/tmp/ags-greeter/greeter.css /var/tmp/ags-greeter/wallpaper.jpg \ + 2>/dev/null || true +fi + +if [ -f /etc/greetd/config.toml ]; then + cp /etc/greetd/config.toml /etc/greetd/config.toml.bak +fi + +chown "$TARGET_USER:$TARGET_USER" /tmp/ags-colors.scss 2>/dev/null || true + +cat > /etc/greetd/config.toml <<'EOF' +[terminal] +vt = 1 + +[default_session] +# greetd prefixes the command with `exec`, so it must be a single executable. +# start-greeter.sh sets HOME/XDG dirs, starts cage, then AGS. +command = "/etc/greetd/ags/start-greeter.sh" +user = "greeter" +EOF + +# Allow the greeter user to power off/reboot without an auth agent. +if [ ! -d /etc/polkit-1/rules.d ]; then + install -d -m 755 /etc/polkit-1/rules.d +fi +cat > /etc/polkit-1/rules.d/49-greetd-greeter.rules <<'EOF' +polkit.addRule(function(action, subject) { + if (subject.user !== "greeter") + return null; + + const allowed = [ + "org.freedesktop.login1.power-off", + "org.freedesktop.login1.power-off-multiple-sessions", + "org.freedesktop.login1.reboot", + "org.freedesktop.login1.reboot-multiple-sessions", + ]; + + if (allowed.indexOf(action.id) !== -1) + return polkit.Result.YES; + + return null; +}); +EOF +chmod 644 /etc/polkit-1/rules.d/49-greetd-greeter.rules + +cat <`, so the command must be a single executable +# (not a `mkdir ... && cage ...` compound list). +set -eu + +LOG=/tmp/greeter.log +exec >>"$LOG" 2>&1 +echo "=== $(date -Is) starting greeter ===" + +mkdir -p /var/lib/greetd/run /var/lib/greetd/.cache +chmod 700 /var/lib/greetd/run + +export HOME=/var/lib/greetd +export XDG_RUNTIME_DIR=/var/lib/greetd/run +export XDG_CACHE_HOME=/var/lib/greetd/.cache + +exec /usr/bin/cage -m last -s -d -- \ + /usr/bin/dbus-run-session -- \ + /usr/bin/ags run /etc/greetd/ags/greeter.tsx diff --git a/greeter/style.scss b/greeter/style.scss new file mode 100644 index 0000000..c9d5bd0 --- /dev/null +++ b/greeter/style.scss @@ -0,0 +1,101 @@ +@use "/tmp/ags-colors.scss" as wal; + +window.Greeter { + background-color: wal.$background; + color: wal.$foreground; + + .scrim { + background-color: rgba(wal.$background, 0.45); + padding: 32px; + } + + .login-card { + background-color: rgba(wal.$background, 0.9); + border: 1px solid rgba(wal.$foreground, 0.12); + border-radius: 16px; + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45); + padding: 32px; + min-width: 320px; + + .greeting { + font-size: 2em; + font-weight: bold; + } + + .subtitle { + opacity: 0.65; + margin-bottom: 8px; + } + } + + entry { + background-color: rgba(wal.$foreground, 0.08); + border: 1px solid rgba(wal.$foreground, 0.12); + border-radius: 8px; + padding: 10px 12px; + color: wal.$foreground; + caret-color: wal.$color2; + transition: border-color 0.2s ease-in-out; + + &:focus { + border-color: wal.$color2; + } + } + + button.login-btn { + background-color: wal.$color2; + color: wal.$background; + border: none; + border-radius: 8px; + padding: 10px; + font-weight: bold; + transition: background-color 0.2s ease-in-out; + + &:hover { + background-color: rgba(wal.$color2, 0.85); + } + + &:disabled { + opacity: 0.6; + } + } + + .messages { + min-height: 1.2em; + } + + .message { + font-size: 0.9em; + opacity: 0.85; + + &.error { + color: wal.$color1; + } + + &.info { + color: wal.$color4; + } + } + + .power-row { + margin-top: 4px; + } + + button.power-action { + background-color: rgba(wal.$foreground, 0.06); + border: 1px solid rgba(wal.$foreground, 0.1); + border-radius: 8px; + padding: 8px; + min-width: 36px; + min-height: 36px; + transition: background-color 0.2s ease-in-out; + + &:hover { + background-color: rgba(wal.$foreground, 0.12); + } + + image { + -gtk-icon-size: 18px; + } + } +}