diff --git a/agents/skills/install-scripts.md b/agents/skills/install-scripts.md index aa993e5e54bd..0db1badab51c 100644 --- a/agents/skills/install-scripts.md +++ b/agents/skills/install-scripts.md @@ -12,6 +12,8 @@ commands and reusable setup leaves: - avoid `exit` in sourced setup scripts unless intentionally aborting setup. - use `$OMARCHY_INSTALL` and `$OMARCHY_PATH` instead of hard-coded Omarchy paths. - keep root-scoped hardware setup under `install/hardware/` and orchestrate it through `install/hardware/all.sh`. +- in an image build every `run_logged` leaf in `install/hardware/all.sh` is queued and runs on the machine's first boot instead (`bin/omarchy-provision-hardware`, see `docs/file-layout.md`), possibly offline, using packages already in the image, and before any user exists; keep those leaves idempotent and put nothing but `run_logged` lines in `all.sh`. +- gate platform-specific setup on `omarchy-hw-platform` or a predicate built on it, not on `uname -m`; architecture checks are only for ABI, binary availability or a repository's `$arch` (see `docs/file-layout.md`). - keep every per-user setup leaf under `install/user/` (including `install/user/hardware/` and `install/user/first-run/`) so it is clear what must run for each user. - The base install supplies matching kernel headers before hardware setup. DKMS installers should install their driver packages and assume the headers exist. - prefer helper commands for package and command checks where available. diff --git a/bin/omarchy-apply-hardware b/bin/omarchy-apply-hardware index 89eb8ab12f9d..9f873d5a2ff3 100755 --- a/bin/omarchy-apply-hardware +++ b/bin/omarchy-apply-hardware @@ -19,6 +19,9 @@ for diagnostics or after hardware/package updates. --defer-provisioning runs without an install user (deferred-provisioning installs create the user at first boot); group grants are recorded in /var/lib/omarchy/provisioning/groups. + +In an image build (/var/lib/omarchy/image/target exists) nothing runs here: every step is queued +for omarchy-provision-hardware on the machine's first boot. USAGE } @@ -66,10 +69,21 @@ else fi export OMARCHY_INSTALL_USER="$install_user" +# Only omarchy-provision-hardware runs steps as an image's first boot. +unset OMARCHY_IMAGE_DEFERRED_HARDWARE export OMARCHY_PATH="${OMARCHY_PATH:-/usr/share/omarchy}" export OMARCHY_INSTALL="${OMARCHY_INSTALL:-$OMARCHY_PATH/install}" export OMARCHY_INSTALL_LOG_FILE="${OMARCHY_INSTALL_LOG_FILE:-/var/log/omarchy-install.log}" export PATH="$OMARCHY_PATH/bin:$PATH" source "$OMARCHY_INSTALL/helpers/logging.sh" -source "$OMARCHY_INSTALL/hardware/all.sh" +source "$OMARCHY_INSTALL/helpers/image-target.sh" + +omarchy_image_init +omarchy_image_lock +if omarchy_image_manifest_present; then + omarchy_image_read_manifest + omarchy_image_defer_hardware +else + source "$OMARCHY_INSTALL/hardware/all.sh" +fi diff --git a/bin/omarchy-audio-input-set-default b/bin/omarchy-audio-input-set-default index 280042d2daa3..97a8c2c70297 100755 --- a/bin/omarchy-audio-input-set-default +++ b/bin/omarchy-audio-input-set-default @@ -15,6 +15,14 @@ fi wpctl set-default "$node_id" 2>/dev/null || true pactl set-default-source "$source_name" 2>/dev/null || true -pactl list short source-outputs 2>/dev/null | awk '{ print $1 }' | while read -r output; do +# A filter's own capture (the input half of a filter chain, loopback or echo +# canceller, which PipeWire joins to its output by node.link-group) is the +# processing, not an app: moving it would feed the filter from the new input, or +# from its own output. pactl translates the listing, so it is read in the C +# locale. +LC_ALL=C pactl list source-outputs 2>/dev/null | awk ' + /^Source Output #/ { if (id != "" && !filter) print id; id = substr($3, 2); filter = 0 } + /^[[:space:]]*node\.link-group = / { filter = 1 } + END { if (id != "" && !filter) print id }' | while read -r output; do [[ -n $output ]] && pactl move-source-output "$output" "$source_name" 2>/dev/null || true done diff --git a/bin/omarchy-audio-sink-availability b/bin/omarchy-audio-sink-availability index 531b5bffafa8..7e794b412d72 100755 --- a/bin/omarchy-audio-sink-availability +++ b/bin/omarchy-audio-sink-availability @@ -1,14 +1,27 @@ #!/bin/bash -# omarchy:summary=Print PulseAudio sink availability for the shell +# omarchy:summary=Print PulseAudio sink or source availability for the shell # omarchy:group=audio +# omarchy:args=[sources] + +# With "sources", print source availability instead: a jack input such as a +# headset microphone reports its port unavailable while nothing is plugged in. +# pactl translates the listing ("not available" too), so it is read in the C +# locale. +kind=sinks +header="Sink #" +if [[ ${1:-} == "sources" ]]; then + kind=sources + header="Source #" +fi # A speaker tuning is a virtual sink in front of the real speakers. Both exist # in the graph, but selecting the physical one would only bypass the tuning, so # report it unavailable and keep it out of the output list. -fronted="$(omarchy-audio-tuning fronted-sink 2>/dev/null || true)" +fronted="" +[[ $kind == sinks ]] && fronted="$(omarchy-audio-tuning fronted-sink 2>/dev/null || true)" -pactl list sinks 2>/dev/null | awk -v fronted="$fronted" ' +LC_ALL=C pactl list "$kind" 2>/dev/null | awk -v fronted="$fronted" -v header="$header" ' function emit_sink() { if (name == "") return if (fronted != "" && name == fronted) { @@ -18,7 +31,7 @@ pactl list sinks 2>/dev/null | awk -v fronted="$fronted" ' print name "\t" ((port_count == 0 || available) ? 1 : 0) } - /^Sink #/ { + index($0, header) == 1 { emit_sink() name = "" in_ports = 0 diff --git a/bin/omarchy-audio-source-level b/bin/omarchy-audio-source-level new file mode 100755 index 000000000000..c570a83cc1fb --- /dev/null +++ b/bin/omarchy-audio-source-level @@ -0,0 +1,76 @@ +#!/usr/bin/python3 +# omarchy:summary=Print an audio input's level for the audio panel +# omarchy:group=audio +# omarchy:args= +# omarchy:hidden=true + +# The panel meters a typed input with Quickshell's peak monitor, which takes +# only nodes of an exact media class. A virtual source (an +# Audio/Source/Virtual) is untyped there, so this records it, as apps do, and +# prints one peak per 40 ms on the panel's scale (the cube root of the sample +# peak). Samples are inspected, never kept. + +import array +import ctypes +import os +import signal +import subprocess +import sys + +RATE = 48000 +CHUNK = RATE // 25 * 4 +NAME = 'omarchy-audio-level' + + +def level(samples): + peak = max((abs(sample) for sample in samples), default=0.0) + return min(1.0, peak) ** (1 / 3) + + +def die_with(parent): + # parec must not outlive this helper, even one killed outright; a parent + # already gone before the request would never send the signal. + def request(): + try: + ctypes.CDLL(None, use_errno=True).prctl(1, signal.SIGTERM) + except (AttributeError, OSError): + pass + if os.getppid() != parent: + os._exit(0) + return request + + +def main(): + if len(sys.argv) != 2 or not sys.argv[1] or sys.argv[1].startswith('-'): + print('Usage: omarchy-audio-source-level ', file=sys.stderr) + return 2 + try: + process = subprocess.Popen(['parec', '--device=' + sys.argv[1], '--raw', '--format=float32le', '--channels=1', + '--rate=' + str(RATE), '--latency-msec=40', '--client-name=' + NAME, + '--property=node.name=' + NAME, '--property=media.category=Monitor'], + stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + preexec_fn=die_with(os.getpid())) + except OSError as error: + print('Could not read the input level: ' + str(error), file=sys.stderr) + return 1 + signal.signal(signal.SIGTERM, lambda *_: sys.exit(0)) + pending = b'' + try: + while True: + chunk = os.read(process.stdout.fileno(), CHUNK) + if not chunk: + return 0 + pending += chunk + while len(pending) >= CHUNK: + print('%.3f' % level(array.array('f', pending[:CHUNK])), flush=True) + pending = pending[CHUNK:] + except (BrokenPipeError, KeyboardInterrupt): + os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno()) + return 0 + finally: + process.kill() + process.wait() + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/bin/omarchy-battery-present b/bin/omarchy-battery-present index fb221e8ccf22..90883a67012e 100755 --- a/bin/omarchy-battery-present +++ b/bin/omarchy-battery-present @@ -2,10 +2,16 @@ # omarchy:summary=Returns true if a battery is present on the system. -for bat in /sys/class/power_supply/BAT*; do - [[ -r $bat/present ]] && - [[ $(cat $bat/present) == "1" ]] && - [[ $(cat $bat/type) == "Battery" ]] && +# Not every battery is called BAT*: Apple Silicon reports macsmc-battery and +# some Framework and Chromebook firmware CMB0, so decide by type, not by name. +power_supply_path="${OMARCHY_POWER_SUPPLY_PATH:-/sys/class/power_supply}" + +for bat in "$power_supply_path"/*; do + [[ -r $bat/present && $(<"$bat/present") == "1" ]] || continue + [[ -r $bat/type && $(<"$bat/type") == "Battery" ]] || continue + # A wireless mouse or keyboard registers a Battery too, scoped Device. The + # one that powers the machine is scoped System, or unscoped on older drivers. + [[ -r $bat/scope && $(<"$bat/scope") == "Device" ]] && continue exit 0 done diff --git a/bin/omarchy-battery-status b/bin/omarchy-battery-status index cc0a6024fbaf..13de4454f7cc 100755 --- a/bin/omarchy-battery-status +++ b/bin/omarchy-battery-status @@ -18,11 +18,23 @@ case "${1:-}" in ;; esac -battery=$(upower -e 2>/dev/null | grep BAT | head -n 1) +# UPower names the device after the sysfs entry, and only some kernels call +# that BAT*: Apple Silicon reports battery_macsmc_battery. Match on the class, +# then take the first one that powers the machine: a wireless mouse or keyboard +# is a battery_ device too, and UPower marks it "power supply: no". +battery="" +battery_info="" +while read -r candidate; do + [[ -n $candidate ]] || continue + candidate_info=$(upower -i "$candidate" 2>/dev/null) || continue + if grep -qE '^\s*power supply:\s+yes' <<<"$candidate_info"; then + battery=$candidate + battery_info=$candidate_info + break + fi +done < <(upower -e 2>/dev/null | grep '/battery_') [[ -z $battery ]] && exit 0 -battery_info=$(upower -i "$battery") - percentage=$(awk '/percentage/ { print int($2); exit }' <<<"$battery_info") capacity=$(awk '/energy-full:/ { printf "%d", $2; exit }' <<<"$battery_info") time_remaining=$(awk '/time to (empty|full)/ { @@ -56,7 +68,10 @@ elif [[ -r $battery_path/current_now && -r $battery_path/voltage_now ]]; then 'BEGIN { print microamps * microvolts / 1000000000000 }') fi +# Some kernels sign power_now by direction (negative while discharging on +# Apple Silicon); the state field already says which way it flows. power_rate=$(awk -v rate="${power_rate_raw:-0}" 'BEGIN { + if (rate < 0) rate = -rate rounded = sprintf("%.1f", rate) sub(/\.0$/, "", rounded) print rounded @@ -65,8 +80,12 @@ state=$(awk '/state/ { print $2; exit }' <<<"$battery_info") threshold_start=$(awk '/charge-start-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info") threshold_end=$(awk '/charge-end-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info") -[[ -z $threshold_end ]] && threshold_end=$(cat "$power_supply_path"/BAT*/charge_control_end_threshold 2>/dev/null | head -1) -[[ -z $threshold_start ]] && threshold_start=$(cat "$power_supply_path"/BAT*/charge_control_start_threshold 2>/dev/null | head -1) +# Read the thresholds from the battery UPower resolved rather than a BAT* glob, +# which misses every battery with another name. +[[ -n $threshold_end || ! -r $battery_path/charge_control_end_threshold ]] || threshold_end=$(<"$battery_path/charge_control_end_threshold") +[[ -n $threshold_start || ! -r $battery_path/charge_control_start_threshold ]] || threshold_start=$(<"$battery_path/charge_control_start_threshold") +[[ -n $threshold_end ]] || threshold_end=$(cat "$power_supply_path"/BAT*/charge_control_end_threshold 2>/dev/null | head -1) +[[ -n $threshold_start ]] || threshold_start=$(cat "$power_supply_path"/BAT*/charge_control_start_threshold 2>/dev/null | head -1) ac_online=false for supply in "$power_supply_path"/*; do @@ -107,7 +126,9 @@ if [[ $shell_output == "true" ]]; then printf 'size\t%s\n' "${capacity}Wh" printf 'time\t%s\n' "$time_remaining" - cycles=$(cat "$power_supply_path"/BAT*/cycle_count 2>/dev/null | head -1) + cycles="" + [[ ! -r $battery_path/cycle_count ]] || cycles=$(<"$battery_path/cycle_count") + [[ -n $cycles ]] || cycles=$(cat "$power_supply_path"/BAT*/cycle_count 2>/dev/null | head -1) [[ -n $cycles ]] && printf 'cycles\t%s\n' "$cycles" diff --git a/bin/omarchy-brightness-display b/bin/omarchy-brightness-display index a6a1e8304bfd..5a3e534e27d4 100755 --- a/bin/omarchy-brightness-display +++ b/bin/omarchy-brightness-display @@ -40,7 +40,30 @@ use_apple_display() { } use_ddc_display() { - [[ -n $monitor ]] && ! monitor_is_internal + [[ -n $monitor ]] && ! monitor_is_internal && monitor_may_have_ddc +} + +# A platform whose display driver registers no DDC channel says so in its +# displays.conf (see docs/file-layout.md): there a probe finds only unrelated +# I2C buses, slowly, so only a connector with a ddc node is probed. Everywhere +# else every external monitor is. +monitor_may_have_ddc() { + local displays_conf=/usr/share/omarchy-platform/displays.conf directive argument + [[ -r $displays_conf ]] || return 0 + + while IFS=$' \t\r' read -r directive argument || [[ -n $directive ]]; do + if [[ $directive == "ddc-require-connector-ddc" && -z $argument ]]; then + compgen -G "/sys/class/drm/card*-$monitor/ddc" >/dev/null + return + fi + done <"$displays_conf" + return 0 +} + +# The kernel backlight is the built-in panel's, so an external monitor without +# DDC fails rather than dimming the panel. +use_backlight_display() { + [[ -z $monitor ]] || monitor_is_internal } if (( $# == 0 )); then @@ -50,6 +73,8 @@ if (( $# == 0 )); then elif use_ddc_display; then omarchy-brightness-display-ddc "$monitor" exit + elif ! use_backlight_display; then + exit 1 fi device="$(omarchy-hw-display)" || exit 1 @@ -85,6 +110,8 @@ if use_apple_display; then elif use_ddc_display; then brightness="$(omarchy-brightness-display-ddc "$monitor" "$step")" || exit 1 (( no_osd )) || omarchy-osd -i brightness -p "$brightness" +elif ! use_backlight_display; then + exit 1 else # Current device highlighted device="$(omarchy-hw-display)" || exit 1 diff --git a/bin/omarchy-brightness-keyboard b/bin/omarchy-brightness-keyboard index f323b94f53ad..2b21791dd58e 100755 --- a/bin/omarchy-brightness-keyboard +++ b/bin/omarchy-brightness-keyboard @@ -13,7 +13,7 @@ direction="${1:-up}" # Find keyboard backlight device (look for *kbd_backlight* pattern in leds class). device="" -for candidate in /sys/class/leds/*kbd_backlight*; do +for candidate in "${OMARCHY_LEDS_DIR:-/sys/class/leds}"/*kbd_backlight*; do if [[ -e $candidate ]]; then device="$(basename "$candidate")" break @@ -25,11 +25,31 @@ if [[ -z $device ]]; then exit 1 fi +# Record each level chosen here, and "blank" for the lock screen's off, so the +# ambient light loop (omarchy-brightness-keyboard-auto) can tell the lock's +# blank and leftovers from keys turned off on purpose, by these keys or a +# firmware Fn key. Per user, never under a fixed name in /tmp: the session's +# runtime directory, or a private state directory without one. +if [[ -n ${XDG_RUNTIME_DIR:-} ]]; then + manual_dir=$XDG_RUNTIME_DIR +else + manual_dir=${XDG_STATE_HOME:-$HOME/.local/state}/omarchy + mkdir -p "$manual_dir" 2>/dev/null && chmod 700 "$manual_dir" 2>/dev/null +fi +manual_file=$manual_dir/omarchy-brightness-keyboard-manual + +# One at a time, so a lock blank racing a wake can't leave the LED and the +# record disagreeing. +{ exec {lock_fd}>>"$manual_file.lock"; } 2>/dev/null && flock -w 2 "$lock_fd" 2>/dev/null + if [[ $direction == "off" ]]; then + { echo blank >"$manual_file"; } 2>/dev/null || true brightnessctl -sd "$device" set 0 >/dev/null exit 0 elif [[ $direction == "restore" ]]; then - brightnessctl -rd "$device" >/dev/null + # A failed restore leaves the keys blank, so the record must still say so. + brightnessctl -rd "$device" >/dev/null || exit 1 + rm -f "$manual_file" 2>/dev/null exit 0 fi @@ -53,6 +73,8 @@ else (( new_brightness < 0 )) && new_brightness=0 fi +{ printf '%s\n' "$new_brightness" >"$manual_file"; } 2>/dev/null || true + # Set the new brightness. brightnessctl -d "$device" set "$new_brightness" >/dev/null (( no_osd )) || omarchy-osd -i keyboard -p "$(( new_brightness * 100 / max_brightness ))" diff --git a/bin/omarchy-brightness-keyboard-auto b/bin/omarchy-brightness-keyboard-auto new file mode 100755 index 000000000000..db6be8ccb85d --- /dev/null +++ b/bin/omarchy-brightness-keyboard-auto @@ -0,0 +1,267 @@ +#!/bin/bash + +# omarchy:summary=Drive keyboard backlight from the ambient light sensor +# omarchy:args=[--once|--available|--map-lux N] +# omarchy:examples=omarchy brightness keyboard auto --once | omarchy brightness keyboard auto --map-lux 12 + +# Inverse of display auto-brightness: dark rooms need the keys lit, bright +# rooms do not. Dedicated keyboard-brightness keys, firmware Fn keys and any +# other write to the LED pause auto until ambient light moves enough that the +# old choice no longer fits, off included. The lock screen's blank and a 1% +# leftover nobody chose are not a choice: auto resumes. + +set -euo pipefail + +POLL_SECONDS=5 +DARK_LUX=8 +BRIGHT_LUX=180 +DEADBAND_PERCENT=4 +OVERRIDE_LUX_DELTA=20 +OVERRIDE_LUX_RATIO=40 +# 2/255 is 1% and invisible, so that band counts as off. +EFFECTIVELY_OFF_PERCENT=2 +MANUAL_LEVEL_FILE="${XDG_RUNTIME_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy}/omarchy-brightness-keyboard-manual" + +once=0 +available_only=0 +map_lux="" + +while (($#)); do + case "$1" in + --once) + once=1 + shift + ;; + --available) + available_only=1 + shift + ;; + --map-lux) + if [[ -z ${2:-} ]]; then + echo "Usage: omarchy-brightness-keyboard-auto --map-lux N" >&2 + exit 1 + fi + map_lux=$2 + shift 2 + ;; + -h|--help) + echo "Usage: omarchy-brightness-keyboard-auto [--once|--available|--map-lux N]" + exit 0 + ;; + *) + echo "Unknown option: $1" >&2 + echo "Usage: omarchy-brightness-keyboard-auto [--once|--available|--map-lux N]" >&2 + exit 1 + ;; + esac +done + +lux_to_percent() { + local lux=$1 + (( lux < 0 )) && lux=0 + + if (( lux <= DARK_LUX )); then + echo 100 + elif (( lux >= BRIGHT_LUX )); then + echo 0 + else + echo $(( (100 * (BRIGHT_LUX - lux)) / (BRIGHT_LUX - DARK_LUX) )) + fi +} + +if [[ -n $map_lux ]]; then + [[ $map_lux =~ ^[0-9]+$ ]] || { + echo "lux must be a non-negative integer" >&2 + exit 1 + } + lux_to_percent "$map_lux" + exit 0 +fi + +find_als() { + local d name + local iio_devices_dir=${OMARCHY_IIO_DEVICES_DIR:-/sys/bus/iio/devices} + + # Prefer a device that names itself as ALS. Apple AOP exposes the lid-angle + # sensor (aop-sensors-las) next to the illuminance sensor (aop-sensors-als); + # walking sysfs in device-number order would otherwise pick whichever one + # happens to publish an illuminance channel first. + for d in "$iio_devices_dir"/iio:device*; do + [[ -r $d/name ]] || continue + name=$(<"$d/name") + [[ $name == *als* ]] || continue + if [[ -r $d/in_illuminance_input ]]; then + printf '%s\n' "$d/in_illuminance_input" + return 0 + fi + if [[ -r $d/in_illuminance_raw ]]; then + printf '%s\n' "$d/in_illuminance_raw" + return 0 + fi + done + + for d in "$iio_devices_dir"/iio:device*; do + if [[ -r $d/in_illuminance_input ]]; then + printf '%s\n' "$d/in_illuminance_input" + return 0 + fi + if [[ -r $d/in_illuminance_raw ]]; then + printf '%s\n' "$d/in_illuminance_raw" + return 0 + fi + done + return 1 +} + +find_kbd() { + local candidate + local leds_dir=${OMARCHY_LEDS_DIR:-/sys/class/leds} + + for candidate in "$leds_dir"/*kbd_backlight*; do + if [[ -e $candidate/brightness && -r $candidate/max_brightness ]]; then + printf '%s\n' "$candidate" + return 0 + fi + done + return 1 +} + +als_path=$(find_als || true) +kbd_path=$(find_kbd || true) + +if [[ $available_only == 1 ]]; then + if [[ -n $als_path && -n $kbd_path ]]; then + exit 0 + else + exit 1 + fi +fi + +if [[ -z $als_path || -z $kbd_path ]]; then + exit 0 +fi + +device=$(basename "$kbd_path") +max=$(<"$kbd_path/max_brightness") +[[ $max =~ ^[1-9][0-9]*$ ]] || exit 0 + +last_set="" +paused=0 +pause_lux=0 + +session_locked() { + omarchy-hyprland-session-locked >/dev/null 2>&1 +} + +lid_closed() { + omarchy-hw-laptop-closed >/dev/null 2>&1 +} + +# in_illuminance_input is lux already. A raw channel is lux only after its +# offset and scale: (raw + offset) * scale, as the IIO ABI defines them. +read_lux() { + local value scale=1 offset=0 dir=${als_path%/*} + + value=$(<"$als_path") || return 1 + if [[ $als_path == *_raw ]]; then + [[ -r $dir/in_illuminance_scale ]] && scale=$(<"$dir/in_illuminance_scale") + [[ -r $dir/in_illuminance_offset ]] && offset=$(<"$dir/in_illuminance_offset") + fi + + awk -v value="$value" -v scale="$scale" -v offset="$offset" 'BEGIN { + number = "^-?[0-9]+([.][0-9]+)?$" + if (value !~ number || scale !~ number || offset !~ number) exit 1 + lux = (value + offset) * scale + printf "%d\n", (lux < 0 ? 0 : lux) + }' +} + +apply_percent() { + local percent=$1 + local target current delta + + (( percent < 0 )) && percent=0 + (( percent > 100 )) && percent=100 + target=$(( max * percent / 100 )) + + current=$(brightnessctl -d "$device" get) || return 0 + delta=$(( current - target )) + (( delta < 0 )) && delta=$(( -delta )) + if (( delta * 100 < max * DEADBAND_PERCENT )); then + last_set=$current + return 0 + fi + + brightnessctl -d "$device" set "$target" >/dev/null + # Auto has overridden any level set by hand, so that record is stale. + rm -f "$MANUAL_LEVEL_FILE" + last_set=$(brightnessctl -d "$device" get || echo "$target") +} + +# Off by accident rather than by choice. The keyboard-brightness keys record +# every level they set, and the lock screen's off records "blank". Firmware Fn +# keys write the LED directly, so an unrecorded 0 is a choice; an unrecorded +# 1% is a leftover. +left_off() { + local current=$1 manual="" + + (( current * 100 <= max * EFFECTIVELY_OFF_PERCENT )) || return 1 + [[ -r $MANUAL_LEVEL_FILE ]] && manual=$(<"$MANUAL_LEVEL_FILE") + [[ $manual != "$current" ]] || return 1 + (( current != 0 )) || [[ $manual == "blank" ]] +} + +tick() { + local lux current delta threshold + + lux=$(read_lux) || return 0 + + if session_locked || lid_closed; then + return 0 + fi + + current=$(brightnessctl -d "$device" get) || return 0 + + if [[ -n $last_set && $current != "$last_set" ]]; then + last_set=$current + if left_off "$current"; then + paused=0 + # Taken back, so a later 0 nobody recorded is a choice again. + rm -f "$MANUAL_LEVEL_FILE" + else + paused=1 + pause_lux=$lux + return 0 + fi + fi + + if (( paused )); then + delta=$(( lux - pause_lux )) + (( delta < 0 )) && delta=$(( -delta )) + threshold=$OVERRIDE_LUX_DELTA + if (( pause_lux * OVERRIDE_LUX_RATIO / 100 > threshold )); then + threshold=$(( pause_lux * OVERRIDE_LUX_RATIO / 100 )) + fi + if (( delta < threshold )); then + return 0 + fi + paused=0 + fi + + apply_percent "$(lux_to_percent "$lux")" +} + +tick + +if (( ! once )); then + while true; do + sleep "$POLL_SECONDS" + als_path=$(find_als || true) + kbd_path=$(find_kbd || true) + [[ -n $als_path && -n $kbd_path ]] || continue + device=$(basename "$kbd_path") + max=$(<"$kbd_path/max_brightness") + [[ $max =~ ^[1-9][0-9]*$ ]] || continue + tick + done +fi diff --git a/bin/omarchy-capture-screenrecording b/bin/omarchy-capture-screenrecording index 0c4c5494c952..2912da969526 100755 --- a/bin/omarchy-capture-screenrecording +++ b/bin/omarchy-capture-screenrecording @@ -2,7 +2,7 @@ # omarchy:summary=Start or stop screen recording # omarchy:group=capture -# omarchy:args=[--fullscreen] [--with-desktop-audio] [--with-microphone-audio] [--with-webcam] [--webcam-device=] [--webcam-size=] [--resolution=] [--stop-recording] +# omarchy:args=[--fullscreen] [--with-desktop-audio] [--with-microphone-audio] [--with-webcam] [--webcam-device=] [--webcam-size=] [--resolution=] [--stop-recording] [--status] # omarchy:examples=omarchy screenrecord | omarchy capture screenrecording --with-desktop-audio # omarchy:aliases=omarchy screenrecord # @@ -14,19 +14,20 @@ # DMA-BUF modifier import on some configurations, leaving recording unable to # start. # -# Env: OMARCHY_SCREENRECORD_DEBUG=true appends gpu-screen-recorder's stderr (and -# the picker target it was launched with) to -# $XDG_RUNTIME_DIR/omarchy-screenrecord.log so users can attach a log when -# reporting capture failures. +# Env: OMARCHY_SCREENRECORD_DEBUG=true appends the recorder's stderr (and the +# picker target it was launched with) to $XDG_RUNTIME_DIR/omarchy-screenrecord.log +# so users can attach a log when reporting capture failures. +# +# Backend: gpu-screen-recorder. Where it is not installed, or it exits before +# the recording starts (it knows only NVIDIA, AMD, Intel and Broadcom GPUs, and +# aborts on others such as Apple Silicon's), wf-recorder records instead when +# it is installed. wf-recorder encodes on the CPU and captures one audio +# device, so desktop and microphone audio together go through a temporary null +# sink. Portal capture and --resolution are gpu-screen-recorder only. [[ -f ~/.config/user-dirs.dirs ]] && source ~/.config/user-dirs.dirs OUTPUT_DIR="${OMARCHY_SCREENRECORD_DIR:-${XDG_VIDEOS_DIR:-$HOME/Videos}}" -if [[ ! -d $OUTPUT_DIR ]]; then - omarchy-notification-send -u critical -t 3000 "Screen recording directory does not exist: $OUTPUT_DIR" - exit 1 -fi - DESKTOP_AUDIO="false" MICROPHONE_AUDIO="false" WEBCAM="false" @@ -35,6 +36,7 @@ WEBCAM_SIZE="medium" RESOLUTION="" FULLSCREEN="false" STOP_RECORDING="false" +STATUS="false" # Both of these live in the per-user runtime directory, which is 0700. What # RECORDING_FILE holds is read back on stop and used as a path -- ffmpeg writes # beside it, `mv` replaces it, and `rm -f` deletes its preview -- so a name in @@ -46,6 +48,10 @@ STOP_RECORDING="false" RUNTIME_DIR="${XDG_RUNTIME_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy}" RECORDING_FILE="$RUNTIME_DIR/omarchy-screenrecord-filename" REGION_FILE="$RUNTIME_DIR/omarchy-screenrecord-region" +# The recorder's pid, so stop and status act on that process alone. +REC_PID_FILE="$RUNTIME_DIR/omarchy-screenrecord-pid" +# The PulseAudio module ids of a combined-audio null sink, for cleanup. +PA_MODULES_FILE="$RUNTIME_DIR/omarchy-screenrecord-pa-modules" LOG_FILE=$([[ ${OMARCHY_SCREENRECORD_DEBUG:-false} == "true" ]] && echo "$RUNTIME_DIR/omarchy-screenrecord.log" || echo "/dev/null") for arg in "$@"; do @@ -58,6 +64,7 @@ for arg in "$@"; do --resolution=*) RESOLUTION="${arg#*=}" ;; --fullscreen) FULLSCREEN="true" ;; --stop-recording) STOP_RECORDING="true" ;; + --status) STATUS="true" ;; esac done @@ -69,6 +76,13 @@ small | medium | large) ;; ;; esac +# --status only answers whether our recording runs, for the bar and the menu, so +# it needs no recordings directory and never notifies. +if [[ $STATUS != "true" && ! -d $OUTPUT_DIR ]]; then + omarchy-notification-send -u critical -t 3000 "Screen recording directory does not exist: $OUTPUT_DIR" + exit 1 +fi + start_webcam_overlay() { cleanup_webcam @@ -153,12 +167,119 @@ select_capture_target() { echo "region:${BASH_REMATCH[3]}x${BASH_REMATCH[4]}+${BASH_REMATCH[1]}+${BASH_REMATCH[2]}" } +# gpu-screen-recorder first, then wf-recorder, of those installed. The base +# packages carry gpu-screen-recorder, but an architecture may build none. +recorder_backends() { + local backend found=false + + for backend in gpu-screen-recorder wf-recorder; do + if omarchy-cmd-present "$backend"; then + echo "$backend" + found=true + fi + done + + # Neither installed: fail the way a missing gpu-screen-recorder always has. + $found || echo gpu-screen-recorder +} + +# wf-recorder records a single audio device. Map the desktop/mic flags onto one: +# desktop -> default sink monitor, mic -> default source. When both are requested, +# route them through a throwaway null sink so a single track carries both; the +# module ids are saved to PA_MODULES_FILE so stop can unload them. A requested +# source that can't be recorded is left out with a notification naming it, and +# the recording carries whichever source remains. +wf_audio_device() { + local sink="" source="" m + [[ $DESKTOP_AUDIO == "true" ]] && sink=$(pactl get-default-sink 2>/dev/null) + [[ $MICROPHONE_AUDIO == "true" ]] && source=$(pactl get-default-source 2>/dev/null) + + if [[ -n $sink && -n $source ]]; then + if m=$(pactl load-module module-null-sink sink_name=omarchy_screenrec sink_properties=device.description=omarchy_screenrec 2>/dev/null); then + echo "$m" >"$PA_MODULES_FILE" + if m=$(pactl load-module module-loopback source="${sink}.monitor" sink=omarchy_screenrec latency_msec=20 2>/dev/null); then + echo "$m" >>"$PA_MODULES_FILE" + else + sink="" + fi + if m=$(pactl load-module module-loopback source="$source" sink=omarchy_screenrec latency_msec=20 2>/dev/null); then + echo "$m" >>"$PA_MODULES_FILE" + else + source="" + fi + if [[ -n $sink && -n $source ]]; then + echo "omarchy_screenrec.monitor" + return + fi + wf_audio_cleanup + else + # No device to mix into: desktop audio records alone, as it always has. + source="" + fi + fi + + [[ $DESKTOP_AUDIO == "true" && -z $sink ]] && wf_audio_unavailable desktop + [[ $MICROPHONE_AUDIO == "true" && -z $source ]] && wf_audio_unavailable microphone + if [[ -n $sink ]]; then + echo "${sink}.monitor" + elif [[ -n $source ]]; then + echo "$source" + fi +} + +wf_audio_unavailable() { + omarchy-notification-send -u critical -t 5000 "Recording without $1 audio" "The $1 audio could not be added to this recording." >/dev/null 2>&1 +} + +wf_audio_cleanup() { + [[ -f $PA_MODULES_FILE ]] || return + # Unload in reverse so the loopbacks drop before the null sink they feed. + tac "$PA_MODULES_FILE" 2>/dev/null | while read -r m; do + [[ -n $m ]] && pactl unload-module "$m" 2>/dev/null + done + rm -f "$PA_MODULES_FILE" +} + +# Launches wf-recorder for the given picker target, backgrounded, with REC_PID set. +launch_wf_recorder() { + local target=$1 filename=$2 + local geom_args=() + + case $target in + monitor:*) + geom_args=(-o "${target#monitor:}") + ;; + region:*) + # region:WxH+X+Y (logical coords) -> wf-recorder -g "X,Y WxH" (same space). + [[ ${target#region:} =~ ^([0-9]+)x([0-9]+)\+(-?[0-9]+)\+(-?[0-9]+)$ ]] || return 1 + geom_args=(-g "${BASH_REMATCH[3]},${BASH_REMATCH[4]} ${BASH_REMATCH[1]}x${BASH_REMATCH[2]}") + ;; + *) + return 1 + ;; + esac + + local audio_args=() dev + dev=$(wf_audio_device) + [[ -n $dev ]] && audio_args=(--audio="$dev") + + # -r 60 caps the framerate: capturing a 120 Hz panel at its native rate makes + # needlessly large files. -R 48000 keeps the AAC track at 48 kHz rather than + # following a sink that advertises 96 kHz into a file many players, and some + # speaker DSPs, play as silence. + wf-recorder "${geom_args[@]}" -r 60 -R 48000 "${audio_args[@]}" -f "$filename" 2>>"$LOG_FILE" & + REC_PID=$! +} + start_screenrecording() { mkdir -p "$RUNTIME_DIR" || return 1 # XDG_STATE_HOME may be outside a private home; protect existing fallbacks too. if [[ -z ${XDG_RUNTIME_DIR:-} ]]; then chmod 700 "$RUNTIME_DIR" || return 1 fi + # A pid left by a recording that ended without a stop would otherwise hide + # this one from the toggle until it has started and saved its own. + rm -f "$REC_PID_FILE" local capture_args=() local target @@ -189,48 +310,83 @@ start_screenrecording() { [[ $WEBCAM == "true" ]] && start_webcam_overlay "$target" local filename="$OUTPUT_DIR/screenrecording-$(date +'%Y-%m-%d_%H-%M-%S').mp4" - local audio_devices="" - local audio_args=() + local backend pid="" - [[ $DESKTOP_AUDIO == "true" ]] && audio_devices+="default_output" + for backend in $(recorder_backends); do + [[ $backend == "wf-recorder" && $target == "portal" ]] && continue + echo "===== $(date '+%F %T') args: $* backend: $backend target: $target =====" >>"$LOG_FILE" - if [[ $MICROPHONE_AUDIO == "true" ]]; then - # Merge audio tracks into one - separate tracks only play one at a time in most players - [[ -n $audio_devices ]] && audio_devices+="|" - audio_devices+="default_input" - fi + if [[ $backend == "wf-recorder" ]]; then + launch_wf_recorder "$target" "$filename" || continue + pid=$REC_PID + else + local audio_devices="" + local audio_args=() + + [[ $DESKTOP_AUDIO == "true" ]] && audio_devices+="default_output" + + if [[ $MICROPHONE_AUDIO == "true" ]]; then + # Merge audio tracks into one - separate tracks only play one at a time in most players + [[ -n $audio_devices ]] && audio_devices+="|" + audio_devices+="default_input" + fi + + [[ -n $audio_devices ]] && audio_args+=(-a "$audio_devices" -ac aac) + + gpu-screen-recorder "${capture_args[@]}" -k auto -f 60 -fm cfr -fallback-cpu-encoding yes -o "$filename" "${audio_args[@]}" 2>>"$LOG_FILE" & + pid=$! + fi - [[ -n $audio_devices ]] && audio_args+=(-a "$audio_devices" -ac aac) + while kill -0 $pid 2>/dev/null && [[ ! -f $filename ]]; do + sleep 0.2 + done - echo "===== $(date '+%F %T') args: $* target: $target =====" >>"$LOG_FILE" - gpu-screen-recorder "${capture_args[@]}" -k auto -f 60 -fm cfr -fallback-cpu-encoding yes -o "$filename" "${audio_args[@]}" 2>>"$LOG_FILE" & - local pid=$! + if kill -0 $pid 2>/dev/null; then + echo "$pid" >"$REC_PID_FILE" + echo "$filename" >"$RECORDING_FILE" + toggle_screenrecording_indicator + return 0 + fi - while kill -0 $pid 2>/dev/null && [[ ! -f $filename ]]; do - sleep 0.2 + wf_audio_cleanup + rm -f "$filename" done - if kill -0 $pid 2>/dev/null; then - echo "$filename" >"$RECORDING_FILE" - toggle_screenrecording_indicator - fi + return 1 +} + +# Selects the recorder this script started, by the pid it saved. A saved pid +# that is no longer a recorder is a recording that ended without a stop, so +# nothing of ours is recording and no other recorder is touched. Without a +# saved pid (a recording started before the pid was saved), every recorder the +# user runs counts, as for the bar indicator and the menu. +select_recorder() { + RECORDER=() + [[ -f $REC_PID_FILE ]] || return 0 + + local pid + pid=$(<"$REC_PID_FILE") + [[ $pid =~ ^[0-9]+$ ]] && omarchy-capture-screenrecording-process --pid "$pid" || return 1 + RECORDER=(--pid "$pid") } +# Stops the recorder screenrecording_active selected. stop_screenrecording() { - pkill -SIGINT -f "^gpu-screen-recorder" # SIGINT required to save video properly + omarchy-capture-screenrecording-process "${RECORDER[@]}" --signal INT # SIGINT required to save video properly # Wait a maximum of 5 seconds to finish before hard killing local count=0 - while pgrep -f "^gpu-screen-recorder" >/dev/null && ((count < 50)); do + while omarchy-capture-screenrecording-process "${RECORDER[@]}" && ((count < 50)); do sleep 0.1 count=$((count + 1)) done toggle_screenrecording_indicator cleanup_webcam + wf_audio_cleanup - if pgrep -f "^gpu-screen-recorder" >/dev/null; then - pkill -9 -f "^gpu-screen-recorder" + if omarchy-capture-screenrecording-process "${RECORDER[@]}"; then + omarchy-capture-screenrecording-process "${RECORDER[@]}" --signal KILL omarchy-notification-send -u critical -t 5000 "Screen recording error" "Recording process had to be force-killed. Video may be corrupted." else finalize_recording @@ -254,7 +410,7 @@ stop_screenrecording() { ) & fi - rm -f "$RECORDING_FILE" + rm -f "$RECORDING_FILE" "$REC_PID_FILE" } toggle_screenrecording_indicator() { @@ -262,7 +418,7 @@ toggle_screenrecording_indicator() { } screenrecording_active() { - pgrep -f "^gpu-screen-recorder" >/dev/null + select_recorder && omarchy-capture-screenrecording-process "${RECORDER[@]}" } finalize_recording() { @@ -283,7 +439,10 @@ finalize_recording() { # Hard-mute the first 400ms to drop the PipeWire capture-open pop (a near-clipping # transient around 130-200ms that a gentle fade-in can't attenuate enough), then a # 50ms fade avoids a click at the boundary before loudnorm normalizes the rest. - args+=(-af "volume=enable='lt(t,0.4)':volume=0,afade=t=in:st=0.4:d=0.05,loudnorm=I=-14:TP=-1.5:LRA=11") + # + # loudnorm declares 192 kHz output, so without -ar the AAC encoder writes + # 96 kHz from a 48 kHz recording. + args+=(-ar 48000 -af "volume=enable='lt(t,0.4)':volume=0,afade=t=in:st=0.4:d=0.05,loudnorm=I=-14:TP=-1.5:LRA=11") fi local processed="${latest%.mp4}-processed.mp4" @@ -294,10 +453,15 @@ finalize_recording() { fi } -if screenrecording_active; then +if [[ $STATUS == "true" ]]; then + screenrecording_active +elif screenrecording_active; then stop_screenrecording elif [[ $STOP_RECORDING == "true" ]]; then exit 1 else - start_screenrecording || cleanup_webcam + start_screenrecording || { + wf_audio_cleanup + cleanup_webcam + } fi diff --git a/bin/omarchy-capture-screenrecording-process b/bin/omarchy-capture-screenrecording-process new file mode 100755 index 000000000000..4524483c5707 --- /dev/null +++ b/bin/omarchy-capture-screenrecording-process @@ -0,0 +1,61 @@ +#!/usr/bin/python3 +# omarchy:summary=Check or signal the current user's recording processes +# omarchy:args=[--pid ] [--signal INT|KILL] +# omarchy:hidden=true + +import argparse +import os +from pathlib import Path +import signal + +RECORDERS = {'gpu-screen-recorder', 'wf-recorder'} + + +def is_recorder(process): + if process.stat().st_uid != os.geteuid(): + return False + executable = os.readlink(process / 'exe') + # Linux appends this when an upgrade unlinks a still-running executable. + executable = executable.removesuffix(' (deleted)') + return Path(executable).name in RECORDERS + + +def recording_processes(signum=None, proc=Path('/proc'), pids=None): + found = False + if pids is None: + candidates = [path for path in proc.iterdir() if path.name.isdecimal()] + else: + candidates = [proc / str(pid) for pid in pids] + for process in candidates: + if not process.name.isdecimal(): + continue + descriptor = None + try: + if signum is not None: + # Open before inspecting identity: a later PID reuse cannot + # redirect the signal to a different process. + descriptor = os.pidfd_open(int(process.name)) + if is_recorder(process): + found = True + if signum is not None: + signal.pidfd_send_signal(descriptor, signum) + except (FileNotFoundError, ProcessLookupError, PermissionError): + # Processes may exit or become inaccessible while /proc is scanned. + continue + finally: + if descriptor is not None: + os.close(descriptor) + return found + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--pid', type=int, action='append') + parser.add_argument('--signal', choices=('INT', 'KILL')) + args = parser.parse_args() + signum = getattr(signal, 'SIG' + args.signal) if args.signal else None + return 0 if recording_processes(signum, pids=args.pid) else 1 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/bin/omarchy-channel-set b/bin/omarchy-channel-set index 039d3292cdf8..98a8da98bda4 100755 --- a/bin/omarchy-channel-set +++ b/bin/omarchy-channel-set @@ -60,6 +60,23 @@ link_dev_checkout() { fi done + # The checkout's own channel refresh runs next, from its own templates. + if [[ ! -f $checkout/${templates#"$OMARCHY_PATH"/}/pacman-$pacman_channel.conf || + ! -f $checkout/${templates#"$OMARCHY_PATH"/}/mirrorlist-$pacman_channel ]]; then + fail "Update the checkout before switching to dev; it has no $pacman_channel templates for $platform." + fi + + # On aarch64 one from before it had platform templates would replace this + # machine's repositories with x86's. + if [[ $platform != "generic" ]]; then + for required in bin/omarchy-hw-platform install/helpers/pacman.sh; do + if [[ ! -f $checkout/$required || ! -r $checkout/$required || + ( $required == "bin/omarchy-hw-platform" && ! -x $checkout/$required ) ]]; then + fail "Update the checkout before switching to dev; on $platform it needs $required for this machine's repositories." + fi + done + fi + omarchy-dev-link "$checkout" --no-reboot } @@ -102,9 +119,6 @@ case "$channel" in packages=(omarchy-dev omarchy-settings-dev) ;; dev) - confirm_dev || { echo "Cancelled."; exit 0; } - dev_checkout="$HOME/omarchy" - validate_dev_checkout "$dev_checkout" pacman_channel=edge packages=(omarchy-dev omarchy-settings-dev) ;; @@ -115,6 +129,20 @@ case "$channel" in ;; esac +# A channel this machine's platform has no templates for is refused before +# anything changes (install/helpers/pacman.sh). +source "$OMARCHY_PATH/install/helpers/pacman.sh" +platform=$(omarchy-hw-platform) || fail "Cannot tell this machine's platform; nothing changed." +templates=$(omarchy_pacman_templates "$platform") || exit 1 +[[ -f $templates/pacman-$pacman_channel.conf && -f $templates/mirrorlist-$pacman_channel ]] || + fail "Omarchy has no $pacman_channel channel for $platform; nothing changed." + +if [[ $channel == "dev" ]]; then + confirm_dev || { echo "Cancelled."; exit 0; } + dev_checkout="$HOME/omarchy" + validate_dev_checkout "$dev_checkout" +fi + # A failure past this point leaves the channel switch half-applied, so say how # to pick it back up rather than dying silently under set -e. trap 'echo -e "\nThe channel switch did not complete. Review the error above, then rerun: omarchy-channel-set '"$channel"'" >&2' ERR diff --git a/bin/omarchy-cmd-desktop-exec-repair b/bin/omarchy-cmd-desktop-exec-repair new file mode 100755 index 000000000000..943fc36cbbf6 --- /dev/null +++ b/bin/omarchy-cmd-desktop-exec-repair @@ -0,0 +1,76 @@ +#!/usr/bin/python3 +# omarchy:summary=Preserve desktop customizations while repairing a known executable token +# omarchy:args= ... +# omarchy:hidden=true + +import os +from pathlib import Path +import re +import shutil +import sys +import tempfile + + +def repair(target, vendor, wrapper, known): + if target.is_symlink() or (target.exists() and not target.is_file()): + print(f"Preserving custom desktop route: {target}", file=sys.stderr) + return + existing = target.exists() + source = target if existing else vendor + if not source.is_file(): + return + data = source.read_bytes() + text = data.decode("utf-8") + # Desktop Exec is not a shell command. Only replace a literal leading token; + # env, shell commands, escaped/complex routes and all following bytes survive. + pattern = re.compile(r'^(Exec)=([ \t]*)("[^"\\\n]+"|[^\s"\\]+)(?=[ \t\r\n]|$)', re.MULTILINE) + def replace(match): + token = match[3] + value = token[1:-1] if token.startswith('"') else token + if value not in known: + return match[0] + new = wrapper + if token.startswith('"') or any(c.isspace() for c in new): + new = '"' + new.replace('\\', '\\\\').replace('"', '\\"') + '"' + return match[1] + '=' + match[2] + new + # TryExec is a desktop string containing the whole executable path, not an + # Exec command line. Quotes are literal bytes to its availability resolver. + escapes = {'s': ' ', 'n': '\n', 't': '\t', 'r': '\r', '\\': '\\'} + def replace_try_exec(match): + value = re.sub(r'\\([sntr\\])', lambda part: escapes[part[1]], match[1]) + # Repair the quoted form emitted by older versions of this helper too. + if value.startswith('"') and value.endswith('"'): + value = value[1:-1] + if value not in known and value != wrapper: + return match[0] + new = wrapper.replace('\\', '\\\\').replace('\n', '\\n').replace('\t', '\\t').replace('\r', '\\r') + return 'TryExec=' + new + match[2] + updated = re.sub(r'^TryExec=([^\r\n]*)(\r?)$', replace_try_exec, + pattern.sub(replace, text), flags=re.MULTILINE).encode("utf-8") + if updated == data: + return + target.parent.mkdir(parents=True, exist_ok=True) + staged = None + try: + with tempfile.NamedTemporaryFile(dir=target.parent, prefix='.' + target.name + '.', delete=False) as stream: + staged = stream.name + stream.write(updated) + os.chmod(staged, source.stat().st_mode & 0o777) + if existing: + with tempfile.NamedTemporaryFile(dir=target.parent, prefix=target.name + '.bak.', delete=False) as backup: + backup.write(data) + shutil.copystat(target, backup.name) + os.replace(staged, target) + staged = None + finally: + if staged: + os.unlink(staged) + + +if __name__ == '__main__': + if len(sys.argv) < 5: + sys.exit('Usage: omarchy-cmd-desktop-exec-repair ...') + try: + repair(Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3], set(sys.argv[4:])) + except (OSError, UnicodeError) as error: + sys.exit(f'Desktop repair failed: {error}') diff --git a/bin/omarchy-cmd-electron-gl-args b/bin/omarchy-cmd-electron-gl-args new file mode 100755 index 000000000000..cd622eb10138 --- /dev/null +++ b/bin/omarchy-cmd-electron-gl-args @@ -0,0 +1,8 @@ +#!/bin/bash + +# omarchy:summary=Print Chromium/Electron flags for software GL when no render GPU is present +# omarchy:hidden=true + +if ! omarchy-hw-render-gpu; then + printf '%s\n' --ozone-platform=wayland --disable-gpu +fi diff --git a/bin/omarchy-cmd-electron-gl-wrap b/bin/omarchy-cmd-electron-gl-wrap new file mode 100755 index 000000000000..ccd40599e568 --- /dev/null +++ b/bin/omarchy-cmd-electron-gl-wrap @@ -0,0 +1,117 @@ +#!/bin/bash + +# omarchy:summary=Install a PATH wrapper that adds software GL flags when no render GPU is present +# omarchy:args=[--check] +# omarchy:examples=omarchy-cmd-electron-gl-wrap 1password /opt/1Password/1password | omarchy-cmd-electron-gl-wrap chromium /usr/bin/chromium +# omarchy:hidden=true +# omarchy:requires-sudo=true + +set -euo pipefail + +# Exit 3 means an administrator owns the launcher; callers may skip that case. +# Exit 4 is reserved for --check: the managed wrapper is not ready yet. +check=false +if [[ ${1:-} == "--check" ]]; then + check=true + shift +fi + +if (($# != 2)); then + echo "Usage: omarchy-cmd-electron-gl-wrap [--check] " >&2 + exit 2 +fi + +name=$1 +real=$2 +if [[ -z $name || $name == */* || $name == "." || $name == ".." ]]; then + echo "omarchy-cmd-electron-gl-wrap: command name must be a basename: $name" >&2 + exit 2 +fi + +bind_dir=${OMARCHY_ELECTRON_GL_BIND_DIR:-/usr/local/bin} +dest=$bind_dir/$name + +if [[ ! -x $real ]]; then + echo "omarchy-cmd-electron-gl-wrap: not executable: $real" >&2 + exit 1 +fi + +# A symlink is never an Omarchy-owned launcher, even when it aliases the real +# binary. Keep literal and hardlinked self-wraps fatal before inspecting markers. +if [[ $dest == "$real" ]] || [[ ! -L $dest && $dest -ef $real ]]; then + echo "omarchy-cmd-electron-gl-wrap: refusing to wrap a binary over itself: $dest" >&2 + exit 1 +fi + +if [[ -e $dest || -L $dest ]]; then + owned=false + if [[ ! -L $dest && -f $dest ]]; then + if grep -q '^# omarchy-electron-gl-wrapper$' "$dest"; then + owned=true + else + status=$? + if ((status != 1)); then + exit "$status" + fi + fi + fi + if ! $owned; then + echo "omarchy-cmd-electron-gl-wrap: refusing to replace an unmanaged launcher: $dest" >&2 + exit 3 + fi +fi + +as_root() { + if ((EUID == 0)) || [[ -d $bind_dir && -w $bind_dir ]]; then + "$@" + else + sudo "$@" + fi +} + +quoted_real=$(printf '%q' "$real") +wrapper=$(cat </dev/null <<<"$wrapper" + as_root chmod 755 -- "$staged" + as_root mv -fT -- "$staged" "$dest" + staged="" +fi diff --git a/bin/omarchy-drive-password b/bin/omarchy-drive-password index 2fbbb18753e4..935d05557b81 100755 --- a/bin/omarchy-drive-password +++ b/bin/omarchy-drive-password @@ -1,32 +1,393 @@ #!/bin/bash -# omarchy:summary=Set a new encryption password for a drive selected. +# omarchy:summary=Set a new password for an encrypted drive; for the system disk, also the login and root passwords # omarchy:requires-sudo=true -encrypted_drives=$(blkid -t TYPE=crypto_LUKS -o device) +# Installation gives the login user, root and the disk holding / one password. +# Changing that disk's password keeps them together: the LUKS key changes first +# and must open the disk before either account changes. The journal records only +# the disk's UUID, key slots and phase, so a change that stops part way is +# finished by the next run. A platform whose boot checks prove the disk's key +# slots records the owner's new one before the run finishes. Other encrypted +# drives change alone. -if [[ -n $encrypted_drives ]]; then - if (( $(wc -l <<<"$encrypted_drives") == 1 )); then - drive_to_change="$encrypted_drives" +# Passwords live in variables; never trace them. +set +x + +REKEY_STATE=${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/drive-password.state +source "$OMARCHY_PATH/install/provisioning/luks-rekey.sh" + +# The LUKS partition beneath /, or nothing when / is not encrypted. Fails when +# it cannot tell, including a crypt layer whose LUKS partition lsblk cannot name. +root_luks_device() { + local root_source ancestry device + root_source=$(findmnt -no SOURCE /) && [[ -n $root_source ]] || return 1 + ancestry=$(lsblk -nsrpo NAME,TYPE,FSTYPE "${root_source%%[*}") || return 1 + device=$(awk '$3 == "crypto_LUKS" { print $1; exit }' <<<"$ancestry") + if [[ -n $device ]]; then + readlink -f "$device" + elif awk '$2 == "crypt" { found = 1 } END { exit !found }' <<<"$ancestry"; then + return 1 + fi +} + +# Every LUKS device, from udev's probe as root_luks_device reads it. An +# unprivileged blkid only reads /run/blkid, empty until root runs blkid. lsblk +# repeats a device under each parent (RAID members), so each is listed once. +luks_drives() { + lsblk -nrpo NAME,FSTYPE | awk '$2 == "crypto_LUKS" && !seen[$1]++ { print $1 }' +} + +# Every cryptsetup call holds the disk lock, so one left running by a run that +# was killed finishes before the next run looks at the drive. + +# Print the key slot the password opens. Returns 2 when it opens none, and +# another failure when the drive cannot be checked. cryptsetup open tries +# enrolled tokens (TPM2, FIDO2, keyring) before the key and reports a token's +# slot whatever key it was given, so allow only a token type no token has. +drive_key_slot() { + local drive=$1 password=$2 out status=0 + out=$(printf '%s' "$password" | sudo flock "$disk_lock" env LC_ALL=C cryptsetup open --test-passphrase --verbose --token-type passphrase-only --key-file - "$drive" 2>&1) || status=$? + (( status == 0 )) || return "$status" + grep -o 'Key slot [0-9]* unlocked' <<<"$out" | grep -o '[0-9]*' | head -1 +} + +# The enabled key slots, comma-separated. A luks2-keyring token under "Tokens:" +# looks like a keyslot, so read only the keyslot section. +drive_slots() { + local dump + dump=$(sudo flock "$disk_lock" cryptsetup luksDump "$1") || return 1 + awk ' + /^[^ \t]/ { keyslots = ($0 == "Keyslots:") } + keyslots && /^ +[0-9]+: luks2/ { sub(":", "", $1); print $1 } + /^Key Slot [0-9]+: ENABLED/ { sub(":", "", $3); print $3 }' <<<"$dump" | paste -sd, - +} + +kill_slot() { + local drive=$1 password=$2 slot=$3 + printf '%s' "$password" | sudo flock "$disk_lock" cryptsetup luksKillSlot -q --key-file - "$drive" "$slot" +} + +login_user() { + local user=${SUDO_USER:-$(id -un)} + [[ -n $user && $user != "root" ]] || return 1 + printf '%s' "$user" +} + +# luksChangeKey writes the new key before it retires the old one (LUKS2 swaps +# them in one header update), so a failure that leaves the new key unable to +# open the drive changed nothing: that returns 2. One that stops in between +# leaves both keys, so the old key is retired here until it opens nothing. +# Sets new_slot. +change_luks_key() { + local drive=$1 current=$2 new=$3 old_slot=$4 remains attempt status + local -a pbkdf=(--iter-time 2000) + + echo "Changing full-disk encryption password for $drive" + if sudo flock "$disk_lock" cryptsetup isLuks --type luks2 "$drive"; then + pbkdf+=(--pbkdf argon2id) + fi + # Both keys travel over stdin, the current one on the first line. + printf '%s\n%s' "$current" "$new" | + sudo flock "$disk_lock" bash -c 'IFS= read -r current; drive=$1; shift; exec cryptsetup luksChangeKey "$@" --key-file <(printf "%s" "$current") "$drive" <(cat)' bash "$drive" "${pbkdf[@]}" + + new_slot=$(drive_key_slot "$drive" "$new") || return + for (( attempt = 0; attempt < 8; attempt++ )); do + status=0 + remains=$(drive_key_slot "$drive" "$current") || status=$? + (( status == 2 )) && return 0 + (( status == 0 )) && [[ $remains != "$new_slot" ]] || return 1 + kill_slot "$drive" "$new" "$remains" || return 1 + done + return 1 +} + +sync_accounts() { + local user=$1 password=$2 + echo "Setting the login password for $user and root to match" + printf '%s:%s\n' "$user" "$password" | sudo chpasswd || return 1 + printf 'root:%s\n' "$password" | sudo chpasswd +} + +# The entrypoint that records the system disk's key slots for the platform's +# boot checks, or nothing where none does. A machine without its platform's +# boot package at all (dispatch exit 3) predates it, and nothing checks its +# slots. Fails with the dispatcher's message otherwise, such as a boot package +# too old to ship luks-slots. +slot_recorder() { + local out status=0 + out=$("$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" --resolve luks-slots 2>&1) || status=$? + if (( status == 0 )); then + printf '%s' "$out" + elif (( status != 3 )); then + printf '%s\n' "$out" >&2 + return 1 + fi +} + +# Refuse a current password that opens any slot but the owner's where the +# platform records the slots: on a disk an earlier setup gave a second key (a +# recovery key), changing that one would leave the owner's password as it was. +# luks-slots --owner prints the recorded slot (0), or exits 4 when no owner slot +# is recorded, as on a Mac set up before its boot package recorded slots; any +# other status is a failure the entrypoint explains, and stops the change. +# Where nothing is recorded, a password that opens the disk's only key slot is +# the owner's, and the change records its new slot (unrecorded_owner). +owner_slot_matches() { + local slot=$1 recorder owner errors reason status=0 + recorder=$(slot_recorder) || return 1 + [[ -n $recorder ]] || return 0 + errors=$(mktemp) || return 1 + owner=$(sudo "$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" luks-slots --owner 2>"$errors") || status=$? + reason=$(<"$errors") + rm -f "$errors" + if (( status == 4 )); then + only_key_slot "$slot" || return 1 + unrecorded_owner=1 + return 0 + elif (( status != 0 )); then + echo "Could not read which key slot holds the system disk password: ${reason:-luks-slots --owner exited $status}" + return 1 + elif [[ ! $owner =~ ^[0-9]+$ ]]; then + echo "Could not read which key slot holds the system disk password: the platform's boot package named '$owner', which is not a key slot." + return 1 + fi + if [[ $owner != "$slot" ]]; then + echo "That password opens another key slot on the system disk, such as a recovery key from an earlier setup. Enter the system disk password." + return 1 + fi +} + +# With no owner slot recorded, the password must open the system disk's one key +# slot: with more keys there is no telling which is the owner's, and recording +# only the owner's would fail the platform's boot check, which wants every kept +# slot recorded. Says how to settle it. +only_key_slot() { + local slot=$1 slots others + slots=$(drive_slots "$root_drive") || { echo "Could not read the key slots of $root_drive."; return 1; } + [[ $slots != "$slot" ]] || return 0 + others=$(tr ',' '\n' <<<"$slots" | grep -vx -- "$slot" | paste -sd, -) + echo "No key slot is recorded as the system disk password's, and $root_drive has keys in slots $slots, so which one is yours can't be told. The password you entered opens slot $slot." + if [[ $others =~ ^[0-9]+$ ]]; then + echo "If that password is yours and slot $others holds a recovery key you keep, record both: sudo omarchy-lifecycle-dispatch luks-slots owner=$slot recovery=$others" + echo "Otherwise remove the key you don't use: sudo cryptsetup luksKillSlot $root_drive " + else + echo "Remove the keys you don't use: sudo cryptsetup luksKillSlot $root_drive " + fi + echo "Then run omarchy-drive-password again." + return 1 +} + +# Record the slot the owner's password opens now; a no-op where nothing does. +record_owner_slot() { + local recorder + recorder=$(slot_recorder) || return 1 + [[ -z $recorder ]] || sudo "$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" luks-slots "owner=$1" +} + +# Phase luks: the key change may or may not have landed. Phase accounts: the +# new key alone opens the recorded slot and the accounts still need it. +change_system_password() { + local current=$1 new=$2 old_slot=$3 user=$4 slots status=0 + + slots=$(drive_slots "$root_drive") + if [[ ,$slots, != *",$old_slot,"* ]]; then + echo "Could not read the key slots of $root_drive." + return 1 + fi + # A disk taken as the owner's for holding one key must still hold only it. + if (( unrecorded_owner )) && [[ $slots != "$old_slot" ]]; then + echo "The key slots of $root_drive changed while you entered the new password. Run omarchy-drive-password again." + return 1 + fi + mkdir -p "${REKEY_STATE%/*}" && + rekey_state_put uuid "$root_uuid" old_slot "$old_slot" slots "$slots" phase luks || return 1 + + change_luks_key "$root_drive" "$current" "$new" "$old_slot" || status=$? + if (( status == 2 )); then + rm -f "$REKEY_STATE" + echo "The disk password did not change, so the login and root passwords did not either." + return 1 + elif (( status != 0 )); then + echo "Could not confirm the disk password change. Run omarchy-drive-password again to finish." + return 1 + fi + rekey_state_put slot "$new_slot" phase accounts || return 1 + + if ! sync_accounts "$user" "$new"; then + echo "The login and root passwords were not updated. Run omarchy-drive-password again to finish." + return 1 + fi + if ! record_owner_slot "$new_slot"; then + echo "Could not record the new key slot for the boot checks. Run omarchy-drive-password again to finish." + return 1 + fi + rm -f "$REKEY_STATE" +} + +# A journal left on this system's disk by a change that stopped part way. +change_pending() { + local phase uuid + phase=$(rekey_state_get phase) || return 1 + uuid=$(rekey_state_get uuid) + if [[ $phase == "luks" || $phase == "accounts" ]] && [[ -n $uuid && $uuid == "$root_uuid" ]]; then + return 0 + fi + rm -f "$REKEY_STATE" + return 1 +} + +# Settle the disk on the password it opens with now, then match the accounts. +# Before the change was confirmed that is the old key (removing the slot the +# change added, if any) or the new key in the one slot the change added +# (retiring the old slot); after, only the confirmed slot. Any other key on the +# disk, such as a recovery passphrase, is refused, and so is a disk that gained +# more than one slot since. +finish_interrupted_change() { + local phase old_slot slots slot user header password opened now added extra count=0 status=0 + + phase=$(rekey_state_get phase) + old_slot=$(rekey_state_get old_slot) + slots=$(rekey_state_get slots) + slot=$(rekey_state_get slot) + + if [[ $phase == "luks" && ! ( $old_slot =~ ^[0-9]+$ && ,$slots, == *",$old_slot,"* ) ]] || + [[ $phase == "accounts" && ! $slot =~ ^[0-9]+$ ]]; then + echo "$REKEY_STATE is unreadable. Remove it once the disk and login passwords agree." + return 1 + fi + + user=$(login_user) || { echo "Run omarchy-drive-password as the user who logs in to finish the last password change."; return 1; } + + echo "The last password change for $root_drive stopped before the login and root passwords matched it." + if [[ $phase == "accounts" ]]; then + header="New encryption password" else - drive_to_change="$(omarchy-drive-select "$encrypted_drives")" + header="Encryption password that opens $root_drive now (the new one if the change went through)" + fi + password=$(gum input --password --header "$header") || return 1 + + opened=$(drive_key_slot "$root_drive" "$password") || status=$? + if (( status != 0 && status != 2 )); then + echo "Could not check the password against $root_drive." + return 1 fi - if [[ -n $drive_to_change ]]; then - new_password=$(gum input --password --header "New encryption password") || exit 1 - [[ -n $new_password ]] || { echo "Password cannot be empty."; exit 1; } + if [[ $phase == "luks" && -n $opened ]]; then + now=$(drive_slots "$root_drive") || { echo "Could not read the key slots of $root_drive."; return 1; } + for extra in ${now//,/ }; do + if [[ ,$slots, != *",$extra,"* ]]; then + added=$extra + count=$(( count + 1 )) + fi + done + if (( count > 1 )); then + echo "The key slots of $root_drive changed since the interrupted change. Finish it by hand." + return 1 + fi - confirmation=$(gum input --password --header "Confirm new encryption password") || exit 1 - [[ $new_password == "$confirmation" ]] || { echo "Passwords do not match."; exit 1; } + if [[ $opened == "$old_slot" ]]; then + [[ -z $added ]] || kill_slot "$root_drive" "$password" "$added" || return 1 + elif [[ -n $added && $opened == "$added" ]]; then + if [[ ,$now, == *",$old_slot,"* ]]; then + kill_slot "$root_drive" "$password" "$old_slot" || return 1 + fi + else + opened="" + fi + [[ -z $opened ]] || rekey_state_put slot "$opened" phase accounts || return 1 + elif [[ $phase == "accounts" && $opened != "$slot" ]]; then + opened="" + fi + + if [[ -z $opened ]]; then + echo "That password does not finish the change on $root_drive. Run omarchy-drive-password again to finish." + return 1 + fi + + sync_accounts "$user" "$password" || return 1 + if ! record_owner_slot "$opened"; then + echo "Could not record the key slot for the boot checks. Run omarchy-drive-password again to finish." + return 1 + fi + rm -f "$REKEY_STATE" + echo "The login and root passwords now match the disk password." +} + +change_selected_drive() { + local encrypted_drives drive login current old_slot new_password confirmation status=0 system_drive=false + + encrypted_drives=$(luks_drives) + if [[ -z $encrypted_drives ]]; then + echo "No encrypted drives available." + return 1 + fi - echo "Changing full-disk encryption password for $drive_to_change" - # The new key travels over stdin and reaches cryptsetup as a keyfile via - # <(cat), leaving the tty free for the current-passphrase prompt. - printf "%s" "$new_password" | sudo bash -c 'exec cryptsetup luksChangeKey --pbkdf argon2id --iter-time 2000 "$1" <(cat) "${REKEY_STATE%/*}/drive-password.lock" && flock -n "$lock_fd" || + { echo "Another drive password change is running."; exit 1; } + +unrecorded_owner=0 +root_drive=$(root_luks_device) || { echo "Could not tell which drive holds the system."; exit 1; } +root_uuid="" +if [[ -n $root_drive ]]; then + root_uuid=$(lsblk -dno UUID "$root_drive") && [[ -n $root_uuid ]] || { echo "Could not read the UUID of $root_drive."; exit 1; } +fi + +if change_pending; then + finish_interrupted_change else - echo "No encrypted drives available." - exit 1 + change_selected_drive fi diff --git a/bin/omarchy-hibernation-setup b/bin/omarchy-hibernation-setup index 1aeb0d7552b5..03d07ec2a8a2 100755 --- a/bin/omarchy-hibernation-setup +++ b/bin/omarchy-hibernation-setup @@ -13,6 +13,12 @@ for arg in "$@"; do esac done +# Resume is Limine-oriented and hibernate is unreliable on Asahi. +if omarchy-hw-apple-silicon; then + echo "Skipping hibernation setup (not supported on Apple Silicon)" + exit 0 +fi + if [[ ! -f /sys/power/image_size ]]; then echo -e "Hibernation is not supported on your system" >&2 exit 0 diff --git a/bin/omarchy-hw-apple-silicon b/bin/omarchy-hw-apple-silicon new file mode 100755 index 000000000000..d10777e55c2c --- /dev/null +++ b/bin/omarchy-hw-apple-silicon @@ -0,0 +1,17 @@ +#!/bin/bash -p + +# omarchy:summary=Detect whether the computer is an Apple Silicon Mac. + +# Callers run this by absolute path, from systemd and from the shell, so use the +# detector shipped beside it rather than whichever one PATH finds. -p keeps a +# root caller's exported functions out, so a launch without it is refused, and +# nothing here searches PATH. +if [[ $- != *p* ]]; then + echo "Refusing an unsafe Bash startup." >&2 + exit 126 +fi + +dir=${BASH_SOURCE[0]%/*} +[[ $dir != "${BASH_SOURCE[0]}" ]] || dir=. +platform=$("$dir/omarchy-hw-platform") || exit 1 +[[ $platform == "apple-silicon" ]] diff --git a/bin/omarchy-hw-display b/bin/omarchy-hw-display index d3c6d3e66ff9..aa17c341c50c 100755 --- a/bin/omarchy-hw-display +++ b/bin/omarchy-hw-display @@ -5,14 +5,48 @@ backlight_path="${OMARCHY_BACKLIGHT_PATH:-/sys/class/backlight}" +# The platform package may name backlights to skip and to prefer (see +# docs/file-layout.md). +displays_conf=/usr/share/omarchy-platform/displays.conf +skips=() +prefers=() +if [[ -r $displays_conf ]]; then + # \r in IFS: a file saved with CRLF line ends reads the same. + while IFS=$' \t\r' read -r directive argument extra || [[ -n $directive ]]; do + [[ -n $argument && -z $extra && $argument != */* && $argument != . && $argument != .. ]] || continue + case $directive in + backlight-skip) skips+=("$argument") ;; + backlight-prefer) prefers+=("$argument") ;; + esac + done <"$displays_conf" +fi + +usable() { + local skip + # The Touch Bar on T2 Macs registers a backlight that never drives the display panel. + [[ -n $1 && $1 != appletb_backlight ]] || return 1 + for skip in "${skips[@]}"; do + # shellcheck disable=SC2053 # a skip is a glob + [[ $1 == $skip ]] && return 1 + done + return 0 +} + # Start with the first possible output, then refine to the most likely given an order heuristic. -# Glob the candidates in the loop list: [[ ]] does not do pathname expansion. -# The Touch Bar on T2 Macs registers a backlight that never drives the display panel. -device="$(ls -1 "$backlight_path" 2>/dev/null | grep -vx appletb_backlight | head -n1)" +device="" +while IFS= read -r name; do + if usable "$name"; then + device="$name" + break + fi +done < <(ls -1 "$backlight_path" 2>/dev/null) + # gmux comes first: apple-gmux only registers when the kernel has already picked it, and on -# dual-GPU Macs the GPU's own PWM stops driving the panel once that GPU suspends. -for candidate in "$backlight_path"/gmux_backlight "$backlight_path"/amdgpu_bl* "$backlight_path"/intel_backlight "$backlight_path"/acpi_video*; do - if [[ -e $candidate ]]; then +# dual-GPU Macs the GPU's own PWM stops driving the panel once that GPU suspends. The +# platform's preferred backlights follow, then the GPU and ACPI ones. +# Glob the candidates in the loop list: [[ ]] does not do pathname expansion. +for candidate in "$backlight_path"/gmux_backlight "${prefers[@]/#/$backlight_path/}" "$backlight_path"/amdgpu_bl* "$backlight_path"/intel_backlight "$backlight_path"/acpi_video*; do + if [[ -e $candidate ]] && usable "${candidate##*/}"; then device="${candidate##*/}" break fi diff --git a/bin/omarchy-hw-fingerprint b/bin/omarchy-hw-fingerprint index cdaf040a2475..990888cdb0dc 100755 --- a/bin/omarchy-hw-fingerprint +++ b/bin/omarchy-hw-fingerprint @@ -11,6 +11,22 @@ fingerprint_vendors=" 27c6 138a 06cb 08ff 1c7a 147e " usb_devices_path="${OMARCHY_USB_DEVICES_PATH:-/sys/bus/usb/devices}" +# A reader the kernel drives without USB (a Mac's Touch ID, behind its Secure +# Enclave) is the platform's to name: each line of its fingerprint-readers file +# is a file (a shell glob) and the value that file reads once the reader is +# usable (see docs/file-layout.md). +platform_readers=/usr/share/omarchy-platform/fingerprint-readers +if [[ -r $platform_readers ]]; then + while IFS=$' \t\r' read -r reader_glob reader_value extra || [[ -n $reader_glob ]]; do + [[ $reader_glob == /* && -n $reader_value && -z $extra ]] || continue + for reader_file in $reader_glob; do + if [[ -r $reader_file && $(<"$reader_file") == "$reader_value" ]]; then + exit 0 + fi + done + done <"$platform_readers" +fi + # libfprint drives every reader it supports from userspace over libusb, so a # real reader sits there with no kernel driver bound to any of its interfaces. # The other things these vendors build — Synaptics webcam bridges (usbio-bridge diff --git a/bin/omarchy-hw-laptop b/bin/omarchy-hw-laptop index 0ffbeeb3a855..47664d12b0a5 100755 --- a/bin/omarchy-hw-laptop +++ b/bin/omarchy-hw-laptop @@ -2,15 +2,39 @@ # omarchy:summary=Returns true when running on a laptop (has a lid or laptop chassis). +acpi_lid="${OMARCHY_ACPI_LID_PATH:-/proc/acpi/button/lid}" +input_class="${OMARCHY_INPUT_CLASS_PATH:-/sys/class/input}" +dmi_chassis="${OMARCHY_DMI_CHASSIS_TYPE_PATH:-/sys/class/dmi/id/chassis_type}" + +shopt -s nullglob +acpi_states=("$acpi_lid"/*/state) +input_switches=("$input_class"/*/capabilities/sw) +shopt -u nullglob + # A lid switch is the definitive signal for clamshell-capable hardware. -for state in /proc/acpi/button/lid/*/state; do +for state in "${acpi_states[@]}"; do [[ -e $state ]] && exit 0 done +# Without an ACPI lid (Apple Silicon, for one), any input device that reports a +# lid switch (SW_LID, bit 0 of its switch capabilities, the last word of the +# bitmap) counts. The switch's name varies: Apple's SMC input device carries +# the same name on a Mac mini, where it reports no lid. +for switches in "${input_switches[@]}"; do + [[ -r $switches ]] || continue + read -r -a words <"$switches" || true + (( ${#words[@]} )) || continue + word=${words[-1]} + [[ $word =~ ^[0-9a-fA-F]+$ ]] || continue + (( 16#$word & 1 )) && exit 0 +done + # Fall back to the DMI chassis type for laptops that don't expose an ACPI lid # button. 8=Portable 9=Laptop 10=Notebook 14=Sub Notebook 30=Tablet # 31=Convertible 32=Detachable. -case $(< /sys/class/dmi/id/chassis_type 2>/dev/null) in +chassis_type="" +read -r chassis_type <"$dmi_chassis" 2>/dev/null || true +case $chassis_type in 8 | 9 | 10 | 14 | 30 | 31 | 32) exit 0 ;; esac diff --git a/bin/omarchy-hw-laptop-closed b/bin/omarchy-hw-laptop-closed index 8e03f9656f36..52eb4778d748 100755 --- a/bin/omarchy-hw-laptop-closed +++ b/bin/omarchy-hw-laptop-closed @@ -3,9 +3,45 @@ # omarchy:summary=Returns true when the laptop lid is closed # omarchy:hidden=true -for state in /proc/acpi/button/lid/*/state; do - [[ -r $state ]] || continue - [[ $(< "$state") == *"closed"* ]] && exit 0 -done +acpi_lid="${OMARCHY_ACPI_LID_PATH:-/proc/acpi/button/lid}" +input_class="${OMARCHY_INPUT_CLASS_PATH:-/sys/class/input}" + +shopt -s nullglob +acpi_states=("$acpi_lid"/*/state) +input_switches=("$input_class"/*/capabilities/sw) +shopt -u nullglob + +# An input device that reports a lid switch: SW_LID, bit 0 of the last word +# of its switch bitmap. +has_lid_switch() { + local switches word + local -a words + + for switches in "${input_switches[@]}"; do + [[ -r $switches ]] || continue + read -r -a words <"$switches" || true + (( ${#words[@]} )) || continue + word=${words[-1]} + [[ $word =~ ^[0-9a-fA-F]+$ ]] || continue + (( 16#$word & 1 )) && return 0 + done + return 1 +} + +if (( ${#acpi_states[@]} )); then + # An ACPI lid reports its own state. + for state in "${acpi_states[@]}"; do + [[ -r $state ]] || continue + [[ $(<"$state") == *"closed"* ]] && exit 0 + done +elif has_lid_switch; then + # Elsewhere (Apple Silicon's SMC, for one) only logind reads the switch. Ask + # it only then: it answers "open" when it tracks no lid. This runs in sudo's + # and polkit's PAM stack where fingerprint login is set up, so a stalled + # logind must not hold up every sudo. + lid_state=$(busctl --timeout=2 get-property org.freedesktop.login1 \ + /org/freedesktop/login1 org.freedesktop.login1.Manager LidClosed 2>/dev/null) + [[ $lid_state == "b true" ]] && exit 0 +fi exit 1 diff --git a/bin/omarchy-hw-platform b/bin/omarchy-hw-platform new file mode 100755 index 000000000000..d3885912bb8f --- /dev/null +++ b/bin/omarchy-hw-platform @@ -0,0 +1,189 @@ +#!/bin/bash -p + +# omarchy:summary=Print the hardware platform: apple-silicon, qualcomm, generic-aarch64 or generic. + +# ARM laptops have no DMI, so the board identity is the root node of the device +# tree the firmware hands the kernel: "apple," from Asahi's m1n1 on every +# Apple Silicon Mac, "qcom," on Snapdragon laptops. Each compatible string +# is a NUL-separated list, and only a token's vendor prefix counts. +# +# An image is built away from the machine it will run on, so while this root is +# being built rather than booted, the image-target manifest the builder wrote +# names the platform and the build host's device tree is never read. +# +# Fixture roots let unprivileged tests fake the device tree, PID 1 and the image +# root. Naming any of them makes the whole view a fixture: nothing live is read, +# not even the device tree when only the image root is named. A privileged +# caller must not let its environment pick the platform at all. +# Privileged mode (-p) keeps exported functions and BASH_ENV out of this shell, +# so a launch without it is refused. Root then starts over in an empty +# environment and reads only the live paths with a fixed PATH. +if [[ $- != *p* ]]; then + echo "Refusing an unsafe Bash startup." >&2 + exit 126 +fi + +if (( EUID == 0 )) && [[ ${1:-} != --clean-environment ]]; then + exec /usr/bin/env -i PATH=/usr/bin /bin/bash -p -- "${BASH_SOURCE[0]}" --clean-environment "$@" +fi + +if (( EUID == 0 )); then + export PATH=/usr/bin + fixtures="" +else + fixtures=${OMARCHY_PROC_ROOT:-}${OMARCHY_SYS_ROOT:-}${OMARCHY_IMAGE_ROOT:-} +fi + +if [[ -z $fixtures ]]; then + proc=/proc + sources=(/proc/device-tree/compatible /sys/firmware/devicetree/base/compatible) + image_root="" + image_dir=/var/lib/omarchy/image + image_owner=0 +else + proc=${OMARCHY_PROC_ROOT:-} + sources=() + [[ -z ${OMARCHY_PROC_ROOT:-} ]] || sources+=("$OMARCHY_PROC_ROOT/device-tree/compatible") + [[ -z ${OMARCHY_SYS_ROOT:-} ]] || sources+=("$OMARCHY_SYS_ROOT/firmware/devicetree/base/compatible") + image_root=${OMARCHY_IMAGE_ROOT:-} + image_dir=${image_root:+$image_root/var/lib/omarchy/image} + image_owner=$EUID +fi +manifest=$image_dir/target + +contradiction() { + echo "Error: contradictory platform identity: $1" >&2 + exit 1 +} + +if ! machine=$(uname -m) || [[ -z $machine ]]; then + echo "Error: cannot read the CPU architecture" >&2 + exit 1 +fi + +# A booted system is one systemd runs as PID 1 from this root. An image build +# is told apart by what it shows: no /run/systemd/system (a root with a /run of +# its own), PID 1's root being another (a chroot on the build host, even with +# the host's /run bound in, as arch-chroot does), or PID 1 being something other +# than systemd (a PID namespace whose PID 1 is the build). What cannot be seen, +# such as PID 1's root to a normal user or /proc/1 behind hidepid, never makes a +# build: a booted system always answers from its hardware, so a build runs as +# root. A caller in a private PID namespace looks like a build too, so nothing +# that asks the detector runs with PrivatePIDs=. +booted() { + local comm + + [[ -d $image_root/run/systemd/system ]] || return 1 + if [[ -n $proc && -e $proc/1/root ]]; then + [[ $proc/1/root -ef $image_root/ ]] || return 1 + fi + if [[ -n $proc && -r $proc/1/comm ]]; then + read -r comm <"$proc/1/comm" && [[ $comm == "systemd" ]] || return 1 + fi +} + +# Owned by root (the test's own user for a fixture), not a symlink, and not +# writable by anyone else. +trusted() { + local owner mode + + [[ -e $1 && ! -L $1 ]] || return 1 + read -r owner mode < <(stat -c '%u %a' -- "$1") || return 1 + [[ $owner =~ ^[0-9]+$ && $mode =~ ^[0-7]+$ ]] && (( owner == image_owner && (8#$mode & 8#022) == 0 )) +} + +# The manifest: format=1, platform= one of the four names, comments and unknown +# keys ignored, and any other line refused. The detector reads it itself, so it +# stands alone wherever a package ships a copy of it. +image_platform() { + local line key value format="" platform="" + + if [[ ! -d $image_dir || ! -f $manifest ]] || ! trusted "$image_dir" || ! trusted "$manifest"; then + echo "Error: the image manifest $manifest is not a root-owned regular file" >&2 + return 1 + fi + + while IFS= read -r line || [[ -n $line ]]; do + [[ -n $line && $line != \#* ]] || continue + if [[ $line != *=* ]]; then + echo "Error: the image manifest $manifest is malformed: $line" >&2 + return 1 + fi + key=${line%%=*} + value=${line#*=} + case $key in + format) format=$value ;; + platform) platform=$value ;; + esac + done <"$manifest" + + if [[ $format != "1" ]]; then + echo "Error: the image manifest $manifest is not format=1" >&2 + return 1 + fi + if [[ ! $platform =~ ^(apple-silicon|qualcomm|generic-aarch64|generic)$ ]]; then + echo "Error: the image manifest $manifest names no known platform: ${platform:-none}" >&2 + return 1 + fi + + echo "$platform" +} + +if [[ -n $image_dir ]] && [[ -e $manifest || -L $manifest ]] && ! booted; then + platform=$(image_platform) || exit 1 + if [[ $platform == "generic" ]]; then + [[ $machine != "aarch64" ]] || contradiction "$manifest names $platform hardware but the CPU is $machine" + else + [[ $machine == "aarch64" ]] || contradiction "$manifest names $platform hardware but the CPU is $machine" + fi + echo "$platform" + exit 0 +fi + +vendor="" +vendor_source="" +for source in "${sources[@]}"; do + [[ -r $source ]] || continue + + apple=0 + qcom=0 + mapfile -d '' -t tokens <"$source" + for token in "${tokens[@]}"; do + [[ $token == "apple,"* ]] && apple=1 + [[ $token == "qcom,"* ]] && qcom=1 + done + + if (( apple && qcom )); then + contradiction "$source names both Apple and Qualcomm hardware" + elif (( apple )); then + found=apple + elif (( qcom )); then + found=qcom + else + found=none + fi + + if [[ -n $vendor && $vendor != "$found" ]]; then + contradiction "$vendor_source says $vendor but $source says $found" + fi + vendor=$found + vendor_source=$source +done + +case $vendor in + apple | qcom) + [[ $machine == "aarch64" ]] || contradiction "$vendor_source names $vendor hardware but the CPU is $machine" + if [[ $vendor == "apple" ]]; then + echo apple-silicon + else + echo qualcomm + fi + ;; + *) + if [[ $machine == "aarch64" ]]; then + echo generic-aarch64 + else + echo generic + fi + ;; +esac diff --git a/bin/omarchy-hw-render-gpu b/bin/omarchy-hw-render-gpu new file mode 100755 index 000000000000..f439bf2ef219 --- /dev/null +++ b/bin/omarchy-hw-render-gpu @@ -0,0 +1,15 @@ +#!/bin/bash + +# omarchy:summary=Detect whether a DRM render GPU is present +# omarchy:hidden=true + +dri=${OMARCHY_DRI_PATH:-/dev/dri} + +shopt -s nullglob +for node in "$dri"/renderD*; do + if [[ -e $node ]]; then + exit 0 + fi +done + +exit 1 diff --git a/bin/omarchy-hw-touchpad b/bin/omarchy-hw-touchpad index b5e4b7772ec2..28ffc4d82ae0 100755 --- a/bin/omarchy-hw-touchpad +++ b/bin/omarchy-hw-touchpad @@ -2,5 +2,30 @@ # omarchy:summary=Print the detected Hyprland touchpad or trackpad device name -device=$(hyprctl devices -j | jq -r '[.mice[] | .name | select(test("touchpad|trackpad"; "i"))] | first // empty') +mice=$(hyprctl devices -j | jq -r '.mice[].name') +device=$(grep -iE -m1 'touchpad|trackpad' <<<"$mice") + +# Some touchpads carry neither word (Apple Silicon's is "apple-mtp-multi-touch"), +# so ask udev which mouse is one. The name alone can't say: some touchscreens' +# mouse interfaces are called multi-touch too. +if [[ -z $device ]]; then + input_class="${OMARCHY_INPUT_CLASS_PATH:-/sys/class/input}" + for event in "$input_class"/event*; do + [[ -r $event/device/name ]] || continue + # Hyprland's name for a device: the kernel's, lowercased, with spaces, + # commas and newlines as dashes, and -1, -2 and so on after a name another + # device already took. + name=$(<"$event/device/name") + name=${name,,} + name=${name//[ ,$'\n']/-} + udevadm info --query=property "$event" 2>/dev/null | grep -Fxq 'ID_INPUT_TOUCHPAD=1' || continue + while IFS= read -r mouse; do + if [[ $mouse == "$name" || ( $mouse == "$name"-* && ${mouse#"$name"-} =~ ^[0-9]+$ ) ]]; then + device=$mouse + break 2 + fi + done <<<"$mice" + done +fi + [[ -n $device ]] && echo "$device" diff --git a/bin/omarchy-hyprland-monitor-watch b/bin/omarchy-hyprland-monitor-watch index 33e8f2c5999b..b7199e17c2fb 100755 --- a/bin/omarchy-hyprland-monitor-watch +++ b/bin/omarchy-hyprland-monitor-watch @@ -10,6 +10,7 @@ sync_clamshell() { ( flock -n 9 || exit 0 omarchy-hyprland-monitor-clamshell + omarchy-system-lid-inhibit ) 9>"$LOCK" } diff --git a/bin/omarchy-install-browser b/bin/omarchy-install-browser index 4593bc72d87b..4e20842224d3 100755 --- a/bin/omarchy-install-browser +++ b/bin/omarchy-install-browser @@ -8,6 +8,8 @@ set -e source "$OMARCHY_PATH/install/helpers/browser-policy.sh" +opt_path="${OMARCHY_OPT_PATH:-/opt}" + setup_chromium_policy_directory() { browser_policy_setup_dir "$1" } @@ -17,6 +19,17 @@ announce_browser_installed() { echo "$1 browser installed. Make it the default via Setup > Defaults > Browser." } +# The widevine package registers its CDM for Chromium and Firefox only; Brave +# looks for one in its own install directory. +link_widevine_cdm() { + local cdm="$opt_path/WidevineCdm/chromium" + local install_dir="$opt_path/$1" + + if [[ -d $cdm && -d $install_dir && ! -e $install_dir/WidevineCdm ]]; then + as_root ln -sfn "$cdm" "$install_dir/WidevineCdm" + fi +} + copy_chromium_flags() { mkdir -p ~/.config cp -f "$OMARCHY_PATH/config/chromium-flags.conf" "$1" @@ -37,6 +50,7 @@ chromium) setup_chromium_policy_directory /etc/chromium/policies/managed copy_chromium_flags ~/.config/chromium-flags.conf omarchy-theme-set-browser + omarchy-lifecycle-dispatch post-install chromium announce_browser_installed "Chromium" ;; chrome) @@ -46,6 +60,7 @@ chrome) setup_chromium_policy_directory /etc/opt/chrome/policies/managed copy_chromium_flags ~/.config/chrome-flags.conf omarchy-theme-set-browser + omarchy-lifecycle-dispatch post-install chrome announce_browser_installed "Chrome" ;; edge) @@ -55,6 +70,7 @@ edge) setup_chromium_policy_directory /etc/opt/edge/policies/managed copy_chromium_flags ~/.config/microsoft-edge-stable-flags.conf omarchy-theme-set-browser + omarchy-lifecycle-dispatch post-install edge announce_browser_installed "Edge" ;; brave) @@ -63,7 +79,9 @@ brave) setup_chromium_policy_directory /etc/brave/policies/managed copy_chromium_flags ~/.config/brave-flags.conf + link_widevine_cdm brave-bin omarchy-theme-set-browser + omarchy-lifecycle-dispatch post-install brave announce_browser_installed "Brave" ;; brave-origin) @@ -72,7 +90,9 @@ brave-origin) setup_chromium_policy_directory /etc/brave/policies/managed copy_chromium_flags ~/.config/brave-origin-flags.conf + link_widevine_cdm brave-origin-bin omarchy-theme-set-browser + omarchy-lifecycle-dispatch post-install brave-origin announce_browser_installed "Brave Origin" ;; firefox) @@ -81,6 +101,7 @@ firefox) browser_policy_setup_firefox_distribution /usr/lib/firefox/distribution setup_firefox_wayland + omarchy-lifecycle-dispatch post-install firefox announce_browser_installed "Firefox" ;; zen) @@ -89,6 +110,7 @@ zen) browser_policy_setup_firefox_distribution /opt/zen-browser/distribution setup_firefox_wayland + omarchy-lifecycle-dispatch post-install zen announce_browser_installed "Zen" ;; *) diff --git a/bin/omarchy-install-gaming-gpu-lib32 b/bin/omarchy-install-gaming-gpu-lib32 index 2e4e3533fe79..97a51801b664 100755 --- a/bin/omarchy-install-gaming-gpu-lib32 +++ b/bin/omarchy-install-gaming-gpu-lib32 @@ -27,4 +27,8 @@ elif omarchy-hw-nvidia-without-gsp; then PACKAGES+=(lib32-nvidia-580xx-utils) fi -(( ${#PACKAGES[@]} > 0 )) && omarchy-pkg-add "${PACKAGES[@]}" +if (( ${#PACKAGES[@]} > 0 )); then + omarchy-pkg-add "${PACKAGES[@]}" +else + echo "No supported GPU detected; skipping lib32 graphics drivers." +fi diff --git a/bin/omarchy-install-gaming-steam b/bin/omarchy-install-gaming-steam index cefe8ce6130c..686e86b43974 100755 --- a/bin/omarchy-install-gaming-steam +++ b/bin/omarchy-install-gaming-steam @@ -7,6 +7,7 @@ set -e echo "Installing Steam..." omarchy-pkg-add steam +omarchy-lifecycle-dispatch post-install steam omarchy-install-gaming-gpu-lib32 echo "" diff --git a/bin/omarchy-install-service-1password b/bin/omarchy-install-service-1password index fc7238a6dd80..cfcb58638700 100755 --- a/bin/omarchy-install-service-1password +++ b/bin/omarchy-install-service-1password @@ -21,8 +21,33 @@ install_chromium_extension() { sudo chmod 644 "$EXTENSION_FILE" } +# Without a DRM render node (a VM, a headless GPU, a GPU with no driver yet) +# Electron draws nothing unless it is told to use software GL. Launch 1Password +# through a wrapper that adds those flags while there is no render node, and +# point the user's desktop entry at it. +wrap_1password_for_software_gl() { + local real=${OMARCHY_1PASSWORD_BIN:-/opt/1Password/1password} + local vendor=${OMARCHY_1PASSWORD_DESKTOP:-/usr/share/applications/1password.desktop} + local status=0 + + if omarchy-hw-render-gpu || [[ ! -x $real ]]; then + return 0 + fi + + omarchy-cmd-electron-gl-wrap 1password "$real" || status=$? + if (( status == 0 )); then + omarchy-cmd-desktop-exec-repair "$HOME/.local/share/applications/1password.desktop" \ + "$vendor" "${OMARCHY_ELECTRON_GL_BIND_DIR:-/usr/local/bin}/1password" "$real" 1password + elif (( status == 3 )); then + echo "Preserving administrator-owned 1Password launcher" >&2 + else + return "$status" + fi +} + echo "Installing 1Password..." omarchy-pkg-add 1password 1password-cli +wrap_1password_for_software_gl echo "Installing 1Password extension for Chromium..." install_chromium_extension diff --git a/bin/omarchy-lifecycle-dispatch b/bin/omarchy-lifecycle-dispatch new file mode 100755 index 000000000000..deabe118eea1 --- /dev/null +++ b/bin/omarchy-lifecycle-dispatch @@ -0,0 +1,199 @@ +#!/bin/bash -p + +# omarchy:summary=Run a fixed lifecycle operation through the platform's own packages +# omarchy:args=[--resolve] [arguments...] +# omarchy:requires-sudo=true +# omarchy:hidden=true + +# Provisioning, factory reset, update, boot, system and user setup and app +# installs call a fixed set of operations here. A platform that needs its own +# handling registers directories of root-owned entrypoints below; on every other +# platform each operation is a no-op and the caller keeps its generic path. The +# contract is docs/lifecycle-dispatch.md. +# +# --resolve prints the entrypoint an operation would run, or nothing when it is +# a no-op here, so a caller can choose between a platform implementation and +# its own generic path. +# +# Root runs this from provisioning, factory reset and update, and it decides +# what runs as root. -p keeps a root caller's environment from running code in +# this shell before the first line (bash then ignores BASH_ENV and exported +# functions), so a launch without it is refused. +if [[ $- != *p* ]]; then + echo "Refusing an unsafe Bash startup." >&2 + exit 126 +fi + +set -euo pipefail + +operations=( + provision-prepare provision-commit provision-verify + reset-prepare reset-verify reset-commit reset-rollback + update-verify update-takeover + luks-slots + setup-boot setup-system setup-user + post-install pre-remove +) + +# Run as the user, never as root, with that user's session. +user_operations=(setup-user post-install pre-remove) + +usage() { + echo "Usage: omarchy-lifecycle-dispatch [--resolve] [arguments...]" >&2 + echo "Operations: ${operations[*]}" >&2 + exit 2 +} + +listed() { + local item=$1 candidate + shift + for candidate in "$@"; do + [[ $candidate == "$item" ]] && return 0 + done + return 1 +} + +resolve=0 +if [[ ${1:-} == "--resolve" ]]; then + resolve=1 + shift +fi +operation=${1:-} +[[ -n $operation ]] || usage +shift +listed "$operation" "${operations[@]}" || usage + +# A privileged caller must not let its environment choose what runs as root: +# root resolves only the fixed paths below, and the detector beside this file +# ignores its fixture roots. Fixture roots exist for unprivileged tests. +fixed_path=/usr/local/sbin:/usr/local/bin:/usr/bin +detect=("$(PATH=$fixed_path dirname -- "${BASH_SOURCE[0]}")/omarchy-hw-platform") +if (( EUID == 0 )); then + export PATH=$fixed_path + detect=(/usr/bin/env -i PATH="$fixed_path" "${detect[@]}") + root="" +else + root=${OMARCHY_LIFECYCLE_ROOT:-} + root=${root%/} + if [[ -n $root && $root != /* ]]; then + echo "Error: OMARCHY_LIFECYCLE_ROOT must be an absolute path" >&2 + exit 1 + fi +fi + +if ! platform=$("${detect[@]}"); then + echo "Error: cannot determine the hardware platform for $operation" >&2 + exit 1 +fi + +# Generic, generic-aarch64 and Qualcomm machines have no registered package: +# every operation is a no-op there and callers keep their generic path. +# Apple Silicon's boot operations belong to omarchy-mac-boot, and its system and +# user setup and app-install hooks to omarchy-mac (both omacom/omarchy-mac-pkgs). +# Provisioning and factory reset have no other path on a Mac, only update-verify +# checks what an update left in its boot chain, its boot checks prove the kept +# LUKS slots that luks-slots records, and hardware setup has no other way to run +# the Mac's boot setup (setup-boot), so those operations are required. So is +# update-takeover: the Mac boot chain and its image own files no package does, +# and a Mac whose boot package can't vouch for a takeover keeps them. Setup and +# the app-install hooks are optional: a Mac without omarchy-mac keeps Omarchy's +# generic setup and installs. Moving an operation to required also changes +# apple_optional in test/shell.d/lifecycle-dispatch-test.sh and the Apple column +# and registration table in docs/lifecycle-dispatch.md. +case $platform:$operation in + apple-silicon:setup-system | apple-silicon:setup-user | apple-silicon:post-install | apple-silicon:pre-remove) + implementation=/usr/lib/omarchy/mac + package=omarchy-mac + required=() + ;; + apple-silicon:*) + implementation=/usr/lib/omarchy/mac-boot + package=omarchy-mac-boot + required=( + provision-prepare provision-commit provision-verify + reset-prepare reset-verify reset-commit reset-rollback + update-verify update-takeover + luks-slots + setup-boot + ) + ;; + *) + exit 0 + ;; +esac + +# Checked once the platform registers the operation, so an installer run with +# sudo still works where the hook is a no-op. +if listed "$operation" "${user_operations[@]}" && (( EUID == 0 )); then + echo "Error: $operation runs as the user, never as root" >&2 + exit 1 +fi + +# Every path from the entrypoint up to / (or the fixture root) must be a real +# file or directory owned by root, or by the unprivileged caller inside a +# fixture root, and writable by nobody else. +trusted() { + local path=$1 top=${root:-/} owner mode + + [[ -f $path && -x $path ]] || return 1 + while true; do + [[ ! -L $path ]] || return 1 + read -r owner mode < <(stat -c '%u %a' -- "$path") || return 1 + if (( owner != 0 )); then + [[ -n $root ]] && (( EUID != 0 && owner == EUID )) || return 1 + fi + (( (8#$mode & 8#022) == 0 )) || return 1 + [[ $path != "$top" ]] || return 0 + [[ $path != "/" ]] || return 1 + path=$(dirname -- "$path") + done +} + +entrypoint=$root$implementation/$operation + +# The package's own pacman record tells a machine without it from one whose +# copy predates the operation. +installed_version() { + local record + for record in "$root/var/lib/pacman/local/$package"-[0-9]*/; do + [[ -d $record ]] || continue + record=${record%/} + printf '%s\n' "${record##*/"$package"-}" + return 0 + done + return 1 +} + +# 3 is a machine without the platform's boot package at all, which a caller may +# tell apart from a failure; 1 is everything else. +if [[ ! -e $entrypoint && ! -L $entrypoint ]]; then + if listed "$operation" "${required[@]}"; then + if version=$(installed_version); then + echo "Error: $operation on $platform needs $implementation/$operation, which $package $version does not provide; update $package" >&2 + exit 1 + fi + echo "Error: $operation on $platform needs $package, which provides $implementation/$operation; it is not installed" >&2 + exit 3 + fi + exit 0 +fi + +if ! trusted "$entrypoint"; then + echo "Error: refusing $implementation/$operation: it must be a root-owned executable file in root-owned directories, writable by no one else" >&2 + exit 1 +fi + +if (( resolve )); then + printf '%s\n' "$entrypoint" + exit 0 +fi + +# A user operation also gets the user's home and session, when there is one, +# and nothing else. +environment=(PATH="$fixed_path") +if listed "$operation" "${user_operations[@]}"; then + for name in HOME USER XDG_CONFIG_HOME XDG_STATE_HOME XDG_RUNTIME_DIR DBUS_SESSION_BUS_ADDRESS WAYLAND_DISPLAY OMARCHY_PATH; do + [[ -z ${!name:-} ]] || environment+=("$name=${!name}") + done +fi +exec /usr/bin/env -i "${environment[@]}" "$entrypoint" "$@" diff --git a/bin/omarchy-menu-keybindings b/bin/omarchy-menu-keybindings index 767b5dac9813..d70844b78386 100755 --- a/bin/omarchy-menu-keybindings +++ b/bin/omarchy-menu-keybindings @@ -9,12 +9,25 @@ declare -A LUA_BIND_KEY_MAP declare -A LUA_BIND_DISPATCHER_MAP declare -A LUA_BIND_ARG_MAP +# A platform package can name keys the way its keyboards print them: one +# " " per line (on a MacBook, XF86MonBrightnessUp is F2), in the +# fixed platform root, which no environment variable moves. +key_names_file=/usr/share/omarchy-platform/key-names + # Hyprland reports XKB keycodes for code: bindings. Resolve them to symbols # via the compiled keymap, with a small fallback for common keys so the menu # remains readable if xkbcli cannot resolve a symbol. parse_keycodes() { - awk ' + awk -v key_names="$key_names_file" ' BEGIN { + while ((getline line < key_names) > 0) { + # Blanks around a line and a CRLF line end read the same, as in + # displays.conf. + gsub(/^[ \t\r]+|[ \t\r]+$/, "", line) + if (split(line, name_parts, /[ \t]+/) == 2 && name_parts[1] ~ /^[A-Za-z0-9_]+$/ && name_parts[2] ~ /^[A-Za-z0-9_]+$/) key_name[name_parts[1]] = name_parts[2] + } + close(key_names) + split("10=1 11=2 12=3 13=4 14=5 15=6 16=7 17=8 18=9 19=0 20=MINUS 21=EQUAL 59=COMMA 60=PERIOD 61=SLASH", fallbacks, " ") for (i in fallbacks) { separator = index(fallbacks[i], "=") @@ -59,6 +72,13 @@ parse_keycodes() { symbol = mouse_symbol[code] if (symbol == "") symbol = "mouse:" code sub("mouse:" code, symbol) + } else if ((first = index($0, ",")) > 0) { + # Only the key (the second field) is renamed: the description, the + # dispatcher and its argument are left as they are. + rest = substr($0, first + 1) + second = index(rest, ",") + key = second > 0 ? substr(rest, 1, second - 1) : rest + if (key in key_name) $0 = substr($0, 1, first) key_name[key] (second > 0 ? substr(rest, second) : "") } print @@ -81,7 +101,9 @@ build_lua_bind_cache() { LUA_BIND_DISPATCHER_MAP["$cache_key"]="$dispatcher" LUA_BIND_ARG_MAP["$cache_key"]="$arg" done < <( - lua <<'LUA' + # -E keeps LUA_INIT and LUA_PATH from the environment out of the scan, which + # loads the user's config. + lua -E - <<'LUA' local modifiers = { SHIFT = 1, CTRL = 4, CONTROL = 4, ALT = 8, SUPER = 64 } local function split_keys(keys) @@ -530,8 +552,9 @@ output_binding_records_uncached() { keybindings_cache_key() { { - printf 'v13\n' + printf 'v14\n' hyprctl devices 2>/dev/null | grep -F 'active keymap:' + cat "$key_names_file" 2>/dev/null hyprctl binds 2>/dev/null } | sha256sum | awk '{ print $1 }' } diff --git a/bin/omarchy-pkg-defaults b/bin/omarchy-pkg-defaults new file mode 100755 index 000000000000..9d28a4a28975 --- /dev/null +++ b/bin/omarchy-pkg-defaults @@ -0,0 +1,34 @@ +#!/bin/bash + +# omarchy:summary=Print the default package set for this computer's platform, or for the named one: the base list plus its architecture's and platform's additions. +# omarchy:args=[apple-silicon|qualcomm|generic-aarch64|generic] +# omarchy:hidden=true + +set -eo pipefail + +platform=${1:-} +if [[ -z $platform ]]; then + platform=$(omarchy-hw-platform) +fi + +case $platform in + apple-silicon | qualcomm | generic-aarch64) lists=(base aarch64 "$platform") ;; + generic) lists=(base) ;; + *) + echo "Error: unknown platform '$platform'" >&2 + exit 1 + ;; +esac + +# The base list always exists. An architecture or platform with nothing to add +# has no list of its own. +files=() +for list in "${lists[@]}"; do + file="$OMARCHY_PATH/install/omarchy-$list.packages" + if [[ $list == "base" || -f $file ]]; then + files+=("$file") + fi +done + +# One name per line, first occurrence kept, in list order. +sed -e 's/[[:space:]]*#.*$//' -e '/^[[:space:]]*$/d' "${files[@]}" | awk '!seen[$0]++' diff --git a/bin/omarchy-provision-first-run b/bin/omarchy-provision-first-run index 11c2eb881654..61568d8cccfe 100755 --- a/bin/omarchy-provision-first-run +++ b/bin/omarchy-provision-first-run @@ -77,6 +77,10 @@ run_first_run_step "install agent setup post-update hook" \ run_first_run_step "enable user systemd units" \ bash "$OMARCHY_PATH/install/user/first-run/enable-user-units.sh" +# Finalization ran it too, maybe without a session; the platform finishes what +# needs one here. +run_first_run_step "run the platform's user setup" \ + omarchy-lifecycle-dispatch setup-user run_first_run_step "set GNOME theme" \ bash "$OMARCHY_PATH/install/user/first-run/gnome-theme.sh" run_first_run_step "set GTK primary paste" \ diff --git a/bin/omarchy-provision-hardware b/bin/omarchy-provision-hardware new file mode 100755 index 000000000000..f0b7de8979e9 --- /dev/null +++ b/bin/omarchy-provision-hardware @@ -0,0 +1,159 @@ +#!/bin/bash + +# omarchy:summary=Finish the hardware setup an image build deferred to this machine +# omarchy:requires-sudo=true +# omarchy:hidden=true + +# Started by omarchy-provision-hardware.service on every boot while an image's +# deferred steps remain (see install/helpers/image-target.sh), and safe to call +# from a platform's own first boot as well: runs are serialised. The first run +# retires the image's build manifest, so from then on this machine is itself. +# Steps run in order and each one that succeeds leaves the queue, so a failure +# stops the run and the next run resumes at that step. Once the queue is empty +# the initramfs is rebuilt if any step changed its configuration or asked for +# the rebuild, and the service is disabled. With nothing deferred this does +# nothing. A failed step, or a failure to make the pacman keyring the build +# asked for, exits 75 (it stays queued for the next run); anything else that +# stops the run, such as an untrusted manifest or queue, exits 1. + +set -euo pipefail + +fail() { + echo "Error: $*" >&2 + exit 1 +} + +if (( EUID == 0 )); then + export OMARCHY_PATH=/usr/share/omarchy + export PATH=/usr/share/omarchy/bin:/usr/local/sbin:/usr/local/bin:/usr/bin +elif [[ -z ${OMARCHY_IMAGE_ROOT:-} || -z ${OMARCHY_PATH:-} ]]; then + fail "omarchy-provision-hardware must run as root" +fi +export OMARCHY_INSTALL=$OMARCHY_PATH/install +export OMARCHY_INSTALL_USER="" + +source "$OMARCHY_INSTALL/helpers/logging.sh" +source "$OMARCHY_INSTALL/helpers/image-target.sh" +omarchy_image_init +export OMARCHY_INSTALL_LOG_FILE=$omarchy_image_root/var/log/omarchy-install.log + +queue_present() { + [[ -e $omarchy_image_queue || -L $omarchy_image_queue ]] +} + +# Anything left for the first boot: the manifest, a queue, or the keyring an +# image build asked for, which is owed even when no step was queued. +work_present() { + omarchy_image_manifest_present || queue_present || [[ -e $omarchy_image_keyring_request ]] +} + +work_present || exit 0 +omarchy_image_lock || exit 1 +# Another run may have finished while this one waited for the lock. +work_present || exit 0 + +if omarchy_image_manifest_present; then + omarchy_image_read_manifest || exit 1 + mv -f "$omarchy_image_manifest" "$omarchy_image_manifest.booted" || fail "could not retire $omarchy_image_manifest" + live_platform=$(omarchy-hw-platform) || live_platform=unknown + echo "First boot of an image built for $omarchy_image_platform, on $live_platform hardware" +fi + +remaining=() +if queue_present; then + [[ -f $omarchy_image_queue ]] && omarchy_image_trusted "$omarchy_image_queue" || + fail "$omarchy_image_queue is not a root-owned regular file" + mapfile -t queued <"$omarchy_image_queue" + for step in "${queued[@]}"; do + [[ -z $step ]] || remaining+=("$step") + done +fi + +# What the boot image is built from: mkinitcpio's configuration and the kernel +# command line Limine's entry tool adds (leaves such as intel/fred.sh add to it). +initramfs_inputs() { + local file + + for file in "$omarchy_image_root/etc/mkinitcpio.conf" "$omarchy_image_root/etc/mkinitcpio.conf.d/"* \ + "$omarchy_image_root/etc/default/limine" "$omarchy_image_root/etc/limine-entry-tool.d/"*; do + [[ -e $file ]] || continue + printf '%s\0' "$file" + cat -- "$file" + done | cksum +} + +# Recorded before the first step and kept until a rebuild lands, so a step that +# changed the initramfs and then failed or lost power still gets one. +if [[ ! -f $omarchy_image_initramfs_baseline ]]; then + baseline=$(mktemp "$omarchy_image_dir/.initramfs-inputs.XXXXXX") && + initramfs_inputs >"$baseline" && chmod 0644 "$baseline" && mv -f "$baseline" "$omarchy_image_initramfs_baseline" || + fail "could not record the initramfs inputs" +fi + +mkdir -p "$(dirname "$OMARCHY_INSTALL_LOG_FILE")" + +# A step that would build the boot image itself (a platform's boot setup) asks +# for this run's rebuild instead, so the image is built once, after the last +# step. The request stays on disk until that rebuild lands. +export OMARCHY_IMAGE_BOOT_REBUILD=$omarchy_image_boot_rebuild +# Tells a step it runs on the booted machine, after boot already brought up the +# targets its units hang off (see install/hardware/bluetooth.sh). +export OMARCHY_IMAGE_DEFERRED_HARDWARE=1 + +# An image build asks for this machine's own pacman keyring (see +# install/post-install/pacman.sh), made before any step installs a package. Any +# keyring the build or an interrupted run left is replaced; the request stays +# until a keyring is complete. +if [[ -e $omarchy_image_keyring_request ]]; then + echo "Creating this machine's pacman keyring" + keyring=$omarchy_image_root/etc/pacman.d/gnupg + if ! { rm -rf "$keyring" && pacman-key --gpgdir "$keyring" --init && + pacman-key --gpgdir "$keyring" --populate; } >"$OMARCHY_INSTALL_LOG_FILE" 2>&1; then + echo "Error: could not create the pacman keyring (see $OMARCHY_INSTALL_LOG_FILE); the next run retries it" >&2 + exit 75 + fi + rm -f "$omarchy_image_keyring_request" +fi + +while (( ${#remaining[@]} )); do + step=${remaining[0]} + [[ $step == install/hardware/*.sh && $step != *..* ]] || fail "Deferred step is not a hardware setup step: $step" + script=$OMARCHY_PATH/$step + + if [[ -L $script ]]; then + fail "Deferred step is a symlink: $step" + elif [[ ! -e $script ]]; then + echo "Skipping deferred hardware step $step: this Omarchy no longer ships it" + else + echo "Running deferred hardware step $step" + if ! run_logged "$script"; then + echo "Error: Deferred hardware step failed: $step (see $OMARCHY_INSTALL_LOG_FILE); the next run resumes here" >&2 + exit 75 + fi + fi + + remaining=("${remaining[@]:1}") + omarchy_image_write_queue "${remaining[@]}" || fail "could not update $omarchy_image_queue" +done + +if [[ -e $omarchy_image_boot_rebuild || $(initramfs_inputs) != "$(<"$omarchy_image_initramfs_baseline")" ]]; then + echo "Rebuilding the initramfs for the deferred hardware setup" + if omarchy-cmd-present limine-mkinitcpio; then + limine-mkinitcpio >"$OMARCHY_INSTALL_LOG_FILE" 2>&1 + else + mkinitcpio -P >"$OMARCHY_INSTALL_LOG_FILE" 2>&1 + fi || fail "Initramfs rebuild failed (see $OMARCHY_INSTALL_LOG_FILE); the next run retries it" + # The new image must be on disk before the queue that owes it is cleared. + sync || fail "could not flush the rebuilt initramfs to disk" +fi +rm -f "$omarchy_image_initramfs_baseline" "$omarchy_image_boot_rebuild" + +# Disabled, not deleted: a platform first boot that runs this while the +# service's start job waits would otherwise fail that job at the next +# daemon-reload. The emptied queue keeps the unit from running. +rm -f "$omarchy_image_systemd_dir/multi-user.target.wants/$omarchy_image_unit" +rm -f "$omarchy_image_queue" +if (( EUID == 0 )); then + systemctl daemon-reload 2>/dev/null || true +fi +echo "Deferred hardware setup is complete" diff --git a/bin/omarchy-provision-owner b/bin/omarchy-provision-owner index b5321a4d118f..2c4ff0b4788d 100755 --- a/bin/omarchy-provision-owner +++ b/bin/omarchy-provision-owner @@ -9,7 +9,9 @@ # behind. Asks for the user (the configurator's Step 2), creates it with the # groups system setup recorded, finalizes it offline from the stashed Node # tarball, re-keys LUKS from the throwaway install passphrase to the user's -# password, and hands off to SDDM. +# password, and hands off to SDDM. A platform whose boot package owns its boot +# chain prepares it, drops the install key's unlock from it and records the +# kept key slot through omarchy-lifecycle-dispatch. set -euo pipefail @@ -33,6 +35,9 @@ fi export PATH="$OMARCHY_PATH/bin:$PATH" +REKEY_STATE=$PROVISIONING_DIR/luks-rekey.state +source "$OMARCHY_PATH/install/provisioning/luks-rekey.sh" + # The shared setup form — the same questions, prompts, and validation the ISO # configurator's user step asks. The ISO vendors this very file out of the # runtime package it bundles, so an install and the first boot that finishes it @@ -579,14 +584,27 @@ greeter_screen() { # The keyboard step the ISO configurator runs — deferred to first boot for OEM # installs, so the machine's owner picks their own layout. Applied immediately # (live VT + persisted) so the password typed next, and the LUKS re-key below, -# use the chosen layout. +# use the chosen layout. A layout that did not take is asked for again: the +# password form must never be typed in a layout the disk unlock won't use. +# Three failures in a row mean the machine can't set a layout at all (a +# missing or broken localectl, say), so the attempt fails and setup's own +# retry-or-console screen takes over instead of asking forever. keyboard_form() { - local status + local status failures=0 while true; do step "Let's setup your keyboard..." omarchy_prompt_keyboard && status=0 || status=$? - ((status == 0)) && break + if ((status == 0)); then + apply_keyboard "$keyboard" && break + if ((++failures >= 3)); then + log_step "no keyboard layout could be set after $failures attempts" + notice "Setup could not set a keyboard layout on this machine." 4 + return 1 + fi + notice "Could not set the ${keyboard_label:-$keyboard} keyboard layout. Choose it again." 4 + continue + fi # Esc means "back", and nothing precedes the first screen, so re-ask. ((status == OMARCHY_FORM_BACK)) && continue @@ -597,8 +615,6 @@ keyboard_form() { # re-key below. Confirm it so a stray Ctrl+C doesn't bounce the machine. confirm_reboot && exec systemctl reboot done - - apply_keyboard "$keyboard" } confirm_reboot() { @@ -609,21 +625,74 @@ confirm_reboot() { gum confirm --affirmative "Yes, reboot" --negative "No, keep setting up" "Reboot this machine?" } -# Load the layout on the live VT and persist it for the installed system. -# systemd-firstboot writes both the console KEYMAP and the XKB layout Hyprland -# reads, matching what the ISO's configure_keyboard does at install time. A -# keymap localectl doesn't know keeps the default rather than failing (defensive; -# the picker no longer offers any such layout). +# XKB settings for the form's keymaps that kbd-model-map lacks, as its rows for +# the nearest keymap give them, so the desktop types what the console does: +# kbd's cz is QWERTY, bg-cp1251 phonetic and azerty French AZERTY. Non-Latin +# layouts keep a US layout to switch to, so the shared password stays typeable. +keyboard_xkb_settings() { + local layout variant="" options=terminate:ctrl_alt_bksp + local switch=terminate:ctrl_alt_bksp,grp:shifts_toggle,grp_led:scroll + + case $1 in + colemak) layout=us variant=colemak ;; + azerty) layout=fr ;; + bg-cp1251) layout=bg,us variant=phonetic, options=$switch ;; + cz) layout=cz,us variant=qwerty, options=$switch ;; + de_CH-latin1) layout=ch ;; + kyrgyz) layout=kg,us options=$switch ;; + no-latin1) layout=no ;; + pl) layout=pl ;; + ua) layout=ua,us options=$switch ;; + *) return 1 ;; + esac + + printf 'XKBLAYOUT=%s\nXKBMODEL=pc105\n' "$layout" + [[ -z $variant ]] || printf 'XKBVARIANT=%s\n' "$variant" + printf 'XKBOPTIONS=%s\n' "$options" +} + +# Load the layout on the live VT and persist it for the installed system, or +# fail. systemd-firstboot writes both the console KEYMAP and the XKB layout +# Hyprland reads, matching what the ISO's configure_keyboard does at install +# time. The boot files ask for the disk password in the layout vconsole.conf +# names, so a keymap localectl doesn't know, a failed loadkeys, or a file that +# does not read back as the chosen keymap with an XKB layout is a failure. apply_keyboard() { - local keymap="$1" - [[ $(tty 2>/dev/null) == /dev/tty* ]] && loadkeys "$keymap" 2>/dev/null || true + local keymap=$1 keymaps saved - if localectl --no-pager list-keymaps 2>/dev/null | grep -qix "$keymap"; then - systemd-firstboot --keymap="$keymap" --force >>"$LOG_FILE" 2>&1 || \ - localectl set-keymap "$keymap" >>"$LOG_FILE" 2>&1 || \ - log_step "could not persist keymap $keymap" - else - log_step "keymap $keymap unknown to localectl; keeping the default" + if [[ $(tty 2>/dev/null) == /dev/tty* ]] && ! loadkeys "$keymap" >>"$LOG_FILE" 2>&1; then + log_step "loadkeys could not load keymap $keymap" + return 1 + fi + + if ! keymaps=$(localectl --no-pager list-keymaps 2>>"$LOG_FILE"); then + log_step "localectl could not list keymaps to check $keymap" + return 1 + fi + if ! grep -qxF -- "$keymap" <<<"$keymaps"; then + log_step "keymap $keymap unknown to localectl" + return 1 + fi + + if ! systemd-firstboot --keymap="$keymap" --force >>"$LOG_FILE" 2>&1 && + ! localectl set-keymap "$keymap" >>"$LOG_FILE" 2>&1; then + log_step "could not persist keymap $keymap" + return 1 + fi + + # systemd-firstboot writes an XKB layout only for keymaps in kbd-model-map, + # and several the form offers are not, so Hyprland would come up in US. + if ! grep -q '^XKBLAYOUT=.' /etc/vconsole.conf 2>/dev/null; then + keyboard_xkb_settings "$keymap" >>/etc/vconsole.conf 2>>"$LOG_FILE" || + log_step "no XKB layout known for keymap $keymap" + fi + + # A clean shell, so nothing this process exported stands in for the file. + saved=$(env -i /bin/bash --noprofile --norc -c \ + '. /etc/vconsole.conf && [[ -n ${XKBLAYOUT:-} ]] && printf %s "${KEYMAP:-}"' 2>>"$LOG_FILE") || saved="" + if [[ $saved != "$keymap" ]]; then + log_step "/etc/vconsole.conf does not read back as keymap $keymap with an XKB layout" + return 1 fi } @@ -714,7 +783,7 @@ luks_device() { local src part src=$(findmnt -no SOURCE / | sed 's/\[.*//') [[ -n $src ]] || return 1 - part=$(lsblk -nspo NAME,FSTYPE "$src" 2>/dev/null | awk '$2=="crypto_LUKS"{print $1; exit}') + part=$(lsblk -nsrpo NAME,FSTYPE "$src" 2>/dev/null | awk '$2=="crypto_LUKS"{print $1; exit}') [[ -n $part ]] && { echo "$part"; return 0; } return 1 } @@ -822,13 +891,24 @@ configure_hostname() { # step, overwriting the placeholder (UTC) the deferred install seeded. configure_timezone() { [[ -n ${timezone:-} ]] || return 0 - if timedatectl set-timezone "$timezone" 2>&1; then - return 0 + if ! timedatectl set-timezone "$timezone" 2>&1; then + # Fallback only if the zone file exists, so a bad zone can't point + # /etc/localtime at nothing and break every localtime read afterward. + [[ -e /usr/share/zoneinfo/$timezone ]] && + ln -sf "/usr/share/zoneinfo/$timezone" /etc/localtime 2>&1 || true + fi + configure_wireless_regdom +} + +# The install's hardware step found only the UTC placeholder, which names no +# country, so the wireless regulatory domain comes from the owner's timezone +# here. No reboot follows setup, so the radios get it now the way udev applies +# it at every boot. +configure_wireless_regdom() { + bash "$OMARCHY_PATH/install/hardware/set-wireless-regdom.sh" 2>&1 || return 0 + if omarchy-cmd-present set-wireless-regdom; then + set-wireless-regdom 2>&1 || true fi - # Fallback only if the zone file exists, so a bad zone can't point - # /etc/localtime at nothing and break every localtime read afterward. - [[ -e /usr/share/zoneinfo/$timezone ]] && - ln -sf "/usr/share/zoneinfo/$timezone" /etc/localtime 2>&1 || true } finalize_user() { @@ -851,79 +931,133 @@ finalize_user() { "$OMARCHY_PATH/bin/omarchy-provision-user" --force --first-install } -# Move the LUKS volume from the throwaway install passphrase to the user's -# password: add the user's key, kill every other slot (throwaway + any seller -# keys a reset left behind), then rebuild the UKI without the embedded -# auto-unlock keyfile. -# -# Failing here must be LOUD (abort the attempt, offer retry): silently keeping -# the staged auto-unlock keyfile would leave the disk effectively unencrypted -# forever. -rekey_luks() { - [[ -f $PROVISIONING_DIR/luks-key ]] || return 0 +# During the provisioning window the staged key rides in the UKI's initramfs, +# named on the kernel cmdline, so boot unlocks unattended. +PROVISIONING_UNLOCK_FILES=( + /etc/omarchy/provisioning.key + /etc/limine-entry-tool.d/99-omarchy-provisioning-unlock.conf + /etc/mkinitcpio.conf.d/99-omarchy-provisioning-key.conf +) - local device - if ! device=$(luks_device) || [[ ! -e $device ]]; then - log_step "cannot locate the LUKS device from /proc/cmdline: $(cat /proc/cmdline)" - say --foreground 1 "Could not locate the LUKS device to re-key." - return 1 - fi +limine_auto_unlock_present() { + local file + for file in "${PROVISIONING_UNLOCK_FILES[@]}"; do + [[ -e $file ]] && return 0 + done + return 1 +} - if ! cryptsetup open --test-passphrase --key-file "$PROVISIONING_DIR/luks-key" "$device" 2>>"$LOG_FILE"; then - log_step "staged LUKS key does not unlock $device" - say --foreground 1 "The staged LUKS key no longer unlocks $device." - return 1 +# Rebuild the UKI without the embedded keyfile. A failed rebuild restores the +# auto-unlock, so the retry still boots unattended. +limine_auto_unlock_drop() { + rm -f "${PROVISIONING_UNLOCK_FILES[@]}" + reset_limine_config + if limine-update >>"$LOG_FILE" 2>&1; then + return 0 fi - # Add the user's key (a retry with a different password just adds another - # slot; all but the current one are killed once the rebuild succeeds). - cryptsetup luksAddKey --key-file "$PROVISIONING_DIR/luks-key" "$device" <(printf '%s' "$password") - - # Rebuild the no-auto-unlock UKI FIRST, keeping the throwaway key and slot as - # a fallback. Only once that succeeds do we kill the other slots and destroy - # the staged key — so a limine-update failure leaves a recoverable, - # still-auto-unlocking state to retry, never a disk locked to a password the - # user may have just changed. - rm -f /etc/omarchy/provisioning.key \ - /etc/limine-entry-tool.d/99-omarchy-provisioning-unlock.conf \ - /etc/mkinitcpio.conf.d/99-omarchy-provisioning-key.conf - reset_limine_config - if ! limine-update >>"$LOG_FILE" 2>&1; then + # Once the staged key is destroyed there is no auto-unlock to restore: the + # boot files already ask for the owner's password. The rebuild is still + # retried for the boot entries. + if [[ -f $PROVISIONING_DIR/luks-key ]]; then log_step "limine-update failed during re-key; restoring auto-unlock for retry" install -Dm600 "$PROVISIONING_DIR/luks-key" /etc/omarchy/provisioning.key echo 'KERNEL_CMDLINE[default]+=" cryptkey=rootfs:/etc/omarchy/provisioning.key"' \ >/etc/limine-entry-tool.d/99-omarchy-provisioning-unlock.conf echo 'FILES+=(/etc/omarchy/provisioning.key)' >/etc/mkinitcpio.conf.d/99-omarchy-provisioning-key.conf - limine-update >>"$LOG_FILE" 2>&1 || true - return 1 + else + log_step "limine-update failed during re-key; the staged key is gone, so no auto-unlock is restored" fi + limine-update >>"$LOG_FILE" 2>&1 || true + return 1 +} - local new_slot slot other_slots - new_slot=$(cryptsetup open --test-passphrase --verbose --key-file <(printf '%s' "$password") "$device" 2>&1 | - grep -o 'Key slot [0-9]* unlocked' | grep -o '[0-9]*' | head -1) - # Retiring the throwaway/seller slots must be all-or-nothing: if we can't - # identify the user's slot or a kill fails, keep the staged key and retry — - # never shred it while a slot the seller knows still unlocks the disk. - if [[ -z $new_slot ]]; then - log_step "could not identify the user's LUKS slot after re-key; keeping the staged key for retry" - say --foreground 1 "Could not confirm the LUKS re-key; will retry." +# A platform whose boot package implements provision-commit and provision-verify +# stages the unlock in its own boot chain and owns it; everywhere else it rides +# in the Limine UKI above. Deciding fails closed: the unlock then counts as +# present and cannot be dropped. +UNLOCK_OWNER="" + +unlock_owner() { + [[ -z ${UNLOCK_OWNER:-} ]] || return 0 + + local commit verify + commit=$("$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" --resolve provision-commit) || return 1 + verify=$("$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" --resolve provision-verify) || return 1 + if [[ -n $commit && -n $verify ]]; then + UNLOCK_OWNER=platform + elif [[ -z $commit && -z $verify ]]; then + UNLOCK_OWNER=limine + else + log_step "the platform implements only one of provision-commit and provision-verify" return 1 fi - if ! other_slots=$(cryptsetup luksDump "$device" | awk '/^ +[0-9]+: luks2/ { sub(":", "", $1); print $1 }'); then - log_step "luksDump failed while retiring slots; keeping the staged key for retry" - say --foreground 1 "Could not enumerate LUKS slots; will retry." +} + +luks_auto_unlock_present() { + unlock_owner || return 0 + if [[ $UNLOCK_OWNER == "platform" ]]; then + ! "$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" provision-verify >>"$LOG_FILE" 2>&1 + else + limine_auto_unlock_present + fi +} + +luks_auto_unlock_drop() { + unlock_owner || return 1 + if [[ $UNLOCK_OWNER == "platform" ]]; then + "$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" provision-commit >>"$LOG_FILE" 2>&1 + else + limine_auto_unlock_drop + fi +} + +# The platform records the slot the re-key keeps, and no recovery slot; a no-op +# where nothing does. +luks_record_slots() { + "$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" luks-slots "owner=$1" "recovery=" >>"$LOG_FILE" 2>&1 +} + +# Move the LUKS volume from the throwaway install passphrase to the user's +# password and retire every other slot (throwaway + any seller keys a reset +# left behind). luks_rekey journals each step, so an interrupted attempt +# resumes where it stopped. +rekey_luks() { + luks_rekey_pending || return 0 + + local device + if ! device=$(luks_device) || [[ ! -e $device ]]; then + log_step "cannot locate the LUKS device from /proc/cmdline: $(cat /proc/cmdline)" + say --foreground 1 "Could not locate the LUKS device to re-key." return 1 fi - for slot in $other_slots; do - [[ $slot == "$new_slot" ]] && continue - if ! cryptsetup luksKillSlot -q --key-file <(printf '%s' "$password") "$device" "$slot"; then - log_step "failed to kill LUKS slot $slot; keeping the staged key for retry" - say --foreground 1 "Could not remove the throwaway LUKS key; will retry." - return 1 - fi - done - shred -u "$PROVISIONING_DIR/luks-key" 2>/dev/null || rm -f "$PROVISIONING_DIR/luks-key" + luks_rekey "$device" +} + +# The re-key's fingerprint of the console keyboard layout the boot files ask for +# the disk password in: a hash of vconsole.conf's keymap and XKB settings, in +# file order since a later duplicate wins, so the journal never names the layout. +luks_boot_layout() { + if [[ ! -f /etc/vconsole.conf ]]; then + echo none + return + fi + awk '/^[ \t]*(KEYMAP|KEYMAP_TOGGLE|XKB[A-Z_]*)=/ { sub(/^[ \t]+/, ""); print }' /etc/vconsole.conf | + sha256sum | cut -d' ' -f1 +} + +# A re-key interrupted after the staged key was retired can only finish with +# the password the disk already holds. +rekey_accepts_password() { + local - + set +x + local device + [[ -f $REKEY_STATE ]] || return 0 + if ! device=$(luks_device) || [[ ! -e $device ]]; then + return 0 + fi + luks_rekey_accepts_password "$device" } # Start the ESP's limine.conf over from the shipped template and drop foreign @@ -990,7 +1124,7 @@ reset_limine_config() { cleanup_oem_state() { # Keep groups + packages: omarchy-system-factory-reset stages the bundled Node # tarball from these live copies when the factory snapshot predates it. - rm -f "$PROVISIONING_DIR/pending" "$PROVISIONING_DIR/authorized_keys" "$PROVISIONING_DIR/setup-user" + rm -f "$PROVISIONING_DIR/pending" "$PROVISIONING_DIR/authorized_keys" "$PROVISIONING_DIR/setup-user" "$REKEY_STATE" rm -f /etc/systemd/system/multi-user.target.wants/omarchy-provision-owner.service systemctl daemon-reload 2>/dev/null || true @@ -1017,7 +1151,7 @@ run_provisioning() { touch "$FINALIZE_WARNING_FLAG" fi - if [[ -f $PROVISIONING_DIR/luks-key ]]; then + if luks_rekey_pending; then log_step "re-keying LUKS to the user's password" echo rekey >"$STATE_FILE" rekey_luks @@ -1034,16 +1168,42 @@ run_provisioning() { limine-update fi + if luks_staged_unlock_remains; then + log_step "refusing to finish setup while the staged LUKS key or its auto-unlock remains" + return 1 + fi + log_step "cleaning up provisioning state" cleanup_oem_state log_step "first-boot setup complete" } +# Where the platform's boot package owns the boot chain, it must be ready to +# finish setup, and able to record the kept key slot, before the owner is asked +# anything. +platform_ready() { + local error + if error=$("$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" provision-prepare 2>&1 >>"$LOG_FILE") && + error=$("$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" --resolve luks-slots 2>&1 >/dev/null); then + return 0 + fi + error=${error:-"The boot package could not prepare this machine for setup."} + + log_step "the boot package is not ready for setup: $error" + clear_logo + echo + say --foreground 1 "$error" + sleep 5 + return 1 +} + run_setup() { local status + platform_ready || return 1 + while true; do - keyboard_form + keyboard_form || return 1 user_form && status=0 || status=$? if ((status != 0)); then # Esc unwinds to the keyboard step. Ctrl+C is the only other way out of a @@ -1051,7 +1211,12 @@ run_setup() { ((status == OMARCHY_FORM_SIGNAL)) && confirm_reboot && exec systemctl reboot continue fi - confirm_form && break + confirm_form || continue + if ! rekey_accepts_password; then + notice "Setup already set the disk password. Enter that same password." 4 + continue + fi + break done touch "$LOG_FILE" diff --git a/bin/omarchy-refresh-pacman b/bin/omarchy-refresh-pacman index 7406e6ad9db3..5c1eb0d56ed8 100755 --- a/bin/omarchy-refresh-pacman +++ b/bin/omarchy-refresh-pacman @@ -24,17 +24,29 @@ omarchy_security_install_sudo_cleanup_traps omarchy_security_revoke_sudo_timestamp || exit 1 omarchy_security_enable_no_update_sudo -channel="${1:-stable}" -if [[ $channel != "stable" && $channel != "rc" && $channel != "edge" ]]; then +channel="${1:-}" +if [[ -n $channel && $channel != "stable" && $channel != "rc" && $channel != "edge" ]]; then echo "Invalid channel: $channel" >&2 exit 2 fi +source "$OMARCHY_PATH/install/helpers/pacman.sh" +if ! platform=$(omarchy-hw-platform); then + echo "Cannot tell this machine's platform. No repository configuration changed." >&2 + exit 1 +fi +templates=$(omarchy_pacman_templates "$platform") || exit 1 +[[ -n $channel ]] || channel=$(omarchy_pacman_default_channel "$platform") || exit 1 +if [[ ! -f $templates/pacman-$channel.conf || ! -f $templates/mirrorlist-$channel ]]; then + echo "Omarchy has no $channel channel for $platform. No repository configuration changed." >&2 + exit 1 +fi + sudo cp -f /etc/pacman.conf /etc/pacman.conf.bak sudo cp -f /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak echo "Setting channel to $channel" -sudo cp -f "$OMARCHY_PATH/default/pacman/pacman-$channel.conf" /etc/pacman.conf -sudo cp -f "$OMARCHY_PATH/default/pacman/mirrorlist-$channel" /etc/pacman.d/mirrorlist +sudo cp -f "$templates/pacman-$channel.conf" /etc/pacman.conf +sudo cp -f "$templates/mirrorlist-$channel" /etc/pacman.d/mirrorlist # Allow user customization of /etc/pacman.conf before the upgrade runs, so # custom repositories and IgnorePkg entries shape the downgrade-capable diff --git a/bin/omarchy-reinstall-pkgs b/bin/omarchy-reinstall-pkgs index 97aff9c6a561..156ab9d48e4a 100755 --- a/bin/omarchy-reinstall-pkgs +++ b/bin/omarchy-reinstall-pkgs @@ -1,18 +1,20 @@ #!/bin/bash -# omarchy:summary=Reinstall all default Omarchy packages from the stable channel +# omarchy:summary=Reinstall all default Omarchy packages from the stable channel, or on aarch64 the edge channel # omarchy:requires-sudo=true set -e -# Reinstall all default Omarchy packages from the stable channel and downgrade any packages that are too new. +# Reinstall all default Omarchy packages from the platform's default channel +# (stable, or edge on aarch64) and downgrade any packages that are too new. -# Set the package repository to the stable mirrors +# Set the package repository to the platform's default channel omarchy-refresh-pacman -# Downgrade any packages to the stable setup +# Downgrade any packages to that channel's setup omarchy-update-pacman -Suu --noconfirm -# Ensure all packages are installed -mapfile -t packages < <(grep -v '^#' "$OMARCHY_PATH/install/omarchy-base.packages" | grep -v '^$') +# Ensure this platform's default packages are installed +default_packages=$(omarchy-pkg-defaults) +mapfile -t packages <<<"$default_packages" omarchy-update-pacman -Syu --noconfirm --needed "${packages[@]}" diff --git a/bin/omarchy-remove-gaming-steam b/bin/omarchy-remove-gaming-steam index 1b20ec22c8b7..16a2570a8bdd 100755 --- a/bin/omarchy-remove-gaming-steam +++ b/bin/omarchy-remove-gaming-steam @@ -5,6 +5,7 @@ set -e +omarchy-lifecycle-dispatch pre-remove steam omarchy-pkg-drop steam rm -rf \ diff --git a/bin/omarchy-setup-direct-boot b/bin/omarchy-setup-direct-boot index 56c7fd5065c7..d704a394c4e2 100755 --- a/bin/omarchy-setup-direct-boot +++ b/bin/omarchy-setup-direct-boot @@ -3,6 +3,14 @@ # omarchy:summary=Add or remove an EFI boot entry for the Omarchy UKI, allowing the system to boot directly # omarchy:requires-sudo=true +# Apple Silicon boots m1n1, then U-Boot, which finds the bootloader on its own +# and creates its boot entries itself, so there is no direct-boot entry to add +# or remove. +if omarchy-hw-apple-silicon; then + echo "Error: Direct boot is not supported on Apple Silicon: U-Boot finds the bootloader itself" >&2 + exit 1 +fi + if [[ ! -d /sys/firmware/efi ]]; then echo "Error: System is not booted in UEFI mode" >&2 exit 1 diff --git a/bin/omarchy-setup-security-fingerprint b/bin/omarchy-setup-security-fingerprint index a57dea4a1269..0bced71543a1 100755 --- a/bin/omarchy-setup-security-fingerprint +++ b/bin/omarchy-setup-security-fingerprint @@ -65,6 +65,87 @@ account include system-local-login EOF } +# A property of fprintd's default reader, such as its name or scan-type ("press" +# for a sensor that takes separate touches, "swipe" for one that reads a moving +# finger). Empty when fprintd can't be asked. +fingerprint_reader() { + local device + device=$(busctl --system call net.reactivated.Fprint /net/reactivated/Fprint/Manager \ + net.reactivated.Fprint.Manager GetDefaultDevice 2>/dev/null) || return 0 + device=${device#o \"} + device=${device%\"} + busctl --system get-property net.reactivated.Fprint "$device" net.reactivated.Fprint.Device "$1" 2>/dev/null | + sed -n 's/^s "\(.*\)"$/\1/p' +} + +# fprintd's own lines that carry nothing for the owner; anything else it +# prints that isn't a result, such as why a claim or start failed, is shown. +fprintd_chatter() { + [[ $1 == "Using device "* || $1 == "Enrolling "*" finger." || $1 == "Listing enrolled fingers:" || + $1 == " - #"* || $1 == "Verify started!" || $1 == "Verifying: "* ]] +} + +# fprintd-enroll's results as instructions, AGAIN asking for more. Stages are +# steps, as one can take several touches. With DUPLICATE_CHECK=1 the first stage +# is fprintd's duplicate check, not an enrollment step, and isn't shown. +enroll_progress() { + local again=$1 duplicate_check=${2:-0} line stages=0 steps=0 + while IFS= read -r line; do + if [[ $line != "Enroll result: "* ]]; then + fprintd_chatter "$line" || echo " $line" + continue + fi + case ${line#Enroll result: } in + enroll-stage-passed) + if (( ++stages > duplicate_check )); then + echo " ✓ Step $(( ++steps )) done. $again" + fi + ;; + enroll-retry-scan) echo " That one didn't read. $again" ;; + enroll-swipe-too-short) echo " Too short. $again" ;; + enroll-finger-not-centered) echo " Not centered: use the middle of the sensor. $again" ;; + enroll-remove-and-retry) echo " Lift your finger off the sensor, then: $again" ;; + enroll-too-fast) echo " Too fast. $again" ;; + enroll-completed) echo " ✓ Enrolled." ;; + enroll-duplicate) echo " ✗ That finger is already enrolled." ;; + enroll-data-full) echo " ✗ The reader has no room for another fingerprint." ;; + enroll-disconnected) echo " ✗ The reader was disconnected." ;; + enroll-failed | enroll-unknown-error) + echo " ✗ Enrollment stopped. If you stopped before it said Enrolled, run this again and keep going." ;; + *) echo " ${line#Enroll result: }" ;; + esac + done +} + +# fprintd-verify's results the same way. +# fprintd-verify suffixes each result with " (done)" or " (not done)"; a +# result that isn't done is a retry, whatever its name. +verify_progress() { + local again=$1 line + while IFS= read -r line; do + if [[ $line != "Verify result: "* ]]; then + fprintd_chatter "$line" || echo " $line" + continue + fi + line=${line#Verify result: } + if [[ $line == *" (not done)" ]]; then + case ${line% (*} in + verify-swipe-too-short) echo " Too short. $again" ;; + verify-finger-not-centered) echo " Not centered: use the middle of the sensor. $again" ;; + verify-remove-and-retry) echo " Lift your finger off the sensor, then: $again" ;; + verify-too-fast) echo " Too fast. $again" ;; + *) echo " That one didn't read. $again" ;; + esac + continue + fi + case ${line% (*)} in + verify-match) echo " ✓ Matched." ;; + verify-no-match) echo " ✗ No match." ;; + *) echo " ✗ The check stopped." ;; + esac + done +} + echo -e "\e[32mSetting up fingerprint scanner for authentication.\n\e[0m" @@ -84,15 +165,46 @@ if omarchy-pkg-missing libfprint-git fprintd usbutils; then fi # Enroll first fingerprint -echo -e "\e[32m\nLet's setup your right index finger as the first fingerprint.\e[0m" -echo -e "Keep moving the finger around on sensor until the process completes.\n" +# Authenticate sudo first, and say so: with fingerprint sudo already set up it +# asks for a touch of its own, which must not pass for the first sample. +if ! sudo -n true 2>/dev/null; then + echo -e "\nFirst, confirm it's you. Setup needs administrator rights to enroll." + if ! sudo -v; then + echo -e "\e[31mSetup needs administrator rights to enroll a fingerprint.\e[0m" + exit 1 + fi +fi -if sudo fprintd-enroll "$USER"; then +echo -e "\e[32m\nLet's set up your right index finger as the first fingerprint.\e[0m" +# A press sensor needs a touch for each sample; a swipe sensor a moving finger. +if [[ $(fingerprint_reader scan-type) == "press" ]]; then + start="Touch the sensor." again="Keep lifting your finger and touching the sensor." + echo -e "Touch the sensor, lift your finger and touch it again, and keep going until it says Enrolled. A step can take a few touches.\n" +else + start="Swipe your finger across the sensor." again="Swipe it again." + echo -e "Keep moving the finger around on sensor until it says Enrolled.\n" +fi + +# fprintd runs a duplicate check before enrolling on a reader that identifies, +# which D-Bus doesn't say. Touch ID does, and its check needs no touch while +# nothing is enrolled; any other reader's first stage counts, touched or not. +# The name is the full_name of libfprint-git's Touch ID driver; if it changes, +# Touch ID shows one step early. +duplicate_check=0 +if [[ $(fingerprint_reader name) == "Apple secure enclave fingerprint sensor" ]]; then + duplicate_check=1 +fi +# The filters only print; the exit status is fprintd's. +sudo fprintd-enroll "$USER" | enroll_progress "$again" "$duplicate_check" +enroll_status=${PIPESTATUS[0]} +if (( enroll_status == 0 )); then echo -e "\e[32m\nFingerprint enrolled successfully!\e[0m" # Verify - echo -e "\nNow let's verify that it's working correctly.\n" - if fprintd-verify; then + echo -e "\nNow let's verify that it's working correctly. $start\n" + fprintd-verify | verify_progress "$again" + verify_status=${PIPESTATUS[0]} + if (( verify_status == 0 )); then # PAM comes last, once a print is enrolled and verified. Detection only # proves a reader is there, not that libfprint can drive it — an Elan MOC # sensor outside the elanmoc table gets this far and then fails to enroll. @@ -104,6 +216,7 @@ if sudo fprintd-enroll "$USER"; then echo "You can use your fingerprint for sudo, polkit, and lock screen (Super + Ctrl + L)." else echo -e "\e[31m\nVerification failed. You may want to try enrolling again.\e[0m" + exit 1 fi else echo -e "\e[31m\nEnrollment failed. Please try again.\e[0m" diff --git a/bin/omarchy-system-factory-reset b/bin/omarchy-system-factory-reset index 43348e5a4d0a..de1e1b134fac 100755 --- a/bin/omarchy-system-factory-reset +++ b/bin/omarchy-system-factory-reset @@ -17,7 +17,9 @@ # The heavy lifting happens on the next boot via omarchy-system-factory-reset-finish, staged # here. On encrypted installs the LUKS volume is re-keyed to a throwaway # passphrase with an auto-unlock keyfile for the provisioning window; first-boot setup -# re-keys it to the new owner's password and removes the keyfile. +# re-keys it to the new owner's password and removes the keyfile. A platform +# whose boot chain the Limine UKI path can't drive rebuilds, verifies and +# unlocks it through omarchy-lifecycle-dispatch. # # Honest limitations: # - On unencrypted disks this is deletion, not forensic erasure. fstrim helps @@ -62,6 +64,51 @@ root_is_btrfs_at() { [[ $(findmnt -no FSTYPE /) == btrfs ]] && findmnt -no OPTIONS / | grep -q "subvol=/@\(,\|$\)" } +DISPATCH="$OMARCHY_PATH/bin/omarchy-lifecycle-dispatch" + +# A platform whose boot package implements the reset operations rebuilds, +# verifies and unlocks the factory root's boot chain itself; everywhere else +# the Limine UKI path below does. Deciding fails closed. +RESET_BOOT="" + +reset_boot_owner() { + [[ -z $RESET_BOOT ]] || return 0 + + local operation path found=0 missing=0 + for operation in reset-prepare reset-verify reset-commit reset-rollback; do + path=$("$DISPATCH" --resolve "$operation") || return 1 + if [[ -n $path ]]; then + found=$((found + 1)) + else + missing=$((missing + 1)) + fi + done + if (( missing == 0 )); then + RESET_BOOT=platform + elif (( found == 0 )); then + RESET_BOOT=generic + else + echo "Error: the boot package implements only some of the factory reset operations" >&2 + return 1 + fi +} + +# Runs a reset operation with its output in the log. A failure keeps the +# operation's last line for the error shown. +RESET_BOOT_ERROR="" + +reset_boot_op() { + local output status=0 + output=$("$DISPATCH" "$@" 2>&1) || status=$? + [[ -z $output ]] || printf '%s\n' "$output" >>"$LOG_FILE" + if (( status == 0 )); then + return 0 + fi + RESET_BOOT_ERROR=$(tail -n 1 <<<"$output") + RESET_BOOT_ERROR=${RESET_BOOT_ERROR:-"$1 failed"} + return 1 +} + # Resolve the crypto_LUKS partition backing the root, or return non-zero if # the root is not on LUKS. Prefers the cmdline cryptdevice= spec (archinstall # writes PARTUUID=, the pre-mounted path UUID=), and falls back to walking the @@ -81,7 +128,7 @@ luks_device() { local src part src=$(findmnt -no SOURCE / | sed 's/\[.*//') [[ -n $src ]] || return 1 - part=$(lsblk -nspo NAME,FSTYPE "$src" 2>/dev/null | awk '$2=="crypto_LUKS"{print $1; exit}') + part=$(lsblk -nsrpo NAME,FSTYPE "$src" 2>/dev/null | awk '$2=="crypto_LUKS"{print $1; exit}') [[ -n $part ]] && { echo "$part"; return 0; } return 1 } @@ -99,6 +146,11 @@ generate_passphrase() { } cleanup() { + # Before the switch the old root still boots: the boot package gives it back + # its boot state before the reset's throwaway slot is revoked. + if [[ ${reset_committed:-0} == 0 && ${RESET_BOOT_PREPARED:-0} == 1 ]]; then + reset_boot_op reset-rollback || echo "Error: $RESET_BOOT_ERROR (see $LOG_FILE)" >&2 + fi if mountpoint -q "$TOP_MNT" 2>/dev/null; then if [[ -d $TOP_MNT/$NEXT_NAME && ${swap_done:-0} == 0 ]]; then btrfs subvolume delete --recursive "$TOP_MNT/$NEXT_NAME" >/dev/null 2>&1 || true @@ -106,6 +158,9 @@ cleanup() { umount -R "$TOP_MNT" 2>/dev/null || true fi rmdir "$TOP_MNT" 2>/dev/null || true + if [[ ${reset_committed:-0} == 0 ]]; then + revoke_reset_luks + fi } trap cleanup EXIT @@ -127,8 +182,54 @@ confirm_reset() { [[ $typed == "reset" ]] || fail "Reset not confirmed." } +# The slot the reset's throwaway key opens, or nothing. Restricting cryptsetup +# to a token type no token has keeps an enrolled token from answering for it. +reset_luks_slot() { + local - + set +x + LC_ALL=C cryptsetup open --test-passphrase --verbose --token-type passphrase-only \ + --key-file <(printf '%s' "$RESET_THROWAWAY") "$RESET_LUKS_DEVICE" 2>&1 | + grep -o 'Key slot [0-9]* unlocked' | grep -o '[0-9]*' | head -1 || true +} + +# Add the throwaway slot where the platform's boot package owns the unlock. +# Called once reset-verify passed, so a failed rebuild adds no credential. +# reset-commit writes the boot-time key only once the factory root is active: +# a power loss before the switch leaves the old root asking for its password, +# never unlocked unattended. +add_reset_luks_slot() { + local - + set +x + [[ -n ${RESET_LUKS_DEVICE:-} && -n ${RESET_LUKS_AUTH:-} && -n ${RESET_THROWAWAY:-} ]] || fail "the reset is missing its throwaway passphrase" + RESET_LUKS_ADDED=1 + { cryptsetup luksAddKey --key-file <(printf '%s' "$RESET_LUKS_AUTH") "$RESET_LUKS_DEVICE" <(printf '%s' "$RESET_THROWAWAY") ; } 2>>"$LOG_FILE" || + fail "could not add the throwaway LUKS keyslot" + RESET_LUKS_SLOT=$(reset_luks_slot) + [[ -n $RESET_LUKS_SLOT ]] || fail "could not identify the throwaway LUKS keyslot" +} + +# Only the slot this attempt added goes, found by its key if the add was not +# confirmed. The platform's boot-time key is written only after activation, so +# a failed reset has none to revoke. +revoke_reset_luks() { + local - + set +x + [[ ${RESET_LUKS_ADDED:-0} == 1 && -n ${RESET_LUKS_AUTH:-} ]] || return 0 + [[ -n ${RESET_LUKS_SLOT:-} ]] || RESET_LUKS_SLOT=$(reset_luks_slot) + if [[ -n $RESET_LUKS_SLOT ]]; then + { cryptsetup luksKillSlot -q --key-file <(printf '%s' "$RESET_LUKS_AUTH") "$RESET_LUKS_DEVICE" "$RESET_LUKS_SLOT" ; } 2>>"$LOG_FILE" || true + else + echo "Could not find the reset's throwaway LUKS slot on $RESET_LUKS_DEVICE to revoke; check its keyslots" >>"$LOG_FILE" + fi + RESET_LUKS_ADDED=0 + RESET_LUKS_SLOT="" +} + # Re-key to a throwaway passphrase whose keyfile auto-unlocks boot during the -# provisioning window. $1 is the root of the staged factory system. +# provisioning window. $1 is the root of the staged factory system. Where the +# platform's boot package owns the unlock, only the staged key is written here: +# reset-prepare stages the unlock with the rest of the boot files, and the +# throwaway slot follows their verification. stage_luks_rekey() { local next="$1" device passphrase @@ -140,19 +241,29 @@ stage_luks_rekey() { local current while true; do current=$(gum input --password --placeholder "Current disk encryption passphrase" --prompt "Passphrase> ") || exit 1 - if cryptsetup open --test-passphrase --key-file <(printf '%s' "$current") "$device" 2>/dev/null; then + if cryptsetup open --test-passphrase --token-type passphrase-only --key-file <(printf '%s' "$current") "$device" 2>/dev/null; then break fi gum style --foreground 1 "That passphrase does not unlock $device. Try again." done passphrase=$(generate_passphrase) - cryptsetup luksAddKey --key-file <(printf '%s' "$current") "$device" <(printf '%s' "$passphrase") + reset_boot_owner || fail "could not tell how this machine's boot chain is reset" + RESET_LUKS_AUTH=$current + RESET_LUKS_DEVICE=$device + RESET_THROWAWAY=$passphrase + if [[ $RESET_BOOT == "generic" ]]; then + # Revoked like the platform's slot if the reset fails before activation. + RESET_LUKS_ADDED=1 + cryptsetup luksAddKey --key-file <(printf '%s' "$current") "$device" <(printf '%s' "$passphrase") + fi install -d -m 755 "$next$PROVISIONING_DIR" printf '%s' "$passphrase" >"$next$PROVISIONING_DIR/luks-key" chmod 600 "$next$PROVISIONING_DIR/luks-key" + [[ $RESET_BOOT == "generic" ]] || return 0 + install -d -m 755 "$next/etc/omarchy" printf '%s' "$passphrase" >"$next/etc/omarchy/provisioning.key" chmod 600 "$next/etc/omarchy/provisioning.key" @@ -235,6 +346,12 @@ verify_limine_hashes() { rebuild_next_boot() { local next="$1" + reset_boot_owner || fail "could not tell how this machine's boot chain is reset" + if [[ $RESET_BOOT == "platform" ]]; then + rebuild_next_boot_platform "$next" + return + fi + local esp_source esp_mount esp_mount=$(awk '$3 == "vfat" && $2 ~ /^\/(boot|efi)/ { print $2; exit }' "$next/etc/fstab") esp_source=$(awk '$3 == "vfat" && $2 ~ /^\/(boot|efi)/ { print $1; exit }' "$next/etc/fstab") @@ -297,6 +414,23 @@ rebuild_next_boot() { umount "$next$esp_mount" } +# The boot package stages the factory root's boot files and unlock, then proves +# they boot it; reset-rollback undoes both if the reset fails before the switch. +# The throwaway slot is added only once they are proven. +rebuild_next_boot_platform() { + local next="$1" + local -a luks=() + [[ -z ${RESET_LUKS_DEVICE:-} ]] || luks=("$RESET_LUKS_DEVICE") + + RESET_BOOT_PREPARED=1 + log "Rebuilding boot files from the factory system (this can take a minute)" + reset_boot_op reset-prepare "$next" "${luks[@]}" || fail "$RESET_BOOT_ERROR (see $LOG_FILE)" + log "Verifying that the factory system boots" + reset_boot_op reset-verify "$next" || fail "$RESET_BOOT_ERROR (see $LOG_FILE)" + + [[ -z ${RESET_LUKS_DEVICE:-} ]] || add_reset_luks_slot +} + # Both the staged system and the retained baseline must lose the old hashes. scrub_factory_accounts() { local root="$1" user users @@ -388,9 +522,30 @@ stage_full_reset() { log "Activating the factory system" local old="@omarchy-old-$(date +%s)" mv "$top/@" "$top/$old" - mv "$next" "$top/@" + # Until the factory root is at @ the reset is not committed: cleanup has the + # platform's boot package roll its boot state back to the old root and + # revokes the throwaway slot, so the old root goes back to @ first. + if ! mv "$next" "$top/@"; then + if mv "$top/$old" "$top/@"; then + [[ $RESET_BOOT == "platform" ]] || + fail "could not activate the factory system; the current root is back at @, but its boot files were already rebuilt from the factory system: run 'sudo limine-update' before rebooting (see $LOG_FILE)" + fail "could not activate the factory system; the current root is back at @ (see $LOG_FILE)" + fi + # Neither root is at @. Keep the staged one as well for recovery. + swap_done=1 + fail "could not activate the factory system or put the current root back; from a live USB, mount the Btrfs top level, rename $old to @, and rebuild its boot files if it does not boot" + fi + # From here the old root is gone, so the reset's unlock must survive. + reset_committed=1 swap_done=1 sync + + # The boot package that prepared the reset gets the key on standard input, + # never in arguments. A failure costs the first boot one prompt for the + # current disk password. + if [[ ${RESET_BOOT_PREPARED:-0} == 1 ]] && ! reset_boot_op reset-commit < <(printf '%s' "${RESET_THROWAWAY:-}"); then + log "$RESET_BOOT_ERROR" + fi } # A reset is a clone of @factory, so a machine without one has nothing to @@ -428,6 +583,10 @@ main() { require_factory_snapshot + # Decided before anything is asked: a platform whose boot package is missing + # stops here, with the dispatcher's error naming the package. + reset_boot_owner || fail "this machine's boot package cannot factory-reset it" + confirm_reset # The running system's limine-snapper-sync must not rewrite the ESP's diff --git a/bin/omarchy-system-lid-close b/bin/omarchy-system-lid-close index eb3dc097efbb..45283143434d 100755 --- a/bin/omarchy-system-lid-close +++ b/bin/omarchy-system-lid-close @@ -11,8 +11,9 @@ # omarchy-system-sleep-lock then usually finds the session already secure. # # A docked lid close does not suspend (HandleLidSwitchDocked defaults to -# ignore), so it must not lock either: that is clamshell mode, still in use on -# the external display. +# ignore, and omarchy-system-lid-inhibit covers displays logind does not count), +# so it must not lock either: that is clamshell mode, still in use on the +# external display. if omarchy-hw-laptop-closed && ! omarchy-hw-external-monitors; then omarchy-system-lock >/dev/null 2>&1 || true fi diff --git a/bin/omarchy-system-lid-inhibit b/bin/omarchy-system-lid-inhibit new file mode 100755 index 000000000000..2dbd496f9fdb --- /dev/null +++ b/bin/omarchy-system-lid-inhibit @@ -0,0 +1,100 @@ +#!/bin/bash + +# omarchy:summary=Keep logind from suspending on lid close while a display it misses is in use +# omarchy:group=system +# omarchy:hidden=true + +# systemd-logind treats a closed lid as docked only when an external display is +# on a connector type it lists (HDMI, DisplayPort, DVI, VGA...), and USB-C +# displays on Apple Silicon arrive on "USB" connectors instead. Left alone, +# logind suspends a laptop still in use on one. Holding its handle-lid-switch +# inhibitor while such a display is in use leaves the lid to Omarchy's +# clamshell handling, as HandleLidSwitchDocked=ignore would. Once released, a +# lid that is still shut suspends at once: logind rechecks a closed lid after +# every event. + +UNIT="omarchy-lid-inhibit.service" +drm_path="${OMARCHY_DRM_PATH:-/sys/class/drm}" +cgroup_path="${OMARCHY_CGROUP_PATH:-/proc/self/cgroup}" + +# Connected connectors of the external types logind does not count, by the name +# Hyprland gives them (card2-USB-2 is USB-2). DVI-A and Composite are on its +# list, but a missing comma there fuses them into one entry. +uncounted_outputs() { + local status connector + + for status in "$drm_path"/card*-*/status; do + [[ -r $status ]] || continue + connector=${status%/status} + connector=${connector##*/} + + case $connector in + card*-USB-* | card*-DVI-A-* | card*-Composite-*) ;; + *) continue ;; + esac + + [[ $(<"$status") == "connected" ]] && printf '%s\n' "${connector#card*-}" + done +} + +held() { + systemctl --user is-active --quiet "$UNIT" +} + +in_use=0 +outputs=$(uncounted_outputs) + +# In use means enabled in Hyprland: a display switched off in the monitor config +# should not keep a closed laptop awake, while one blanked by DPMS still should, +# and the connector's own "enabled" flag cannot tell the two apart on every +# driver. A compositor that does not answer, as while it reconfigures outputs +# around a lid close, changes nothing: releasing then would suspend a closed lid. +if [[ -n $outputs ]]; then + monitors=$(hyprctl monitors all -j 2>/dev/null) || exit 0 + [[ -n $monitors ]] || exit 0 + jq -e --arg outputs "$outputs" \ + '($outputs | split("\n")) as $names | any(.[]; .disabled == false and (.name | IN($names[])))' \ + <<<"$monitors" >/dev/null 2>&1 + case $? in + 0) in_use=1 ;; + 1) ;; + *) exit 0 ;; + esac +fi + +# Only where logind would ignore a docked lid is there anything to stand in for. +# An unanswered query changes nothing, for the same reason as above. +wanted=0 +if (( in_use )) && omarchy-hw-laptop; then + docked_action=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 \ + org.freedesktop.login1.Manager HandleLidSwitchDocked 2>/dev/null) || exit 0 + [[ -n $docked_action ]] || exit 0 + [[ $docked_action == 's "ignore"' ]] && wanted=1 +fi + +# Bound to the user unit of the watcher that keeps it in step, so a watcher that +# is gone cannot leave the lid inhibited until logout. systemd-run unquotes +# property values, so the backslashes in an escaped unit name are doubled. +watcher_binding() { + local cgroup="" unit + + read -r cgroup 2>/dev/null <"$cgroup_path" + unit=${cgroup##*/} + if [[ $cgroup == */user@*.service/* && ( $unit == *.scope || $unit == *.service ) ]]; then + unit=${unit//\\/\\\\} + printf '%s\n' "--property=BindsTo=$unit" "--property=After=$unit" + fi +} + +if (( wanted )); then + if ! held; then + mapfile -t bind < <(watcher_binding) + systemd-run --user --quiet --collect --unit="$UNIT" \ + --description="Omarchy clamshell lid inhibitor" \ + --property=PartOf=graphical-session.target "${bind[@]}" \ + systemd-inhibit --no-ask-password --what=handle-lid-switch --mode=block \ + --who=Omarchy --why="External display in use" sleep infinity + fi +elif held; then + systemctl --user stop "$UNIT" +fi diff --git a/bin/omarchy-update b/bin/omarchy-update index 6e58f6dd8ce3..2a2f41a067c9 100755 --- a/bin/omarchy-update +++ b/bin/omarchy-update @@ -148,6 +148,15 @@ if [[ ${1:-} == "-y" ]] || omarchy-update-confirm; then PATH="$OMARCHY_PATH/default/omarchy/sudo-no-update:$PATH" OMARCHY_SUDO_NO_UPDATE=1 omarchy-update-aur-pkgs omarchy_security_revoke_sudo_timestamp + # The last sudo-capable step: the boot files every package step left, AUR + # packages' initramfs rebuilds included, must be shown to boot the updated + # system. It follows third-party build code, so it starts cold and, like AUR, + # authorizes one command through the no-update wrapper without a reusable + # timestamp. Until it passes, the update neither finishes nor offers a reboot. + boot_verified=1 + PATH="$OMARCHY_PATH/default/omarchy/sudo-no-update:$PATH" OMARCHY_SUDO_NO_UPDATE=1 omarchy-update-boot || boot_verified=0 + omarchy_security_revoke_sudo_timestamp + # The sleep inhibitor covers AUR builds, hooks and mise as well; releasing it # needs no privilege because the held command already dropped to this user. # Release it before offering a reboot: a confirmed reboot can terminate this @@ -156,5 +165,10 @@ if [[ ${1:-} == "-y" ]] || omarchy-update-confirm; then omarchy-update-stay-awake stop update_stay_awake_stopped=1 - "$OMARCHY_PATH/bin/omarchy-update-restart" --reboot-only + if (( boot_verified )); then + "$OMARCHY_PATH/bin/omarchy-update-restart" --reboot-only + else + echo -e "\e[31m\nThe update is not finished: its boot files did not pass verification (see above). Do not reboot until omarchy update completes.\e[0m" >&2 + exit 1 + fi fi diff --git a/bin/omarchy-update-boot b/bin/omarchy-update-boot new file mode 100755 index 000000000000..2c3487c75ce3 --- /dev/null +++ b/bin/omarchy-update-boot @@ -0,0 +1,39 @@ +#!/bin/bash + +# omarchy:summary=Verify the platform's boot chain after the update's package changes +# omarchy:hidden=true +# omarchy:requires-sudo=true + +# Proves that the boot files the package steps left boot the updated system; +# omarchy update neither finishes nor offers a reboot until this passes. It is +# the update-verify lifecycle dispatch operation: a no-op where the platform +# registers no boot package, x86 included, and the platform's own entrypoint +# says what failed. + +set -e + +if (( $# )); then + echo "Usage: omarchy-update-boot" >&2 + exit 2 +fi +operation=update-verify + +# Resolving as the user keeps sudo, and its password prompt, out of updates +# with nothing to run. Root resolves again from its own fixed paths before +# anything runs. +errors=$(mktemp) +trap 'rm -f "$errors"' EXIT +status=0 +resolved=$(omarchy-lifecycle-dispatch --resolve "$operation" 2>"$errors") || status=$? + +# 3: the platform's boot package is not installed at all. Such a machine +# predates it and boots a chain the package does not manage, so verification +# warns instead of failing every update until the package is installed. +if (( status == 3 )); then + echo -e "\e[33m\n$(sed 's/^Error: //' "$errors")\nThe boot files were not verified. This machine predates its platform's boot package; updates verify them once it is installed.\e[0m" >&2 +elif (( status != 0 )); then + cat "$errors" >&2 + exit "$status" +elif [[ -n $resolved ]]; then + sudo omarchy-lifecycle-dispatch "$operation" +fi diff --git a/bin/omarchy-update-keyring b/bin/omarchy-update-keyring index 3a1c1c17692e..c7ccd0fd0afe 100755 --- a/bin/omarchy-update-keyring +++ b/bin/omarchy-update-keyring @@ -22,8 +22,36 @@ fi # Ensure we have the latest archlinux-keyring, maintainer keys might have changed # Always reinstall, as the keyring can be updated without a package version bump. +# An aarch64 machine's Arch Linux ARM repositories are signed with keys of +# their own, so its keyring is kept current the same way. +keyrings=(archlinux-keyring) +case $(omarchy-hw-platform 2>/dev/null) in + apple-silicon | qualcomm | generic-aarch64) + if omarchy-pkg-present archlinuxarm-keyring; then + keyrings+=(archlinuxarm-keyring) + fi + ;; +esac +# A platform package names the signing keyrings of the repositories it adds, +# one package per line. Only installed ones are refreshed, so this adds no +# repository the machine didn't already trust; a refreshed keyring may carry +# that repository's new keys. A line that isn't one keyring package name is +# skipped with a warning, so a mistake in the list can't stop every update. +platform_keyrings=/usr/share/omarchy-platform/keyrings +if [[ -f $platform_keyrings ]]; then + while read -r keyring extra || [[ -n $keyring ]]; do + [[ -n $keyring && $keyring != \#* ]] || continue + if [[ -n $extra || ! $keyring =~ ^[a-z0-9][a-z0-9@._+-]*-keyring$ ]]; then + echo "Warning: skipping ${keyring}${extra:+ $extra} in $platform_keyrings: not a keyring package name" >&2 + continue + fi + if omarchy-pkg-present "$keyring" && [[ " ${keyrings[*]} " != *" $keyring "* ]]; then + keyrings+=("$keyring") + fi + done <"$platform_keyrings" +fi echo -e "\e[32m\nUpdate Arch signing keys\e[0m" -sudo pacman -Sy --noconfirm archlinux-keyring >/dev/null 2> >(grep -vE '^warning: archlinux-keyring-[^ ]+ is up to date -- reinstalling$' >&2) +sudo pacman -Sy --noconfirm "${keyrings[@]}" >/dev/null 2> >(grep -vE '^warning: [a-z0-9@._+-]+-keyring-[^ ]+ is up to date -- reinstalling$' >&2) # Say "correct" only once the key verifiably is: before the failure checks # above, a failed recv or reinstall still ended here with exit 0. diff --git a/bin/omarchy-update-system-pkgs-when-conflicted b/bin/omarchy-update-system-pkgs-when-conflicted index 58a3a0c02c59..37d39556d499 100755 --- a/bin/omarchy-update-system-pkgs-when-conflicted +++ b/bin/omarchy-update-system-pkgs-when-conflicted @@ -98,6 +98,17 @@ mapfile -t leftovers < <( # the moved files inactive for nothing. ((${#leftovers[@]} == $(grep -c ' exists in filesystem' "$errors"))) || exit 1 +# A platform whose boot package owns the boot chain may also have files no +# package owns that it can't do without (written by its image or installer). +# Its boot package vouches for the takeover or refuses it, and the files stay. +# Where the platform registers none, nothing resolves and nothing asks for root. +takeover=$(omarchy-lifecycle-dispatch --resolve update-takeover) || exit 1 +if [[ -n $takeover ]] && ! sudo omarchy-lifecycle-dispatch update-takeover "${leftovers[@]}"; then + echo -e "\e[33m\nThis platform keeps these files, so the upgrade can't take them over:\e[0m" >&2 + printf ' %s\n' "${leftovers[@]}" >&2 + exit 1 +fi + echo -e "\e[33m\nTaking over files pacman doesn't own yet:\e[0m" for path in "${leftovers[@]}"; do sudo mkdir -p "$replaced${path%/*}" diff --git a/bin/omarchy-voxtype-install b/bin/omarchy-voxtype-install index 1fd19f9de6d3..7b2f8fb7de68 100755 --- a/bin/omarchy-voxtype-install +++ b/bin/omarchy-voxtype-install @@ -5,6 +5,8 @@ set -e +voxtype_lib_path="${OMARCHY_VOXTYPE_LIB_PATH:-/usr/lib/voxtype}" + # Install voxtype and configure it for use. if gum confirm "Install Voxtype + AI model (~150MB) to enable dictation?"; then @@ -15,7 +17,8 @@ if gum confirm "Install Voxtype + AI model (~150MB) to enable dictation?"; then cp "$OMARCHY_PATH/default/voxtype/config.toml" ~/.config/voxtype/ voxtype setup --download --no-post-install - if omarchy-hw-vulkan; then + # A Vulkan runtime is not enough: aarch64 voxtype-bin ships no Vulkan backend. + if omarchy-hw-vulkan && [[ -x $voxtype_lib_path/voxtype-vulkan ]]; then voxtype setup gpu --enable || true fi voxtype setup systemd diff --git a/bin/omarchy-windows-vm b/bin/omarchy-windows-vm index 6c8204da905a..b017e3827e19 100755 --- a/bin/omarchy-windows-vm +++ b/bin/omarchy-windows-vm @@ -4,6 +4,12 @@ # omarchy:args= [options] # omarchy:requires-sudo=true +# The VM is an x86_64 Windows guest under KVM, which only an x86_64 host runs. +if [[ $(uname -m) != "x86_64" ]]; then + echo "Windows VM needs an x86_64 machine (it runs an x86_64 guest under KVM)." >&2 + exit 1 +fi + # The Windows VM runs a privileged container (KVM, /dev/net/tun, NET_ADMIN), so # it needs the root-owned Docker daemon. By default the user is NOT in the docker # group (that group is root-equivalent), so Docker access is gated behind a diff --git a/default/hypr/bindings/utilities.lua b/default/hypr/bindings/utilities.lua index 7aa23a4a4787..8b0a13a924a0 100644 --- a/default/hypr/bindings/utilities.lua +++ b/default/hypr/bindings/utilities.lua @@ -34,6 +34,9 @@ o.bind("SUPER + CTRL + Delete", "Toggle laptop display", "omarchy-hyprland-monit o.bind("SUPER + CTRL + ALT + Delete", "Toggle laptop display mirroring", "omarchy-hyprland-monitor-internal-mirror toggle") o.bind("switch:on:Lid Switch", nil, "omarchy-system-lid-close", { locked = true }) o.bind("switch:off:Lid Switch", nil, "omarchy-hyprland-monitor-clamshell", { locked = true }) +-- Apple Silicon reports its lid through the SMC, under its own switch name. +o.bind("switch:on:Apple SMC power/lid events", nil, "omarchy-system-lid-close", { locked = true }) +o.bind("switch:off:Apple SMC power/lid events", nil, "omarchy-hyprland-monitor-clamshell", { locked = true }) o.bind("PRINT", "Screenshot", "omarchy-capture-screenshot") o.bind("ALT + PRINT", "Screenrecording", "omarchy-capture-screenrecording --stop-recording || omarchy-menu toggle trigger.capture.screenrecord") diff --git a/default/hypr/monitor-removal.lua b/default/hypr/monitor-removal.lua new file mode 100644 index 000000000000..deb94a05a7aa --- /dev/null +++ b/default/hypr/monitor-removal.lua @@ -0,0 +1,88 @@ +-- Unplugging a monitor, or the lid disabling the laptop panel, keeps you on the +-- workspace you were on. Hyprland first warps focus to whichever monitor it +-- lists first and only then moves the removed monitor's workspaces over, so the +-- one you were on lands hidden behind that monitor's own. Measured on Hyprland +-- 0.56.2 (CMonitor::onDisconnect). +-- +-- monitor.focused fires before Hyprland records the new focus, so the monitor +-- still reported as active is the one being left. The workspace it showed is +-- kept until the event loop next turns, which the removal that caused the warp +-- finishes well before: a monitor.removed inside that window brings it back. + +local left_behind = nil +local generation = 0 +local moves = 0 +local restoring = false + +local function selector(ws) + if not ws or type(ws.id) ~= "number" then + return nil + end + + -- Named workspaces have negative ids, which a selector reads as relative. + if ws.id > 0 then + return tostring(ws.id) + end + return "name:" .. ws.name +end + +local function later(fn) + hl.timer(fn, { timeout = 1, type = "oneshot" }) +end + +hl.on("monitor.focused", function(monitor) + if restoring then + return + end + + generation = generation + 1 + moves = moves + 1 + left_behind = nil + + local leaving = hl.get_active_monitor() + if not leaving or not monitor or leaving.name == monitor.name then + return + end + + -- An open scratchpad already comes along to the front of the next monitor. + if hl.get_active_special_workspace(leaving) then + return + end + + left_behind = selector(hl.get_active_workspace(leaving)) + local seen = generation + later(function() + if generation == seen then + left_behind = nil + end + end) +end) + +-- Only counts towards giving way to the user once the removal is over: the +-- workspaces it moves between monitors fire this too, before monitor.removed. +hl.on("workspace.active", function() + if not restoring then + moves = moves + 1 + end +end) + +hl.on("monitor.removed", function() + local workspace = left_behind + left_behind = nil + if not workspace then + return + end + + -- Let the removal finish first, and give way to any focus or workspace change + -- since. + local seen = moves + later(function() + if moves ~= seen or selector(hl.get_active_workspace()) == workspace or not hl.get_workspace(workspace) then + return + end + + restoring = true + pcall(hl.dispatch, hl.dsp.focus({ workspace = workspace })) + restoring = false + end) +end) diff --git a/default/hypr/omarchy.lua b/default/hypr/omarchy.lua index abc7c4e0888d..6586024d8a62 100644 --- a/default/hypr/omarchy.lua +++ b/default/hypr/omarchy.lua @@ -16,6 +16,7 @@ end require("default.hypr.envs") require("default.hypr.looknfeel") require("default.hypr.qconsole") +require("default.hypr.monitor-removal") require("default.hypr.input") require("default.hypr.windows") diff --git a/default/omarchy/omarchy-menu.jsonc b/default/omarchy/omarchy-menu.jsonc index 450bc5ac1dc8..271b953bf1a2 100644 --- a/default/omarchy/omarchy-menu.jsonc +++ b/default/omarchy/omarchy-menu.jsonc @@ -57,7 +57,7 @@ "trigger.reminder": {"icon":"󰢌","label":"Reminder","aliases":["reminder"]}, "trigger.capture": {"icon":"","label":"Capture","aliases":["capture","screenshot","screenrecord","screen-record","screenrecording"]}, "trigger.capture.screenshot": {"icon":"","label":"Screenshot","action":"omarchy-capture-screenshot"}, - "trigger.capture.screenrecord.stop": {"icon":"","label":"Stop Screenrecording","when":"pgrep -f '^gpu-screen-recorder'","action":"omarchy-capture-screenrecording --stop-recording"}, + "trigger.capture.screenrecord.stop": {"icon":"","label":"Stop Screenrecording","when":"omarchy-capture-screenrecording --status","action":"omarchy-capture-screenrecording --stop-recording"}, "trigger.capture.screenrecord": {"icon":"","label":"Screenrecord"}, "trigger.capture.text": {"icon":"󰴑","label":"Text","action":"omarchy-capture-text"}, "trigger.capture.qr": {"icon":"󰐲","label":"QR Code","action":"omarchy-capture-qr"}, @@ -188,7 +188,7 @@ "setup.config.hyprland": {"icon":"","label":"Hyprland","action":"omarchy-launch-config-editor \"$HOME/.config/hypr/hyprland.lua\""}, "setup.config.hyprsunset": {"icon":"","label":"Hyprsunset","action":"omarchy-launch-config-editor ~/.config/hypr/hyprsunset.conf && omarchy-restart-hyprsunset"}, "setup.config.xcompose": {"icon":"󰞅","label":"XCompose","action":"omarchy-launch-config-editor ~/.XCompose && omarchy-restart-xcompose"}, - "setup.direct-boot": {"icon":"","label":"Direct Boot","action":"omarchy-launch-floating-terminal-with-presentation omarchy-setup-direct-boot"}, + "setup.direct-boot": {"icon":"","label":"Direct Boot","when":"! omarchy-hw-apple-silicon","action":"omarchy-launch-floating-terminal-with-presentation omarchy-setup-direct-boot"}, "setup.reset": {"icon":"󰑓","label":"Reset Computer","when":"[[ $(findmnt -no FSTYPE /) == btrfs ]]","action":"omarchy-launch-floating-terminal-with-presentation omarchy-system-factory-reset"}, // Install @@ -212,18 +212,18 @@ "install.webapp": {"icon":"","label":"Web App","action":"omarchy-launch-floating-terminal-with-presentation omarchy-webapp-install"}, "install.terminal": {"icon":"","label":"Terminal"}, "install.tui": {"icon":"","label":"TUI","action":"omarchy-launch-floating-terminal-with-presentation omarchy-tui-install"}, - "install.windows": {"icon":"󰍲","label":"Windows","disabled":"[[ -f $HOME/.local/share/applications/windows-vm.desktop ]]","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-windows-vm install'"}, + "install.windows": {"icon":"󰍲","label":"Windows","when":"[[ $(uname -m) == \"x86_64\" ]]","disabled":"[[ -f $HOME/.local/share/applications/windows-vm.desktop ]]","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-windows-vm install'"}, "install.aur": {"icon":"󰣇","label":"AUR","action":"xdg-terminal-exec --app-id=org.omarchy.terminal omarchy-pkg-aur-install"}, "install.preinstalls": {"icon":"󰏓","label":"Preinstalls","disabled":"[[ ! -f $HOME/.local/state/omarchy/preinstalls-removed ]]","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-preinstalls"}, "install.browser.chrome": {"icon":"","label":"Chrome","disabled":"omarchy-pkg-present google-chrome","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser chrome'"}, - "install.browser.edge": {"icon":"󰇩","label":"Edge","disabled":"omarchy-pkg-present microsoft-edge-stable-bin","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser edge'"}, + "install.browser.edge": {"icon":"󰇩","label":"Edge","when":"[[ $(uname -m) == \"x86_64\" ]]","disabled":"omarchy-pkg-present microsoft-edge-stable-bin","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser edge'"}, "install.browser.brave": {"icon":"","label":"Brave","disabled":"omarchy-pkg-present brave-bin","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser brave'"}, "install.browser.brave-origin": {"icon":"","label":"Brave Origin","disabled":"omarchy-pkg-present brave-origin-bin","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser brave-origin'"}, "install.browser.firefox": {"icon":"","label":"Firefox","disabled":"omarchy-pkg-present firefox","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser firefox'"}, "install.browser.zen": {"icon":"󰖟","label":"Zen","disabled":"omarchy-pkg-present zen-browser-bin","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-browser zen'"}, "install.service.1password": {"icon":"󰢁","label":"1Password","disabled":"omarchy-pkg-present 1password","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-1password"}, - "install.service.dropbox": {"icon":"","label":"Dropbox","disabled":"omarchy-pkg-present dropbox","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-dropbox"}, - "install.service.spotify": {"icon":"󰓇","label":"Spotify","disabled":"omarchy-pkg-present spotify","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-spotify"}, + "install.service.dropbox": {"icon":"","label":"Dropbox","when":"[[ $(uname -m) == \"x86_64\" ]]","disabled":"omarchy-pkg-present dropbox","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-dropbox"}, + "install.service.spotify": {"icon":"󰓇","label":"Spotify","when":"[[ $(uname -m) == \"x86_64\" ]]","disabled":"omarchy-pkg-present spotify","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-spotify"}, "install.service.signal": {"icon":"󰭹","label":"Signal","disabled":"omarchy-pkg-present signal-desktop","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-signal"}, "install.service.tailscale": {"icon":"","label":"Tailscale","disabled":"omarchy-pkg-present tailscale","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-tailscale"}, "install.service.nordvpn": {"icon":"󱇱","label":"NordVPN","disabled":"omarchy-pkg-present nordvpn-bin","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-service-nordvpn"}, @@ -253,13 +253,13 @@ "install.ai.t3-code": {"icon":"","iconFont":"omarchy","label":"T3 Code","disabled":"omarchy-pkg-present t3code-bin","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-ai-t3-code"}, "install.gaming.steam": {"icon":"","label":"Steam","disabled":"omarchy-pkg-present steam","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-steam"}, "install.gaming.retroarch": {"icon":"󰯉","label":"RetroArch","disabled":"omarchy-pkg-present retroarch","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-retroarch"}, - "install.gaming.minecraft": {"icon":"󰍳","label":"Minecraft","disabled":"omarchy-pkg-present minecraft-launcher","action":"omarchy-install-and-launch Minecraft minecraft-launcher minecraft-launcher"}, + "install.gaming.minecraft": {"icon":"󰍳","label":"Minecraft","when":"[[ $(uname -m) == \"x86_64\" ]]","disabled":"omarchy-pkg-present minecraft-launcher","action":"omarchy-install-and-launch Minecraft minecraft-launcher minecraft-launcher"}, "install.gaming.geforce-now": {"icon":"󰢹","label":"NVIDIA GeForce NOW","disabled":"flatpak info com.nvidia.geforcenow","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-geforce-now"}, "install.gaming.xbox-cloud": {"icon":"","label":"Xbox Cloud Gaming","disabled":"[[ -f \"$HOME/.local/share/applications/Xbox Cloud Gaming.desktop\" ]]","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-xbox-cloud"}, "install.gaming.xbox-controllers": {"icon":"󰂯","label":"Xbox Controllers","disabled":"omarchy-pkg-present xpadneo-dkms","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-xbox-controllers"}, "install.gaming.battlenet": {"icon":"","label":"Battle.net","disabled":"[[ -d $HOME/Games/battlenet ]]","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-battlenet"}, "install.gaming.lutris": {"icon":"","label":"Lutris","disabled":"omarchy-pkg-present lutris","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-lutris"}, - "install.gaming.heroic": {"icon":"󱓟","label":"Heroic (Epic Games)","disabled":"omarchy-pkg-present heroic-games-launcher-bin","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-heroic"}, + "install.gaming.heroic": {"icon":"󱓟","label":"Heroic (Epic Games)","when":"[[ $(uname -m) == \"x86_64\" ]]","disabled":"omarchy-pkg-present heroic-games-launcher-bin","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-gaming-heroic"}, "install.gaming.retro-launcher": {"icon":"󰯉","label":"RetroArch Game Launcher","action":"omarchy-games-retro-install"}, "install.development.rails": {"icon":"󰫏","label":"Ruby on Rails","disabled":"[[ -d $HOME/.local/share/mise/installs/ruby ]]","action":"omarchy-launch-floating-terminal-with-presentation 'omarchy-install-dev-env ruby'"}, "install.development.docker-dbs": {"icon":"","label":"Docker DB","action":"omarchy-launch-floating-terminal-with-presentation omarchy-install-docker-dbs"}, diff --git a/default/pacman/aarch64/mirrorlist-edge b/default/pacman/aarch64/mirrorlist-edge new file mode 100644 index 000000000000..c7185ed8f652 --- /dev/null +++ b/default/pacman/aarch64/mirrorlist-edge @@ -0,0 +1,2 @@ +# Arch Linux ARM publishes this endpoint with a $arch/$repo layout. +Server = http://mirror.archlinuxarm.org/$arch/$repo diff --git a/default/pacman/aarch64/pacman-edge.conf b/default/pacman/aarch64/pacman-edge.conf new file mode 100644 index 000000000000..9399a5c63a11 --- /dev/null +++ b/default/pacman/aarch64/pacman-edge.conf @@ -0,0 +1,35 @@ +# See the pacman.conf(5) manpage for option and repository directives +# Arch Linux ARM provides the aarch64 repositories used below. +# aarch64 has an edge channel alone: Omarchy publishes aarch64 packages on edge +# so far, and stable and rc templates come once a release supports aarch64. + +[options] +Color +ILoveCandy +VerbosePkgLists +HoldPkg = pacman glibc +Architecture = auto +CheckSpace +ParallelDownloads = 5 +DownloadUser = alpm + +# By default, pacman accepts packages signed by keys that its local keyring +# trusts (see pacman-key and its man page), as well as unsigned packages. +SigLevel = Required DatabaseOptional +LocalFileSigLevel = Optional + +# pacman searches repositories in the order defined here. +[core] +Include = /etc/pacman.d/mirrorlist + +[extra] +Include = /etc/pacman.d/mirrorlist + +[alarm] +Include = /etc/pacman.d/mirrorlist + +[aur] +Include = /etc/pacman.d/mirrorlist + +[omarchy] +Server = https://pkgs.omarchy.org/edge/$arch diff --git a/default/pacman/apple-silicon/mirrorlist-edge b/default/pacman/apple-silicon/mirrorlist-edge new file mode 100644 index 000000000000..c7185ed8f652 --- /dev/null +++ b/default/pacman/apple-silicon/mirrorlist-edge @@ -0,0 +1,2 @@ +# Arch Linux ARM publishes this endpoint with a $arch/$repo layout. +Server = http://mirror.archlinuxarm.org/$arch/$repo diff --git a/default/pacman/apple-silicon/pacman-edge.conf b/default/pacman/apple-silicon/pacman-edge.conf new file mode 100644 index 000000000000..2355cd68d052 --- /dev/null +++ b/default/pacman/apple-silicon/pacman-edge.conf @@ -0,0 +1,39 @@ +# See the pacman.conf(5) manpage for option and repository directives +# aarch64 has an edge channel alone: Omarchy publishes aarch64 packages on edge +# so far, and stable and rc templates come once a release supports aarch64. + +[options] +Color +ILoveCandy +VerbosePkgLists +HoldPkg = pacman glibc +Architecture = auto +CheckSpace +ParallelDownloads = 5 +DownloadUser = alpm + +# By default, pacman accepts packages signed by keys that its local keyring +# trusts (see pacman-key and its man page), as well as unsigned packages. +SigLevel = Required DatabaseOptional +LocalFileSigLevel = Optional + +# pacman searches repositories in the order defined here. Omarchy comes first on +# Apple Silicon, so its builds for the Mac (the Aurora kernel, m1n1, U-Boot and +# the Hyprland stack) win over the same names in Asahi ALARM and Arch Linux ARM. +[omarchy] +Server = https://pkgs.omarchy.org/edge/$arch + +[asahi-alarm] +Server = https://github.com/asahi-alarm/asahi-alarm/releases/download/aarch64 + +[core] +Include = /etc/pacman.d/mirrorlist + +[extra] +Include = /etc/pacman.d/mirrorlist + +[alarm] +Include = /etc/pacman.d/mirrorlist + +[aur] +Include = /etc/pacman.d/mirrorlist diff --git a/default/settings-runtime-profile b/default/settings-runtime-profile new file mode 100644 index 000000000000..4e3fbe0195b1 --- /dev/null +++ b/default/settings-runtime-profile @@ -0,0 +1,24 @@ +# omarchy-settings ships the same files on every architecture when its source +# has this file: the recipe stops leaving drop-ins out of the aarch64 package. +# Each platform-specific file below, by its path in this source, takes its +# decision at runtime instead. +# +# etc/mkinitcpio.conf.d/00-omarchy-hooks.conf +# Sets each platform's HOOKS baseline (omarchy-hw-platform), and leaves alone +# a Mac whose own mkinitcpio.conf carries asahi or busybox encrypt. +# etc/mkinitcpio.conf.d/omarchy_hooks.conf +# Only filters kms for NVIDIA and handles the vconsole hooks, from the +# machine's own devices and configuration. +# etc/mkinitcpio.conf.d/thunderbolt_module.conf +# Adds thunderbolt to MODULES only where modinfo finds it as a loadable module. +# etc/limine-entry-tool.d/, default/limine/ +# Read only where Limine and its entry tool are installed. +# etc/systemd/oomd.conf.d/, default/systemd/user/app.slice.d/10-oomd.conf +# Inert unless systemd-oomd runs (install/config/enable-services.sh, +# migration 1785424256). +# default/systemd/zram-generator.conf.d/90-omarchy.conf, +# etc/sysctl.d/99-omarchy-sysctl.conf, etc/tmpfiles.d/omarchy-zswap.conf +# Swap on zram everywhere: the installer provides zram-generator on x86_64, +# install/omarchy-aarch64.packages on aarch64. +# etc/modprobe.d/omarchy-usb-autosuspend.conf +# Applies where usbcore is a module; a kernel that builds it in ignores it. diff --git a/default/systemd/user/omarchy-brightness-keyboard-auto.service b/default/systemd/user/omarchy-brightness-keyboard-auto.service new file mode 100644 index 000000000000..f43200f0a57e --- /dev/null +++ b/default/systemd/user/omarchy-brightness-keyboard-auto.service @@ -0,0 +1,15 @@ +[Unit] +Description=Omarchy keyboard backlight from ambient light +After=graphical-session.target +PartOf=graphical-session.target +ConditionEnvironment=WAYLAND_DISPLAY + +[Service] +Type=simple +ExecCondition=/usr/bin/omarchy-brightness-keyboard-auto --available +ExecStart=/usr/bin/omarchy-brightness-keyboard-auto +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=graphical-session.target diff --git a/docs/file-layout.md b/docs/file-layout.md index 73c48c4f340a..5fa07f5c8256 100644 --- a/docs/file-layout.md +++ b/docs/file-layout.md @@ -54,6 +54,12 @@ behind, which arms `omarchy-provision-owner.service` (shipped from `setup-form.sh`). On first boot `bin/omarchy-provision-owner` creates the user on tty1 and runs the finalize step itself. +On an encrypted install `omarchy-provision-owner` then re-keys LUKS from the staged install key to the owner's password through `install/provisioning/luks-rekey.sh`, which journals each step (phase and slot numbers, never keys) in `/var/lib/omarchy/provisioning/luks-rekey.state` so an interrupted first boot resumes. Setup finishes only once the staged key opens nothing, and removes the journal with `pending`, so a retry after the staged key is retired must use the password the disk holds. On a platform whose boot package owns the boot chain, the check before the owner form, the boot-time unlock and the record of the kept key slot go through `omarchy-lifecycle-dispatch`. See [lifecycle-dispatch.md](lifecycle-dispatch.md). + +`omarchy-drive-password` reuses those journal helpers when the owner changes the password of the disk holding `/`: it changes the LUKS key, confirms the new key opens the disk and the old one no longer does, and only then sets the login and root passwords. `~/.local/state/omarchy/drive-password.state` records the disk's UUID, the key slots and the phase (never a password) until the accounts match, and the next run finishes an interrupted change with whichever of the old or new password the disk opens with. + +Prebuilt images are set up away from the machine they will run on, so their hardware setup waits for that machine. The builder writes a root-owned manifest, `/var/lib/omarchy/image/target` (`format=1`, `platform=`), before `omarchy-apply-system`. While it exists, `omarchy-apply-hardware` runs no hardware leaf: it queues each one in `/var/lib/omarchy/image/deferred-steps` and arms `omarchy-provision-hardware.service` (shipped from `install/provisioning/`). On the machine's first boot, before owner setup and the login screen, `bin/omarchy-provision-hardware` renames the manifest to `target.booted` and runs the queue in order, dropping each step that succeeds. Before the first step it makes the machine's own pacman keyring when the build asked for one (`/var/lib/omarchy/image/pacman-keyring`, which install finalization writes on Qualcomm and generic aarch64 instead of running `pacman-key --init`, so no two machines share a master key). A failed step stays queued for the next boot. It refreshes no package databases: the image carries its sync databases and every package its queued steps install, so the queue runs offline (the image build checks that it carries them; a step whose package is missing stays queued). Once the queue is empty it rebuilds the initramfs if a step changed its inputs (the mkinitcpio configuration, or the kernel command line in `/etc/default/limine` and `/etc/limine-entry-tool.d/`) or asked for the rebuild (`/var/lib/omarchy/image/boot-rebuild`), and disables the service; the unit file stays, so a start job already queued is skipped on the emptied queue rather than failed. A platform's own first boot may run `omarchy-provision-hardware` itself: runs are serialised, and it exits 75 when a step or the keyring failed and stays queued, and 1 when it refused to run (an untrusted manifest or queue) or the final rebuild failed, which the next run retries. Steps know they run on an image's first boot from `OMARCHY_IMAGE_DEFERRED_HARDWARE=1` (Bluetooth starts its service there, since boot already brought up its target). `install/helpers/image-target.sh` holds the contract. Root always uses these fixed paths; no environment variable turns a live system into an image build. + Current generated theme state lives under `~/.local/state/omarchy/current/`. Keep `~/.config/omarchy/` for files a user may intentionally version in a dotfile manager, such as user themes, hooks, @@ -94,7 +100,7 @@ default/applications/battlenet.desktop applications/icons/* ──► omarchy-settings /usr/share/icons/hicolor/{48,256,scalable}/apps/ etc/** ──► omarchy-settings /etc/** (drop-ins we own outright) - ├─ mkinitcpio.conf.d/{omarchy_hooks,thunderbolt_module}.conf + ├─ mkinitcpio.conf.d/{00-omarchy-hooks,omarchy_hooks,thunderbolt_module}.conf ├─ limine-entry-tool.d/{omarchy-defaults,omarchy-uki}.conf ├─ NetworkManager/, sudoers.d/, sysctl.d/, tmpfiles.d/, │ profile.d/omarchy.sh, … (a summary — `ls etc/` for the full ~17-entry tree) @@ -140,6 +146,55 @@ logo.{txt,svg}, icon.{txt,png} ──► omarchy-settings /usr/share/omarchy The hardware-conditional `force-igpu` and `keyboard-backlight` sources also live under `default/systemd/system-sleep/`, but their setup commands publish root-owned copies only on machines that need them; they are not installed by `omarchy-settings`. +### Platform root + +A platform's runtime package (omarchy-mac on Apple Silicon, say) adds its desktop files under a fixed root that only the one installed platform package owns. Omarchy ships nothing there, and a missing root or file means no platform additions. No environment variable moves it; tests reach fixtures through test-only seams, never through a desktop session's environment. + +``` +/usr/share/omarchy-platform/ + displays.conf backlight and DDC hints for the display commands (below) + key-names " " lines the keybindings menu shows in place of keysyms + display-cutouts.json camera cutouts the top bar keeps out of (see omarchy-shell.md) + keyrings signing keyring packages of the repositories the platform adds, one per line + audio.json audio processing nodes the audio panel and microphone widget leave out (below) + fingerprint-readers readers the kernel drives without USB, and the file that shows each one is usable (below) +``` + +See [lifecycle-dispatch.md](lifecycle-dispatch.md#platform-files). + +`omarchy-update-keyring` reinstalls each installed package `keyrings` names (`#` comments and blank lines skipped, and a line that isn't a `*-keyring` package name skipped with a warning) with Arch's, before the system upgrade, so a key rotation in the platform's own repository never fails that upgrade's signature checks. On Apple Silicon, omarchy-mac names `asahi-alarm-keyring` there. + +#### Display hints (`displays.conf`) + +`omarchy-hw-display` picks the built-in panel's backlight by name (`gmux_backlight`, then `amdgpu_bl*`, `intel_backlight`, `acpi_video*`, else the first one listed, never a T2 Touch Bar's `appletb_backlight`), and `omarchy-brightness-display` probes every external monitor over DDC. Where a platform's names or display driver differ, its platform package says so in `/usr/share/omarchy-platform/displays.conf`; Omarchy ships none, and without it both behave as above. One directive per line, whitespace-separated; blank lines, whole-line `#` comments, unknown directives, and lines with a wrong word count or a name that is `.`, `..` or has a `/` are ignored: + +- `backlight-skip `: a `/sys/class/backlight` name (a shell glob) that never drives the built-in panel, such as a Touch Bar's. It is never picked, not even when a later directive or the built-in order names it. +- `backlight-prefer `: the built-in panel's backlight, a literal name, tried after `gmux_backlight` and before the GPU and ACPI ones, in file order. +- `ddc-require-connector-ddc`: the display driver registers no DDC channel, so a probe would only walk unrelated I2C buses. An external monitor (other than an Apple Studio or XDR Display, which `omarchy-brightness-display-apple` drives with asdcontrol) is probed only when its DRM connector has a `ddc` node (`/sys/class/drm/card*-/ddc`); otherwise it gets no DDC or backlight control, and the built-in panel is never dimmed in its place. + +No environment variable moves the file: these commands also run under `sudo` and from the brightness keys. Their tests (`test/shell.d/hw-display-test.sh`, `test/shell.d/brightness-display-test.sh`) run a copy rewritten to read a fixture in its place. + +#### Fingerprint readers (`fingerprint-readers`) + +`omarchy-hw-fingerprint` finds USB readers by their product string or vendor. A reader the kernel drives some other way (a Mac's Touch ID sensor, behind its Secure Enclave) is named by the platform package in `/usr/share/omarchy-platform/fingerprint-readers`; Omarchy ships none. Each line is an absolute path, a shell glob, then the value that file reads once the reader is usable; the reader is present when any file the glob matches reads that value. Three details decide a match: the file's trailing newlines are dropped before comparing (sysfs ends every value with one), fields are split on whitespace (so neither the glob nor the value can contain a space), and the file is read as the user running the check, the session user, so it must be readable by that user and not only by root. Blank lines, whole-line `#` comments, relative paths and lines with a missing or extra word are ignored. A platform file never hides a USB reader: when no platform-named file matches, the USB scan still runs. On Apple Silicon, omarchy-mac names `/sys/bus/platform/drivers/apple_sep/*/diag/touchid ready`. + +The rest of the fingerprint path is the same for every reader: the setup's first-run invitation follows `omarchy-hw-fingerprint`, so it reaches a machine once its platform's reader is usable, and `omarchy-setup-security-fingerprint` installs `libfprint-git`. Setup can only enroll a platform's reader once that package carries its driver; for a Mac's Touch ID that is the aarch64 build from omacom/omarchy-pkgs#806. `test/shell.d/hw-fingerprint-test.sh` runs a copy rewritten to read a fixture in place of the file. + +#### Audio hints (`audio.json`) + +The audio panel lists every output, input and playback stream PipeWire has, and the microphone widget counts every recording as the microphone in use (the shell's own level meters never count). A platform whose audio runs through its own processing (DSP filter graphs in front of raw devices, say) has nodes that are neither devices nor apps, and its platform package names them in `/usr/share/omarchy-platform/audio.json`; Omarchy ships none, and without it nothing is left out. + +```json +{ + "hidden": ["", "..."], + "replaced": [{ "node": "", "by": "" }] +} +``` + +A pattern is a JavaScript regular expression matched against a whole `node.name`. A `hidden` node is never listed as an output, an input or an app stream, unless it is the current default output or input (so the panel always shows what's in use), and never lights the microphone widget; a `replaced` node is left out only while a node matching `by` exists (a mono processed microphone behind a stereo copy of it, say). Missing or malformed JSON means no hints, and an invalid pattern or entry is skipped. The shell reads the file at that fixed path, which no environment variable moves (`shell/Commons/AudioNodes.qml`), and `test/shell.d/audio-test.sh` covers the parsing. + +A virtual source (an `Audio/Source/Virtual`, such as EasyEffects' or a platform's microphone mapping) needs no hint: Quickshell leaves it untyped, so the panel and widget set its volume and mute through `wpctl` (`shell/Commons/UntypedInput.qml`, one `pactl subscribe` for the whole shell) and the panel meters it with `omarchy-audio-source-level`, and the panel lists it because PulseAudio does (`omarchy-audio-sink-availability sources`). + ### Why `etc-overrides/` exists Some files under `/etc/` (`.bashrc` in `/etc/skel`, `nsswitch.conf`, @@ -280,6 +335,9 @@ and/or a working user systemd instance: Done here, not at finalize, because the user manager isn't reachable from the ISO chroot; `ConditionPath*` in the unit files keeps services inert when they don't apply. +- `omarchy-lifecycle-dispatch setup-user` — the platform's own user setup, + now with the session up (a no-op where the platform registers none; see + [lifecycle-dispatch.md](lifecycle-dispatch.md)). - `install/user/first-run/gnome-theme.sh`, `install/user/first-run/gtk-primary-paste.sh` — GNOME/GTK settings that need the dconf daemon. @@ -320,10 +378,26 @@ finalization. It sources: Logging goes to `/var/log/omarchy-install.log` via `install/helpers/logging.sh`. +Platform-specific setup asks `omarchy-hw-platform`, which prints `apple-silicon`, `qualcomm`, `generic-aarch64` or `generic`. It reads the vendor prefix of each token in the device tree's root `compatible` (`apple,` or `qcom,`, from `/proc/device-tree` or `/sys/firmware/devicetree/base`) and the CPU architecture, and fails when they contradict each other. + +An image built away from the machine it will run on names its target in a root-owned manifest, `/var/lib/omarchy/image/target` (`format=1`, `platform=`, unknown keys ignored). While the root is being built rather than booted, the detector answers from the manifest and never reads the build host's device tree. The root counts as built when it shows it: no `/run/systemd/system`, PID 1's root is another one (a chroot), or PID 1 is not systemd (a PID namespace). A booted system always answers from its hardware, even with a manifest left behind, so no unit that asks the detector may use `PrivatePIDs=`. As root the detector restarts in an empty environment and ignores the fixture variables its tests use. + The package lists the ISO pacstraps live at `install/omarchy-base.packages` and `install/omarchy-other.packages`; the ISO builder also reads them when constructing its offline mirror. +A platform's default package set is the base list, then its architecture's additions, then its own: `install/omarchy-aarch64.packages` on every aarch64 platform, then `install/omarchy-.packages` when that platform has one (`install/omarchy-apple-silicon.packages` on Apple Silicon, `install/omarchy-qualcomm.packages` on Qualcomm). x86_64 installs the base list alone. `omarchy-pkg-defaults [platform]` prints the composed set, for the running machine by default (via `omarchy-hw-platform`, so an image build gets its target's set), and `omarchy-reinstall-pkgs` installs it. + +## Platform display hints (`/usr/share/omarchy-platform/displays.conf`) + +`omarchy-hw-display` picks the built-in panel's backlight by name (`gmux_backlight`, then `amdgpu_bl*`, `intel_backlight`, `acpi_video*`, else the first one listed, never a T2 Touch Bar's `appletb_backlight`), and `omarchy-brightness-display` probes every external monitor over DDC. Where a platform's names or display driver differ, its platform package says so in `/usr/share/omarchy-platform/displays.conf`; Omarchy ships none, and without it both behave as above. One directive per line, whitespace-separated; blank lines, whole-line `#` comments, unknown directives, and lines with a wrong word count or a name that is `.`, `..` or has a `/` are ignored: + +- `backlight-skip `: a `/sys/class/backlight` name (a shell glob) that never drives the built-in panel, such as a Touch Bar's. It is never picked, not even when a later directive or the built-in order names it. +- `backlight-prefer `: the built-in panel's backlight, a literal name, tried after `gmux_backlight` and before the GPU and ACPI ones, in file order. +- `ddc-require-connector-ddc`: the display driver registers no DDC channel, so a probe would only walk unrelated I2C buses. An external monitor (other than an Apple Studio or XDR Display, which `omarchy-brightness-display-apple` drives with asdcontrol) is probed only when its DRM connector has a `ddc` node (`/sys/class/drm/card*-/ddc`); otherwise it gets no DDC or backlight control, and the built-in panel is never dimmed in its place. + +No environment variable moves the file: these commands also run under `sudo` and from the brightness keys. Their tests (`test/shell.d/hw-display-test.sh`, `test/shell.d/brightness-display-test.sh`) run a copy rewritten to read a fixture in its place. + ## Explicit resync (`omarchy-reinstall-configs`) When an existing user wants to reset to shipped defaults: diff --git a/docs/lifecycle-dispatch.md b/docs/lifecycle-dispatch.md new file mode 100644 index 000000000000..af211d434993 --- /dev/null +++ b/docs/lifecycle-dispatch.md @@ -0,0 +1,169 @@ +# Lifecycle dispatch + +Omarchy owns the boot lifecycle flows: the owner wizard, account creation, LUKS discovery, retry journals, snapshots, the migration runner and the update flow. Some platforms boot through a chain those flows can't drive generically. Apple Silicon Macs boot m1n1 → U-Boot → Limine, keep the install key on an ext4 boot partition and name it on the kernel command line. For those platforms, the flows call a small fixed set of operations through `bin/omarchy-lifecycle-dispatch`, and a platform boot package implements them as root-owned entrypoints. System and user setup, and the installers of apps a platform needs to adjust, call a few more, which a platform's runtime package implements, so its own setup runs from Omarchy's without an inline platform branch. Every other platform keeps the generic path, and each dispatch call is a no-op there. + +## The command + +``` +omarchy-lifecycle-dispatch [arguments...] +omarchy-lifecycle-dispatch --resolve +``` + +The first form runs the operation. `--resolve` prints the entrypoint the operation would run, or nothing when the operation is a no-op on this machine. A caller uses it when a platform implementation replaces a generic step (see [Callers](#callers)). + +| Situation | Run | `--resolve` | +| --- | --- | --- | +| The platform registers no package (`generic`, `generic-aarch64`, and `qualcomm` today) | no-op, exit 0 | prints nothing, exit 0 | +| The entrypoint exists and passes the trust rules | execs it; its exit status is the result | prints its path | +| A required operation has no entrypoint, and the package is not installed | exit 3 (an entrypoint's own status could also be 3; with `--resolve` it is only this): `Error: on needs , which provides ; it is not installed` | same error | +| A required operation has no entrypoint, but the package is installed (its pacman record says so) | exit 1: `Error: on needs , which does not provide; update ` | same error | +| An optional operation has no entrypoint (every setup and app-install operation is optional, so none of them ever fails for a missing package) | no-op, exit 0 | prints nothing, exit 0 | +| A user operation (`setup-user`, `post-install`, `pre-remove`) is run as root on a platform that registers it (Apple Silicon), its entrypoint installed or not | exit 1: `Error: runs as the user, never as root` | same error | +| The entrypoint fails the trust rules | exit 1: `Error: refusing : ...` (optional operations too) | same error | +| `omarchy-hw-platform` can't settle the platform | exit 1 | exit 1 | +| No operation, or one outside the fixed set | exit 2 with usage | exit 2 | + +## Operations + +The set is fixed in the dispatcher; adding one is a change to Omarchy. Every operation runs as root except `setup-user`, `post-install` and `pre-remove`, which run as the user. The `provision-*` operations take no arguments and work on fixed paths: `/var/lib/omarchy/provisioning` holds the staged install key (`luks-key`) and the re-key journal (`luks-rekey.state`). `luks-slots` takes the slot numbers it records. Factory reset names the factory root it is about to activate, since that is not `/` yet, and hands `reset-commit` the throwaway key on standard input, never in arguments. + +| Operation | Called | Contract | Apple | Caller | +| --- | --- | --- | --- | --- | +| `provision-prepare` | Owner provisioning, at the start of every setup attempt, before the owner is asked anything | Succeeds when the platform can finish setup on this machine. On failure its stderr is shown on tty1 and logged, and the attempt fails into the usual retry screen. It must leave nothing a retry can't repeat. | required | `omarchy-provision-owner` | +| `provision-commit` | Owner provisioning, during the LUKS re-key: after the owner's key is added, before any other slot is retired | Removes every boot-time copy of the staged key and its unlock configuration from the platform's boot chain, and rebuilds the boot files so the next boot asks for the password. Idempotent. If it fails, it leaves or restores a boot chain that still unlocks unattended with the staged key, so the retry boots. | required | `omarchy-provision-owner` | +| `provision-verify` | Owner provisioning, whenever it asks whether the staged unlock remains: before the re-key, during it, and before setup drops `pending` | Read-only. Exits 0 only when the boot chain holds no staged key or unlock configuration. Any other status counts as "remains", so setup never finishes. | required | `omarchy-provision-owner` | +| `reset-prepare []` | Factory reset, once the factory root is cloned and scrubbed, before switching to it. The device is there only when the root is encrypted. | Stages the platform's boot state for the factory root: the unlock's command line, the rebuilt boot files, and encryption state reopened for the next owner. It keeps what `reset-rollback` restores, and writes no key. | required | `omarchy-system-factory-reset` | +| `reset-verify ` | Factory reset, right after `reset-prepare` and before the throwaway slot is added and the switch committed | Read-only. Proves the rebuilt boot files boot the factory root on this boot chain. A failure rolls the reset back. | required | `omarchy-system-factory-reset` | +| `reset-commit` (key on standard input) | Factory reset, once the factory root is the active root | Puts the throwaway key where the staged unlock reads it, so the first boot unlocks unattended until owner setup re-keys, and drops what `reset-rollback` would restore. A failure is logged, not fatal: that boot asks for the current disk password once. | required | `omarchy-system-factory-reset` | +| `reset-rollback` | Factory reset, when anything fails after `reset-prepare` started and before the factory root is active, `reset-prepare`'s own failure included | Restores the previous boot state. Nothing to restore is a success. | required | `omarchy-system-factory-reset` | +| `update-verify` | Update, after the last package step: the transaction, migrations, orphan removal and AUR packages | Read-only. Verifies the boot chain boots the updated system, whose new kernel may still wait for its reboot. A failure leaves the update unfinished: it exits non-zero and offers no reboot. | required | `omarchy-update-boot` (`omarchy update`) | +| `update-takeover ...` | Update, when an Omarchy package's upgrade stopped only on files no package owns, before they are moved aside for the retry | Read-only. Gets those paths. Exit 0 lets the takeover go ahead; any other status keeps every file where it is, and the update fails as it would have without the retry. A platform whose boot chain or image writes files it can't do without refuses those. | required | `omarchy-update-system-pkgs-when-conflicted` (`omarchy update`) | +| `luks-slots` | Owner provisioning, once the re-key keeps only the owner's slot, before it destroys the staged key; the disk password change, with `--owner` before anything changes on the system disk, then once the owner's new key is confirmed | `luks-slots owner= [recovery=]` records the root volume's kept slots wherever the platform's boot checks look for them. Owner setup passes an empty `recovery=`, which records none. Omarchy creates no recovery key; `recovery=` exists only for a slot an earlier Mac setup recorded, which stays when the argument is left out. `luks-slots --owner` changes nothing: it exits 0 printing the recorded owner slot when that slot is in the LUKS header; 4 with `No owner key slot is recorded` on stderr when there is no owner record at all (a Mac set up before its boot package recorded slots); and 1 with the reason on stderr for anything else: a record that isn't a slot number or names a slot the header lacks, an unreadable header, or a boot package too old to know `--owner`. Idempotent. It fails when a slot is not in the LUKS header, and the caller then retries. | required | `omarchy-provision-owner`, `omarchy-drive-password` | +| `setup-boot [image-first-boot]` | Hardware setup's last leaf (`install/hardware/platform-setup.sh`), before `setup-system`, on every install and rerun of `omarchy-apply-hardware`, and on an image's first boot with `image-first-boot` | The platform boot package's own boot setup (on a Mac, the GRUB console settings and the Limine activation). Idempotent. With `image-first-boot` it asks for the boot-file rebuild by creating `/var/lib/omarchy/image/boot-rebuild` where it would build one. A failure fails the leaf, and `setup-system` does not run. | required | `install/hardware/platform-setup.sh` | +| `setup-system [image-first-boot]` | Hardware setup, as its last leaf (`install/hardware/platform-setup.sh`), after `setup-boot`, on every install and every rerun of `omarchy-apply-hardware`. In an image build it is queued with the other leaves and runs on the machine's first boot, with `image-first-boot`. | The platform's machine-wide setup: services, configuration and drivers its runtime package owns. Idempotent. Needs no network with `image-first-boot` (that boot may be offline), and there it asks for a boot-file rebuild by creating `/var/lib/omarchy/image/boot-rebuild` rather than building one, since the deferred setup rebuilds once after its last step. A failure fails the leaf, and on an image's first boot the step stays queued for the next boot. | optional | `install/hardware/platform-setup.sh` | +| `setup-user` | Runs as the user being set up: at the end of user finalization (`install/user/platform-setup.sh`, from `install/user/all.sh`), and in first run once the session is up. Finalization inside an image build skips it; first run on the machine runs it. | The platform's per-user setup. Idempotent. Finalization may have no session (the ISO chroot, owner setup), so work that needs one (a user unit started now, a D-Bus call) is left for the first-run call, and its absence is not a failure. A failure fails finalization or the first-run step, so first run stays pending and runs it again at the next login. | optional | `install/user/platform-setup.sh`, `omarchy-provision-first-run` | +| `post-install ` | Runs as the user, from an installer once `` is installed and Omarchy's own setup of it is done: `chromium`, `chrome`, `edge`, `brave`, `brave-origin`, `firefox`, `zen` (`omarchy-install-browser`) and `steam` (`omarchy-install-gaming-steam`) | The platform's adjustments for that app, such as its browser flags or a launcher the app needs on this hardware (which may install a package through `sudo`). Idempotent. A failure fails the install. | optional | `omarchy-install-browser`, `omarchy-install-gaming-steam` | +| `pre-remove ` | Runs as the user, from a remover before ``'s packages go: `steam` (`omarchy-remove-gaming-steam`) | Undoes what `post-install ` added, packages that depend on the app first. Idempotent; nothing to undo is a success. A failure stops the removal before anything is removed. | optional | `omarchy-remove-gaming-steam` | + +Snapshot restore is not an operation: a Limine machine restores through `limine-snapper-restore`, and a platform boot package that checks a restore does so through limine-snapper-sync's own hooks. + +## Platform registration + +Registration is code in `bin/omarchy-lifecycle-dispatch`, not configuration. No file, environment variable or `PATH` entry decides what runs as root. + +| Platform | Implementation directory | Package | Required operations | +| --- | --- | --- | --- | +| `apple-silicon` | `/usr/lib/omarchy/mac-boot` | `omarchy-mac-boot` | all its operations | +| `apple-silicon`: `setup-system`, `setup-user`, `post-install`, `pre-remove` | `/usr/lib/omarchy/mac` | `omarchy-mac` | none | +| `generic`, `generic-aarch64`, `qualcomm` | none | none | none: every operation is a no-op, and callers keep their generic path | + +The entrypoint for an operation is `/`. A registered platform's required operations must be shipped. Its optional operations may be left out, and then they are no-ops. + +On a Mac, owner provisioning and factory reset have no other path, so their operations are required: a Mac without `omarchy-mac-boot`'s entrypoints stops before the owner form or before the reset is confirmed, with the dispatcher's error naming the package, where the generic Limine path would leave the boot-partition key behind or rebuild a UKI the Mac does not boot. `luks-slots` is required because the Mac's boot checks prove the disk's key slots. `update-verify` is required because nothing else checks what an update left in a Mac's boot chain. `update-takeover` is required because a Mac's boot chain and image write files no package owns, so a Mac whose boot package can't vouch for a takeover keeps its files and the update stops at the conflict. `setup-boot` is required because the Mac's boot setup has no other path in hardware setup: a Mac whose `omarchy-mac-boot` lacks it stops the hardware leaf, asking for the update, where an optional operation would skip the Limine activation. System and user setup and the app-install hooks belong to the Mac's runtime package, `omarchy-mac`, not its boot package, and are optional: a Mac without `omarchy-mac` gets Omarchy's generic setup and installs. + +## Trust rules + +- The dispatcher runs as `bash -p`, so a root caller's `BASH_ENV` and exported functions run nothing in it, and it refuses an ordinary Bash launch. As root it uses a fixed `PATH`, runs the detector installed beside it with nothing in its environment but that `PATH` (the detector reads only the live device tree as root), and resolves only the fixed implementation directory. +- An entrypoint runs only if it is a regular executable file. Neither the file nor any directory up to `/` may be a symlink, and all of them must be owned by root and not writable by group or others. An entrypoint that fails these rules is refused, even for an optional operation. +- The entrypoint runs with an empty environment apart from `PATH=/usr/local/sbin:/usr/local/bin:/usr/bin`. It gets the caller's arguments, standard streams and working directory. Entrypoints use fixed paths, never environment variables. Anything that can also run them directly re-checks the platform itself. +- `setup-user`, `post-install` and `pre-remove` refuse root where the platform registers them, so they never run as anyone but their caller. Where it registers none they stay no-ops for root too, so an installer run with `sudo` still finishes there. Their entrypoints follow the same trust rules, and besides `PATH` they get only these, when the caller has them set: `HOME`, `USER`, `XDG_CONFIG_HOME` and `XDG_STATE_HOME` (where the user's configuration and setup markers live), `XDG_RUNTIME_DIR`, `DBUS_SESSION_BUS_ADDRESS`, `WAYLAND_DISPLAY` and `OMARCHY_PATH`. +- For unprivileged tests, `OMARCHY_LIFECYCLE_ROOT` (absolute) prefixes the implementation directory, and the detector's fixture roots apply. Inside that root, files the caller owns count as root's. Root ignores both. + +## Callers + +A dispatch point takes one of two shapes: + +- **A platform step with no generic counterpart:** `omarchy-lifecycle-dispatch || fail`. It is a no-op on platforms without a boot package. +- **A platform implementation that replaces a generic step:** `--resolve `. A path means dispatch the operation, an empty result means run the generic step, and a failure fails closed. + +Failing closed holds on every platform: where `omarchy-hw-platform` can't settle the platform (contradicting device-tree evidence, for one), first-boot setup and factory reset stop instead of guessing a boot path, and an update fails its verification without the reboot prompt. There is no check before an update's packages change: on a machine whose platform can't be told, the update installs its packages first, and it only then fails verification. A platform that needs an earlier refusal adds that operation with its first entrypoint. + +### Owner provisioning (`bin/omarchy-provision-owner`) + +- `platform_ready` runs `provision-prepare` and resolves `luks-slots` at the start of each setup attempt, before the keyboard and account forms. If either fails, the owner sees its error, the log records it, and the attempt ends in the retry or root-shell screen. +- The shared re-key (`install/provisioning/luks-rekey.sh`) asks the caller for two callbacks: `luks_auto_unlock_present` and `luks_auto_unlock_drop`. `unlock_owner` resolves `provision-commit` and `provision-verify` once per process. If both resolve, the platform owns the unlock: drop is `provision-commit`, and present is `provision-verify` failing. If neither resolves, the Limine UKI callbacks run unchanged (x86, Snapdragon, generic aarch64). If only one resolves, or resolution fails, the unlock counts as present and can't be dropped, so setup never finishes. +- After a factory reset left Limine entries for another machine identity, `run_provisioning` starts the menu over from the template and runs `limine-update`, on every platform. +- The shared re-key calls the caller's `luks_record_slots` once it has verified that only the owner's slot remains and before it destroys the staged key; `omarchy-provision-owner` runs `luks-slots owner= recovery=` there, a no-op where nothing records them. +- Everything else stays as it is: the wizard, account and login, the journal, slot retirement, the proof that the staged key opens nothing, and cleanup. + +### Factory reset (`bin/omarchy-system-factory-reset`) + +- `reset_boot_owner` resolves the four reset operations before the reset is confirmed. If all resolve, the platform owns the factory root's boot chain; if none do, the generic path runs unchanged (x86, Snapdragon, generic aarch64: throwaway slot, keyfile in the Limine UKI, `limine-update`, `verify_limine_hashes`). If only some resolve, or resolution fails (a Mac without `omarchy-mac-boot`'s entrypoints), the reset stops before anything changes. +- Where the platform owns it, the order is: authorise with the current passphrase and stage the throwaway in the factory root's `/var/lib/omarchy/provisioning/luks-key`; `reset-prepare`; `reset-verify`; add the throwaway slot; switch the subvolumes; `reset-commit` with the throwaway on standard input. The slot comes after verification, so a failed rebuild adds no credential, and the platform writes its boot-time key only after the switch, so a power loss before it leaves the previous root asking for its password, never unlocked unattended. +- Any failure before the switch runs `reset-rollback` (once `reset-prepare` started), then revokes the slot this attempt added (found by the throwaway key when the add was not confirmed) and deletes the clone: the previous root stays the one that boots, with its boot files and encryption state. Operation output goes to the reset log; a failure shows the operation's last line. +- Everything else stays as it is: the @factory clone, identity and account scrub, provisioning markers and units, LUKS discovery, the passphrase check, the throwaway slot, and the subvolume switch. + +### Update (`bin/omarchy-update`) + +- `omarchy-update-boot` runs `update-verify` after AUR packages, which the update runs last, so it is the last sudo-capable step; like AUR it follows third-party build code, so it authenticates through the no-update wrapper without a reusable timestamp. When it fails, the update still releases Stay Awake, then says the update is not finished and exits 1 without the reboot prompt. +- It resolves the operation as the user first and runs it with `sudo` only when it resolves to an entrypoint, so an update with nothing to run never asks for root. A failed resolution fails the step with the dispatcher's message, except one: `update-verify` on a machine without its platform's boot package at all (exit 3) warns that the boot files were not verified and lets the update finish. Such a machine predates the package and boots a chain it does not manage; once the package is installed, a failed verification blocks. A package too old to ship `update-verify` blocks, since the fix is one package update away. +- The update path rebuilds no boot file itself: package hooks do, and `update-verify` catches what they missed. +- When an Omarchy package's upgrade stops only on files no package owns, `omarchy-update-system-pkgs-when-conflicted` moves them aside and retries. Before it moves anything it resolves `update-takeover` as the user and, when it resolves, runs `sudo omarchy-lifecycle-dispatch update-takeover ...` (inside the update's authorization, which the move needs anyway). A refusal, or a failed resolution (a Mac without its boot package, or with one too old to ship the operation), keeps every file and fails the update as it did before the retry existed. Where nothing resolves, the takeover goes ahead with no extra step. + +### Hardware setup (`install/hardware/platform-setup.sh`) + +- The last leaf of `install/hardware/all.sh` runs `setup-boot`, then `setup-system`, so both build on every generic leaf. As a `run_logged` leaf it is queued in an image build like any other, and `omarchy-provision-hardware` runs it on the first boot with `OMARCHY_IMAGE_DEFERRED_HARDWARE=1`, which the leaf passes on as `image-first-boot` to both. The environment variable itself never reaches the entrypoints. + +### User setup (`install/user/platform-setup.sh`, `bin/omarchy-provision-first-run`) + +- The last leaf of `install/user/all.sh` runs `setup-user` at the end of finalization. While `/var/lib/omarchy/image/target` exists the root is still being built, where a normal user's detector would read the build host, so the leaf says so and skips it. +- First run runs `setup-user` again as its own step, after the user units are enabled, now that the session is up. Its failure, like finalization's, leaves first run pending for the next login. + +### App installs (`bin/omarchy-install-browser`, `bin/omarchy-install-gaming-steam`, `bin/omarchy-remove-gaming-steam`) + +- A browser install runs `post-install ` once the browser's package, policy directory, flags file and theme are in place, and before it says the browser is installed. Steam's install runs `post-install steam` after the package and before the 32-bit drivers and the first launch, and Steam's removal runs `pre-remove steam` before its packages go, so a package the hook added that depends on Steam doesn't stop the removal. +- On a platform that registers none, each call is a no-op and the installer runs as before. + +### Disk password change (`bin/omarchy-drive-password`) + +- After the system disk's key changed and the login and root passwords follow it, `record_owner_slot` resolves `luks-slots` as the user and, when it resolves, runs `sudo omarchy-lifecycle-dispatch luks-slots owner=`, so no other platform sees an extra `sudo`. Until that succeeds the journal stays, and the next run finishes the change and records the slot. The new key can land in another slot (cryptsetup 2.8's `luksChangeKey` moves a LUKS1 key to the first free slot, and keeps a LUKS2 one in place), and the boot check would then find a slot the platform did not record. +- Before anything changes, where `luks-slots` resolves, `owner_slot_matches` runs `sudo omarchy-lifecycle-dispatch luks-slots --owner` and refuses a current password whose slot is not the owner's, so another key on the system disk, such as a recovery key an earlier Mac setup added, stays as it is and can't take the owner's place. Exit 4 (no owner slot recorded) is taken as a machine nobody recorded: the change goes on only when the current password opens the disk's one key slot, checked again before the key changes, and records the new slot as the owner's once it is confirmed; with other keys on the disk it stops, naming the slots and saying how to record them (`luks-slots owner= recovery=`) or remove the extra key. Any other failure stops the change with the boot package's reason. + +## Key slots + +Omarchy has no disk recovery key: the disk, the login user and root share one password, as on every install. The re-key keeps the owner's slot alone. + +- **The owner's password:** the owner's slot is the re-key's own step, so a retry may still choose a new password while the staged key opens the disk, even after the boot step: the re-key then retires the old password's slot and records the final slot with `luks-slots`. +- **Temporary key:** setup finishes only after the re-key proved the staged key opens nothing, the boot package's `provision-verify` found no boot-time copy, and `luks-slots` recorded the kept slot. + +## Platform files + +A platform's runtime package describes its hardware in files under the platform root, `/usr/share/omarchy-platform/`, without an operation and without a branch in Omarchy's code: display hints, camera cutouts, audio nodes, keyrings, key names and fingerprint readers (see [file-layout.md](file-layout.md#platform-root)). Only the one installed platform package owns the root, and Omarchy ships nothing in it. The root is fixed: no environment variable moves it, and a development checkout in `OMARCHY_PATH` doesn't replace it. A missing root or file means no platform additions. It changes no binds, settings or gestures: Omarchy behaves the same on every machine. + +- **Key names: `key-names`**, one ` ` per line, which the keybindings menu shows in place of the keysym (a MacBook's `XF86MonBrightnessUp` is its F2 key). Its tests run copies of the menu that read a fixture root (`platform_root_copy` in `base-test.sh`). + +## Apple Silicon + +`omarchy-mac-boot`, from omacom/omarchy-mac-pkgs, implements the Apple boot operations as small entrypoints around its boot modules and installs them in `/usr/lib/omarchy/mac-boot`. `omarchy-mac`, from the same repository, implements `setup-system`, `setup-user`, `post-install` and `pre-remove` in `/usr/lib/omarchy/mac`. Their documentation describes each one. Nothing Apple-specific lives in Omarchy beyond the registration above. + +## Qualcomm + +Snapdragon laptops boot Limine with unified kernel images, like x86, and `qualcomm` is unregistered. Every operation is a no-op there, and provisioning uses the Limine UKI callbacks, so Dragon behaves exactly as before. To plug in a Qualcomm implementation: + +1. Ship the entrypoints from a Qualcomm boot package as `/usr/lib/omarchy//`, root-owned, mode 755. `` is a short directory name, as `mac-boot` is for `omarchy-mac-boot`. +2. Add a `qualcomm)` case to the registration in `bin/omarchy-lifecycle-dispatch` with that directory, the package name, and the operations it requires. +3. Operations it neither requires nor ships stay no-ops. For provisioning, ship `provision-commit` and `provision-verify` together, or neither to keep the Limine UKI path. +4. Move `qualcomm` in `test/shell.d/lifecycle-dispatch-test.sh` from the no-op platforms to its own cases, like Apple's. + +## Tests + +- `test/shell.d/lifecycle-dispatch-test.sh` covers the dispatcher on every platform fixture: + - the setup and app-install operations: no-ops off Apple, `omarchy-mac`'s directory on Apple (never `omarchy-mac-boot`'s), no-ops without `omarchy-mac`, the user operations refused as root and given only their allowlisted environment + - no-ops on x86, generic aarch64 and Qualcomm, even with Mac entrypoints on disk + - Apple with and without the boot package, and with one too old to ship an operation + - arguments, exit status and the cleared environment + - untrusted entrypoints + - usage errors, and an undetermined platform + - an ordinary Bash launch, and root ignoring fixture roots, `BASH_ENV` and exported functions +- `test/shell.d/luks-rekey-journal-test.sh` runs owner provisioning through the real dispatcher: + - the crash-and-resume matrix on x86 (Limine UKI path unchanged, no Mac entrypoint runs) and on Apple with a fake boot package (the owner's slot recorded), on a slot-table fake and on file-backed LUKS2 and LUKS1 volumes + - setup stopping before the owner form when the boot package is not ready, missing, or too old to ship the provisioning entrypoints + - the stale-entry refresh rebuilding through `limine-update`, behind the boot package's `provision-verify` on Apple + - the worker failing closed without the provisioning entrypoints, or when a boot package implements only one of `provision-commit` and `provision-verify` +- `test/shell.d/platform-setup-test.sh` runs the two setup leaves (the system one running `setup-boot`, then `setup-system`, and stopping when either fails or the boot package lacks `setup-boot`) and first run through the real dispatcher: each is the last of its setup, a no-op on x86, `image-first-boot` only on an image's first boot, the user leaf waiting for first run in an image build, and a failing entrypoint failing the leaf and keeping first run pending. `test/shell.d/image-deferred-hardware-test.sh` checks that an image build queues every hardware leaf, the system one included. +- `test/shell.d/first-run-test.sh` checks that a failed finalization keeps first run pending. +- `test/shell.d/platform-app-hooks-test.sh` runs every browser install and Steam's install and removal through the real dispatcher: on Apple each calls its hook once, after the package and flags (before removal for `pre-remove`), and fails with it; on x86 none runs, even with Mac entrypoints on disk. +- `test/shell.d/update-file-conflict-test.sh` checks the takeover: on x86 it goes ahead asking no one; on Apple it goes ahead only when the boot package's `update-takeover` vouches for exactly the files that move, and a refusal, a missing boot package or one too old keep every file. +- `test/shell.d/factory-reset-dispatch-test.sh` runs the reset through the real dispatcher: on x86 with Mac entrypoints on disk that must not run (generic path unchanged), and on Apple into a fake boot package, covering a finished reset, a failed verification and a failed switch that roll back, an unconfirmed throwaway slot found by its key and revoked, a failed commit after the switch, and a missing or partial package. +- `test/shell.d/update-boot-verify-test.sh` runs `omarchy update` through the real `omarchy-update-boot` and dispatcher inside the sudo boundary fixture: no-ops and no root on x86, generic aarch64 and Qualcomm; on Apple, verify after AUR through the no-update wrapper, a failed verification that offers no reboot, and a Mac without the package or with one too old. +- `test/shell.d/drive-password-test.sh` checks that an Apple password change records the owner's new slot through `luks-slots`, including after an interruption, and that x86 never calls it; that `--owner` failing (1, or a slot that isn't a number) stops the change with the reason; and that with no owner slot recorded (4) a one-key disk changes and records its slot, also after an interruption, while a disk with another key, or one that gains a key during the prompts, is refused unchanged. diff --git a/docs/omarchy-shell.md b/docs/omarchy-shell.md index 32d7af9fb6de..9306ac05b3fe 100644 --- a/docs/omarchy-shell.md +++ b/docs/omarchy-shell.md @@ -382,6 +382,16 @@ size-vertical = 28 # left/right bar width at base-size 12 Set `scale-with-font = false` to keep those bar sizes as fixed pixels. +A panel with a camera cutout (a notch) at its top keeps a top bar out of it: the bar is never shorter than the cutout, and its center section sits beside the right one. Omarchy knows no panel's cutout itself; the platform's own package describes them in `/usr/share/omarchy-platform/display-cutouts.json`, a fixed path no environment variable moves (see [file-layout.md](file-layout.md#platform-root)): + +```json +{ "panels": [ { "connector": "eDP", "width": 3024, "height": 1964, "top": 64 } ] } +``` + +A panel matches a screen whose connector name starts with `connector` and whose mode, as `hyprctl monitors` reports it, is `width` x `height` physical pixels, at any scale; `top` is the physical rows the cutout covers. On such a panel, `[bar] notch-height` (logical pixels, not scaled with the font) sets the floor by hand. The bar reads the file when the shell starts and whenever it changes; a file installed where its directory didn't exist yet (the platform root itself, on a first install) is read at the next shell start. + +Until Hyprland reports a screen's mode, a top bar there waits to appear if a description names the screen's connector, unless the logical size times Qt's whole-number scale already matches a panel. It then appears at its floor rather than growing once windows are laid out, and never guesses a floor for a panel nothing describes. After two seconds without a mode it appears without a floor. Notifications clear the bar as thick as it is on their screen. A plugin's `shell.bar` gives the same through `barSizeFor(screenName)` and the `barSizes` map (screen name to thickness); its `barSize` stays the configured size, which the widgets are drawn at. + ## Custom bar modules If a full plugin is overkill, declare a one-off module inline in diff --git a/docs/update-process.md b/docs/update-process.md index b5e00925b461..0f954598dfc3 100644 --- a/docs/update-process.md +++ b/docs/update-process.md @@ -143,9 +143,11 @@ omarchy-update ├─ run the post-update hook, then update mise tools ├─ stop the keepalive and invalidate sudo, then update AUR packages with │ no-update authentication, and invalidate again + ├─ omarchy-update-boot, with no-update authentication, and invalidate again + │ └─ the platform's boot package proves the boot files boot the updated system ├─ omarchy-update-stay-awake stop │ └─ release the sleep inhibitor and restore shell idle state, if changed - └─ offer the unprivileged reboot prompt + └─ offer the unprivileged reboot prompt, only when the boot files were verified ``` Important behavior: @@ -166,6 +168,7 @@ Important behavior: `omarchy-migrate` after pacman finishes. - A failure should leave enough output in `/tmp/omarchy-update.log` and the terminal transcript to debug. +- The boot check is the [lifecycle dispatch](lifecycle-dispatch.md) operation `update-verify`, a no-op on platforms whose boot chain needs no handling of its own, x86 included: nothing runs and nothing asks for root. It is the last sudo-capable step, after AUR packages, so it also covers the initramfs rebuilds they trigger; since it follows third-party build code, it authenticates through the no-update wrapper like AUR does, which on a platform that implements it without passwordless sudo is one more prompt. When it fails, the update finishes its remaining steps, then exits non-zero without the reboot prompt: the update is not finished. A machine without its platform's boot package at all predates it: the update warns that its boot files were not verified and finishes. There is no check before the packages change: where `omarchy-hw-platform` can't tell the platform, the update installs its packages, and the update then fails verification, without the reboot prompt. ## Path 2: direct `sudo pacman -Syu` attempt @@ -274,6 +277,29 @@ Channel switching runs the `pre-refresh-pacman` hook once, during its refresh step: cold, behind the no-update wrapper, after the package config is re-synced and before the refresh transaction. It does not run if the switch fails earlier. +Every platform has its own pacman.conf and mirrorlist for each channel it +offers (x86_64 stable, rc and edge; aarch64 edge alone, below), and a channel +change or install finalization copies them into place whole, the same way on +every platform (`install/helpers/pacman.sh`); a channel change backs up +the old pair first. +x86_64's are `default/pacman/pacman-.conf` and +`mirrorlist-`; Snapdragon and other aarch64 machines use +`default/pacman/aarch64/`, Arch Linux ARM's repositories with Omarchy's; Apple +Silicon uses `default/pacman/apple-silicon/`, which puts Omarchy and Asahi ALARM +ahead of Arch Linux ARM. Omarchy publishes aarch64 packages on edge alone so +far, and the `omarchy` and `omarchy-settings` packages there for stable and rc +are the release line, which has no aarch64 support, so ARM platforms have edge +templates only: switching an ARM machine to stable or rc would replace its +runtime with one that cannot run it. Adding a stable or rc template once a +release supports aarch64 is what opens that channel. A channel change refuses a +channel without both files for the platform before anything changes. On aarch64, +`omarchy-channel-set` refuses to link a dev checkout without +`bin/omarchy-hw-platform` and `install/helpers/pacman.sh`, whose own refresh +would write the x86_64 templates. `omarchy-reinstall-pkgs` resets to the +platform's default channel (`omarchy_pacman_default_channel`: stable, or edge on +aarch64) and installs its default packages; install finalization does the same +when the install's channel has no templates for the platform. + There is no version file at runtime. `omarchy-version` derives the version from `pacman -Q` on whichever package is installed, or reports `dev ()` for a linked checkout, and `omarchy-version-channel` sniffs the mirrorlist and @@ -293,7 +319,7 @@ scripts. | `omarchy-update-status` | Hidden helper that refreshes or clears the shell update indicator after rechecking available updates. | **Keep internal/hidden.** Keeps shell status synchronization out of the main pipeline. | | `omarchy-update-confirm` | Gum confirmation copy for `omarchy update`. | **Question.** Could be inlined into `omarchy-update`; separate file only helps keep copy isolated. | | `omarchy-update-dev` | Fast-forwards the active dev-linked checkout from its configured upstream; no-ops for package-backed installs. | **Keep.** Runs before package updates so a checkout conflict stops the update before system mutation. | -| `omarchy-update-keyring` | Ensures Omarchy keyring and Arch keyring are current before the main transaction. | **Keep, but review.** It uses targeted `pacman -Sy` for keyring bootstrapping; acceptable for this special case but should remain tightly scoped. | +| `omarchy-update-keyring` | Ensures Omarchy keyring and Arch keyring (and Arch Linux ARM keyring on aarch64, where installed) are current before the main transaction. | **Keep, but review.** It uses targeted `pacman -Sy` for keyring bootstrapping; acceptable for this special case but should remain tightly scoped. | | `omarchy-update-system-pkgs` | Runs `omarchy-update-pacman -Syu --noconfirm` with `--overwrite '/usr/share/omarchy/*'`, capturing stderr to a report file; on failure it execs `omarchy-update-system-pkgs-when-conflicted`. | **Keep for now.** Small leaf command, clear/testable. | | `omarchy-update-system-pkgs-when-conflicted` | Hidden conflict handler: quarantines unowned conflicting files under `/var/lib/omarchy/replaced`, retries the upgrade once, restores files the upgrade didn't claim, and hands package-vs-package conflicts to an interactive pacman run (never under `-y`). | **Keep internal/hidden.** Keeps conflict recovery out of the happy path. | | `omarchy-update-pkg-prune` | Trims the pacman cache to two versions per package (`paccache -rk2`) before the snapshot, keeping the offline downgrade path while capping snapshot growth. | **Keep internal/hidden.** | @@ -308,6 +334,7 @@ scripts. | `omarchy-update-mise` | Runs `MISE_MINIMUM_RELEASE_AGE=0 mise up` for mise-managed tools — the override of mise's release-age cooldown is the point. | **Keep.** Mise-managed tools are intentionally part of the blessed update path. | | `omarchy-update-orphan-pkgs` | Lists orphans and prompts before removal; noninteractive mode never removes. | **Keep for now.** Safe because it is prompt-only. | | `omarchy-update-analyze-logs` | Scans `/tmp/omarchy-update.log` for known failure patterns, currently initramfs generation. | **Keep/expand.** Useful safety net; should grow only for high-signal checks. | +| `omarchy-update-boot` | Hidden helper that runs the platform's `update-verify` lifecycle operation through `omarchy-lifecycle-dispatch`, with `sudo` only when the platform implements it. | **Keep internal/hidden.** Keeps platform boot checks out of the pipeline and stubbable in tests. | | `omarchy-update-restart` | Restarts components selected by `restart-*-required` markers, always restarts the shell, and prompts for reboot after kernel/Hyprland updates. Internal phase flags let the update finish sudo-capable restarts before user hooks and defer only the unprivileged reboot prompt. | **Keep.** Important final step; may eventually include service-restart checks. | | `omarchy-update-firmware` | Manual firmware update command using fwupd. Not part of the normal update pipeline. | **Keep separate.** Firmware is not a routine system update step. | | `omarchy-update-time` | Restarts `systemd-timesyncd`. | **Question.** Not really an update command. Consider renaming/moving under system/time maintenance. | diff --git a/etc/mkinitcpio.conf.d/00-omarchy-hooks.conf b/etc/mkinitcpio.conf.d/00-omarchy-hooks.conf new file mode 100644 index 000000000000..f48e2da264dc --- /dev/null +++ b/etc/mkinitcpio.conf.d/00-omarchy-hooks.conf @@ -0,0 +1,34 @@ +# The platform's HOOKS baseline. mkinitcpio sources mkinitcpio.conf and then +# every drop-in here in name order, so the baseline sorts first and the drop-ins +# after it add their hooks to it instead of being overwritten by it. +# +# Apple Silicon boots a systemd initramfs, and its platform package adds the +# firmware and encryption hooks. Every other machine keeps the busybox line, as +# does one without the runtime's detector. A detector that cannot place the +# machine stops the build rather than let it produce an image for the wrong +# platform. +_omarchy_platform="" +if _omarchy_detector=$(command -v omarchy-hw-platform); then + _omarchy_platform=$("$_omarchy_detector") || return 1 +fi + +# A Mac whose mkinitcpio.conf carries the asahi hook, or the busybox encrypt +# hook that unlocks it through cryptdevice= (sd-encrypt cannot parse that), boots +# the initramfs its platform set up without the Apple boot package, as a legacy +# install does. Its line stays as it is. The asahi hook also marks such a root +# off a Mac, as in a chroot or a VM. +if [[ " ${HOOKS[*]:-} " == *" asahi "* ]] || + [[ $_omarchy_platform == "apple-silicon" && " ${HOOKS[*]:-} " == *" encrypt "* ]]; then + _omarchy_platform=platform-owned +fi + +case $_omarchy_platform in + platform-owned) ;; + apple-silicon) + HOOKS=(base systemd plymouth autodetect microcode modconf kms keyboard sd-vconsole block filesystems fsck) + ;; + *) + HOOKS=(base udev plymouth keyboard autodetect microcode modconf kms keymap consolefont block encrypt filesystems fsck btrfs-overlayfs) + ;; +esac +unset _omarchy_platform _omarchy_detector diff --git a/etc/mkinitcpio.conf.d/omarchy_hooks.conf b/etc/mkinitcpio.conf.d/omarchy_hooks.conf index 68408b00ba1d..0dfcb8e932f7 100644 --- a/etc/mkinitcpio.conf.d/omarchy_hooks.conf +++ b/etc/mkinitcpio.conf.d/omarchy_hooks.conf @@ -1,4 +1,5 @@ -HOOKS=(base udev plymouth keyboard autodetect microcode modconf kms keymap consolefont block encrypt filesystems fsck btrfs-overlayfs) +# Adjusts the baseline HOOKS from 00-omarchy-hooks.conf. It stays apart from +# the baseline because it reads what the hardware drop-ins sorting before it set. # The proprietary NVIDIA driver does early KMS itself: nvidia.conf (written by # install/hardware/nvidia.sh, sourced before this file) early-loads nvidia_drm diff --git a/etc/mkinitcpio.conf.d/thunderbolt_module.conf b/etc/mkinitcpio.conf.d/thunderbolt_module.conf index 9518f8b1201c..2d3082692ab5 100644 --- a/etc/mkinitcpio.conf.d/thunderbolt_module.conf +++ b/etc/mkinitcpio.conf.d/thunderbolt_module.conf @@ -1 +1,7 @@ -MODULES+=(thunderbolt) +# Early-load thunderbolt where the kernel builds it as a module. The optional +# form, thunderbolt?, is not enough: a systemd initramfs copies MODULES as +# written into modules-load.d, where "thunderbolt?" names no module, and a +# kernel without the module would still list it there. +if [[ $(modinfo -k "${KERNELVERSION:-$(uname -r)}" -F filename thunderbolt 2>/dev/null) == /* ]]; then + MODULES+=(thunderbolt) +fi diff --git a/install/config/all.sh b/install/config/all.sh index a221a39896d9..da57760e18bf 100644 --- a/install/config/all.sh +++ b/install/config/all.sh @@ -6,6 +6,7 @@ run_logged "$OMARCHY_INSTALL/config/fix-powerprofilesctl-shebang.sh" run_logged "$OMARCHY_INSTALL/config/ssh-command-path.sh" run_logged "$OMARCHY_INSTALL/config/ssh-keepalive.sh" run_logged "$OMARCHY_INSTALL/config/docker.sh" +run_logged "$OMARCHY_INSTALL/config/locale.sh" run_logged "$OMARCHY_INSTALL/config/snapper.sh" run_logged "$OMARCHY_INSTALL/config/enable-services.sh" run_logged "$OMARCHY_INSTALL/config/firewall.sh" diff --git a/install/config/locale.sh b/install/config/locale.sh new file mode 100644 index 000000000000..e70713fb3580 --- /dev/null +++ b/install/config/locale.sh @@ -0,0 +1,50 @@ +# An image built from a distribution's root tarball rather than the ISO (Arch +# Linux ARM's ships LANG=C) never went through the ISO's locale step, so it +# runs non-UTF-8: byte-wise sorting, ASCII-only \u escapes, and any tool that +# reads the locale for its encoding. +# Root always writes the real files; the overrides are for unprivileged tests. +locale_conf=/etc/locale.conf +locale_gen=/etc/locale.gen +if (( EUID != 0 )); then + locale_conf=${OMARCHY_LOCALE_CONF:-$locale_conf} + locale_gen=${OMARCHY_LOCALE_GEN:-$locale_gen} +fi + +# Repair only the stock state -- an unset LANG, or the bare C/POSIX the image +# ships. Any named locale is somebody's choice, C.UTF-8 included, so leave it. +# A machine with no locale.conf at all reads as unset, not as a failure: +# under pipefail the missing file would otherwise abort the installer. +current=$(sed -n 's/^LANG=//p' "$locale_conf" 2>/dev/null | tail -1 | tr -d '"') || current="" + +case ${current:-C} in + C | POSIX) ;; + *) + echo "Leaving the locale as $current" + return 0 2>/dev/null || exit 0 + ;; +esac + +echo "Setting up locale (en_US.UTF-8)..." + +if ! locale -a 2>/dev/null | grep -qi "en_US.utf-\?8"; then + if grep -q '^#en_US.UTF-8' "$locale_gen" 2>/dev/null; then + sed -i 's/^#en_US.UTF-8/en_US.UTF-8/' "$locale_gen" + elif ! grep -q '^en_US.UTF-8' "$locale_gen" 2>/dev/null; then + echo "en_US.UTF-8 UTF-8" >>"$locale_gen" + fi + + locale-gen >/dev/null 2>&1 +fi + +# Only LANG changes; LC_* lines somebody set stay. +if grep -q '^LANG=' "$locale_conf" 2>/dev/null; then + sed -i 's/^LANG=.*/LANG=en_US.UTF-8/' "$locale_conf" +else + echo "LANG=en_US.UTF-8" >>"$locale_conf" +fi + +# The session that ran this keeps its inherited LANG; everything after it here +# should see the new one. +export LANG=en_US.UTF-8 + +echo "Locale set to en_US.UTF-8" diff --git a/install/hardware/all.sh b/install/hardware/all.sh index 12add110c51f..387e934ad65b 100644 --- a/install/hardware/all.sh +++ b/install/hardware/all.sh @@ -44,3 +44,5 @@ run_logged "$OMARCHY_INSTALL/hardware/fix-yt6801-ethernet-adapter.sh" run_logged "$OMARCHY_INSTALL/hardware/fix-tuxedo-backlight.sh" run_logged "$OMARCHY_INSTALL/hardware/speaker-tuning.sh" run_logged "$OMARCHY_INSTALL/hardware/pacman.sh" + +run_logged "$OMARCHY_INSTALL/hardware/platform-setup.sh" diff --git a/install/hardware/apple/fix-brcmfmac-supplicant.sh b/install/hardware/apple/fix-brcmfmac-supplicant.sh index 33238c9cf37c..d8da7d7dc4d6 100644 --- a/install/hardware/apple/fix-brcmfmac-supplicant.sh +++ b/install/hardware/apple/fix-brcmfmac-supplicant.sh @@ -18,6 +18,14 @@ # machines including the T2-less iMac19,1 and iMac19,2, and BCM4377/4378/4387 # from the T2 era on. The BCM4360 in 2013-2015 Macs is deliberately absent: it # runs the out-of-tree wl driver, which never reads a brcmfmac option. +# +# Apple Silicon is excluded even though its BCM4387 is in that list: there the +# firmware supplicant is what works, and disabling it makes firmware commands +# time out and breaks scanning entirely. +if omarchy-hw-apple-silicon; then + return 0 +fi + sys_vendor="$(cat /sys/class/dmi/id/sys_vendor 2>/dev/null || true)" if lspci -nn | grep "106b:180[12]" >/dev/null || diff --git a/install/hardware/apple/fix-spi-keyboard.sh b/install/hardware/apple/fix-spi-keyboard.sh index 330081b76318..59d4b12f881b 100644 --- a/install/hardware/apple/fix-spi-keyboard.sh +++ b/install/hardware/apple/fix-spi-keyboard.sh @@ -1,5 +1,7 @@ -# Detect MacBook models that need SPI keyboard modules -product_name="$(cat /sys/class/dmi/id/product_name 2>/dev/null)" +# Detect MacBook models that need SPI keyboard modules. +# Not every machine has a DMI product name, and under set -e an unguarded read +# of a missing one aborts the hardware pass. +product_name="$(cat /sys/class/dmi/id/product_name 2>/dev/null || true)" if [[ $product_name =~ MacBook[89],1|MacBook1[02],1|MacBookPro13,[123]|MacBookPro14,[123] ]]; then echo "Detected MacBook with SPI keyboard" diff --git a/install/hardware/bluetooth.sh b/install/hardware/bluetooth.sh index 40345cf995d8..8ba4f897b367 100644 --- a/install/hardware/bluetooth.sh +++ b/install/hardware/bluetooth.sh @@ -1,5 +1,15 @@ systemctl enable bluetooth.service +# An image's first boot runs this after the adapter has brought up (or queued) +# bluetooth.target, and a target never starts a unit enabled after its start +# job was made, so Bluetooth would stay off until the next boot. Only there +# (omarchy-provision-hardware sets the marker): an install or a rerun of +# hardware setup leaves the service as it finds it. Without Bluetooth support +# the unit's own condition skips it. +if [[ ${OMARCHY_IMAGE_DEFERRED_HARDWARE:-} == "1" ]]; then + systemctl start --no-block bluetooth.service || true +fi + # AutoEnable stays at its stock default on purpose. It was set to false here to # persist the power state, which it never did: BlueZ has no such behaviour, so # all it bought was Bluetooth coming up off on every boot. omarchy-bluetooth-power diff --git a/install/hardware/fix-fkeys.sh b/install/hardware/fix-fkeys.sh index ae7fee52bc97..93b02de91ac9 100644 --- a/install/hardware/fix-fkeys.sh +++ b/install/hardware/fix-fkeys.sh @@ -1,5 +1,7 @@ -# Ensure that F-keys on Apple-like keyboards (such as Lofree Flow84) are always F-keys -if [[ ! -f /etc/modprobe.d/hid_apple.conf ]]; then - sudo mkdir -p /etc/modprobe.d - echo "options hid_apple fnmode=2" | sudo tee /etc/modprobe.d/hid_apple.conf >/dev/null +# Ensure that F-keys on Apple-like keyboards (such as Lofree Flow84) are always F-keys. +# Apple Silicon is left alone: the omarchy-mac package owns its keyboard mode. +conf="${OMARCHY_HID_APPLE_CONF:-/etc/modprobe.d/hid_apple.conf}" +if [[ ! -f $conf ]] && ! omarchy-hw-apple-silicon; then + sudo mkdir -p "$(dirname "$conf")" + echo "options hid_apple fnmode=2" | sudo tee "$conf" >/dev/null fi diff --git a/install/hardware/platform-setup.sh b/install/hardware/platform-setup.sh new file mode 100644 index 000000000000..946df9ea5a17 --- /dev/null +++ b/install/hardware/platform-setup.sh @@ -0,0 +1,13 @@ +# The platform packages' own setup, last so it builds on every other leaf +# (omarchy-lifecycle-dispatch setup-boot and setup-system, see +# docs/lifecycle-dispatch.md): the boot package's first, then the runtime +# package's. A no-op where the platform registers none. An image's first boot +# says so: there the network may be down, and a boot-file rebuild is asked for +# rather than built, since the deferred setup rebuilds once after its last step. +if [[ ${OMARCHY_IMAGE_DEFERRED_HARDWARE:-} == "1" ]]; then + omarchy-lifecycle-dispatch setup-boot image-first-boot + omarchy-lifecycle-dispatch setup-system image-first-boot +else + omarchy-lifecycle-dispatch setup-boot + omarchy-lifecycle-dispatch setup-system +fi diff --git a/install/helpers/image-target.sh b/install/helpers/image-target.sh new file mode 100644 index 000000000000..b3c68176383d --- /dev/null +++ b/install/helpers/image-target.sh @@ -0,0 +1,157 @@ +# Image builds and the hardware setup they defer to the machine's first boot. +# Sourced by omarchy-apply-hardware and omarchy-provision-hardware; no shebang. +# +# An image is built away from the machine it will run on, so hardware setup +# during the build would describe the builder, not the target. The builder +# declares the build in a root-owned manifest before it runs +# omarchy-apply-system in the image root: +# +# /var/lib/omarchy/image/target +# format=1 +# platform=apple-silicon|qualcomm|generic-aarch64|generic +# +# While it exists, omarchy-apply-hardware queues each hardware leaf in +# /var/lib/omarchy/image/deferred-steps instead of running it, and arms +# omarchy-provision-hardware.service. On the machine's first boot that service, +# or the platform's own first boot, retires the manifest (to target.booted) and +# runs the queue on the real hardware. Unknown manifest keys are ignored so the +# format can grow. +# +# An image carries no pacman master key of its own, or every machine flashed +# from it would share one: where install finalization would make the keyring +# (install/post-install/pacman.sh), a build requests it in +# /var/lib/omarchy/image/pacman-keyring instead, and the first boot makes it +# before any deferred step runs. +# +# The manifest is also the only source of the image's platform: until that boot, +# omarchy-hw-platform reports its platform instead of the build host's, so the +# initramfs, services and packages the build sets up are the target's. It reads +# the manifest by the same rules as omarchy_image_read_manifest below. +# +# Root always uses the fixed paths. Only a non-root test may move them under +# OMARCHY_IMAGE_ROOT, so no environment variable can make a live system defer, +# skip or replay its hardware setup. + +omarchy_image_init() { + if (( EUID == 0 )); then + omarchy_image_root="" + elif [[ -n ${OMARCHY_IMAGE_ROOT:-} ]]; then + omarchy_image_root=$OMARCHY_IMAGE_ROOT + else + echo "Error: image setup state is root-owned; run as root" >&2 + return 1 + fi + + omarchy_image_dir=$omarchy_image_root/var/lib/omarchy/image + omarchy_image_manifest=$omarchy_image_dir/target + omarchy_image_queue=$omarchy_image_dir/deferred-steps + omarchy_image_initramfs_baseline=$omarchy_image_dir/initramfs-inputs + omarchy_image_boot_rebuild=$omarchy_image_dir/boot-rebuild + omarchy_image_keyring_request=$omarchy_image_dir/pacman-keyring + omarchy_image_unit=omarchy-provision-hardware.service + omarchy_image_systemd_dir=$omarchy_image_root/etc/systemd/system +} + +# Owned by whoever runs this (root on a live system), not a symlink, and not +# writable by anyone else. +omarchy_image_trusted() { + local path=$1 owner mode + + [[ -e $path && ! -L $path ]] || return 1 + read -r owner mode < <(stat -c '%u %a' -- "$path") || return 1 + (( owner == EUID && (8#$mode & 8#022) == 0 )) +} + +omarchy_image_manifest_present() { + [[ -e $omarchy_image_manifest || -L $omarchy_image_manifest ]] +} + +# Sets omarchy_image_platform, or fails without guessing whether this is a build. +omarchy_image_read_manifest() { + local line key value format="" platform="" + + if [[ ! -d $omarchy_image_dir || ! -f $omarchy_image_manifest ]] || ! omarchy_image_trusted "$omarchy_image_dir" || + ! omarchy_image_trusted "$omarchy_image_manifest"; then + echo "Error: $omarchy_image_manifest is not a root-owned regular file" >&2 + return 1 + fi + + while IFS= read -r line || [[ -n $line ]]; do + [[ -n $line && $line != \#* ]] || continue + if [[ $line != *=* ]]; then + echo "Error: $omarchy_image_manifest is malformed: $line" >&2 + return 1 + fi + key=${line%%=*} + value=${line#*=} + case $key in + format) format=$value ;; + platform) platform=$value ;; + esac + done <"$omarchy_image_manifest" + + if [[ $format != "1" ]]; then + echo "Error: $omarchy_image_manifest is not format=1" >&2 + return 1 + fi + case $platform in + apple-silicon | qualcomm | generic-aarch64 | generic) ;; + *) + echo "Error: $omarchy_image_manifest names no known platform: ${platform:-none}" >&2 + return 1 + ;; + esac + + omarchy_image_platform=$platform +} + +# Holds the image state still for the rest of the caller, so hardware setup and +# a first-boot run never overlap or decide from a manifest the other retires. +# A machine that was never an image has no state and nothing to lock. +omarchy_image_lock() { + [[ -e $omarchy_image_dir || -L $omarchy_image_dir ]] || return 0 + if [[ ! -d $omarchy_image_dir ]] || ! omarchy_image_trusted "$omarchy_image_dir"; then + echo "Error: $omarchy_image_dir is not a root-owned directory" >&2 + return 1 + fi + exec {omarchy_image_lock_fd}>>"$omarchy_image_dir/lock" || return 1 + flock "$omarchy_image_lock_fd" +} + +omarchy_image_write_queue() { + local tmp + + tmp=$(mktemp "$omarchy_image_dir/.deferred-steps.XXXXXX") || return 1 + if (( $# )); then + printf '%s\n' "$@" >"$tmp" || { rm -f "$tmp"; return 1; } + fi + chmod 0644 "$tmp" && mv -f "$tmp" "$omarchy_image_queue" +} + +# Queue every leaf install/hardware/all.sh would run, in its order, running +# none of them, then arm the first-boot service. Rerunning rewrites the same queue. +omarchy_image_defer_hardware() { + local listing + local -a steps=() + + listing=$( + run_logged() { + [[ $1 == "$OMARCHY_INSTALL"/hardware/*.sh && $1 != *..* ]] || { + echo "Error: cannot defer a step outside the hardware setup: $1" >&2 + exit 1 + } + printf 'install/%s\n' "${1#"$OMARCHY_INSTALL"/}" + } + source "$OMARCHY_INSTALL/hardware/all.sh" + ) || return 1 + + [[ -z $listing ]] || mapfile -t steps <<<"$listing" + omarchy_image_write_queue "${steps[@]}" || return 1 + + install -d -m 0755 "$omarchy_image_systemd_dir/multi-user.target.wants" && + install -m 0644 "$OMARCHY_INSTALL/provisioning/$omarchy_image_unit" "$omarchy_image_systemd_dir/$omarchy_image_unit" && + ln -sfn "/etc/systemd/system/$omarchy_image_unit" "$omarchy_image_systemd_dir/multi-user.target.wants/$omarchy_image_unit" || + return 1 + + echo "Image build for $omarchy_image_platform: deferred ${#steps[@]} hardware steps to first boot" +} diff --git a/install/helpers/pacman.sh b/install/helpers/pacman.sh new file mode 100644 index 000000000000..5fb4d3fe7842 --- /dev/null +++ b/install/helpers/pacman.sh @@ -0,0 +1,33 @@ +# Pacman repository templates. Every platform has a pacman.conf and mirrorlist +# for each channel it offers, copied into place whole, as x86_64's always were: +# x86_64's in default/pacman, each ARM platform's in a directory of its own. Sourcing this +# file only defines functions; it never changes the system. + +# The directory holding 's pacman-.conf and +# mirrorlist-. A channel without both files isn't offered there. +omarchy_pacman_templates() { + case ${1:-} in + generic) echo "$OMARCHY_PATH/default/pacman" ;; + qualcomm | generic-aarch64) echo "$OMARCHY_PATH/default/pacman/aarch64" ;; + apple-silicon) echo "$OMARCHY_PATH/default/pacman/apple-silicon" ;; + *) + echo "Error: Unknown platform '${1:-}'." >&2 + return 1 + ;; + esac +} + +# The channel a machine takes when none is named: stable, except on aarch64. +# Omarchy publishes aarch64 packages on edge alone so far, and the stable and rc +# packages there are the release line, which has no aarch64 support, so ARM +# platforms have edge templates only until a release does. +omarchy_pacman_default_channel() { + case ${1:-} in + generic) echo stable ;; + qualcomm | generic-aarch64 | apple-silicon) echo edge ;; + *) + echo "Error: Unknown platform '${1:-}'." >&2 + return 1 + ;; + esac +} diff --git a/install/omarchy-aarch64.packages b/install/omarchy-aarch64.packages new file mode 100644 index 000000000000..fa7dcab5c51c --- /dev/null +++ b/install/omarchy-aarch64.packages @@ -0,0 +1,4 @@ +# Additions for every aarch64 platform, composed after omarchy-base.packages +# (omarchy-pkg-defaults). omarchy-settings configures swap on zram, and the +# x86_64 installer provides zram-generator for it; aarch64 gets it here. +zram-generator diff --git a/install/omarchy-apple-silicon.packages b/install/omarchy-apple-silicon.packages new file mode 100644 index 000000000000..6f0af9d3a449 --- /dev/null +++ b/install/omarchy-apple-silicon.packages @@ -0,0 +1,26 @@ +# Apple Silicon additions, composed after omarchy-base.packages and +# omarchy-aarch64.packages (omarchy-pkg-defaults apple-silicon); never on Intel +# Macs or other aarch64 machines. The kernel and boot chain come with the +# Mac's image, as the kernel and bootloader do from the ISO on x86_64. What a +# Mac cannot work without (its speaker stack, audio server and GPU driver) +# comes as omarchy-mac's dependencies, so a new Mac driver needs no change +# here; the rest are defaults an owner may remove. + +# The Mac's runtime and boot packages (omacom/omarchy-mac-pkgs). +omarchy-mac +omarchy-mac-boot + +# Picks the startup volume (macOS or Omarchy) from Linux. +asahi-bless + +# Hardware video decode. +avd-fw +libva-v4l2_request-avd + +# Widevine for DRM streaming. +widevine + +# Screen recording: gpu-screen-recorder finds no display device to capture on +# Apple Silicon, whose display and GPU are separate DRM devices, so recording +# falls back to wf-recorder (omarchy-capture-screenrecording). +wf-recorder diff --git a/install/omarchy-qualcomm.packages b/install/omarchy-qualcomm.packages new file mode 100644 index 000000000000..735a5bb93b52 --- /dev/null +++ b/install/omarchy-qualcomm.packages @@ -0,0 +1,3 @@ +# Qualcomm (Snapdragon) additions, composed after omarchy-base.packages and +# omarchy-aarch64.packages (omarchy-pkg-defaults qualcomm). +linux-firmware-qcom diff --git a/install/post-install/pacman.sh b/install/post-install/pacman.sh index da84975b3ee1..ee4ffa9c8279 100644 --- a/install/post-install/pacman.sh +++ b/install/post-install/pacman.sh @@ -1,7 +1,35 @@ # Configure pacman after package installation completes. Offline target package -# installs use the live ISO's offline pacman.conf until this final restore. -cp -f "$OMARCHY_PATH/default/pacman/pacman-${OMARCHY_MIRROR:-stable}.conf" /etc/pacman.conf -cp -f "$OMARCHY_PATH/default/pacman/mirrorlist-${OMARCHY_MIRROR:-stable}" /etc/pacman.d/mirrorlist +# installs use the live ISO's offline pacman.conf until this final restore, +# which copies the platform's channel templates (install/helpers/pacman.sh). A +# Mac's image brings its keyrings, as the ISO does on x86_64; other aarch64 +# platforms install Arch Linux ARM's before its repositories replace the +# offline ones, and trust every installed keyring before the first signed sync. +source "$OMARCHY_PATH/install/helpers/pacman.sh" +source "$OMARCHY_PATH/install/helpers/image-target.sh" +platform=$(omarchy-hw-platform) +templates=$(omarchy_pacman_templates "$platform") + +if [[ $platform == "qualcomm" || $platform == "generic-aarch64" ]]; then + omarchy-pkg-add archlinuxarm-keyring +fi + +# An install built for a channel this platform has no templates for (stable or +# rc on aarch64) gets the platform's default channel instead. +channel=${OMARCHY_MIRROR:-} +[[ -n $channel && -f $templates/pacman-$channel.conf ]] || channel=$(omarchy_pacman_default_channel "$platform") +cp -f "$templates/pacman-$channel.conf" /etc/pacman.conf +cp -f "$templates/mirrorlist-$channel" /etc/pacman.d/mirrorlist + +# An image build leaves the keyring to each machine's first boot, so no two +# share a master key (install/helpers/image-target.sh). +if [[ $platform == "qualcomm" || $platform == "generic-aarch64" ]]; then + if omarchy_image_init && omarchy_image_manifest_present; then + install -m 0644 /dev/null "$omarchy_image_keyring_request" + else + pacman-key --init + pacman-key --populate + fi +fi # Wait for CUPS to own the file, the way omarchy-settings does, so pacman does # not turn the override into a .pacnew during ISO package installation. diff --git a/install/provisioning/luks-rekey.sh b/install/provisioning/luks-rekey.sh new file mode 100644 index 000000000000..ab7194fc76ed --- /dev/null +++ b/install/provisioning/luks-rekey.sh @@ -0,0 +1,290 @@ +# Resumable re-key of the root LUKS volume from the staged install key to the +# owner's password. Sourcing performs no setup. +# +# Caller contract: +# - Set PROVISIONING_DIR (holds the staged luks-key), REKEY_STATE, LOG_FILE and +# password. Secrets reach cryptsetup through key files and process +# substitution, never argv, and the entry points turn tracing off. +# - Define luks_boot_layout, printing a fingerprint of the keyboard layout the +# boot files ask for the disk password in (the same value while it is +# unchanged, and never the layout's name). +# - Define log_step and say, plus the platform's boot-time unlock: +# luks_auto_unlock_present succeeds while any boot-time copy of the staged +# key or its unlock configuration remains; luks_auto_unlock_drop removes +# them and rebuilds the boot files, restoring the unlock before it fails. +# luks_record_slots records the kept slot wherever the platform's +# boot checks look for it, and succeeds where nothing does. +# +# The journal (REKEY_STATE) records only the phase, slot numbers and the layout +# fingerprint, never key material. Phases advance staged → owner → boot → done, each written durably +# after its step, so an attempt interrupted anywhere resumes from the last one; +# the retire and record steps before done are idempotent. A retry may still +# choose a new password while the staged key opens the disk, even after the +# boot step, so the kept slot is recorded only once it is final. The +# caller removes the journal with the rest of its provisioning state, so until +# then a retry can only use the password the disk holds. + +# cryptsetup open tries enrolled tokens (TPM2, FIDO2, keyring) before the key +# and reports a token's slot whatever key it was given. Restricting it to a +# token type no token has leaves only the key to decide. +luks_key_slot() { + local out + out=$(LC_ALL=C cryptsetup open --test-passphrase --verbose --token-type passphrase-only --key-file "$1" "$2" 2>&1) || return 0 + grep -o 'Key slot [0-9]* unlocked' <<<"$out" | grep -o '[0-9]*' | head -1 || true +} + +luks_slot_for() { + local - + set +x + luks_key_slot <(printf '%s' "$1") "$2" +} + +# LUKS2 lists keyslots under "Keyslots:", and a luks2-keyring token under +# "Tokens:" looks the same, so read only the keyslot section. +luks_dump_slots() { + cryptsetup luksDump "$1" | awk ' + /^[^ \t]/ { keyslots = ($0 == "Keyslots:") } + keyslots && /^ +[0-9]+: luks2/ { sub(":", "", $1); print $1 } + /^Key Slot [0-9]+: ENABLED/ { sub(":", "", $3); print $3 }' +} + +rekey_state_get() { + [[ -f $REKEY_STATE ]] || return 1 + awk -F= -v k="$1" '$1 == k { print $2; exit }' "$REKEY_STATE" +} + +# Set key/value pairs, keeping every other recorded key. +rekey_state_put() { + local line key tmp + local -A updates=() + local -a lines=() + + while (( $# >= 2 )); do + updates[$1]=$2 + shift 2 + done + + if [[ -f $REKEY_STATE ]]; then + while IFS= read -r line || [[ -n $line ]]; do + if [[ -n $line && -z ${updates[${line%%=*}]+set} ]]; then + lines+=("$line") + fi + done <"$REKEY_STATE" + fi + for key in "${!updates[@]}"; do + lines+=("$key=${updates[$key]}") + done + + tmp=$(mktemp "$REKEY_STATE.XXXXXX") || return 1 + if printf '%s\n' "${lines[@]}" >"$tmp" && chmod 600 "$tmp" && sync "$tmp" && mv -f "$tmp" "$REKEY_STATE"; then + sync "$(dirname "$REKEY_STATE")" + return + fi + rm -f "$tmp" + return 1 +} + +staged_key_slot() { + [[ -f $PROVISIONING_DIR/luks-key ]] || return 0 + luks_key_slot "$PROVISIONING_DIR/luks-key" "$1" +} + +# The staged key or its boot-time auto-unlock is still on this machine. +luks_staged_unlock_remains() { + [[ -e $PROVISIONING_DIR/luks-key ]] || luks_auto_unlock_present +} + +# Re-key work is due while the staged unlock remains or the journal is open. +luks_rekey_pending() { + [[ -e $REKEY_STATE ]] || luks_staged_unlock_remains +} + +# Whether an interrupted re-key can finish with $password. Until the staged key +# is retired it can add any password; afterwards only one the disk holds. +luks_rekey_accepts_password() { + local - + set +x + local device=$1 + + [[ -f $REKEY_STATE ]] || return 0 + [[ -n $(luks_slot_for "$password" "$device") || -n $(staged_key_slot "$device") ]] +} + +# Record the slot $password opens, adding it with the staged key when none does. +# A retry after a crash between the add and the journal write finds the slot +# instead of adding a duplicate, and a retry with a new password adds its own +# while the staged key still works; the retire step removes the stale one. +luks_rekey_owner() { + local device=$1 phase=$2 owner + + owner=$(luks_slot_for "$password" "$device") + if [[ -z $owner ]]; then + if [[ -z $(staged_key_slot "$device") ]]; then + log_step "the password opens no LUKS slot and the staged key no longer unlocks $device" + say --foreground 1 "Use the disk password chosen earlier in setup." + return 1 + fi + if ! cryptsetup luksAddKey --key-file "$PROVISIONING_DIR/luks-key" "$device" <(printf '%s' "$password"); then + log_step "could not add the owner's key to $device" + say --foreground 1 "Could not add your password to the disk; will retry." + return 1 + fi + owner=$(luks_slot_for "$password" "$device") + if [[ -z $owner ]]; then + log_step "could not identify the owner's LUKS slot after adding it" + say --foreground 1 "Could not confirm the LUKS re-key; will retry." + return 1 + fi + fi + + if [[ $owner == "$(rekey_state_get staged_slot || true)" ]]; then + log_step "the owner's password is the staged install key; refusing to keep it" + say --foreground 1 "Choose a disk password different from the temporary install key." + return 1 + fi + + if [[ $phase == "staged" ]]; then + rekey_state_put owner_slot "$owner" phase owner + else + rekey_state_put owner_slot "$owner" + fi +} + +luks_rekey_retire() { + local device=$1 owner slot slots + + owner=$(rekey_state_get owner_slot || true) + if [[ -z $owner ]]; then + log_step "no owner slot is recorded; refusing to retire LUKS slots" + return 1 + fi + + if ! slots=$(luks_dump_slots "$device"); then + log_step "luksDump failed while retiring slots; keeping the staged key for retry" + say --foreground 1 "Could not enumerate LUKS slots; will retry." + return 1 + fi + for slot in $slots; do + [[ $slot == "$owner" ]] && continue + if ! cryptsetup luksKillSlot -q --key-file <(printf '%s' "$password") "$device" "$slot"; then + log_step "failed to kill LUKS slot $slot; keeping the staged key for retry" + say --foreground 1 "Could not remove the throwaway LUKS key; will retry." + return 1 + fi + done +} + +# The staged key must open nothing before it is destroyed: only the owner slot +# remains and no boot-time copy or unlock configuration is left behind. +luks_rekey_verify() { + local device=$1 owner slots + + owner=$(rekey_state_get owner_slot || true) + if ! slots=$(luks_dump_slots "$device") || [[ -z $owner || $slots != "$owner" ]]; then + log_step "LUKS slots other than the owner's remain on $device" + return 1 + fi + if [[ -n $(staged_key_slot "$device") ]]; then + log_step "the staged LUKS key still unlocks $device" + return 1 + fi + if luks_auto_unlock_present; then + log_step "the boot-time auto-unlock is still configured" + return 1 + fi +} + +# The boot files carry the console keyboard layout the disk password is typed +# in at boot, so rebuild them again when the owner chose another layout since +# they were built, or when an older journal recorded none. The phase stays. +luks_rekey_boot_layout() { + local layout=$1 + + [[ $(rekey_state_get boot_layout || true) != "$layout" ]] || return 0 + log_step "the boot files were not built with the current keyboard layout; rebuilding them" + if ! luks_auto_unlock_drop; then + say --foreground 1 "Could not rebuild the boot files for your keyboard layout; will retry." + return 1 + fi + sync + rekey_state_put boot_layout "$layout" +} + +# Order: record the staged slot, add the owner's key, rebuild boot without the +# auto-unlock (keeping the staged slot as the fallback while that can fail), +# retire every other slot, then verify, record the kept slot for the platform, +# destroy the staged key and record done. +# Failing is loud: silently keeping the staged key would leave the disk +# effectively unencrypted. +luks_rekey() { + local - + set +x + local device=$1 phase staged layout + + if ! layout=$(luks_boot_layout) || [[ -z $layout ]]; then + log_step "could not fingerprint the keyboard layout for the boot files" + say --foreground 1 "Could not read the keyboard layout for the boot files; will retry." + return 1 + fi + + phase=$(rekey_state_get phase || true) + case $phase in + "") + staged=$(staged_key_slot "$device") + if [[ -z $staged ]]; then + log_step "the staged LUKS key is missing or does not unlock $device" + say --foreground 1 "The staged LUKS key no longer unlocks $device." + return 1 + fi + rekey_state_put staged_slot "$staged" phase staged || return 1 + phase=staged + ;; + staged | owner | boot | done) ;; + *) + log_step "unknown LUKS re-key phase '$phase' in $REKEY_STATE" + say --foreground 1 "The LUKS re-key journal is unreadable." + return 1 + ;; + esac + + if [[ $phase != "done" ]]; then + luks_rekey_owner "$device" "$phase" || return 1 + [[ $phase == "staged" ]] && phase=owner + + if [[ $phase == "owner" ]] || luks_auto_unlock_present; then + if ! luks_auto_unlock_drop; then + say --foreground 1 "Could not rebuild the boot files without the install key; will retry." + return 1 + fi + sync + rekey_state_put phase boot boot_layout "$layout" || return 1 + else + luks_rekey_boot_layout "$layout" || return 1 + fi + + luks_rekey_retire "$device" || return 1 + else + if [[ $(luks_slot_for "$password" "$device") != "$(rekey_state_get owner_slot || true)" ]]; then + log_step "the password does not open the owner slot the finished re-key recorded on $device" + say --foreground 1 "Use the disk password chosen earlier in setup." + return 1 + fi + luks_rekey_boot_layout "$layout" || return 1 + fi + + if ! luks_rekey_verify "$device"; then + say --foreground 1 "Could not confirm the temporary install key was removed; will retry." + return 1 + fi + if ! luks_record_slots "$(rekey_state_get owner_slot)"; then + log_step "the platform could not record the kept LUKS slot" + say --foreground 1 "Could not record the disk's key slot for the boot checks; will retry." + return 1 + fi + + if [[ -e $PROVISIONING_DIR/luks-key ]]; then + shred -u "$PROVISIONING_DIR/luks-key" 2>/dev/null || rm -f "$PROVISIONING_DIR/luks-key" || return 1 + fi + rm -f "$REKEY_STATE".?????? + [[ $phase == "done" ]] || rekey_state_put phase done +} diff --git a/install/provisioning/omarchy-provision-hardware.service b/install/provisioning/omarchy-provision-hardware.service new file mode 100644 index 000000000000..a6638c8decc9 --- /dev/null +++ b/install/provisioning/omarchy-provision-hardware.service @@ -0,0 +1,21 @@ +# Runs the hardware setup an image build deferred, on the machine it was built +# for, before owner setup and the login screen. Armed with its queue by +# omarchy-apply-hardware during the build (install/helpers/image-target.sh); +# retried on every boot until the queue is empty and any keyring an image build +# asked for exists, then it disables itself (the unit file stays; with neither +# file left it no longer runs). +[Unit] +Description=Omarchy first-boot hardware setup deferred from the image build +ConditionPathExists=|/var/lib/omarchy/image/deferred-steps +ConditionPathExists=|/var/lib/omarchy/image/pacman-keyring +Before=omarchy-provision-owner.service display-manager.service + +[Service] +Type=oneshot +ExecStart=/usr/bin/omarchy-provision-hardware +TimeoutStartSec=infinity +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=multi-user.target diff --git a/install/user/all.sh b/install/user/all.sh index 39d98ea57f44..3c0e2ca11453 100644 --- a/install/user/all.sh +++ b/install/user/all.sh @@ -12,3 +12,5 @@ run_logged "$OMARCHY_INSTALL/user/hardware/fix-nouveau-cursor.sh" run_logged "$OMARCHY_INSTALL/user/default-keyring.sh" run_logged "$OMARCHY_INSTALL/user/mise.sh" + +run_logged "$OMARCHY_INSTALL/user/platform-setup.sh" diff --git a/install/user/first-run/enable-user-units.sh b/install/user/first-run/enable-user-units.sh index e08885c9e0dc..c8e96da81388 100755 --- a/install/user/first-run/enable-user-units.sh +++ b/install/user/first-run/enable-user-units.sh @@ -19,6 +19,7 @@ systemctl --user enable --now \ omarchy-sleep-lock.service \ omarchy-migrate-notify.service \ omarchy-fcitx5.service \ - omarchy-crash-watch.service + omarchy-crash-watch.service \ + omarchy-brightness-keyboard-auto.service omarchy-hook-install theme-set /usr/share/owe/10-owe-sync diff --git a/install/user/platform-setup.sh b/install/user/platform-setup.sh new file mode 100644 index 000000000000..a83f3b01d6c0 --- /dev/null +++ b/install/user/platform-setup.sh @@ -0,0 +1,10 @@ +# The platform package's own setup for this user, last so it builds on every +# other leaf (omarchy-lifecycle-dispatch setup-user, see +# docs/lifecycle-dispatch.md). A no-op where the platform registers none. In an +# image build the detector would read the build host, so it waits for first run +# on the machine, which runs it again. +if [[ -e ${OMARCHY_IMAGE_ROOT:-}/var/lib/omarchy/image/target ]]; then + echo "Image build: the platform's user setup waits for first run on the machine" +else + omarchy-lifecycle-dispatch setup-user +fi diff --git a/manual/34-keyboard-mouse-trackpad.md b/manual/34-keyboard-mouse-trackpad.md index 648e7713d8d1..c0ff0182ddf2 100644 --- a/manual/34-keyboard-mouse-trackpad.md +++ b/manual/34-keyboard-mouse-trackpad.md @@ -57,6 +57,10 @@ hl.gesture({ fingers = 3, direction = "horizontal", action = "workspace" }) On Dell XPS laptops with a haptic touchpad, you can also set the click strength to low, mid, or high under _Trigger > Hardware > Touchpad Haptics_. +### Keyboard backlight + +Laptops with a keyboard backlight LED and an ambient light sensor light the keys in the dark and dim them as the room brightens. Dedicated keyboard-brightness keys still set the level by hand; automatic control resumes when the ambient light changes enough that the old choice no longer fits. Automatic control pauses while the screen is locked or the lid is closed. Keys turned off with the brightness keys or a firmware key (such as Fn + Space) stay off the same way; keys left off by the lock screen light up again with the room. + ### Typing in Chinese, Japanese, and other languages Omarchy runs the [fcitx5](https://fcitx-im.org/) input method framework as part of every session — it's what powers the CapsLock compose sequences. That means the plumbing for non-Latin input is already in place: install an input engine like `fcitx5-mozc` (Japanese) or `fcitx5-chinese-addons` (Chinese) with `omarchy pkg add`, plus `fcitx5-configtool` to add the engine to your input methods and set the key that switches between them. diff --git a/manual/48-security.md b/manual/48-security.md index 6db0dff5f17c..4640c37e9696 100644 --- a/manual/48-security.md +++ b/manual/48-security.md @@ -10,7 +10,7 @@ Omarchy takes security extremely seriously. This is meant to be an operating sys ## Changing your passwords -You have two passwords on an encrypted install: the one that unlocks the drive at boot, and the one you log in and `sudo` with. Both can be changed under _Update > Password_ in the Omarchy menu — _Drive Encryption_ for the first, _User_ for the second. Changing the drive password asks for the current one first, so have it handy. +You have two passwords on an encrypted install: the one that unlocks the drive at boot, and the one you log in and `sudo` with. Setup makes them the same. Both can be changed under _Update > Password_ in the Omarchy menu. _Drive Encryption_ changes a drive's password. For the drive Omarchy runs from, it then sets your login and root passwords to match, but only once the drive has accepted the new one; other encrypted drives change on their own. _User_ changes only the login password. Changing the drive password asks for the current one first, so have it handy. If a change is interrupted, run _Drive Encryption_ again: it asks for the password that unlocks the drive now and finishes the change. ## Passing on a machine you've already used diff --git a/migrations/1786391100.sh b/migrations/1786391100.sh index b67ecf820896..989333224906 100644 --- a/migrations/1786391100.sh +++ b/migrations/1786391100.sh @@ -1,5 +1,12 @@ echo "Run the WPA handshake in software on Macs with Broadcom Wi-Fi" +# This quirk targets Intel Macs. Apple Silicon uses brcmfmac too and reports +# "Apple Inc." with a listed part (BCM4387 is 14e4:4433), but disabling its +# firmware supplicant makes firmware commands time out and breaks scanning +# entirely. A fresh install marks this done only for its first user, so an +# account added later still runs it. +omarchy-hw-apple-silicon && exit 0 + # The install-time quirk only reaches machines set up after it shipped, and it # never covered Macs without a T2 at all, so an existing install on one still # cannot join a WPA2/WPA3 transition-mode network. See diff --git a/shell/Commons/AudioNodes.qml b/shell/Commons/AudioNodes.qml new file mode 100644 index 000000000000..448cfe555639 --- /dev/null +++ b/shell/Commons/AudioNodes.qml @@ -0,0 +1,41 @@ +pragma Singleton +import QtQuick +import Quickshell +import Quickshell.Io +import "AudioNodesModel.js" as Model + +// Which PipeWire nodes the audio panel and the microphone widget leave out: +// the shell's own level meters, and the nodes a platform's audio processing +// adds, which its platform package names in the fixed platform root (see +// Model.parsePlatformAudio). No environment variable moves that file, and most +// machines have none, so nothing of theirs is left out. +Singleton { + id: root + + readonly property var hints: Model.parsePlatformAudio(hintsFile.missing ? "" : hintsFile.text()) + + // Whether the platform's hints hide a node, given the names of every node + // now in the graph (for a "replaced" node). + function platformHides(name, nodeNames) { + return Model.platformHidesNode(name, hints, nodeNames) + } + + function isShellLevelMeter(name) { + return Model.isShellLevelMeter(name) + } + + FileView { + id: hintsFile + + // A file that went away hints nothing, whatever text() still holds. + property bool missing: false + + path: "/usr/share/omarchy-platform/audio.json" + blockLoading: true + watchChanges: true + printErrors: false + onLoaded: missing = false + onLoadFailed: missing = true + onFileChanged: reload() + } +} diff --git a/shell/Commons/AudioNodesModel.js b/shell/Commons/AudioNodesModel.js new file mode 100644 index 000000000000..e38bb49352dc --- /dev/null +++ b/shell/Commons/AudioNodesModel.js @@ -0,0 +1,97 @@ +// Audio node rules the bar and the audio panel share. Plain functions, so the +// tests load this file in Node. + +// The level meters the shell runs itself: Quickshell's peak monitor stream +// (named after the shell or after its stream, depending on the PipeWire +// version) and omarchy-audio-source-level's recording. None is an app using +// the input. +function isShellLevelMeter(name) { + var value = String(name || "") + return value === "quickshell" || value === "quickshell-peak-monitor" || value === "omarchy-audio-level" +} + +function wholeNamePattern(pattern) { + if (typeof pattern !== "string" || pattern === "") return null + try { + return new RegExp("^(?:" + pattern + ")$") + } catch (e) { + return null + } +} + +// A platform's audio hints, which its own package ships in the platform root +// (/usr/share/omarchy-platform/audio.json, read by AudioNodes.qml; Omarchy +// ships none): nodes of its audio processing that are neither devices nor apps. +// +// { "hidden": ["", ...], +// "replaced": [{ "node": "", "by": "" }, ...] } +// +// A pattern is a regular expression matched against a whole node.name. A +// hidden node is never listed as an output, an input or an app stream, and +// never counts as a recording; a replaced node is hidden while a node matching +// "by" exists. Missing or malformed text means no hints, and an invalid pattern +// or entry is skipped. +function parsePlatformAudio(text) { + var hints = { hidden: [], replaced: [] } + var data = null + try { + data = JSON.parse(String(text || "")) + } catch (e) { + return hints + } + if (!data || typeof data !== "object" || Array.isArray(data)) return hints + + var hidden = Array.isArray(data.hidden) ? data.hidden : [] + for (var i = 0; i < hidden.length; i++) { + var pattern = wholeNamePattern(hidden[i]) + if (pattern) hints.hidden.push(pattern) + } + + var replaced = Array.isArray(data.replaced) ? data.replaced : [] + for (var j = 0; j < replaced.length; j++) { + var entry = replaced[j] + if (!entry || typeof entry !== "object") continue + var node = wholeNamePattern(entry.node) + var by = wholeNamePattern(entry.by) + if (node && by) hints.replaced.push({ node: node, by: by }) + } + return hints +} + +// Whether the platform's hints hide a node, given the names of every node now +// in the graph (for "replaced"). +function platformHidesNode(name, hints, nodeNames) { + var value = String(name || "") + if (value === "" || !hints) return false + var hidden = hints.hidden || [] + for (var i = 0; i < hidden.length; i++) + if (hidden[i].test(value)) return true + + var replaced = hints.replaced || [] + var names = Array.isArray(nodeNames) ? nodeNames : [] + for (var j = 0; j < replaced.length; j++) { + if (!replaced[j].node.test(value)) continue + for (var k = 0; k < names.length; k++) { + var other = String(names[k] || "") + if (other !== value && replaced[j].by.test(other)) return true + } + } + return false +} + +// `wpctl get-volume ` prints "Volume: 0.50", with " [MUTED]" when muted, on +// the same cubic scale as PwNodeAudio.volume. +function parseWpctlVolume(text) { + var match = /^Volume:\s+([0-9]+(?:\.[0-9]+)?)(\s+\[MUTED\])?$/.exec(String(text || "").trim()) + if (!match) return null + return { volume: parseFloat(match[1]), muted: !!match[2] } +} + +if (typeof module !== "undefined") { + module.exports = { + isShellLevelMeter: isShellLevelMeter, + parsePlatformAudio: parsePlatformAudio, + platformHidesNode: platformHidesNode, + parseWpctlVolume: parseWpctlVolume + } +} diff --git a/shell/Commons/Style.qml b/shell/Commons/Style.qml index ec61252b7626..e5e219e9ddbd 100644 --- a/shell/Commons/Style.qml +++ b/shell/Commons/Style.qml @@ -341,6 +341,13 @@ QtObject { readonly property QtObject bar: QtObject { readonly property int sizeHorizontal: root.barToken("size-horizontal", 26) readonly property int sizeVertical: root.barToken("size-vertical", 28) + // Calibrated notch cutout height in logical px, for panels whose real + // cutout differs from the derived default (see BarModel.notchHeight). + // 0 means derive it. Physical alignment, so it never scales with font. + readonly property int notchHeight: { + var v = Number(root.barOverrides["notch-height"]) + return (isFinite(v) && v > 0) ? Math.round(v) : 0 + } readonly property int iconSlot: root.barToken("icon-slot", 27) readonly property int iconCanvas: root.barToken("icon-canvas", 16) readonly property int iconFont: root.barToken("icon-font", 13) @@ -405,7 +412,7 @@ QtObject { } else if (section === "bar") { if (key === "scale-with-font") { nextBarScaleWithFont = boolToken(raw, nextBarScaleWithFont) - } else if (key === "size-horizontal" || key === "size-vertical") { + } else if (key === "size-horizontal" || key === "size-vertical" || key === "notch-height") { var b = parseInt(raw, 10) if (isFinite(b)) barOut[key] = b } diff --git a/shell/Commons/UntypedInput.qml b/shell/Commons/UntypedInput.qml new file mode 100644 index 000000000000..f7dfd9365fd4 --- /dev/null +++ b/shell/Commons/UntypedInput.qml @@ -0,0 +1,152 @@ +pragma Singleton +import QtQuick +import Quickshell +import Quickshell.Io +import Quickshell.Services.Pipewire +import "AudioNodesModel.js" as Model + +// Volume and mute of the default input when Quickshell gives it no +// PwNode.audio: Quickshell types only exact media classes, so a virtual source +// (an Audio/Source/Virtual, such as EasyEffects' or a platform's microphone +// mapping) has no volume or mute there. They go through wpctl instead, once for +// the whole shell. Reads and writes name the node by id, a reply for a node no +// longer tracked is dropped, and writes run one at a time with only the latest +// volume kept, so a slider drag cannot land on another input later. +Singleton { + id: root + + readonly property var source: Pipewire.defaultAudioSource + // The default input is untyped, so this drives it. + readonly property bool active: !!source && !source.audio + // Every handler reads nodeId itself: a property bound to it could still hold + // its old value when onNodeIdChanged runs. + readonly property int nodeId: active ? source.id : -1 + + property bool known: false + property real volume: 0 + property bool muted: false + + property int generation: 0 + property int readGeneration: -1 + property bool readAgain: false + property var pendingVolume: null + property var pendingMute: null + + // pactl subscribe is started again when it ends, unless it keeps ending at + // once (no pipewire-pulse), which would only respawn it forever. + property real subscribedAt: 0 + property int quickExits: 0 + + onNodeIdChanged: reset() + Component.onCompleted: reset() + + function reset() { + generation++ + known = false + pendingVolume = null + pendingMute = null + quickExits = 0 + events.running = nodeId >= 0 + refresh() + } + + function refresh() { + if (nodeId < 0) return + if (reader.running || writer.running) { + readAgain = true + return + } + readGeneration = generation + reader.command = ["wpctl", "get-volume", String(nodeId)] + reader.running = true + } + + function setVolume(value) { + if (nodeId < 0) return + volume = Math.max(0, Math.min(1, value)) + pendingVolume = [String(nodeId), volume.toFixed(2)] + flush() + } + + function setMuted(value) { + if (nodeId < 0 || !known) return + muted = value + pendingMute = [String(nodeId), value ? "1" : "0"] + flush() + } + + function flush() { + if (reader.running || writer.running) return true + var args = null + if (pendingMute !== null) { + args = ["wpctl", "set-mute"].concat(pendingMute) + pendingMute = null + } else if (pendingVolume !== null) { + args = ["wpctl", "set-volume"].concat(pendingVolume) + pendingVolume = null + } + if (!args) return false + writer.command = args + writer.running = true + return true + } + + function settle() { + if (flush()) return + if (readAgain) { + readAgain = false + refresh() + } + } + + Process { + id: reader + stdout: StdioCollector { + waitForEnd: true + onStreamFinished: { + var level = Model.parseWpctlVolume(text) + if (!level || root.nodeId < 0 || root.readGeneration !== root.generation) return + if (root.pendingVolume !== null || root.pendingMute !== null || writer.running) return + root.volume = level.volume + root.muted = level.muted + root.known = true + } + } + onExited: root.settle() + } + + Process { + id: writer + onExited: { + root.readAgain = true + root.settle() + } + } + + // The volume keys, the mute key and other apps change the node too. pactl + // translates its events, so they are read in the C locale. + Process { + id: events + command: ["pactl", "subscribe"] + environment: ({ LC_ALL: "C" }) + onRunningChanged: if (running) root.subscribedAt = Date.now() + onExited: { + root.quickExits = Date.now() - root.subscribedAt < 1000 ? root.quickExits + 1 : 0 + if (root.nodeId >= 0 && root.quickExits < 5) resubscribe.start() + } + stdout: SplitParser { + onRead: function(line) { + if (String(line).indexOf(" on source #") !== -1) root.refresh() + } + } + } + + Timer { + id: resubscribe + interval: 2000 + onTriggered: if (root.nodeId >= 0 && !events.running) { + events.running = true + root.refresh() + } + } +} diff --git a/shell/Commons/qmldir b/shell/Commons/qmldir index 1c9101c12847..660f59013b5a 100644 --- a/shell/Commons/qmldir +++ b/shell/Commons/qmldir @@ -1,7 +1,9 @@ module qs.Commons +singleton AudioNodes 1.0 AudioNodes.qml singleton Border 1.0 Border.qml singleton Color 1.0 Color.qml singleton IpcRegistry 1.0 IpcRegistry.qml singleton Style 1.0 Style.qml +singleton UntypedInput 1.0 UntypedInput.qml singleton Util 1.0 Util.qml ShellIpc 1.0 ShellIpc.qml diff --git a/shell/plugins/bar/Bar.qml b/shell/plugins/bar/Bar.qml index ffd33a851dac..281ce3cf0992 100644 --- a/shell/plugins/bar/Bar.qml +++ b/shell/plugins/bar/Bar.qml @@ -555,6 +555,61 @@ Item { readonly property bool vertical: position === "left" || position === "right" readonly property int barSize: vertical ? Style.bar.sizeVertical : Style.bar.sizeHorizontal + // Display cutouts (a camera notch) the platform's own package describes in + // the fixed platform root, which no environment variable moves. Most machines + // have none. It is read as the shell starts (blockLoading), so a bar surface + // knows before it maps whether its screen may have one. See + // BarModel.parseCutouts. + readonly property var displayCutouts: displayCutoutsFile.missing ? [] : BarModel.parseCutouts(displayCutoutsFile.text()) + FileView { + id: displayCutoutsFile + + // A file that went away describes nothing, whatever text() still holds. + property bool missing: false + + path: "/usr/share/omarchy-platform/display-cutouts.json" + blockLoading: true + watchChanges: true + printErrors: false + onLoaded: missing = false + onLoadFailed: missing = true + onFileChanged: reload() + } + + // The physical mode Hyprland reports for a screen, as hyprctl monitors does. + // Qt's whole-number devicePixelRatio can't rebuild it at a fractional scale. + function panelModeFor(screen) { + var monitor = screen ? Hyprland.monitorFor(screen) : null + return monitor ? ({ + width: monitor.width, + height: monitor.height, + transform: monitor.lastIpcObject ? monitor.lastIpcObject.transform : 0 + }) : null + } + + // How thick the bar is on a screen. A top bar shorter than a panel's camera + // cutout leaves a sliver of every window peeking out beside the camera, so + // the cutout is this panel's minimum sensible top-bar height. An + // intentionally taller bar still wins. + function thicknessFor(screen) { + if (root.vertical || !screen) return root.barSize + return Math.max(root.barSize, BarModel.notchFloor(root.displayCutouts, root.position, screen.name, screen.width, screen.height, screen.devicePixelRatio, root.panelModeFor(screen), Style.bar.notchHeight)) + } + + // Each screen's bar thickness, by screen name: barSize, or more where a notch + // floor raises a top bar. Toasts and plugins clear the bar by it; barSize + // stays the configured size the widgets are drawn at. + readonly property var screenBarSizes: { + var sizes = ({}) + var screens = Quickshell.screens + for (var i = 0; i < screens.length; i++) sizes[screens[i].name] = root.thicknessFor(screens[i]) + return sizes + } + + function barSizeFor(screenName) { + return BarModel.barSizeFor(root.screenBarSizes, screenName, root.barSize) + } + function normalizePosition(value) { return BarModel.normalizePosition(value) } @@ -1239,7 +1294,12 @@ Item { // reveal has to rebuild them — new surface, re-shaped glyphs, re-uploaded // textures — which measures ~150ms against ~20ms to tear down. Parking // keeps the surface alive, so showing is only a margin change. - visible: !remapGuard.remapping + // + // A top bar that can't tell its notch floor yet waits for Hyprland's mode + // before it maps (see BarModel.cutoutPending), so it maps at its floor + // instead of growing once the windows are laid out. The wait ends once, for + // good: when the mode settles it, or after two seconds without one. + visible: !remapGuard.remapping && (cutoutWaitOver || !cutoutPending) exclusionMode: root.barHidden ? ExclusionMode.Ignore : ExclusionMode.Auto ScreenMoveRemap { @@ -1247,11 +1307,13 @@ Item { window: barWindow } + // Parked by its full thickness, which a notch floor may make more than + // barSize, so no strip of it stays on screen. margins { - top: root.barHidden && root.position === "top" ? -root.barSize : 0 - bottom: root.barHidden && root.position === "bottom" ? -root.barSize : 0 - left: root.barHidden && root.position === "left" ? -root.barSize : 0 - right: root.barHidden && root.position === "right" ? -root.barSize : 0 + top: root.barHidden && root.position === "top" ? -barWindow.thickness : 0 + bottom: root.barHidden && root.position === "bottom" ? -barWindow.thickness : 0 + left: root.barHidden && root.position === "left" ? -barWindow.thickness : 0 + right: root.barHidden && root.position === "right" ? -barWindow.thickness : 0 } anchors { @@ -1261,8 +1323,26 @@ Item { right: root.position === "right" || !root.vertical } - implicitWidth: root.vertical ? root.barSize : 0 - implicitHeight: root.vertical ? 0 : root.barSize + readonly property var panelMode: root.panelModeFor(screen) + + readonly property bool centerBesideRight: BarModel.centerBesideRight(root.displayCutouts, root.position, screen.name, screen.width, screen.height, screen.devicePixelRatio, panelMode) + + readonly property bool cutoutPending: BarModel.cutoutPending(root.displayCutouts, root.position, screen.name, screen.width, screen.height, screen.devicePixelRatio, panelMode) + property bool cutoutWaitOver: false + onCutoutPendingChanged: if (!cutoutPending) cutoutWaitOver = true + Component.onCompleted: if (!cutoutPending) cutoutWaitOver = true + + Timer { + interval: 2000 + running: barWindow.cutoutPending && !barWindow.cutoutWaitOver + onTriggered: barWindow.cutoutWaitOver = true + } + + // The same thickness the bar publishes for its screen (screenBarSizes). + readonly property int thickness: root.thicknessFor(screen) + + implicitWidth: root.vertical ? thickness : 0 + implicitHeight: root.vertical ? 0 : thickness color: root.transparent ? "transparent" : root.background surfaceFormat.opaque: false WlrLayershell.namespace: "omarchy-bar" @@ -1353,7 +1433,10 @@ Item { Item { anchors.fill: parent - CenterModules { anchors.fill: parent } + CenterModules { + anchors.fill: parent + entries: barWindow.centerBesideRight ? [] : root.layoutEntries("center") + } LeftModules { anchors.left: parent.left @@ -1362,10 +1445,22 @@ Item { } RightModules { + id: rightModules anchors.right: parent.right anchors.rightMargin: Style.space(8) anchors.verticalCenter: parent.verticalCenter } + + // Keeps the center region, so settings pushes, drag and drop, and panel + // routing still address it. The gap lets a drop at the seam land in + // the section the pointer is nearer to. + ModuleList { + entries: barWindow.centerBesideRight ? root.layoutEntries("center") : [] + region: "center" + anchors.right: rightModules.left + anchors.rightMargin: Style.space(4) + anchors.verticalCenter: parent.verticalCenter + } } } @@ -1515,8 +1610,7 @@ Item { } } - function findCenterAnchorEntry() { - var entries = root.layoutEntries("center") + function findCenterAnchorEntry(entries) { var idx = root.entryIndex(entries, root.centerAnchor) return idx === -1 ? null : entries[idx] } @@ -1536,7 +1630,7 @@ Item { property var entries: root.layoutEntries("center") readonly property bool hasAnchor: root.entryIndex(entries, root.centerAnchor) !== -1 - readonly property var anchorEntry: root.findCenterAnchorEntry() + readonly property var anchorEntry: root.findCenterAnchorEntry(entries) Loader { anchors.fill: parent diff --git a/shell/plugins/bar/BarModel.js b/shell/plugins/bar/BarModel.js index 36900e5b4825..b9ce90e8d54c 100644 --- a/shell/plugins/bar/BarModel.js +++ b/shell/plugins/bar/BarModel.js @@ -208,9 +208,122 @@ function nearestDropTarget(candidates, point, vertical) { return best } +// Display cutouts (a camera notch at the top of a laptop panel) are described +// by the platform's own package, in /usr/share/omarchy-platform/display-cutouts.json: +// +// { "panels": [ { "connector": "eDP", "width": 3024, "height": 1964, "top": 64 } ] } +// +// A panel matches a screen whose connector name starts with `connector` and +// whose mode is width x height physical pixels; `top` is how many physical rows +// at its top the cutout covers. Anything malformed is dropped. +function parseCutouts(text) { + var parsed + try { + parsed = JSON.parse(String(text || "")) + } catch (error) { + return [] + } + var panels = parsed && Array.isArray(parsed.panels) ? parsed.panels : [] + var cutouts = [] + for (var i = 0; i < panels.length; i++) { + var panel = panels[i] || {} + var width = Number(panel.width) + var height = Number(panel.height) + var top = Number(panel.top) + if (typeof panel.connector !== "string" || panel.connector === "") continue + if (!(width > 0) || !(height > 0) || !(top > 0) || top >= height) continue + cutouts.push({ connector: panel.connector, width: width, height: height, top: top }) + } + return cutouts +} + +// The cutout at the top of this screen, in logical pixels, or 0. `mode` is the +// output's physical mode as Hyprland reports it ({ width, height, transform }). +// Qt's devicePixelRatio is a whole number even at a fractional scale, so the +// logical size times it rebuilds the mode only at whole scales; that stands in +// just until Hyprland's answer arrives. A mode Hyprland reports that matches no +// panel has no cutout. +function cutoutTop(cutouts, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode) { + var name = String(screenName || "") + var logical = Number(logicalWidth) + var logicalTall = Number(logicalHeight) + if (!(logical > 0) || !(logicalTall > 0)) return 0 + var width = Number(mode && mode.width) + var height = Number(mode && mode.height) + if (width > 0 && height > 0) { + // Turned a quarter or upside down, the cutout is on another edge. + var transform = Number(mode.transform) || 0 + if (transform !== 0 && transform !== 4) return 0 + if ((width > height) !== (logical > logicalTall)) return 0 + } else { + var scale = Number(devicePixelRatio) > 0 ? Number(devicePixelRatio) : 1 + width = Math.round(logical * scale) + height = Math.round(logicalTall * scale) + } + var list = Array.isArray(cutouts) ? cutouts : [] + for (var i = 0; i < list.length; i++) { + var panel = list[i] + if (name.indexOf(panel.connector) !== 0) continue + // Logical sizes are rounded, so a rebuilt mode can be a couple of pixels off. + if (Math.abs(width - panel.width) <= 4 && Math.abs(height - panel.height) <= 4) + return Math.ceil(panel.top * logical / width) + } + return 0 +} + +// The height a top bar on this screen must reach to cover its cutout, or a +// calibrated [bar] notch-height in its place. A screen without a cutout, or a +// bar on another edge, has no floor, so a calibration never reaches an +// external monitor. +function notchFloor(cutouts, position, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode, calibrated) { + if (position !== "top") return 0 + var top = cutoutTop(cutouts, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode) + if (!(top > 0)) return 0 + return Number(calibrated) > 0 ? Math.round(Number(calibrated)) : top +} + +// A cutout covers the middle of a top bar, so that bar draws its center +// section beside the right one. +function centerBesideRight(cutouts, position, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode) { + return position === "top" && cutoutTop(cutouts, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode) > 0 +} + +// Whether a top bar can't tell its floor yet: Hyprland hasn't reported the +// screen's mode, the logical size times Qt's ratio matched no panel (as at a +// fractional scale), and a described panel's connector matches the screen, so +// the mode may still bring a cutout. Such a bar waits for the mode before it +// maps rather than guess a floor. A screen no entry's connector matches never +// waits. +function cutoutPending(cutouts, position, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode) { + if (position !== "top") return false + if (Number(mode && mode.width) > 0 && Number(mode && mode.height) > 0) return false + var name = String(screenName || "") + var list = Array.isArray(cutouts) ? cutouts : [] + var described = false + for (var i = 0; i < list.length; i++) { + if (name.indexOf(list[i].connector) === 0) described = true + } + return described && !(cutoutTop(cutouts, screenName, logicalWidth, logicalHeight, devicePixelRatio, mode) > 0) +} + +// The bar's thickness on the screen with this name, from the sizes a bar +// publishes by screen name, or `fallback` (its configured size) for a screen +// it has none for. +function barSizeFor(sizes, screenName, fallback) { + var name = String(screenName || "") + var size = sizes && Object.prototype.hasOwnProperty.call(sizes, name) ? Number(sizes[name]) : 0 + return size > 0 ? size : fallback +} + if (typeof module !== "undefined") { module.exports = { + barSizeFor: barSizeFor, + centerBesideRight: centerBesideRight, + cutoutPending: cutoutPending, + cutoutTop: cutoutTop, isDrawnSlot: isDrawnSlot, + notchFloor: notchFloor, + parseCutouts: parseCutouts, pickDrawnSlot: pickDrawnSlot, pickPanelSlot: pickPanelSlot, nearestDropTarget: nearestDropTarget, diff --git a/shell/plugins/bar/indicators/ScreenRecording.qml b/shell/plugins/bar/indicators/ScreenRecording.qml index 271e1c399c28..59b4cab2959b 100644 --- a/shell/plugins/bar/indicators/ScreenRecording.qml +++ b/shell/plugins/bar/indicators/ScreenRecording.qml @@ -15,7 +15,7 @@ BarIndicator { function refresh() { if (!root.bar || statusProc.running) return - statusProc.command = ["pgrep", "--quiet", "-f", "^gpu-screen-recorder"] + statusProc.command = ["omarchy-capture-screenrecording", "--status"] statusProc.running = true } diff --git a/shell/plugins/bar/widgets/Microphone.qml b/shell/plugins/bar/widgets/Microphone.qml index 4f1bf211cf2a..10b0e1f98b49 100644 --- a/shell/plugins/bar/widgets/Microphone.qml +++ b/shell/plugins/bar/widgets/Microphone.qml @@ -2,6 +2,7 @@ import QtQuick import Quickshell import Quickshell.Services.Pipewire import qs.Ui +import qs.Commons BarWidget { id: root @@ -9,15 +10,28 @@ BarWidget { readonly property var source: Pipewire.defaultAudioSource - readonly property bool muted: source && source.audio ? source.audio.muted : true - readonly property real volume: source && source.audio ? source.audio.volume : 0 + // A virtual source is untyped in Quickshell, so it has no PwNode.audio; its + // volume and mute go through wpctl (UntypedInput). + readonly property bool viaWpctl: UntypedInput.active + readonly property bool muted: viaWpctl ? (!UntypedInput.known || UntypedInput.muted) : (source && source.audio ? source.audio.muted : true) + readonly property real volume: viaWpctl ? UntypedInput.volume : (source && source.audio ? source.audio.volume : 0) readonly property var nodes: Pipewire.nodes ? Pipewire.nodes.values : [] + readonly property var nodeNames: { + var names = [] + for (var i = 0; i < nodes.length; i++) + if (nodes[i] && nodes[i].name) names.push(String(nodes[i].name)) + return names + } readonly property var activeStreams: { var list = [] for (var i = 0; i < nodes.length; i++) { var node = nodes[i] - if (node && node.isStream && node.isSink === false && !node.audio?.muted) list.push(node) + if (!node || !node.isStream || node.isSink !== false || node.audio?.muted) continue + // The shell's own level meters, and a platform's audio processing that + // records all the time, are not an app using the microphone. + if (AudioNodes.isShellLevelMeter(node.name) || AudioNodes.platformHides(node.name, nodeNames)) continue + list.push(node) } return list } @@ -29,7 +43,10 @@ BarWidget { implicitHeight: button.implicitHeight function toggleMute() { - if (source && source.audio) source.audio.muted = !source.audio.muted + if (viaWpctl) { + if (UntypedInput.known) UntypedInput.setMuted(!UntypedInput.muted) + } + else if (source && source.audio) source.audio.muted = !source.audio.muted } PwObjectTracker { objects: root.source ? [root.source] : [] } @@ -46,8 +63,12 @@ BarWidget { else root.toggleMute() } onWheelMoved: function(delta) { - if (!root.source || !root.source.audio) return var step = 0.05 + if (root.viaWpctl) { + if (UntypedInput.known) UntypedInput.setVolume(root.volume + (delta > 0 ? step : -step)) + return + } + if (!root.source || !root.source.audio) return root.source.audio.volume = Math.max(0, Math.min(1, root.volume + (delta > 0 ? step : -step))) } } diff --git a/shell/plugins/menu/MenuModel.js b/shell/plugins/menu/MenuModel.js index 53c49768e1ff..9472799ed22d 100644 --- a/shell/plugins/menu/MenuModel.js +++ b/shell/plugins/menu/MenuModel.js @@ -398,7 +398,8 @@ var GUARD_READERS = [ "omarchy-default-browser", "omarchy-default-editor", "omarchy-default-terminal", - "omarchy-dns" + "omarchy-dns", + "uname -m" ] // Package and command presence account for most of what the guards ask, and diff --git a/shell/plugins/notifications/NotificationLogic.js b/shell/plugins/notifications/NotificationLogic.js index b5a6f3ea9910..49bbb183a654 100644 --- a/shell/plugins/notifications/NotificationLogic.js +++ b/shell/plugins/notifications/NotificationLogic.js @@ -395,6 +395,19 @@ function popupExpired(entry, duration, now) { return (Number(now) - Number((entry || {}).timestamp || 0)) >= lifetime } +// How far a toast on a screen keeps from a top or right bar: the bar's +// thickness on that screen (a notch floor can make a top bar thicker than its +// configured size on one screen), plus the gap. A bar without per-screen +// sizes counts its configured size; a hidden or missing one, the default size. +function barClearance(bar, screenName, defaultBarSize, gapsOut) { + var size = Number(defaultBarSize) + if (bar && !bar.barHidden) { + size = Number(typeof bar.barSizeFor === "function" ? bar.barSizeFor(screenName) : bar.barSize) + size = isFinite(size) ? Math.max(0, size) : 0 + } + return size + Number(gapsOut) +} + function popupPlacement(barPosition, barClearance, gapsOut) { var position = String(barPosition || "top") var clearance = Number(barClearance) @@ -474,6 +487,7 @@ if (typeof module !== "undefined") { serializePopup: serializePopup, parsePopupFiles: parsePopupFiles, popupExpired: popupExpired, + barClearance: barClearance, popupPlacement: popupPlacement } } diff --git a/shell/plugins/notifications/Service.qml b/shell/plugins/notifications/Service.qml index 25f5feffba4d..28e6d8d09fa8 100644 --- a/shell/plugins/notifications/Service.qml +++ b/shell/plugins/notifications/Service.qml @@ -45,11 +45,15 @@ Item { // pull notification popups away from the expected top-right location. // Falls back to the bar's default size (26 horizontal / 28 vertical) when // shell.bar isn't reachable so the popup never lands on top of the bar. + // Each screen clears the bar as thick as it is there: a notch floor can make + // a top bar taller than its configured size on one screen only. readonly property string barPosition: shell && shell.barConfig ? String(shell.barConfig.position || "top") : "top" readonly property bool barVertical: barPosition === "left" || barPosition === "right" readonly property int defaultBarSize: barVertical ? Style.bar.sizeVertical : Style.bar.sizeHorizontal - readonly property int liveBarSize: shell && shell.bar && !shell.bar.barHidden ? Math.max(0, shell.bar.barSize) : defaultBarSize - readonly property int barClearance: liveBarSize + Style.gapsOut + + function barClearanceFor(screenName) { + return NotificationLogic.barClearance(shell ? shell.bar : null, screenName, defaultBarSize, Style.gapsOut) + } // Live Notification objects by originalId, kept OUT of the ListModels: a // QObject stored in a model role becomes a dangling C++ pointer when the @@ -974,7 +978,7 @@ Item { color: "transparent" readonly property var popupPlacement: NotificationLogic.popupPlacement( - service.barPosition, service.barClearance, Style.gapsOut) + service.barPosition, service.barClearanceFor(modelData ? modelData.name : ""), Style.gapsOut) // Full-screen, fixed-size surface (like the OSD overlay). Adding or // removing a toast changes only the content inside; the Wayland surface diff --git a/shell/plugins/panels/audio/Model.js b/shell/plugins/panels/audio/Model.js index 94b7cda39ea3..77618d60a705 100644 --- a/shell/plugins/panels/audio/Model.js +++ b/shell/plugins/panels/audio/Model.js @@ -233,6 +233,16 @@ function streamRepresentsPlayer(node, player, players, streams) { return streamRepresentsMprisPlayer(streamLabel(node, players, streams), playerLabel) } +// Quickshell types only exact media classes, so a virtual source (an +// Audio/Source/Virtual, such as EasyEffects' or a platform's microphone +// mapping) is a node with no PwNode.audio. PulseAudio lists it as a source, so +// the source availability listing names it; a non-stream node it names is one. +function isUntypedSource(node, sourceAvailability) { + if (!node || node.audio || node.isStream || node.isSink) return false + var name = String(node.name || "") + return name !== "" && !!sourceAvailability && Object.prototype.hasOwnProperty.call(sourceAvailability, name) +} + if (typeof module !== "undefined") { module.exports = { isPlaybackStream: isPlaybackStream, @@ -257,6 +267,7 @@ if (typeof module !== "undefined") { matchingMprisStreamLabel: matchingMprisStreamLabel, unmatchedMprisStreamLabel: unmatchedMprisStreamLabel, streamLabel: streamLabel, - streamRepresentsPlayer: streamRepresentsPlayer + streamRepresentsPlayer: streamRepresentsPlayer, + isUntypedSource: isUntypedSource } } diff --git a/shell/plugins/panels/audio/Panel.qml b/shell/plugins/panels/audio/Panel.qml index 26d0c58d083c..c6bb6765fb35 100644 --- a/shell/plugins/panels/audio/Panel.qml +++ b/shell/plugins/panels/audio/Panel.qml @@ -20,11 +20,25 @@ Panel { readonly property var mediaService: bar?.shell?.firstPartyServiceFor("omarchy.media") readonly property var activeMediaPlayer: mediaService ? mediaService.activePlayer : null + // Nodes of a platform's audio processing, which are neither devices nor apps, + // stay out of every list (AudioNodes.platformHides). Most machines have none. + readonly property var nodeNames: { + var names = [] + for (var i = 0; i < nodes.length; i++) + if (nodes[i] && nodes[i].name) names.push(String(nodes[i].name)) + return names + } + + function platformHides(node) { + return !!node && AudioNodes.platformHides(node.name, nodeNames) + } + readonly property var candidateSinks: { var list = [] for (var i = 0; i < nodes.length; i++) { var n = nodes[i] - if (n && n.isSink && !n.isStream) list.push(n) + if (!n || !n.isSink || n.isStream || platformHides(n)) continue + list.push(n) } return list } @@ -33,11 +47,11 @@ Panel { var list = [] for (var i = 0; i < nodes.length; i++) { var n = nodes[i] - if (n && !n.isSink && !n.isStream && isAudioSource(n)) { - var name = n.name || "" - if (name === "quickshell") continue - list.push(n) - } + if (!n || n.isSink || n.isStream) continue + if (!isAudioSource(n) && !Model.isUntypedSource(n, sourceAvailability)) continue + var name = n.name || "" + if (name === "quickshell" || platformHides(n)) continue + list.push(n) } return list } @@ -50,6 +64,7 @@ Panel { // A tuning's output is a playback stream too, but it is the processing // itself rather than an application, so it does not belong in the list. if (String(n.name || "").indexOf("omarchy_speaker_tuning") === 0) continue + if (platformHides(n)) continue list.push(n) } return list @@ -57,6 +72,7 @@ Panel { property var sinkAvailability: ({}) property bool sinkAvailabilityLoaded: false + property var sourceAvailability: ({}) // Identify true playback streams without reading node.properties here: // PwNode.properties is invalid until the node is bound, and reading it while @@ -145,8 +161,21 @@ Panel { readonly property real outputVolume: volumeSink && volumeSink.audio ? volumeSink.audio.volume : 0 readonly property bool outputMuted: volumeSink && volumeSink.audio ? volumeSink.audio.muted : false - readonly property real inputVolume: source && source.audio ? source.audio.volume : 0 - readonly property bool inputMuted: source && source.audio ? source.audio.muted : false + // A virtual source is untyped in Quickshell (see Model.isUntypedSource), so + // its volume and mute go through wpctl (UntypedInput), and its level through + // omarchy-audio-source-level: Quickshell's peak monitor takes typed nodes only. + readonly property bool inputViaWpctl: UntypedInput.active + readonly property bool inputLevelKnown: !inputViaWpctl || UntypedInput.known + readonly property real inputVolume: inputViaWpctl ? UntypedInput.volume : (source && source.audio ? source.audio.volume : 0) + readonly property bool inputMuted: inputViaWpctl ? UntypedInput.muted : (source && source.audio ? source.audio.muted : false) + readonly property var inputPeakNode: inputViaWpctl ? null : source + property real untypedInputPeak: 0 + readonly property string meteredSourceName: opened && inputViaWpctl && source.name ? String(source.name) : "" + // Setting running again restarts the meter on the new source. + onMeteredSourceNameChanged: { + untypedInputLevel.running = false + untypedInputLevel.running = meteredSourceName !== "" + } onRawAudioSinksChanged: if (rawAudioSinks.length > 0) cachedAudioSinks = rawAudioSinks onRawAudioSourcesChanged: if (rawAudioSources.length > 0) cachedAudioSources = rawAudioSources @@ -172,7 +201,7 @@ Panel { // would otherwise report "input unmuted" forever, leaving the hero switch // able to mute but never to unmute. readonly property bool hasOutput: !!(volumeSink && volumeSink.audio) - readonly property bool hasInput: !!(source && source.audio) + readonly property bool hasInput: !!(source && source.audio) || (inputViaWpctl && UntypedInput.known) readonly property bool anyAudible: (hasOutput && !outputMuted) || (hasInput && !inputMuted) readonly property string toggleHint: anyAudible ? "Mute" : "Unmute" @@ -412,7 +441,7 @@ Panel { } function inputIcon() { - if (!source || !source.audio) return "󰍭" + if (!hasInput) return "󰍭" return inputMuted ? "󰍭" : "󰍬" } @@ -439,6 +468,10 @@ Panel { } function setInputVolume(v) { + if (inputViaWpctl) { + UntypedInput.setVolume(v) + return + } if (!source || !source.audio) return source.audio.volume = Math.max(0, Math.min(1, v)) } @@ -448,7 +481,10 @@ Panel { } function toggleInputMute() { - if (source && source.audio) source.audio.muted = !source.audio.muted + if (inputViaWpctl) { + if (UntypedInput.known) UntypedInput.setMuted(!UntypedInput.muted) + } + else if (source && source.audio) source.audio.muted = !source.audio.muted } // The hero switch is the whole panel's on/off, so it carries both channels @@ -457,7 +493,8 @@ Panel { function toggleAllMuted() { var mute = anyAudible if (hasOutput) volumeSink.audio.muted = mute - if (hasInput) source.audio.muted = mute + if (hasInput && inputViaWpctl) UntypedInput.setMuted(mute) + else if (hasInput) source.audio.muted = mute } function setDefaultSink(node) { @@ -474,7 +511,8 @@ Panel { function setDefaultSource(node) { if (!node) return - Pipewire.preferredDefaultAudioSource = node + // Quickshell refuses an untyped node; the command below still selects it. + if (node.audio) Pipewire.preferredDefaultAudioSource = node if (node.id !== undefined && node.name) { Quickshell.execDetached([ "omarchy-audio-input-set-default", @@ -579,8 +617,25 @@ Panel { PwNodePeakMonitor { id: inputPeakMonitor - node: root.source - enabled: root.opened && !!root.source + node: root.inputPeakNode + enabled: root.opened && !!root.inputPeakNode + } + + // Tied to the shell's life, and started again if audio restarts under it. + Process { + id: untypedInputLevel + command: ["setpriv", "--pdeathsig", "TERM", "omarchy-audio-source-level", root.meteredSourceName] + stdout: SplitParser { + onRead: function(line) { root.untypedInputPeak = Math.max(0, Math.min(1, parseFloat(line) || 0)) } + } + onRunningChanged: if (!running) root.untypedInputPeak = 0 + onExited: if (root.meteredSourceName !== "") untypedInputLevelRetry.restart() + } + + Timer { + id: untypedInputLevelRetry + interval: 2000 + onTriggered: if (root.meteredSourceName !== "" && !untypedInputLevel.running) untypedInputLevel.running = true } Process { @@ -592,6 +647,15 @@ Panel { } } + Process { + id: sourceAvailabilityProc + command: ["omarchy-audio-sink-availability", "sources"] + stdout: StdioCollector { + waitForEnd: true + onStreamFinished: root.sourceAvailability = Model.parseSinkAvailability(text) + } + } + Process { id: volumeSinkProc command: ["omarchy-audio-output-sink"] @@ -606,7 +670,10 @@ Panel { running: root.opened repeat: true triggeredOnStart: true - onTriggered: if (!sinkAvailabilityProc.running) sinkAvailabilityProc.running = true + onTriggered: { + if (!sinkAvailabilityProc.running) sinkAvailabilityProc.running = true + if (!sourceAvailabilityProc.running) sourceAvailabilityProc.running = true + } } // Runs whether or not the panel is open: the bar shows and scrolls the output @@ -890,7 +957,7 @@ Panel { Text { id: microphonePercent textFormat: Text.PlainText - text: Math.round((inputSlider.dragging ? inputSlider.liveValue : root.inputVolume) * 100) + "%" + text: root.inputLevelKnown ? Math.round((inputSlider.dragging ? inputSlider.liveValue : root.inputVolume) * 100) + "%" : "–" color: Qt.darker(root.bar.foreground, 1.4) font.family: root.bar.fontFamily font.pixelSize: Style.font.caption @@ -942,7 +1009,7 @@ Panel { Rectangle { height: parent.height - width: parent.width * Math.max(0, Math.min(1, inputPeakMonitor.peak)) + width: parent.width * Math.max(0, Math.min(1, root.inputViaWpctl ? (root.inputMuted ? 0 : root.untypedInputPeak) : inputPeakMonitor.peak)) color: root.bar.foreground Behavior on width { NumberAnimation { duration: 70 } } } diff --git a/shell/services/PluginBarStateApi.qml b/shell/services/PluginBarStateApi.qml index db802b775142..82b227180279 100644 --- a/shell/services/PluginBarStateApi.qml +++ b/shell/services/PluginBarStateApi.qml @@ -6,7 +6,18 @@ QtObject { required property string ownerPluginId property bool barHidden: false + // The configured size. A notch floor can make a top bar thicker on one + // screen; barSizes holds each screen's thickness by screen name. property int barSize: 0 + property var barSizes: ({}) property string fontFamily: "" property string position: "top" + + // The bar's thickness on the screen with this name, to clear the bar by; + // barSize for a screen the bar reports none for. + function barSizeFor(screenName) { + var name = String(screenName || "") + var size = barSizes && Object.prototype.hasOwnProperty.call(barSizes, name) ? Number(barSizes[name]) : 0 + return size > 0 ? size : barSize + } } diff --git a/shell/shell.qml b/shell/shell.qml index 888d871ff9cf..7c6239f0f00a 100644 --- a/shell/shell.qml +++ b/shell/shell.qml @@ -436,12 +436,26 @@ ShellRoot { return api } + // A copy of the active bar's per-screen thickness (screen name -> number), + // so a plugin holds no reference into the bar. A bar that reports none + // gives an empty map. + function detachedBarSizes(sizes) { + var copy = ({}) + if (!Util.isPlainObject(sizes)) return copy + for (var name in sizes) { + var size = Number(sizes[name]) + if (size > 0) copy[name] = size + } + return copy + } + function pluginBarStateFor(cacheKey, pluginId) { if (_pluginBarStateApis[cacheKey]) return _pluginBarStateApis[cacheKey] var api = pluginBarStateApiComponent.createObject(null, { ownerPluginId: pluginId }) if (!api) return null api.barHidden = Qt.binding(function() { return shell.bar ? shell.bar.barHidden === true : false }) api.barSize = Qt.binding(function() { return shell.bar ? Math.max(0, shell.bar.barSize || 0) : 0 }) + api.barSizes = Qt.binding(function() { return shell.bar ? shell.detachedBarSizes(shell.bar.screenBarSizes) : ({}) }) api.fontFamily = Qt.binding(function() { return shell.bar ? String(shell.bar.fontFamily || "") : "" }) api.position = Qt.binding(function() { return shell.bar ? String(shell.bar.position || "top") : "top" }) var next = ({}) diff --git a/test/shell.d/apple-silicon-quirks-test.sh b/test/shell.d/apple-silicon-quirks-test.sh new file mode 100644 index 000000000000..bce2a894154c --- /dev/null +++ b/test/shell.d/apple-silicon-quirks-test.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +# PC and Intel Mac quirks that must leave Apple Silicon alone, run on both sides +# of the detector. + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +stub_bin="$test_tmp/bin" +calls="$test_tmp/calls.log" +conf="$test_tmp/modprobe.d/hid_apple.conf" +mkdir -p "$stub_bin" + +cat >"$stub_bin/omarchy-hw-apple-silicon" <<'SH' +#!/bin/bash +[[ ${APPLE_SILICON:-0} == "1" ]] +SH + +cat >"$stub_bin/uname" <<'SH' +#!/bin/bash +if [[ $1 == "-m" ]]; then + echo "${UNAME_M:-x86_64}" +else + exec /usr/bin/uname "$@" +fi +SH + +cat >"$stub_bin/sudo" <<'SH' +#!/bin/bash +printf 'sudo %s\n' "$*" >>"$TEST_LOG" +"$@" +SH + +chmod +x "$stub_bin"/* + +run_fkeys() { + APPLE_SILICON="$1" OMARCHY_HID_APPLE_CONF="$conf" PATH="$stub_bin:$PATH" TEST_LOG="$calls" \ + bash -eE -o pipefail -c 'source "$1"' _ "$ROOT/install/hardware/fix-fkeys.sh" +} + +run_fkeys 0 +[[ $(<"$conf") == "options hid_apple fnmode=2" ]] || fail "a PC keeps F-keys first on Apple-like keyboards" "$(cat "$conf")" +pass "a PC keeps F-keys first on Apple-like keyboards" + +printf 'options hid_apple fnmode=0\n' >"$conf" +run_fkeys 0 +[[ $(<"$conf") == "options hid_apple fnmode=0" ]] || fail "an existing hid_apple.conf is left alone" +pass "an existing hid_apple.conf is left alone" + +rm -rf "$test_tmp/modprobe.d" +: >"$calls" +run_fkeys 1 +[[ ! -e $conf && ! -s $calls ]] || fail "Apple Silicon keeps its own keyboard mode" "$(cat "$calls")" +pass "Apple Silicon keeps its own keyboard mode" + +# The Windows guest is x86_64, so every other CPU refuses it before it touches +# anything, Apple Silicon or not. +for machine in aarch64 armv7l; do + output=$(UNAME_M=$machine APPLE_SILICON=0 PATH="$stub_bin:$PATH" bash "$ROOT/bin/omarchy-windows-vm" install 2>&1) && + fail "Windows VM install fails on $machine" + [[ $output == *"needs an x86_64 machine"* ]] || fail "Windows VM says why it refuses on $machine" "$output" +done +pass "Windows VM refuses every CPU but x86_64" + +# The firmware boot entry and hibernation are refused or skipped on Apple +# Silicon before they touch anything. +output=$(APPLE_SILICON=1 PATH="$stub_bin:$PATH" bash "$ROOT/bin/omarchy-setup-direct-boot" 2>&1) && + fail "direct boot fails on Apple Silicon" +[[ $output == *"not supported on Apple Silicon"* ]] || fail "direct boot says why it refuses" "$output" +output=$(APPLE_SILICON=1 PATH="$stub_bin:$PATH" bash "$ROOT/bin/omarchy-hibernation-setup" --force 2>&1) || + fail "hibernation setup skips Apple Silicon without failing" "$output" +[[ $output == "Skipping hibernation setup (not supported on Apple Silicon)" ]] || + fail "hibernation setup stops before its own checks on Apple Silicon" "$output" +pass "direct boot and hibernation step aside on Apple Silicon" + +# On x86_64 the Windows VM goes on to its own commands, as before. +output=$(UNAME_M=x86_64 PATH="$stub_bin:$PATH" bash "$ROOT/bin/omarchy-windows-vm" help 2>&1) || true +[[ $output != *"x86_64 machine"* && $output == *"install"* ]] || fail "Windows VM runs as before on x86_64" "$output" +pass "Windows VM runs as before on x86_64" + +run_node_test <<'JS' +const fs = require('fs') +const menu = requireFromRoot('shell/plugins/menu/MenuModel.js') +const items = menu.parseMenuJsonc(fs.readFileSync(path.join(root, 'default/omarchy/omarchy-menu.jsonc'), 'utf8')) +const when = id => (items.find(entry => entry.id === id) || {}).when +assertEqual(when('install.windows'), '[[ $(uname -m) == "x86_64" ]]', 'install.windows is offered on x86_64 only') +assertEqual(when('setup.direct-boot'), '! omarchy-hw-apple-silicon', 'setup.direct-boot is hidden on Apple Silicon only') +JS +pass "the menu offers Windows on x86_64 only and hides Direct Boot on Apple Silicon only" diff --git a/test/shell.d/audio-input-set-default-test.sh b/test/shell.d/audio-input-set-default-test.sh new file mode 100644 index 000000000000..5d9d5ab5c4d8 --- /dev/null +++ b/test/shell.d/audio-input-set-default-test.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$work/bin" + +cat >"$work/bin/wpctl" <<'SH' +#!/bin/bash +printf 'wpctl %s\n' "$*" >>"$CALLS" +SH +cat >"$work/bin/pactl" <<'SH' +#!/bin/bash +printf 'pactl %s\n' "$*" >>"$CALLS" +if [[ $* == "list source-outputs" ]]; then + [[ ${LC_ALL:-} == C ]] || echo "pactl read outside the C locale" >>"$CALLS" + cat "$OUTPUTS" +fi +exit 0 +SH +chmod +x "$work/bin/wpctl" "$work/bin/pactl" + +moved() { + grep '^pactl move-source-output ' "$CALLS" | awk '{ print $3 }' | tr '\n' ' ' +} + +export CALLS="$work/calls" OUTPUTS="$work/outputs" + +# A filter chain's capture half (joined to its source by node.link-group) +# records its device all the time. Choosing an input moves apps, never it. +cat >"$OUTPUTS" <<'OUT' +Source Output #67 + Driver: PipeWire + Properties: + node.name = "filter_capture.mic" + node.group = "filter-chain-100-18" + node.link-group = "filter-chain-100-18" + media.name = "Microphone filter" +Source Output #90 + Driver: PipeWire + Properties: + application.name = "Firefox" + node.name = "Firefox" +Source Output #91 + Properties: + node.name = "native-recorder" + media.name = "node.link-group = in a title" +OUT +: >"$CALLS" +PATH="$work/bin:$PATH" "$ROOT/bin/omarchy-audio-input-set-default" 113 virtual_mic +grep -qx 'wpctl set-default 113' "$CALLS" || fail 'input set-default selects the node' +grep -qx 'pactl set-default-source virtual_mic' "$CALLS" || fail 'input set-default names the default source' +! grep -q 'outside the C locale' "$CALLS" || fail 'input set-default reads the recordings in the C locale' +[[ $(moved) == "90 91 " ]] || fail 'input set-default moves apps but not a filter'"'"'s own capture' "$(moved)" +pass 'input set-default moves apps but not a filter'"'"'s own capture' + +# Without filters every recording moves, as before, whatever its properties. +cat >"$OUTPUTS" <<'OUT' +Source Output #12 + Properties: + application.name = "OBS" +Source Output #13 + Properties: + node.name = "native-recorder" +Source Output #14 + Properties: + node.name = "filter_capture.mic-copy" + node.group = "pipewire.dummy" +OUT +: >"$CALLS" +PATH="$work/bin:$PATH" "$ROOT/bin/omarchy-audio-input-set-default" 43 alsa_input.pci-0000_00_1f.3.analog-stereo +[[ $(moved) == "12 13 14 " ]] || fail 'input set-default still moves every other recording' "$(moved)" +pass 'input set-default still moves every other recording' diff --git a/test/shell.d/audio-source-availability-test.sh b/test/shell.d/audio-source-availability-test.sh new file mode 100644 index 000000000000..04a3e173a325 --- /dev/null +++ b/test/shell.d/audio-source-availability-test.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$work/bin" + +cat >"$work/bin/pactl" <<'SH' +#!/bin/bash +printf '%s %s\n' "${LC_ALL:-unset}" "$*" >>"$CALLS" +case $* in + "list sources") cat "$SOURCES" ;; + "list sinks") cat "$SINKS" ;; +esac +SH +cat >"$work/bin/omarchy-audio-tuning" <<'SH' +#!/bin/bash +exit 1 +SH +chmod +x "$work/bin/pactl" "$work/bin/omarchy-audio-tuning" +export CALLS="$work/calls" SOURCES="$work/sources" SINKS="$work/sinks" + +# A headset jack input with nothing plugged in, a virtual source (no ports) +# and a filter's source (one port of unknown availability). +cat >"$SOURCES" <<'OUT' +Source #1074 + Name: alsa_input.pci-0000_00_1f.3.analog-stereo + Ports: + [In] analog-input-headset-mic: Headset Microphone (type: Headset, priority: 300, availability group: Headset Mic, not available) + Active Port: [In] analog-input-headset-mic + Formats: + pcm +Source #126 + Name: easyeffects_source + Formats: + pcm +Source #1102 + Name: effect_output.mic-filter + Ports: + [In] Mic: Microphone (type: Mic, priority: 100, availability unknown) + Active Port: [In] Mic +OUT +cat >"$SINKS" <<'OUT' +Sink #57 + Name: alsa_output.pci-0000_00_1f.3.analog-stereo + Ports: + [Out] analog-output-headphones: Headphones (type: Headphones, priority: 300, availability group: Headphone, not available) + Active Port: [Out] analog-output-headphones +OUT + +: >"$CALLS" +expected=$'alsa_input.pci-0000_00_1f.3.analog-stereo\t0\neasyeffects_source\t1\neffect_output.mic-filter\t1' +actual=$(PATH="$work/bin:$PATH" "$ROOT/bin/omarchy-audio-sink-availability" sources) +[[ $actual == "$expected" ]] || fail 'source availability reports an empty jack input unavailable' "$actual" +grep -qx 'C list sources' "$CALLS" || fail 'source availability reads pactl in the C locale' "$(cat "$CALLS")" +pass 'source availability reports an empty jack input unavailable' + +: >"$CALLS" +actual=$(LC_ALL=de_DE.UTF-8 PATH="$work/bin:$PATH" "$ROOT/bin/omarchy-audio-sink-availability") +[[ $actual == $'alsa_output.pci-0000_00_1f.3.analog-stereo\t0' ]] || fail 'sink availability reports an empty headphone jack unavailable' "$actual" +grep -qx 'C list sinks' "$CALLS" || fail 'sink availability reads pactl in the C locale whatever the caller'"'"'s' "$(cat "$CALLS")" +pass 'sink availability reports an empty headphone jack unavailable, in the C locale' diff --git a/test/shell.d/audio-source-level-test.sh b/test/shell.d/audio-source-level-test.sh new file mode 100644 index 000000000000..2df82dda1e20 --- /dev/null +++ b/test/shell.d/audio-source-level-test.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +require_command python3 + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$work/bin" + +# A recorder that yields 40 ms of silence, 40 ms at peak 0.125 and 40 ms of a +# full-scale sample, then keeps running like a live microphone. +cat >"$work/bin/parec" <<'SH' +#!/bin/bash +printf '%s\n' "$@" >"$ARGS" +echo $$ >"$PIDFILE" +python3 -c " +import struct, sys +n = 48000 // 25 +out = sys.stdout.buffer +out.write(struct.pack('<%df' % n, *([0.0] * n))) +out.write(struct.pack('<%df' % n, *([0.0] * (n - 1) + [-0.125]))) +out.write(struct.pack('<%df' % n, *([0.0] * (n - 1) + [1.5]))) +out.flush()" +exec sleep 30 +SH +chmod +x "$work/bin/parec" +export ARGS="$work/args" PIDFILE="$work/pid" + +PATH="$work/bin:$PATH" python3 "$ROOT/bin/omarchy-audio-source-level" virtual_mic >"$work/out" & +helper=$! +for _ in $(seq 50); do [[ $(wc -l <"$work/out") -ge 3 ]] && break; sleep 0.1; done +[[ $(head -3 "$work/out" | tr '\n' ' ') == "0.000 0.500 1.000 " ]] || + fail 'source level is the cube root of each 40 ms peak, capped at full scale' "$(cat "$work/out")" +pass 'source level is the cube root of each 40 ms peak, capped at full scale' + +grep -qx -- '--device=virtual_mic' "$ARGS" && grep -qx -- '--property=node.name=omarchy-audio-level' "$ARGS" && + grep -qx -- '--property=media.category=Monitor' "$ARGS" || + fail 'source level records the named source as a named monitor stream' "$(cat "$ARGS")" +pass 'source level records the named source as a named monitor stream' + +kill "$helper" +wait "$helper" 2>/dev/null || true +recorder=$(cat "$PIDFILE") +for _ in $(seq 30); do kill -0 "$recorder" 2>/dev/null || break; sleep 0.1; done +! kill -0 "$recorder" 2>/dev/null || fail 'stopping source level stops its recorder' +pass 'stopping source level stops its recorder' + +for args in "" "--device=x" "a b"; do + status=0 + # shellcheck disable=SC2086 + PATH="$work/bin:$PATH" python3 "$ROOT/bin/omarchy-audio-source-level" $args >/dev/null 2>&1 || status=$? + [[ $status == 2 ]] || fail "source level refuses arguments '$args'" "status $status" +done +pass 'source level takes exactly one source name' diff --git a/test/shell.d/audio-test.sh b/test/shell.d/audio-test.sh index 710835d8314f..3accca3e08dc 100644 --- a/test/shell.d/audio-test.sh +++ b/test/shell.d/audio-test.sh @@ -46,4 +46,67 @@ assertEqual(audio.matchingMprisStreamLabel('Chromium', players), 'Chromium', 'au assertEqual(audio.unmatchedMprisStreamLabel('audio-src', players, streams), 'Spotify', 'audio uses unmatched MPRIS player for generic streams') assertEqual(audio.streamLabel(streams[1], players, streams), 'Spotify', 'audio labels generic streams from MPRIS') assert(audio.streamRepresentsPlayer(streams[1], players[0], players, streams), 'audio links generic streams to active player') + +// A virtual source is untyped in Quickshell (no audio); PulseAudio's source +// listing is what names it a source. +const availability = { virtual_mic: true, 'alsa_input.usb': false } +assert(audio.isUntypedSource({ name: 'virtual_mic', isSink: false, isStream: false }, availability), 'audio finds an untyped source PulseAudio lists') +assert(audio.isUntypedSource({ name: 'unplugged_virtual', isSink: false, isStream: false }, { unplugged_virtual: false }), 'an untyped source PulseAudio lists as unavailable is still a source') +assert(!audio.isUntypedSource({ name: 'v4l2_input.camera', isSink: false, isStream: false }, availability), 'audio skips an untyped node PulseAudio does not list') +assert(!audio.isUntypedSource({ name: 'virtual_mic', audio: {}, isSink: false, isStream: false }, availability), 'a typed source is not untyped') +assert(!audio.isUntypedSource({ name: 'virtual_mic', isSink: false, isStream: true }, availability), 'a stream is never an untyped source') +assert(!audio.isUntypedSource({ name: '', isSink: false, isStream: false }, { '': true }), 'a nameless node is never an untyped source') +assert(!audio.isUntypedSource({ name: 'virtual_mic', isSink: false, isStream: false }, {}), 'nothing is untyped before the listing') + +const nodes = requireFromRoot('shell/Commons/AudioNodesModel.js') + +for (const name of ['quickshell', 'quickshell-peak-monitor', 'omarchy-audio-level']) + assert(nodes.isShellLevelMeter(name), 'audio knows the shell meter ' + name) +for (const name of ['Firefox', 'quickshell-other', '', undefined]) + assert(!nodes.isShellLevelMeter(name), 'audio counts ' + name + ' as a recording') + +// Platform hints: none by default, whole-name patterns, and "replaced" only +// while its replacement exists. +const none = nodes.parsePlatformAudio('') +assertDeepEqual([none.hidden.length, none.replaced.length], [0, 0], 'audio has no platform hints without the file') +for (const text of ['not json', '[]', 'null', '{"hidden": "x"}', '{"replaced": [1, {"node": "a"}, {"node": "(", "by": "b"}]}']) { + const parsed = nodes.parsePlatformAudio(text) + assertEqual(parsed.hidden.length + parsed.replaced.length, 0, 'audio ignores platform hints ' + text) +} +const hints = nodes.parsePlatformAudio(JSON.stringify({ + hidden: ['dsp_capture\\.[a-z]+', 'raw_speakers', '(', 7], + replaced: [{ node: 'dsp_mic\\.[0-9]+', by: 'stereo_mic' }] +})) +assertEqual(hints.hidden.length, 2, 'audio skips invalid hidden patterns') +const names = ['dsp_capture.mic', 'dsp_mic.1', 'stereo_mic', 'raw_speakers'] +assert(nodes.platformHidesNode('dsp_capture.mic', hints, names), 'platform hints hide a matching node') +assert(nodes.platformHidesNode('raw_speakers', hints, []), 'platform hints hide a literal name') +assert(!nodes.platformHidesNode('raw_speakers.monitor', hints, []), 'platform patterns match whole names only') +assert(!nodes.platformHidesNode('xdsp_capture.mic', hints, []), 'platform patterns are anchored at the start') +assert(nodes.platformHidesNode('dsp_mic.1', hints, names), 'platform hints hide a replaced node while its replacement exists') +assert(!nodes.platformHidesNode('dsp_mic.1', hints, ['dsp_mic.1']), 'platform hints keep a replaced node without its replacement') +assert(!nodes.platformHidesNode('stereo_mic', hints, names), 'platform hints keep the replacement') +for (const name of ['Firefox', 'alsa_input.pci-0000_00_1f.3.analog-stereo', '', undefined]) + assert(!nodes.platformHidesNode(name, hints, names), 'platform hints keep ' + name) +assert(!nodes.platformHidesNode('dsp_capture.mic', none, names), 'no platform hints hide nothing') + +// An untyped source's level is read from wpctl. +assertDeepEqual(nodes.parseWpctlVolume('Volume: 0.50\n'), { volume: 0.5, muted: false }, 'audio reads a wpctl volume') +assertDeepEqual(nodes.parseWpctlVolume('Volume: 1.00 [MUTED]'), { volume: 1, muted: true }, 'audio reads a muted wpctl volume') +for (const text of ['', 'Translate ID error: 404', 'Volume: loud', 'Volume: 0.50 [muted]']) + assertEqual(nodes.parseWpctlVolume(text), null, 'audio rejects wpctl output ' + JSON.stringify(text)) + +// Source wiring (no Quickshell here): the hints come from the fixed platform +// root, and the audio code names no platform or hardware of its own. +const fs = require('fs') +const read = (file) => fs.readFileSync(path.join(root, file), 'utf8') +const audioNodes = read('shell/Commons/AudioNodes.qml') +assert(/path: "\/usr\/share\/omarchy-platform\/audio\.json"/.test(audioNodes) && !/Quickshell\.env|OMARCHY_/.test(audioNodes), + 'audio reads the platform hints from the fixed platform root') +assert(/onLoadFailed: missing = true/.test(audioNodes), 'a removed hints file hides nothing') +const sources = ['shell/plugins/panels/audio/Panel.qml', 'shell/plugins/panels/audio/Model.js', 'shell/Commons/AudioNodes.qml', + 'shell/Commons/AudioNodesModel.js', 'shell/Commons/UntypedInput.qml', 'shell/plugins/bar/widgets/Microphone.qml', + 'bin/omarchy-audio-input-set-default', 'bin/omarchy-audio-source-level', 'bin/omarchy-audio-sink-availability'] +for (const file of sources) + assert(!/apple|asahi|macbook|j[0-9]{3}|omarchy-hw-|platform-sound/i.test(read(file)), file + ' names no platform') JS diff --git a/test/shell.d/bar-notch-test.sh b/test/shell.d/bar-notch-test.sh new file mode 100644 index 000000000000..fab5b1b080bd --- /dev/null +++ b/test/shell.d/bar-notch-test.sh @@ -0,0 +1,235 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +run_node_test <<'JS' +const fs = require('fs') +const bar = requireFromRoot('shell/plugins/bar/BarModel.js') +const barSource = fs.readFileSync(root + '/shell/plugins/bar/Bar.qml', 'utf8') +const styleSource = fs.readFileSync(root + '/shell/Commons/Style.qml', 'utf8') +const shellJson = fs.readFileSync(root + '/config/omarchy/shell.json', 'utf8') + +// A platform package's cutout description, as it would ship it. +const cutouts = bar.parseCutouts(JSON.stringify({ panels: [ + { connector: 'eDP', width: 3024, height: 1964, top: 64 }, + { connector: 'eDP', width: 2560, height: 1664, top: 56 } +] })) +assertEqual(cutouts.length, 2, 'a well-formed description yields its panels') + +// Malformed descriptions and entries are dropped, never guessed at. +for (const text of ['', 'not json', '[]', '{}', '{"panels": {}}']) + assertDeepEqual(bar.parseCutouts(text), [], `an unusable description (${JSON.stringify(text)}) yields no cutouts`) +assertDeepEqual(bar.parseCutouts(JSON.stringify({ panels: [ + { width: 3024, height: 1964, top: 64 }, + { connector: '', width: 3024, height: 1964, top: 64 }, + { connector: 'eDP', width: 0, height: 1964, top: 64 }, + { connector: 'eDP', width: 3024, height: 1964, top: 0 }, + { connector: 'eDP', width: 3024, height: 1964, top: 1964 }, + null +] })), [], 'entries without a connector, a size or a sensible cutout are dropped') + +// A cutout is described in physical rows; the bar works in logical pixels at +// any scale. Hyprland reports each output's physical mode, as hyprctl monitors +// does; Qt's devicePixelRatio is a whole number even at a fractional scale. +const mode = (width, height, transform = 0) => ({ width, height, transform }) +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1512, 982, 2, mode(3024, 1964)), 32, 'a 64-row cutout is 32 px at scale 2') +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 3024, 1964, 1, mode(3024, 1964)), 64, 'scale 1 uses the rows as they are') +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1280, 832, 2, mode(2560, 1664)), 28, 'each described panel gets its own cutout') + +// A 16" MacBook Pro panel at scale 1.33: Qt says 2, Hyprland says 3456x2234. +const pro16 = [{ connector: 'eDP', width: 3456, height: 2234, top: 64 }] +assertEqual(bar.cutoutTop(pro16, 'eDP-1', 2592, 1676, 2, mode(3456, 2234)), 48, 'a fractional scale matches its panel by the mode Hyprland reports') +assertEqual(bar.notchFloor(pro16, 'top', 'eDP-1', 2592, 1676, 2, mode(3456, 2234), 0), 48, 'a top bar at a fractional scale floors at the cutout') +assertEqual(bar.centerBesideRight(pro16, 'top', 'eDP-1', 2592, 1676, 2, mode(3456, 2234)), true, 'a top bar at a fractional scale moves the center section') +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1890, 1228, 2, mode(3024, 1964)), 40, 'scale 1.6 matches too') + +// Until Hyprland answers, the logical size times Qt's ratio stands in for the +// mode, which holds at whole scales only. +for (const pending of [null, undefined, {}, mode(0, 0)]) + assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1512, 982, 2, pending), 32, `a whole scale matches before Hyprland reports the mode (${JSON.stringify(pending)})`) +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 3024, 1964, 0, null), 64, 'a missing scale reads as 1') +assertEqual(bar.cutoutTop(pro16, 'eDP-1', 2592, 1676, 2, null), 0, 'a fractional scale finds nothing on Qt\'s ratio alone') + +// A mode Hyprland reports is the answer: one no panel describes has no cutout, +// whatever the logical size times the ratio would say. +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1512, 982, 2, mode(3024, 1890)), 0, 'the same panel with its cutout rows hidden has none') +assertEqual(bar.cutoutTop(cutouts, 'DP-1', 1512, 982, 2, mode(3024, 1964)), 0, 'another connector with the same mode has none') +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1728, 1117, 2, mode(3456, 2234)), 0, 'a panel nobody described has none') +assertEqual(bar.cutoutTop([], 'eDP-1', 1512, 982, 2, mode(3024, 1964)), 0, 'with no description no panel has a cutout') +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 0, 0, 2, mode(3024, 1964)), 0, 'degenerate screen sizes have none') + +// Turned a quarter or upside down, the cutout is on another edge; mirrored, +// it is still at the top. +for (const transform of [1, 2, 3, 5, 6, 7]) + assertEqual(bar.cutoutTop(cutouts, 'eDP-1', transform % 2 ? 982 : 1512, transform % 2 ? 1512 : 982, 2, mode(3024, 1964, transform)), 0, `a panel with transform ${transform} has no cutout at its top`) +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 1512, 982, 2, mode(3024, 1964, 4)), 32, 'a mirrored panel keeps its cutout at the top') +assertEqual(bar.cutoutTop(cutouts, 'eDP-1', 982, 1512, 2, mode(3024, 1964)), 0, 'a portrait screen on a landscape mode is turned, whatever its transform says') + +// Only a top bar on a described panel has a floor; a calibrated [bar] +// notch-height replaces the described value there and nowhere else. +assertEqual(bar.notchFloor(cutouts, 'top', 'eDP-1', 1512, 982, 2, mode(3024, 1964), 0), 32, 'a top bar floors at the cutout') +for (const position of ['bottom', 'left', 'right']) + assertEqual(bar.notchFloor(cutouts, position, 'eDP-1', 1512, 982, 2, mode(3024, 1964), 0), 0, `a ${position} bar has no floor`) +assertEqual(bar.notchFloor(cutouts, 'top', 'eDP-1', 1512, 982, 2, mode(3024, 1964), 36), 36, 'a calibrated notch-height overrides the described floor') +assertEqual(bar.notchFloor(cutouts, 'top', 'DP-1', 2560, 1440, 1, mode(2560, 1440), 36), 0, 'a calibrated notch-height never floors an external monitor') +assertEqual(bar.notchFloor([], 'top', 'eDP-1', 1512, 982, 2, mode(3024, 1964), 36), 0, 'a calibrated notch-height never floors a panel without a cutout') +assertEqual(bar.notchFloor(cutouts, 'top', 'eDP-1', 1512, 982, 2, mode(3024, 1964), 'x'), 32, 'an unreadable calibration falls back to the described floor') + +// Source wiring only (no Quickshell here): the bar reads the description from +// the fixed platform root, which no environment variable moves, not from a +// detector. +assert( + /path: "\/usr\/share\/omarchy-platform\/display-cutouts\.json"/.test(barSource) && + /readonly property var displayCutouts: displayCutoutsFile\.missing \? \[\] : BarModel\.parseCutouts\(displayCutoutsFile\.text\(\)\)/.test(barSource), + 'bar reads the platform package\'s cutout description from the platform root' +) +assert( + /id: displayCutoutsFile[\s\S]{0,400}blockLoading: true[\s\S]{0,200}onLoadFailed: missing = true/.test(barSource), + 'bar reads the description as the shell starts, before its surfaces map, and a removed file describes nothing' +) +assert(!/_PACKAGED_PATH|packagedPath/.test(barSource), 'no environment variable moves the cutout description') +assert(!/omarchy-hw-/.test(barSource), 'bar asks no hardware detector') +assert( + /BarModel\.notchFloor\(root\.displayCutouts, root\.position, screen\.name, screen\.width, screen\.height, screen\.devicePixelRatio, root\.panelModeFor\(screen\), Style\.bar\.notchHeight\)/.test(barSource), + 'bar derives the floor from its own screen geometry, its mode and the calibration' +) +assert( + /function panelModeFor\(screen\) \{\s*var monitor = screen \? Hyprland\.monitorFor\(screen\) : null\s*return monitor \? \(\{\s*width: monitor\.width,\s*height: monitor\.height,\s*transform: monitor\.lastIpcObject \? monitor\.lastIpcObject\.transform : 0\s*\}\) : null/.test(barSource) && + /readonly property var panelMode: root\.panelModeFor\(screen\)/.test(barSource), + 'bar takes its screen\'s physical mode from Hyprland' +) +assert( + /function thicknessFor\(screen\) \{\s*if \(root\.vertical \|\| !screen\) return root\.barSize\s*return Math\.max\(root\.barSize, BarModel\.notchFloor\(/.test(barSource) && + /readonly property int thickness: root\.thicknessFor\(screen\)/.test(barSource) && + /implicitHeight: root\.vertical \? 0 : thickness/.test(barSource), + 'bar height is floored at the cutout, never shrunk to it' +) +// A hidden bar parks by that same thickness (bar-test.sh), so a floored top +// bar leaves no strip of itself on screen. + +// The calibration must not scale with the font: it describes physical pixels +// beside the camera. +assert( + /notchHeight:[\s\S]{0,240}barOverrides\["notch-height"\]/.test(styleSource) && + !/barToken\("notch-height"/.test(styleSource), + 'notch-height is read raw, not through the font-scaled bar tokens' +) + +// A top bar on a described panel draws its center section beside the right +// one; everything else keeps the configured layout. +for (const scale of [1, 2]) + assertEqual(bar.centerBesideRight(cutouts, 'top', 'eDP-1', 3024 / scale, 1964 / scale, scale, mode(3024, 1964)), true, `a described panel at scale ${scale} moves the center section`) +assertEqual(bar.centerBesideRight(cutouts, 'top', 'USB-2', 3440, 1440, 1, mode(3440, 1440)), false, 'an external monitor keeps the center section') +assertEqual(bar.centerBesideRight([], 'top', 'eDP-1', 1512, 982, 2, mode(3024, 1964)), false, 'a machine without a description keeps the center section') +for (const position of ['bottom', 'left', 'right']) + assertEqual(bar.centerBesideRight(cutouts, position, 'eDP-1', 1512, 982, 2, mode(3024, 1964)), false, `a ${position} bar keeps the center section`) + +// The move is per screen and draw-time only: the user's layout is read as +// configured, and the center entries keep their order and their own region. +assert( + /centerBesideRight: BarModel\.centerBesideRight\(root\.displayCutouts, root\.position, screen\.name, screen\.width, screen\.height, screen\.devicePixelRatio, panelMode\)/.test(barSource), + 'each bar surface decides the move from its own screen' +) +assert( + /CenterModules \{\s*anchors\.fill: parent\s*entries: barWindow\.centerBesideRight \? \[\] : root\.layoutEntries\("center"\)\s*\}/.test(barSource), + 'a moved center section draws nothing in the middle of the bar' +) +assert( + /ModuleList \{\s*entries: barWindow\.centerBesideRight \? root\.layoutEntries\("center"\) : \[\]\s*region: "center"\s*anchors\.right: rightModules\.left\s*anchors\.rightMargin: Style\.space\(\d+\)/.test(barSource), + 'the center entries sit just left of the right section in their own region' +) + +// With no gap the last center slot and the first right slot would share an +// edge, and a drop there would always land in whichever registered first. +const seam = [ + { slot: 'right-first', x: 104, width: 20 }, + { slot: 'center-last', x: 80, width: 20 } +] +assertDeepEqual(bar.nearestDropTarget(seam, { x: 101 }, false), { slot: 'center-last', after: true }, 'a drop at the seam nearer the center section lands after its last entry') +assertDeepEqual(bar.nearestDropTarget(seam, { x: 103 }, false), { slot: 'right-first', after: false }, 'a drop at the seam nearer the right section lands before its first entry') +assert( + /anchorEntry: root\.findCenterAnchorEntry\(entries\)/.test(barSource), + 'an emptied center section does not keep a hidden copy of its anchor' +) + +// Until Hyprland reports the mode, a top bar waits to map where the mode may +// still bring a cutout: a described connector whose whole-scale rebuild +// matched nothing, as at a fractional scale. It never guesses a floor there. +assertEqual(bar.cutoutPending(pro16, 'top', 'eDP-1', 2592, 1676, 2, null), true, 'a fractional scale on a described connector waits for the mode') +for (const pending of [undefined, {}, mode(0, 0)]) + assertEqual(bar.cutoutPending(pro16, 'top', 'eDP-1', 2592, 1676, 2, pending), true, `no mode yet (${JSON.stringify(pending)}) still waits`) +assertEqual(bar.notchFloor(pro16, 'top', 'eDP-1', 2592, 1676, 2, null, 0), 0, 'a waiting bar has no guessed floor') +assertEqual(bar.cutoutPending(pro16, 'top', 'eDP-1', 2592, 1676, 2, mode(3456, 2234)), false, 'the mode ends the wait') +assertEqual(bar.cutoutPending(pro16, 'top', 'eDP-1', 1728, 1117, 2, mode(3456, 2160)), false, 'a mode no panel describes ends the wait too') +assertEqual(bar.cutoutPending(cutouts, 'top', 'eDP-1', 1512, 982, 2, null), false, 'a whole scale that matches needs no wait') +assertEqual(bar.cutoutPending(cutouts, 'top', 'DP-1', 2560, 1440, 1, null), false, 'a connector no entry names never waits') +assertEqual(bar.cutoutPending([], 'top', 'eDP-1', 2592, 1676, 2, null), false, 'without a description nothing waits') +for (const position of ['bottom', 'left', 'right']) + assertEqual(bar.cutoutPending(pro16, position, 'eDP-1', 2592, 1676, 2, null), false, `a ${position} bar never waits`) +assert( + /readonly property bool cutoutPending: BarModel\.cutoutPending\(root\.displayCutouts, root\.position, screen\.name, screen\.width, screen\.height, screen\.devicePixelRatio, panelMode\)/.test(barSource) && + /visible: !remapGuard\.remapping && \(cutoutWaitOver \|\| !cutoutPending\)/.test(barSource), + 'a bar surface maps only once its floor is known, and still remaps on a screen move' +) +assert( + /property bool cutoutWaitOver: false\s*onCutoutPendingChanged: if \(!cutoutPending\) cutoutWaitOver = true\s*Component\.onCompleted: if \(!cutoutPending\) cutoutWaitOver = true/.test(barSource) && + /Timer \{\s*interval: 2000\s*running: barWindow\.cutoutPending && !barWindow\.cutoutWaitOver\s*onTriggered: barWindow\.cutoutWaitOver = true\s*\}/.test(barSource), + 'the wait ends once, for good: when the mode settles it or after two seconds' +) + +// Toasts and plugins clear the bar as thick as it is on their screen. The bar +// publishes each screen's thickness by name from the same calculation its +// surfaces use; barSize stays the configured size the widgets are drawn at. +const sizes = { 'eDP-1': 40, 'DP-1': 26 } +assertEqual(bar.barSizeFor(sizes, 'eDP-1', 26), 40, 'a notch screen reports its floored thickness') +assertEqual(bar.barSizeFor(sizes, 'DP-1', 26), 26, 'another screen reports its own') +assertEqual(bar.barSizeFor(sizes, 'HDMI-A-1', 26), 26, 'a screen without a surface falls back to the configured size') +assertEqual(bar.barSizeFor({}, 'eDP-1', 26), 26, 'no published sizes fall back to the configured size') +assertEqual(bar.barSizeFor(sizes, 'constructor', 26), 26, 'inherited object keys are no screen') +assert( + /readonly property var screenBarSizes: \{\s*var sizes = \(\{\}\)\s*var screens = Quickshell\.screens\s*for \(var i = 0; i < screens\.length; i\+\+\) sizes\[screens\[i\]\.name\] = root\.thicknessFor\(screens\[i\]\)\s*return sizes\s*\}/.test(barSource) && + /function barSizeFor\(screenName\) \{\s*return BarModel\.barSizeFor\(root\.screenBarSizes, screenName, root\.barSize\)\s*\}/.test(barSource), + 'bar publishes every screen\'s thickness from the same calculation its surfaces use' +) +assert( + /readonly property int barSize: vertical \? Style\.bar\.sizeVertical : Style\.bar\.sizeHorizontal/.test(barSource), + 'barSize stays the configured size' +) + +// The plugin facade copies the sizes, holding no reference into the bar, and +// keeps barSize the configured size. +const shellSource = fs.readFileSync(root + '/shell/shell.qml', 'utf8') +const stateApiSource = fs.readFileSync(root + '/shell/services/PluginBarStateApi.qml', 'utf8') +const extract = (source, name) => { + const match = source.match(new RegExp(`function ${name}\\((\\w+)\\) \\{([\\s\\S]*?)\\n \\}\\n`)) + assert(match, `${name} is defined`) + return match +} +const detached = extract(shellSource, 'detachedBarSizes') +const Util = { isPlainObject: value => value !== null && typeof value === 'object' && !Array.isArray(value) } +const detachedBarSizes = new Function('Util', detached[1], detached[2]) +const live = { 'eDP-1': 40, 'DP-1': 26, broken: 'x', none: 0 } +const copy = detachedBarSizes.call(null, Util, live) +assertDeepEqual(copy, { 'eDP-1': 40, 'DP-1': 26 }, 'the plugin facade copies each screen\'s thickness') +assert(copy !== live, 'the copy is detached from the bar\'s map') +assertDeepEqual(detachedBarSizes.call(null, Util, undefined), {}, 'a bar without per-screen sizes gives an empty map') +assert( + /api\.barSize = Qt\.binding\(function\(\) \{ return shell\.bar \? Math\.max\(0, shell\.bar\.barSize \|\| 0\) : 0 \}\)/.test(shellSource) && + /api\.barSizes = Qt\.binding\(function\(\) \{ return shell\.bar \? shell\.detachedBarSizes\(shell\.bar\.screenBarSizes\) : \(\{\}\) \}\)/.test(shellSource), + 'plugins get the configured barSize and every screen\'s thickness' +) +const apiFor = extract(stateApiSource, 'barSizeFor') +const stateBarSizeFor = new Function('barSizes', 'barSize', apiFor[1], apiFor[2]) +assertEqual(stateBarSizeFor(copy, 26, 'eDP-1'), 40, 'a plugin reads a notch screen\'s thickness') +assertEqual(stateBarSizeFor(copy, 26, 'HDMI-A-1'), 26, 'a plugin falls back to barSize for a screen with none') +assertEqual(stateBarSizeFor({}, 26, 'eDP-1'), 26, 'a plugin under a bar without per-screen sizes gets barSize') + +const parsed = JSON.parse(shellJson) +assertEqual(parsed.bar.centerAnchor, 'omarchy.clock', 'shipped bar layout still anchors on the clock') +assert( + JSON.stringify(parsed.bar.layout.center).indexOf('omarchy.indicators') >= 0, + 'shipped bar layout still keeps the default center widgets' +) +JS diff --git a/test/shell.d/bar-test.sh b/test/shell.d/bar-test.sh index 0b06258a4ca8..e5f5b10971c7 100644 --- a/test/shell.d/bar-test.sh +++ b/test/shell.d/bar-test.sh @@ -58,8 +58,8 @@ assert( ) for (const edge of ['top', 'bottom', 'left', 'right']) { assert( - new RegExp(`${edge}: root\\.barHidden && root\\.position === "${edge}" \\? -root\\.barSize : 0`).test(barSource), - `a hidden bar parks past the ${edge} edge` + new RegExp(`${edge}: root\\.barHidden && root\\.position === "${edge}" \\? -barWindow\\.thickness : 0`).test(barSource), + `a hidden bar parks past the ${edge} edge by its whole thickness` ) } diff --git a/test/shell.d/base-test.sh b/test/shell.d/base-test.sh index 476b8f8316d6..7852269a6d5b 100644 --- a/test/shell.d/base-test.sh +++ b/test/shell.d/base-test.sh @@ -89,6 +89,59 @@ require_compositor() { exit 0 } +# Fake the hardware identity omarchy-hw-platform reads: a device tree under +# $1/proc and a uname in $1/bin reporting the platform's CPU. Run the code under +# test with OMARCHY_PROC_ROOT="$1/proc" and PATH="$1/bin:$ROOT/bin:...", so the +# real detector and its wrappers answer from the fixture. +fake_platform() { + local dir="$1" platform="$2" machine=aarch64 + local -a compatible=() + + case $platform in + apple-silicon) compatible=(apple,j416c apple,t6021 apple,arm-platform) ;; + qualcomm) compatible=(lenovo,yoga-slim7x qcom,x1e80100) ;; + generic-aarch64) compatible=(raspberrypi,5-model-b brcm,bcm2712) ;; + generic) machine=x86_64 ;; + *) fail "fake_platform knows the platform $platform" ;; + esac + + rm -rf "$dir/proc" + mkdir -p "$dir/proc" "$dir/bin" + if (( ${#compatible[@]} > 0 )); then + mkdir -p "$dir/proc/device-tree" + printf '%s\0' "${compatible[@]}" >"$dir/proc/device-tree/compatible" + fi + + cat >"$dir/bin/uname" <"$copy" && + chmod +x "$copy" || fail "copy $script for a fixture platform root" + grep -qF -- "$fixture" "$copy" && ! grep -qF /usr/share/omarchy-platform "$copy" || + fail "$(basename -- "$script") reads the platform root from the fixture" +} + run_node_test() { require_command node diff --git a/test/shell.d/battery-present-test.sh b/test/shell.d/battery-present-test.sh new file mode 100755 index 000000000000..2ec66f960789 --- /dev/null +++ b/test/shell.d/battery-present-test.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT +mkdir -p "$test_tmp/macsmc-ac" "$test_tmp/CMB0" +printf 'Mains\n' >"$test_tmp/macsmc-ac/type" +printf 'Battery\n' >"$test_tmp/CMB0/type" +printf '1\n' >"$test_tmp/CMB0/present" + +OMARCHY_POWER_SUPPLY_PATH="$test_tmp" "$ROOT/bin/omarchy-battery-present" || fail "battery presence accepts non-BAT native paths" +pass "battery presence accepts non-BAT native paths" + +printf '0\n' >"$test_tmp/CMB0/present" +if OMARCHY_POWER_SUPPLY_PATH="$test_tmp" "$ROOT/bin/omarchy-battery-present"; then + fail "battery presence rejects an absent battery" +fi +pass "battery presence rejects an absent battery" + +# A wireless mouse registers a Battery of its own, scoped Device; it must not +# make a desktop look like a laptop. +printf '1\n' >"$test_tmp/CMB0/present" +printf 'System\n' >"$test_tmp/CMB0/scope" +mkdir -p "$test_tmp/hidpp_battery_0" +printf 'Battery\n' >"$test_tmp/hidpp_battery_0/type" +printf '1\n' >"$test_tmp/hidpp_battery_0/present" +printf 'Device\n' >"$test_tmp/hidpp_battery_0/scope" +OMARCHY_POWER_SUPPLY_PATH="$test_tmp" "$ROOT/bin/omarchy-battery-present" || fail "battery presence accepts a System-scoped battery beside a peripheral" +pass "battery presence accepts a System-scoped battery beside a peripheral" + +rm -r "$test_tmp/CMB0" +if OMARCHY_POWER_SUPPLY_PATH="$test_tmp" "$ROOT/bin/omarchy-battery-present"; then + fail "battery presence ignores a peripheral's Device-scoped battery" +fi +pass "battery presence ignores a peripheral's Device-scoped battery" diff --git a/test/shell.d/battery-status-test.sh b/test/shell.d/battery-status-test.sh index 88a7fa12f74b..8478cdb8888a 100644 --- a/test/shell.d/battery-status-test.sh +++ b/test/shell.d/battery-status-test.sh @@ -8,20 +8,40 @@ tmp_dir=$(mktemp -d) trap 'rm -rf "$tmp_dir"' EXIT mkdir -p "$tmp_dir/bin" -mkdir -p "$tmp_dir/power/BAT0" -printf '900000\n' >"$tmp_dir/power/BAT0/current_now" -printf '12000000\n' >"$tmp_dir/power/BAT0/voltage_now" +# Apple Silicon: a battery not called BAT*, power_now signed by direction, and +# the thresholds and cycle count only under the native path. +mkdir -p "$tmp_dir/power/macsmc-battery" +printf '%s\n' '-10800000' >"$tmp_dir/power/macsmc-battery/power_now" +printf '75\n' >"$tmp_dir/power/macsmc-battery/charge_control_start_threshold" +printf '80\n' >"$tmp_dir/power/macsmc-battery/charge_control_end_threshold" +printf '212\n' >"$tmp_dir/power/macsmc-battery/cycle_count" cat >"$tmp_dir/bin/upower" <<'STUB' #!/bin/bash +# A wireless mouse enumerates first, as a battery_ device that is not a power +# supply; the machine's own battery follows it. if [[ $1 == "-e" ]]; then - echo "/org/freedesktop/UPower/devices/battery_BAT0" + echo "/org/freedesktop/UPower/devices/line_power_macsmc_ac" + echo "/org/freedesktop/UPower/devices/battery_hidpp_battery_0" + echo "/org/freedesktop/UPower/devices/battery_${OMARCHY_TEST_NATIVE_PATH//-/_}" + exit 0 +fi + +if [[ $1 == "-i" && $2 == */battery_hidpp_battery_0 ]]; then + cat <<'INFO' + native-path: hidpp_battery_0 + model: Wireless Mouse + power supply: no + state: discharging + percentage: 5% +INFO exit 0 fi if [[ $1 == "-i" ]]; then cat <<'INFO' - native-path: BAT0 + native-path: macsmc-battery + power supply: yes state: discharging energy: 28.3 Wh energy-full: 56.7 Wh @@ -36,13 +56,69 @@ exit 1 STUB chmod +x "$tmp_dir/bin/upower" -shell_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) +shell_output=$(OMARCHY_TEST_NATIVE_PATH=macsmc-battery OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) -grep -Fx $'percentage\t51%' <<<"$shell_output" >/dev/null || fail "battery status reports percentage" +grep -Fx $'percentage\t51%' <<<"$shell_output" >/dev/null || fail "battery status reports the machine's battery, not the mouse's" grep -Fx $'state\tdischarging' <<<"$shell_output" >/dev/null || fail "battery status reports state" grep -Fx $'rate\t10.8W' <<<"$shell_output" >/dev/null || fail "battery status reports live sysfs power rate" grep -Fx $'size\t56Wh' <<<"$shell_output" >/dev/null || fail "battery status reports full capacity" grep -Fx $'time\t2h 30m' <<<"$shell_output" >/dev/null || fail "battery status reports remaining time" +grep -Fx $'cycles\t212' <<<"$shell_output" >/dev/null || fail "battery status reports native-path cycle count" +grep -Fx $'threshold\t75-80%' <<<"$shell_output" >/dev/null || fail "battery status reports native-path charge thresholds" + +# The BAT* name the script used to key on is one of many; CMB0 is another. +mkdir -p "$tmp_dir/power/CMB0" +printf '7300000\n' >"$tmp_dir/power/CMB0/power_now" +sed -i 's/native-path: macsmc-battery/native-path: CMB0/' "$tmp_dir/bin/upower" +generic_output=$(OMARCHY_TEST_NATIVE_PATH=CMB0 OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) +grep -Fx $'rate\t7.3W' <<<"$generic_output" >/dev/null || fail "battery status accepts arbitrary UPower battery paths" +pass "battery status supports Apple Silicon and arbitrary native battery paths" + +# An 80% hold threshold doesn't trip while the battery is still below it. +hold_dir=$(mktemp -d) +trap 'rm -rf "$tmp_dir" "$hold_dir"' EXIT + +mkdir -p "$hold_dir/bin" "$hold_dir/power/BAT0" "$hold_dir/power/ac" +printf 'Mains\n' >"$hold_dir/power/ac/type" +printf '1\n' >"$hold_dir/power/ac/online" +printf '80\n' >"$hold_dir/power/BAT0/charge_control_end_threshold" +cat >"$hold_dir/bin/upower" <<'STUB' +#!/bin/bash + +if [[ $1 == "-e" ]]; then + echo "/org/freedesktop/UPower/devices/battery_BAT0" + exit 0 +fi + +if [[ $1 == "-i" ]]; then + cat <<'INFO' + native-path: BAT0 + power supply: yes + state: charging + energy-full: 69.6 Wh + energy-rate: 0.1 W + time to full: 0.2 hours + percentage: 79.5% + charge-end-threshold: 80% +INFO + exit 0 +fi + +exit 1 +STUB +chmod +x "$hold_dir/bin/upower" + +hold_output=$(OMARCHY_POWER_SUPPLY_PATH="$hold_dir/power" PATH="$hold_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) + +grep -Fx $'state\tcharging' <<<"$hold_output" >/dev/null || fail "a hold below its threshold is still charging" +pass "battery status doesn't trip a hold early" + +# Once the battery reaches the threshold, idle charging is holding. +sed -i 's/percentage: 79.5%/percentage: 80.0%/' "$hold_dir/bin/upower" +held_output=$(OMARCHY_POWER_SUPPLY_PATH="$hold_dir/power" PATH="$hold_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) +grep -Fx $'percentage\t80%' <<<"$held_output" >/dev/null || fail "threshold percentage still displays as 80%" +grep -Fx $'state\tholding' <<<"$held_output" >/dev/null || fail "idle charging at the threshold is holding" +pass "battery status reports holding once the raw percentage reaches the threshold" if matches=$(rg -n 'omarchy-battery-(capacity|remaining|remaining-time)' "$ROOT/bin" "$ROOT/test" "$ROOT/shell" "$ROOT/docs"); then fail "battery status owns capacity and remaining calculations" "$matches" diff --git a/test/shell.d/brave-widevine-test.sh b/test/shell.d/brave-widevine-test.sh new file mode 100644 index 000000000000..ca9750c107b3 --- /dev/null +++ b/test/shell.d/brave-widevine-test.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT +stub_bin="$test_tmp/bin" +omarchy_path="$test_tmp/omarchy" +opt_path="$test_tmp/opt" +cdm="$opt_path/WidevineCdm/chromium" +calls="$test_tmp/calls.log" +mkdir -p "$stub_bin" "$test_tmp/home" "$omarchy_path/install/helpers" "$omarchy_path/config" + +# The real policy helper writes under /etc without sudo when run as root, so +# stand it in; this test covers only the CDM link. Its as_root goes through the +# sudo stub, which logs every link, whatever the test's EUID. +printf 'browser_policy_setup_dir() { :; }\nas_root() { sudo "$@"; }\n' >"$omarchy_path/install/helpers/browser-policy.sh" +cp "$ROOT/config/chromium-flags.conf" "$omarchy_path/config/" + +for command in omarchy-pkg-add omarchy-pkg-aur-add omarchy-install-chromium-copy-url omarchy-install-chromium-ytdlp omarchy-theme-set-browser omarchy-lifecycle-dispatch; do + printf '#!/bin/bash\nexit 0\n' >"$stub_bin/$command" +done +cat >"$stub_bin/sudo" <<'SH' +#!/bin/bash +printf 'sudo:%s\n' "$*" >>"$TEST_LOG" +"$@" +SH +chmod +x "$stub_bin"/* + +reset_opt() { + rm -rf "$opt_path" + mkdir -p "$opt_path" + local dir + for dir in "$@"; do + mkdir -p "$opt_path/$dir" + done +} + +run() { + : >"$calls" + HOME="$test_tmp/home" OMARCHY_PATH="$omarchy_path" OMARCHY_OPT_PATH="$opt_path" \ + TEST_LOG="$calls" PATH="$stub_bin:$PATH" "$@" >/dev/null +} + +install_browser() { + run bash "$ROOT/bin/omarchy-install-browser" "$1" +} + +links_made() { + grep -c '^sudo:ln ' "$calls" || true +} + +assert_cdm_linked() { + [[ -L $opt_path/$1/WidevineCdm && $(readlink "$opt_path/$1/WidevineCdm") == "$cdm" ]] || + fail "$2" "$(ls -la "$opt_path/$1" 2>&1)" +} + +for browser in brave brave-origin; do + install_dir=$browser-bin + + reset_opt WidevineCdm/chromium "$install_dir" + install_browser "$browser" + assert_cdm_linked "$install_dir" "$browser install links the system Widevine CDM into $install_dir" + install_browser "$browser" + (( $(links_made) == 0 )) || fail "$browser reinstall keeps the existing CDM link" "$(<"$calls")" + assert_cdm_linked "$install_dir" "$browser reinstall keeps the existing CDM link" + pass "$browser install links the system Widevine CDM into $install_dir once" + + reset_opt "$install_dir" + install_browser "$browser" + [[ ! -e $opt_path/$install_dir/WidevineCdm && ! -L $opt_path/$install_dir/WidevineCdm ]] || + fail "$browser install links nothing without the widevine package" + pass "$browser install links nothing without the widevine package" + + reset_opt WidevineCdm/chromium "$install_dir" own-cdm + ln -s "$opt_path/own-cdm" "$opt_path/$install_dir/WidevineCdm" + install_browser "$browser" + [[ $(readlink "$opt_path/$install_dir/WidevineCdm") == "$opt_path/own-cdm" ]] || + fail "$browser install keeps a CDM the browser already has" + pass "$browser install keeps a CDM the browser already has" +done + +reset_opt WidevineCdm/chromium +install_browser chromium +(( $(links_made) == 0 )) || fail "Chromium install leaves Widevine to the widevine package" "$(<"$calls")" +pass "Chromium install leaves Widevine to the widevine package" diff --git a/test/shell.d/brcmfmac-supplicant-test.sh b/test/shell.d/brcmfmac-supplicant-test.sh index 7e476d73de61..2b0c343bfee4 100755 --- a/test/shell.d/brcmfmac-supplicant-test.sh +++ b/test/shell.d/brcmfmac-supplicant-test.sh @@ -9,6 +9,11 @@ fix_t2="$ROOT/install/hardware/apple/fix-t2.sh" all="$ROOT/install/hardware/all.sh" migration="$ROOT/migrations/1786391100.sh" +# Apple Silicon's BCM4387 is in the ID list, but there the firmware supplicant +# is the one that works, so the leaf and the migration step aside: a fresh +# install marks the migration done only for its first user. +grep -Fq 'omarchy-hw-apple-silicon && exit 0' "$migration" || fail "Broadcom migration excludes Apple Silicon" + grep -q 'apple/fix-brcmfmac-supplicant.sh' "$all" || fail "the brcmfmac quirk runs during hardware setup" @@ -61,13 +66,19 @@ printf '\t%s' "$@" >>"$TEST_LOG" printf '\n' >>"$TEST_LOG" SH +cat >"$stub_bin/omarchy-hw-apple-silicon" <<'SH' +#!/bin/bash + +(( ${APPLE_SILICON:-0} == 1 )) +SH + chmod +x "$stub_bin"/* # The leaf reads the vendor from an absolute path, so point it at a fixture by # running with a fake root on PATH-independent state. pipefail is on, so a # grep -q gate would go silent here the way #6608 did. run_leaf() { - local vendor="$1" wifi_id="${2:-}" t2="${3:-0}" + local vendor="$1" wifi_id="${2:-}" t2="${3:-0}" apple_silicon="${4:-0}" rm -rf "$test_tmp/etc" mkdir -p "$test_tmp/etc" printf '%s' "$vendor" >"$test_tmp/dmi/sys_vendor" @@ -78,7 +89,7 @@ run_leaf() { -e "s|/etc/modprobe.d|$test_tmp/etc/modprobe.d|g" \ "$leaf" >"$script" - WIFI_ID="$wifi_id" T2_HARDWARE="$t2" PATH="$stub_bin:$PATH" \ + WIFI_ID="$wifi_id" T2_HARDWARE="$t2" APPLE_SILICON="$apple_silicon" PATH="$stub_bin:$PATH" \ bash -eE -o pipefail -c 'source "$1"' bash "$script" /dev/null +[[ ! -f $conf ]] || fail "Apple Silicon is excluded from the Intel Mac quirk" +pass "Apple Silicon is excluded from the Intel Mac quirk" + # Older Macs report the vendor differently. run_leaf "Apple Computer, Inc." 43ba 0 >/dev/null [[ -f $conf ]] || fail "the older Apple vendor string is recognized" @@ -120,11 +135,11 @@ pass "a Mac with no wireless device is left alone" # Installs that predate the quirk never ran the leaf, so the migration has to # reach them. It runs as the user under pipefail, the context #6608 was about. run_migration() { - local vendor="$1" wifi_id="${2:-}" t2="${3:-0}" + local vendor="$1" wifi_id="${2:-}" t2="${3:-0}" apple_silicon="${4:-0}" printf '%s' "$vendor" >"$test_tmp/dmi/sys_vendor" : >"$calls" - WIFI_ID="$wifi_id" T2_HARDWARE="$t2" PATH="$stub_bin:$PATH" TEST_LOG="$calls" \ + WIFI_ID="$wifi_id" T2_HARDWARE="$t2" APPLE_SILICON="$apple_silicon" PATH="$stub_bin:$PATH" TEST_LOG="$calls" \ OMARCHY_BRCMFMAC_DMI_VENDOR="$test_tmp/dmi/sys_vendor" \ OMARCHY_BRCMFMAC_CONF="$conf" \ bash -euo pipefail "$migration" >/dev/null @@ -148,6 +163,12 @@ run_migration "Apple Inc." 4488 1 [[ ! -s $calls ]] || fail "a repaired install is left untouched" "$(cat "$calls")" pass "the migration is idempotent" +rm -rf "$test_tmp/etc" +run_migration "Apple Inc." 4433 0 1 +[[ ! -e $conf ]] || fail "the migration leaves Apple Silicon Wi-Fi alone" +[[ ! -s $calls ]] || fail "the migration escalates nothing on Apple Silicon" "$(cat "$calls")" +pass "the migration leaves Apple Silicon Wi-Fi alone" + # The machine this was written for, with no T2 to fall back on. rm -rf "$test_tmp/etc" run_migration "Apple Inc." 43ba 0 diff --git a/test/shell.d/brightness-display-test.sh b/test/shell.d/brightness-display-test.sh index 3e41c511ae06..4399f437cb4e 100755 --- a/test/shell.d/brightness-display-test.sh +++ b/test/shell.d/brightness-display-test.sh @@ -53,9 +53,19 @@ SH chmod +x "$mock_bin"/* +# A copy of the command reads its platform file from a fixture root (none +# unless a case writes one, whatever the machine running the suite has +# installed) and its DRM connectors from a fixture directory. +platform_root="$test_tmp/platform" +mkdir -p "$platform_root" "$test_tmp/copy" +brightness_display="$test_tmp/copy/omarchy-brightness-display" +platform_root_copy "$ROOT/bin/omarchy-brightness-display" "$brightness_display" "$platform_root" +sed -i "s|/sys/class/drm|$test_tmp/drm|g" "$brightness_display" +grep -qF "$test_tmp/drm/card" "$brightness_display" || fail "omarchy-brightness-display reads DRM connectors from the fixture" + run_brightness() { - CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" PATH="$mock_bin:$ROOT/bin:$PATH" \ - "$ROOT/bin/omarchy-brightness-display" "$@" + CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" \ + PATH="$mock_bin:$ROOT/bin:$PATH" "$brightness_display" "$@" } brightness=$(run_brightness --monitor DP-1) @@ -144,3 +154,43 @@ if PATH="$mock_bin:$PATH" "$ROOT/bin/omarchy-hyprland-monitor-focused-apple"; th fail "focused non-Apple display is not detected as Apple" fi pass "named Apple display is detected independently of focus" + +# A platform whose display driver has no DDC channel says so: an external +# monitor whose connector has no ddc node is not probed and never dims the +# built-in panel in its place. +mkdir -p "$test_tmp/drm/card2-USB-1" "$test_tmp/drm/card2-eDP-1" +# The last line needs no newline. +printf '%s\n%s' '# no DDC channel on the display driver' 'ddc-require-connector-ddc' >"$platform_root/displays.conf" +: >"$call_log" +if run_brightness --monitor USB-1 >/dev/null 2>&1; then + fail "an external monitor without a ddc node has no brightness backend" +fi +if run_brightness --no-osd --monitor USB-1 +5% >/dev/null 2>&1; then + fail "setting an external monitor without a ddc node fails" +fi +printf 'ddc-require-connector-ddc\r\n' >"$platform_root/displays.conf" +if run_brightness --monitor USB-1 >/dev/null 2>&1; then + fail "a displays.conf saved with CRLF line ends reads the same" +fi +printf '%s\n%s' '# no DDC channel on the display driver' 'ddc-require-connector-ddc' >"$platform_root/displays.conf" +[[ ! -s $call_log ]] || fail "an external monitor without a ddc node probes nothing" "$(<"$call_log")" +brightness=$(run_brightness --monitor eDP-1) +(( brightness == 40 )) || fail "the built-in panel still uses the kernel backlight" "actual: $brightness" +pass "a platform without DDC channels skips DDC for a connector without one" + +rm -f "$runtime_dir/omarchy-brightness-display-ddc/"* +: >"$test_tmp/drm/card2-USB-1/ddc" +brightness=$(DDC_CONNECTOR=USB-1 run_brightness --monitor USB-1) +(( brightness == 50 )) || fail "an external monitor with a ddc node uses DDC" "actual: $brightness" +pass "a platform without DDC channels still uses DDC where a connector has one" + +# Without the directive, every external monitor is probed, ddc node or not. +rm -f "$runtime_dir/omarchy-brightness-display-ddc/"* "$test_tmp/drm/card2-USB-1/ddc" +for conf in "" "ddc-require-connector-ddc extra" "ddc-future"; do + printf '%s\n' "$conf" >"$platform_root/displays.conf" + rm -f "$runtime_dir/omarchy-brightness-display-ddc/"* + brightness=$(DDC_CONNECTOR=USB-1 run_brightness --monitor USB-1) + (( brightness == 50 )) || fail "an external monitor is probed without the directive" "conf: $conf, actual: $brightness" +done +rm -f "$platform_root/displays.conf" +pass "every external monitor is probed without the directive" diff --git a/test/shell.d/brightness-keyboard-auto-test.sh b/test/shell.d/brightness-keyboard-auto-test.sh new file mode 100755 index 000000000000..e98404214c5b --- /dev/null +++ b/test/shell.d/brightness-keyboard-auto-test.sh @@ -0,0 +1,305 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +auto="$ROOT/bin/omarchy-brightness-keyboard-auto" + +[[ -x $auto ]] || fail "omarchy-brightness-keyboard-auto is executable" + +map_lux() { + "$auto" --map-lux "$1" +} + +[[ $(map_lux 0) == 100 ]] || fail "pitch dark lights the keyboard fully" "got $(map_lux 0)" +[[ $(map_lux 8) == 100 ]] || fail "dim indoor still uses full keyboard light" "got $(map_lux 8)" +[[ $(map_lux 94) == 50 ]] || fail "mid lux maps to half keyboard light" "got $(map_lux 94)" +[[ $(map_lux 180) == 0 ]] || fail "bright room turns the keyboard light off" "got $(map_lux 180)" +[[ $(map_lux 400) == 0 ]] || fail "daylight keeps the keyboard light off" "got $(map_lux 400)" +pass "ambient lux maps inversely onto keyboard backlight" + +if ! "$auto" --map-lux >/dev/null 2>&1; then + pass "map-lux without a value is an error" +else + fail "map-lux without a value should fail" +fi + +grep -F 'Drive keyboard backlight from the ambient light sensor' "$auto" >/dev/null +pass "auto helper declares command metadata" + +grep -F 'POLL_SECONDS=5' "$auto" >/dev/null || fail "ALS keyboard loop still wakes every second" +pass "ALS keyboard loop polls every 5 seconds" + +grep -F 'exit $?' "$auto" >/dev/null && + fail "--available still relies on set -e to turn a failed [[ ]] into the exit status" +pass "--available uses an explicit if/else exit" + +eval "$(sed -n '/^find_als()/,/^}/p' "$auto")" + +fake=$(mktemp -d) +leds=$(mktemp -d) +trap 'rm -rf "$fake" "$leds"' EXIT +mkdir -p "$fake/iio:device0" "$fake/iio:device1" "$fake/iio:device2" + +printf 'aop-sensors-las\n' >"$fake/iio:device0/name" +printf '12\n' >"$fake/iio:device0/in_illuminance_raw" +printf 'aop-sensors-als\n' >"$fake/iio:device1/name" +printf '23\n' >"$fake/iio:device1/in_illuminance_input" +printf 'ambient-light\n' >"$fake/iio:device2/name" +printf '40\n' >"$fake/iio:device2/in_illuminance_input" + +got=$(OMARCHY_IIO_DEVICES_DIR=$fake find_als) +[[ $got == "$fake/iio:device1/in_illuminance_input" ]] || + fail "find_als prefers a device whose name contains als" "got $got" +pass "find_als prefers a named ALS device over an earlier illuminance channel" + +rm -r "$fake/iio:device1" +got=$(OMARCHY_IIO_DEVICES_DIR=$fake find_als) +[[ $got == "$fake/iio:device0/in_illuminance_raw" ]] || + fail "find_als falls back to the first readable illuminance channel" "got $got" +pass "find_als falls back when no device name contains als" + +# A raw channel is lux only after its offset and scale. +raw_dir=$(mktemp -d) +printf '9400\n' >"$raw_dir/in_illuminance_raw" +printf '0.01\n' >"$raw_dir/in_illuminance_scale" +eval "$(sed -n '/^read_lux()/,/^}/p' "$auto")" +als_path="$raw_dir/in_illuminance_raw" +[[ $(read_lux) == 94 ]] || fail "a raw channel is scaled into lux" "got $(read_lux)" +printf '10\n' >"$raw_dir/in_illuminance_offset" +[[ $(read_lux) == 94 ]] || fail "a raw channel adds its offset before scaling" "got $(read_lux)" +printf '9390\n' >"$raw_dir/in_illuminance_raw" +[[ $(read_lux) == 94 ]] || fail "a raw channel adds its offset before scaling" "got $(read_lux)" +printf '94.7\n' >"$raw_dir/in_illuminance_input" +als_path="$raw_dir/in_illuminance_input" +[[ $(read_lux) == 94 ]] || fail "a processed channel is lux already, scale ignored" "got $(read_lux)" +printf 'bogus\n' >"$raw_dir/in_illuminance_input" +if read_lux >/dev/null; then fail "an unreadable value is not a lux reading"; fi +rm -rf "$raw_dir" +unset als_path +pass "raw light sensor channels are converted to lux with their offset and scale" + +mkdir -p "$leds/kbd_backlight" +printf '255\n' >"$leds/kbd_backlight/max_brightness" +printf '0\n' >"$leds/kbd_backlight/brightness" + +if OMARCHY_IIO_DEVICES_DIR=$fake OMARCHY_LEDS_DIR=$leds "$auto" --available; then + pass "--available succeeds when both ALS and keyboard LED are present" +else + fail "--available should succeed when both ALS and keyboard LED are present" +fi + +rm -r "$leds/kbd_backlight" +if OMARCHY_IIO_DEVICES_DIR=$fake OMARCHY_LEDS_DIR=$leds "$auto" --available; then + fail "--available should fail when the keyboard LED is missing" +else + pass "--available fails when the keyboard LED is missing" +fi + +manual="$ROOT/manual/34-keyboard-mouse-trackpad.md" +grep -F 'Lock and lid-close keep the keys off' "$manual" >/dev/null && + fail "manual still claims lock and lid-close turn the keys off" +grep -F 'Automatic control pauses while the screen is locked or the lid is closed' "$manual" >/dev/null || + fail "manual does not describe lock and lid-close as a pause" +pass "manual describes lock and lid-close as pausing automatic control" + +# Drive the real keyboard-brightness command and the loop's tick against a fake +# sensor and LED, the way lock blanking, wake restore and the keys interleave. +loop=$(mktemp -d) +trap 'rm -rf "$fake" "$leds" "$loop"' EXIT +mkdir -p "$loop/iio/iio:device1" "$loop/leds/kbd_backlight" "$loop/bin" "$loop/runtime" +printf 'aop-sensors-als\n' >"$loop/iio/iio:device1/name" +printf '26\n' >"$loop/iio/iio:device1/in_illuminance_input" +printf '255\n' >"$loop/leds/kbd_backlight/max_brightness" +printf '0\n' >"$loop/leds/kbd_backlight/brightness" + +cat >"$loop/bin/brightnessctl" <<'SH' +#!/bin/bash +save=0 +restore=0 +device="" +while (( $# )); do + case $1 in + -sd) save=1; device=$2; shift 2 ;; + -rd) restore=1; device=$2; shift 2 ;; + -d) device=$2; shift 2 ;; + -m) shift ;; + *) break ;; + esac +done +led="$OMARCHY_LEDS_DIR/$device" +if (( restore )); then + [[ ${RESTORE_FAILS:-0} == 0 ]] || exit 1 + cp "$led/saved" "$led/brightness" + exit 0 +fi +case ${1:-} in + get) cat "$led/brightness" ;; + max) cat "$led/max_brightness" ;; + set) + (( ! save )) || cp "$led/brightness" "$led/saved" + printf '%s\n' "$2" >"$led/brightness" + ;; +esac +SH +cat >"$loop/bin/omarchy-hyprland-session-locked" <<'SH' +#!/bin/bash +[[ ${LOCKED:-0} == "1" ]] +SH +cat >"$loop/bin/omarchy-hw-laptop-closed" <<'SH' +#!/bin/bash +exit 1 +SH +chmod +x "$loop/bin/"* + +export PATH="$loop/bin:$PATH" OMARCHY_LEDS_DIR="$loop/leds" XDG_RUNTIME_DIR="$loop/runtime" +eval "$(grep -E '^(DARK_LUX|BRIGHT_LUX|DEADBAND_PERCENT|OVERRIDE_LUX_DELTA|OVERRIDE_LUX_RATIO|EFFECTIVELY_OFF_PERCENT|MANUAL_LEVEL_FILE)=' "$auto")" +for fn in lux_to_percent read_lux session_locked lid_closed apply_percent left_off tick; do + eval "$(sed -n "/^$fn()/,/^}/p" "$auto")" +done +als_path="$loop/iio/iio:device1/in_illuminance_input" +device=kbd_backlight +max=255 +last_set="" +paused=0 +pause_lux=0 + +keys() { "$ROOT/bin/omarchy-brightness-keyboard" --no-osd "$1"; } +# Step down with the keys, letting the loop see each press, until they are off. +keys_to_off() { + for _ in {1..12}; do + (( $(led) == 0 )) && return 0 + keys down + tick + done + fail "the keyboard-brightness keys turn the keys off" "got $(led)" +} +led() { cat "$loop/leds/kbd_backlight/brightness"; } +lux() { printf '%s\n' "$1" >"$als_path"; } + +tick +(( $(led) == 226 )) || fail "a dark room lights the keys" "got $(led)" +keys off +LOCKED=1 tick +(( $(led) == 0 )) || fail "a locked session keeps the keys blank" "got $(led)" +tick +(( $(led) == 226 )) || fail "keys left blank after unlock light up again" "got $(led)" +printf '2\n' >"$loop/leds/kbd_backlight/brightness" +tick +(( $(led) == 226 )) || fail "a 1% leftover lights up again" "got $(led)" +pass "keys left off by lock blanking or a leftover light up again with the room" + +keys_to_off +tick +(( $(led) == 0 )) || fail "keys turned off with the brightness keys stay off" "got $(led)" +keys off +LOCKED=1 tick +keys restore +tick +(( $(led) == 0 )) || fail "a deliberate off survives lock and wake" "got $(led)" +lux 150 +tick +(( $(led) == 43 )) || fail "a deliberate off resumes once the room changes enough" "got $(led)" +keys off +tick +(( $(led) == 43 )) || fail "after auto resumes, a lock blank is a leftover again" "got $(led)" +pass "keys turned off by hand stay off until the room changes enough" + +keys up +tick +tick +(( $(led) == 68 )) || fail "a visible level set by hand still pauses auto" "got $(led)" +pass "a visible level set by hand still pauses automatic control" + +lux 26 +tick +keys_to_off +printf '2\n' >"$loop/leds/kbd_backlight/brightness" +tick +(( $(led) == 226 )) || fail "a leftover after a deliberate off lights up again" "got $(led)" +keys off +tick +(( $(led) == 226 )) || fail "a relit leftover forgets the earlier deliberate off" "got $(led)" +pass "a relit leftover forgets the earlier deliberate off" + +keys_to_off +last_set="" +paused=0 +tick +(( $(led) == 226 )) || fail "a restarted loop takes the keys back" "got $(led)" +keys off +tick +(( $(led) == 226 )) || fail "a restarted loop forgets the earlier deliberate off" "got $(led)" +pass "a restarted loop forgets the earlier deliberate off" + +lux 400 +tick +(( $(led) == 0 )) || fail "a bright room turns the keys off" "got $(led)" +keys down +tick +lux 26 +for _ in 1 2; do tick; done +(( $(led) == 226 )) || fail "the room darkening lights the keys again" "got $(led)" +keys off +tick +(( $(led) == 226 )) || fail "a level auto changed forgets the earlier key press" "got $(led)" +pass "any level auto sets forgets an earlier key press" + +# A firmware Fn key (ThinkPad and Framework Fn+Space, Dell Fn+F10) writes the +# LED directly, past omarchy-brightness-keyboard. Its off is a choice as much as +# the keys' is. +lux 26 +tick +tick +(( $(led) == 226 )) || fail "a dark room lights the keys before the Fn key" "got $(led)" +printf '0\n' >"$loop/leds/kbd_backlight/brightness" +for _ in 1 2 3; do tick; done +(( $(led) == 0 )) || fail "keys turned off with a firmware key stay off" "got $(led)" +lux 150 +tick +(( $(led) == 43 )) || fail "a firmware off resumes once the room changes enough" "got $(led)" +pass "keys turned off with a firmware key stay off until the room changes enough" + +# A wake whose restore fails leaves the keys blank, and still recorded as the +# lock's blank, so auto takes them back. +lux 26 +tick +tick +(( $(led) == 226 )) || fail "a dark room lights the keys before the lock" "got $(led)" +keys off +LOCKED=1 tick +RESTORE_FAILS=1 keys restore && fail "a failed restore reports failure" +tick +(( $(led) == 226 )) || fail "keys a failed restore left blank light up again" "got $(led)" +pass "keys a failed wake restore left blank light up again" + +# A lock blank auto takes back without moving the LED (a bright room, where it +# wants them off anyway) is spent: a later firmware off is still a choice. +lux 400 +tick +(( $(led) == 0 )) || fail "a bright room turns the keys off before the lock" "got $(led)" +printf '226\n' >"$loop/leds/kbd_backlight/brightness" +tick +keys off +LOCKED=1 tick +lux 180 +tick +printf '255\n' >"$loop/leds/kbd_backlight/brightness" +tick +lux 150 +printf '0\n' >"$loop/leds/kbd_backlight/brightness" +for _ in 1 2; do tick; done +(( $(led) == 0 )) || fail "a firmware off after a spent lock blank stays off" "got $(led)" +pass "a lock blank auto already took back never stands in for a later firmware off" + +# Without a session runtime directory the keys record their level in a private +# state directory they create, never under a fixed name in /tmp. +fallback_home="$loop/home" +mkdir -p "$fallback_home" +env -u XDG_RUNTIME_DIR -u XDG_STATE_HOME HOME="$fallback_home" "$ROOT/bin/omarchy-brightness-keyboard" --no-osd up +[[ -s $fallback_home/.local/state/omarchy/omarchy-brightness-keyboard-manual ]] || + fail "the keys record their level without a runtime directory" +[[ $(stat -c %a "$fallback_home/.local/state/omarchy") == 700 ]] || + fail "the fallback state directory is private" "$(stat -c %a "$fallback_home/.local/state/omarchy")" +pass "without a runtime directory the keys record their level in a private state directory" diff --git a/test/shell.d/channel-sudo-boundary-test.sh b/test/shell.d/channel-sudo-boundary-test.sh index f98934c2bb20..c8acc3cddbff 100755 --- a/test/shell.d/channel-sudo-boundary-test.sh +++ b/test/shell.d/channel-sudo-boundary-test.sh @@ -69,6 +69,51 @@ for channel in stable rc edge dev; do pass "$channel starts cold, authorizes the switch per command, runs the refresh hook cold, hands off to one update authorization and exits cold" done +# Every aarch64 platform switches to edge and dev, copying its own templates. +for platform in qualcomm generic-aarch64 apple-silicon; do + case $platform in + apple-silicon) templates=default/pacman/apple-silicon ;; + *) templates=default/pacman/aarch64 ;; + esac + for channel in edge dev; do + # dev refreshes from the checkout it links, on edge. + pacman_channel=$channel root=$SUDO_TEST_ROOT + [[ $channel != "dev" ]] || pacman_channel=edge root=$SUDO_TEST_HOME/omarchy + reset_boundary + SUDO_TEST_PLATFORM=$platform run_channel "$channel" || fail "$platform $channel failed" "$(<"$boundary_tmp/output")" + assert_scoped_channel "$platform $channel" + grep -Fqx "step:cp -f $root/$templates/pacman-$pacman_channel.conf /etc/pacman.conf" "$SUDO_TEST_LOG" || + fail "$platform $channel copies its own template" "$(<"$SUDO_TEST_LOG")" + done +done +pass "aarch64 platforms switch to edge and dev through their own templates" + +# stable and rc would install the release line, which has no aarch64 support: +# an aarch64 machine refuses them before anything changes. +for platform in qualcomm generic-aarch64 apple-silicon; do + for channel in stable rc; do + reset_boundary + if SUDO_TEST_PLATFORM=$platform run_channel "$channel"; then fail "$platform accepted $channel"; fi + if grep -Eq '^step:|^sudo -N ' "$SUDO_TEST_LOG"; then fail "$platform $channel was refused before any change" "$(<"$SUDO_TEST_LOG")"; fi + grep -q "Omarchy has no $channel channel for $platform" "$boundary_tmp/output" || fail "$platform $channel: the refusal says why" "$(<"$boundary_tmp/output")" + assert_boundary_cold "$platform $channel" + done +done +pass "aarch64 platforms refuse stable and rc before any change" + +# A channel the platform has no template for stops before anything, the dev +# confirmation included. +mv "$SUDO_TEST_ROOT/default/pacman/apple-silicon/pacman-edge.conf" "$boundary_tmp/saved-template" +for channel in edge dev; do + reset_boundary + if SUDO_TEST_PLATFORM=apple-silicon run_channel "$channel"; then fail "a channel without a template was accepted ($channel)"; fi + if grep -Eq '^step:|^sudo -N ' "$SUDO_TEST_LOG"; then fail "$channel was refused before any change" "$(<"$SUDO_TEST_LOG")"; fi + grep -q "Omarchy has no edge channel for apple-silicon" "$boundary_tmp/output" || fail "the refusal says why" "$(<"$boundary_tmp/output")" + assert_boundary_cold "missing template $channel" +done +mv "$boundary_tmp/saved-template" "$SUDO_TEST_ROOT/default/pacman/apple-silicon/pacman-edge.conf" +pass "a channel without a template for the platform is refused before any change" + reset_boundary wrapper="$SUDO_TEST_HOME/omarchy/default/omarchy/sudo-no-update/sudo" mv "$wrapper" "$boundary_tmp/saved-wrapper" @@ -81,6 +126,40 @@ assert_boundary_cold "stale checkout" mv "$boundary_tmp/saved-wrapper" "$wrapper" pass "a stale dev checkout is rejected before linking or privileged work" +# On aarch64 the checkout's own refresh keeps the machine's repositories only +# once it tells platforms apart: a checkout without that is refused the same way. +checkout="$SUDO_TEST_HOME/omarchy" +for required in bin/omarchy-hw-platform install/helpers/pacman.sh; do + for platform in qualcomm generic-aarch64; do + reset_boundary + mv "$checkout/$required" "$boundary_tmp/saved-required" + if SUDO_TEST_PLATFORM=$platform run_channel dev; then fail "$platform: a dev checkout without $required was accepted"; fi + mv "$boundary_tmp/saved-required" "$checkout/$required" + if grep -Eq '^step:omarchy-(dev-link|state)|^step:pacman|^sudo -N ' "$SUDO_TEST_LOG"; then + fail "$platform: a dev checkout without $required changed the system before rejection" "$(<"$SUDO_TEST_LOG")" + fi + grep -q "Update the checkout before switching to dev; on $platform it needs $required" "$boundary_tmp/output" || + fail "$platform: the rejection names $required" "$(<"$boundary_tmp/output")" + assert_boundary_cold "$platform checkout without $required" + done +done +pass "on aarch64 a dev checkout that can't keep the machine's repositories is rejected before linking" + +# The checkout's own refresh copies its own templates, so one without them for +# this platform is refused before linking. +template=$checkout/default/pacman/aarch64/mirrorlist-edge +mv "$template" "$boundary_tmp/saved-template" +reset_boundary +if SUDO_TEST_PLATFORM=qualcomm run_channel dev; then fail "a dev checkout without its templates was accepted"; fi +mv "$boundary_tmp/saved-template" "$template" +if grep -Eq '^step:omarchy-(dev-link|state)|^step:pacman|^sudo -N ' "$SUDO_TEST_LOG"; then + fail "a dev checkout without its templates changed the system before rejection" "$(<"$SUDO_TEST_LOG")" +fi +grep -q "Update the checkout before switching to dev; it has no edge templates for qualcomm" "$boundary_tmp/output" || + fail "the rejection names the missing templates" "$(<"$boundary_tmp/output")" +assert_boundary_cold "checkout without templates" +pass "a dev checkout without its templates for the platform is rejected before linking" + reset_boundary OMARCHY_PATH="$SUDO_TEST_HOME/omarchy" run_channel stable || fail "leaving dev failed" "$(<"$boundary_tmp/output")" assert_scoped_channel "dev to stable" diff --git a/test/shell.d/default-apps-test.sh b/test/shell.d/default-apps-test.sh index d4aaa26a46d3..b450c2993dbe 100755 --- a/test/shell.d/default-apps-test.sh +++ b/test/shell.d/default-apps-test.sh @@ -125,6 +125,9 @@ for setup_command in \ ln -s omarchy-test-setup-call "$mock_bin/$setup_command" done +# The real browser installer below calls the platform's post-install hook; on a +# Mac with the platform package installed that would run, so answer it here. +printf '#!/bin/bash\nexit 0\n' >"$mock_bin/omarchy-lifecycle-dispatch" chmod +x "$mock_bin"/* export HOME="$test_home" diff --git a/test/shell.d/desktop-entry-launch-test.sh b/test/shell.d/desktop-entry-launch-test.sh index c6014b103d6c..16fe5a0552a1 100644 --- a/test/shell.d/desktop-entry-launch-test.sh +++ b/test/shell.d/desktop-entry-launch-test.sh @@ -22,7 +22,7 @@ cat >"$mock_bin/omarchy-font-set" <<'SH' printf 'font:%s\n' "$*" >>"$OMARCHY_TEST_LOG" SH -for command in omarchy-pkg-aur-add omarchy-install-emacs omazed omarchy-theme-set-vscode omarchy-install-gaming-gpu-lib32; do +for command in omarchy-pkg-aur-add omarchy-install-emacs omazed omarchy-theme-set-vscode omarchy-install-gaming-gpu-lib32 omarchy-lifecycle-dispatch; do cat >"$mock_bin/$command" <<'SH' #!/bin/bash exit 0 diff --git a/test/shell.d/drive-password-test.sh b/test/shell.d/drive-password-test.sh index 7cd32b178377..673aef7766b7 100644 --- a/test/shell.d/drive-password-test.sh +++ b/test/shell.d/drive-password-test.sh @@ -4,46 +4,786 @@ set -euo pipefail source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" -tmp_dir=$(mktemp -d) -trap 'rm -r "$tmp_dir"' EXIT +# omarchy-drive-password against fake drives. findmnt and lsblk describe a +# system drive under / and a data drive, and blkid has the empty cache a user +# sees until root runs blkid in that boot; cryptsetup is either a slot-table fake +# or the real binary on file-backed volumes; chpasswd records the accounts. Each +# cryptsetup and chpasswd call is a crash point, so a run can be killed after +# every step and rerun, like a power loss. The runs repeat on an Apple fixture, +# where a fake boot package records the owner's slot through the real +# omarchy-lifecycle-dispatch; on the x86 fixture that is a no-op. -cat >"$tmp_dir/blkid" <<'EOF' +# Root's dispatcher ignores the fixtures and sees this machine. +if (( EUID == 0 )) && [[ $("$ROOT/bin/omarchy-hw-platform") == "apple-silicon" ]]; then + skip "running as root on Apple Silicon, where dispatch ignores fixtures; skipping" + exit 0 +fi + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +old_password=old-password +new_password='new pass:word' +recovery_key=recovery-passphrase +data_password=data-password +journal=$tmp/state/omarchy/drive-password.state + +mkdir -p "$tmp/bin" "$tmp/real" "$tmp/dev" + +cat >"$tmp/crash.sh" <<'SH' +crash_point() { + local count + count=$(( $(cat "$TEST_TMP/steps") + 1 )) + echo "$count" >"$TEST_TMP/steps" + printf '%s %s\n' "$count" "$1" >>"$TEST_TMP/trace" + if (( count == ${CRASH_AT:-0} )); then + kill -9 "$TEST_PID" + # An orphan outlives the script for a moment, like a cryptsetup child. + if [[ -n ${CRASH_ORPHAN:-} ]]; then sleep 1; else kill -9 $$; fi + fi +} +SH + +cat >"$tmp/bin/blkid" <<'SH' +#!/bin/bash +exit 2 +SH + +cat >"$tmp/bin/findmnt" <<'SH' +#!/bin/bash +[[ $* == "-no SOURCE /" && -z ${TEST_FINDMNT_FAIL:-} ]] || exit 1 +echo '/dev/mapper/root[/@]' +SH + +cat >"$tmp/bin/lsblk" <<'SH' #!/bin/bash -echo /dev/test-luks -EOF +if [[ $1 == "-dno" && $2 == "UUID" ]]; then + cat "$3.uuid" + exit +fi +if [[ $* == "-nrpo NAME,FSTYPE" ]]; then + echo "/dev/fake-disk " + while IFS= read -r drive; do + printf '%s crypto_LUKS\n/dev/mapper/%s btrfs\n' "$drive" "${drive##*/}" + done <"$TEST_TMP/drives" + exit +fi +[[ $* == "-nsrpo NAME,TYPE,FSTYPE /dev/mapper/root" ]] || exit 98 +cat "$TEST_TMP/root-ancestry" +SH + +cat >"$tmp/bin/omarchy-drive-select" <<'SH' +#!/bin/bash +printf '%s\n' "$@" >"$TEST_TMP/offered" +cat "$TEST_TMP/select" +SH + +cat >"$tmp/bin/gum" <<'SH' +#!/bin/bash +[[ $1 == "input" ]] || exit 97 +printf '%s\n' "$*" >>"$TEST_TMP/prompts" +[[ -s $TEST_TMP/inputs ]] || exit 130 +# Another key enrolled while the owner types the new password. +if [[ -n ${TEST_ENROLL_AT_PROMPT:-} && $* == *"$TEST_ENROLL_AT_PROMPT"* ]]; then + printf '3\tlater-key\n' >>"$TEST_ENROLL_DEVICE.slots" +fi +head -n 1 "$TEST_TMP/inputs" +sed -i '1d' "$TEST_TMP/inputs" +SH -cat >"$tmp_dir/gum" <<'EOF' +# Runs as the caller, closing every descriptor above stderr as sudo does. +cat >"$tmp/bin/sudo" <<'SH' #!/bin/bash -head -n 1 "$TEST_INPUTS" -sed -i '1d' "$TEST_INPUTS" -EOF +printf '%s\n' "$*" >>"$TEST_TMP/sudo-calls" +for fd in /proc/$$/fd/*; do + fd=${fd##*/} + (( fd > 2 && fd != 255 )) && eval "exec $fd>&-" 2>/dev/null +done +exec "$@" +SH -cat >"$tmp_dir/sudo" <<'EOF' +cat >"$tmp/bin/chpasswd" <<'SH' #!/bin/bash -printf '%s\n' "$@" >"$TEST_ARGS" -cat >"$TEST_STDIN" -EOF +source "$TEST_TMP/crash.sh" +IFS= read -r line +name=${line%%:*} +crash_point "chpasswd $name" +[[ $name != "${TEST_CHPASSWD_FAIL:-}" ]] || exit 1 +{ + grep -v "^$name " "$TEST_TMP/accounts" || true + printf '%s\t%s\n' "$name" "${line#*:}" +} >"$TEST_TMP/accounts.next" +mv -f "$TEST_TMP/accounts.next" "$TEST_TMP/accounts" +crash_point "$name password set" +SH + +# A LUKS1-style volume as a table of "slotpassphrase" lines next to the +# device: luksChangeKey adds the new key to a free slot, then removes the old, +# and luksKillSlot needs a key that opens another slot, as cryptsetup does. +cat >"$tmp/bin/cryptsetup" <<'SH' +#!/bin/bash +source "$TEST_TMP/crash.sh" + +slot_of() { + local slot key + while IFS=$'\t' read -r slot key; do + [[ $key == "$1" && $slot != "${2:-}" ]] && { echo "$slot"; return; } + done <"$slots" +} -chmod +x "$tmp_dir/blkid" "$tmp_dir/gum" "$tmp_dir/sudo" -export PATH="$tmp_dir:$ROOT/bin:$PATH" -export TEST_ARGS="$tmp_dir/args" TEST_INPUTS="$tmp_dir/inputs" TEST_STDIN="$tmp_dir/stdin" +op=$1 +shift +key_file="" token_type="" positional=() +while (( $# )); do + case $1 in + --key-file) key_file=$2; shift 2 ;; + --token-type) token_type=$2; shift 2 ;; + --pbkdf | --iter-time) shift 2 ;; + -*) shift ;; + *) positional+=("$1"); shift ;; + esac +done +slots=${positional[0]}.slots +[[ -f $slots ]] || { echo "Device ${positional[0]} does not exist or access denied."; exit 4; } +if [[ $key_file == "-" ]]; then key=$(cat); elif [[ -n $key_file ]]; then key=$(cat "$key_file"); fi -printf '\n' >"$TEST_INPUTS" -if "$ROOT/bin/omarchy-drive-password" >/dev/null; then - fail "drive password rejects an empty passphrase" +[[ $op != "isLuks" ]] || exit 1 +if [[ $op == "luksDump" ]]; then + echo "Keyslots:" + cut -f1 "$slots" | sed 's/^/ /; s/$/: luks2/' + [[ -z ${TEST_TOKEN_SLOT:-} ]] || printf 'Tokens:\n 0: luks2-keyring\n\tKeyslot: %s\n' "$TEST_TOKEN_SLOT" + printf 'Digests:\n 0: pbkdf2\n' + exit 0 fi -[[ ! -e $TEST_ARGS ]] || fail "drive password does not run cryptsetup for an empty passphrase" -printf 'secret123\n*\n' >"$TEST_INPUTS" -if "$ROOT/bin/omarchy-drive-password" >/dev/null; then - fail "drive password rejects a mismatched confirmation" +crash_point "cryptsetup $op" +[[ $op != "open" || -z ${TEST_OPEN_FAIL:-} ]] || exit 1 +# An enrolled token unlocks its slot whatever key is given, unless the allowed +# token types exclude it, as with cryptsetup. +if [[ $op == "open" && -z $token_type && -n ${TEST_TOKEN_SLOT:-} ]]; then + echo "Key slot $TEST_TOKEN_SLOT unlocked." + exit 0 fi -[[ ! -e $TEST_ARGS ]] || fail "drive password does not run cryptsetup for a mismatched confirmation" +target="" +[[ $op == "luksKillSlot" ]] && target=${positional[1]} +slot=$(slot_of "$key" "$target") +[[ -n $slot ]] || { echo "No key available with this passphrase."; exit 2; } + +case $op in + open) echo "Key slot $slot unlocked." ;; + luksChangeKey) + [[ -z ${TEST_CHANGE_FAIL:-} ]] || exit 1 + for (( next = 0; next < 32; next++ )); do + cut -f1 "$slots" | grep -qx "$next" || break + done + printf '%s\t%s\n' "$next" "$(cat "${positional[1]}")" >>"$slots" + crash_point "new key added" + [[ -z ${TEST_CHANGE_PARTIAL:-} ]] || exit 137 + awk -F'\t' -v s="$slot" '$1 != s' "$slots" >"$slots.next" + mv -f "$slots.next" "$slots" + crash_point "old key removed" + ;; + luksKillSlot) + [[ -z ${TEST_KILL_FAIL:-} ]] || exit 1 + awk -F'\t' -v s="$target" '$1 != s' "$slots" >"$slots.next" + mv -f "$slots.next" "$slots" + crash_point "slot $target killed" + ;; + *) exit 1 ;; +esac +SH + +# The real cryptsetup, keeping the command's KDF but at a fast cost, counting +# the same crash points. +cat >"$tmp/real/cryptsetup" <<'SH' +#!/bin/bash +source "$TEST_TMP/crash.sh" +args=() +while (( $# )); do + case $1 in + --iter-time) shift 2 ;; + *) args+=("$1"); shift ;; + esac +done +if [[ ${args[0]} == "luksChangeKey" ]]; then + if [[ " ${args[*]} " == *" argon2id "* ]]; then + args+=(--pbkdf-force-iterations 4 --pbkdf-memory 32768) + else + args+=(--pbkdf-force-iterations 1000) + fi +fi +[[ ${args[0]} == "luksDump" || ${args[0]} == "isLuks" ]] || crash_point "cryptsetup ${args[0]}" +[[ ${args[0]} != "open" || -z ${TEST_OPEN_FAIL:-} ]] || exit 1 +status=0 +"$REAL_CRYPTSETUP" "${args[@]}" || status=$? +if (( status == 0 )) && [[ ${args[0]} == "luksChangeKey" || ${args[0]} == "luksKillSlot" ]]; then + crash_point "${args[0]} done" +fi +exit "$status" +SH + +chmod +x "$tmp"/bin/* "$tmp/real/cryptsetup" + +# The boot package's luks-slots, run by dispatch with an empty environment: it +# records its arguments and is a crash point like the rest. +fake_platform "$tmp/x86" generic +fake_platform "$tmp/apple" apple-silicon +mkdir -p "$tmp/lifecycle/usr/lib/omarchy/mac-boot" +cat >"$tmp/lifecycle/usr/lib/omarchy/mac-boot/luks-slots" <&2; exit 1; } + [[ ! -e $tmp/owner-query-garbage ]] || { echo "slot zero"; exit 0; } + [[ ! -e $tmp/owner-slot ]] || { cat "$tmp/owner-slot"; exit 0; } + [[ ! -e $tmp/owner-unrecorded ]] || { echo "No owner key slot is recorded." >&2; exit 4; } + echo 0 + exit 0 +fi +[[ ! -e $tmp/record-fail ]] || exit 1 +printf '%s\n' "\$*" >>"$tmp/slot-record" +[[ \$1 != owner=* ]] || echo "\${1#owner=}" >"$tmp/owner-slot" +count=\$(( \$(cat "$tmp/steps") + 1 )) +echo "\$count" >"$tmp/steps" +printf '%s slots recorded\n' "\$count" >>"$tmp/trace" +if (( count == \$(cat "$tmp/crash-at") )); then + kill -9 "\$PPID" + kill -9 \$\$ +fi +SH +chmod 755 "$tmp/lifecycle/usr/lib/omarchy/mac-boot/luks-slots" +chmod -R go-w "$tmp/lifecycle" + +export TEST_TMP=$tmp OMARCHY_PATH=$ROOT XDG_STATE_HOME=$tmp/state SUDO_USER=owner OMARCHY_LIFECYCLE_ROOT=$tmp/lifecycle +base_path=$PATH +platform=x86 + +# The command's PATH for this backend on this platform fixture. +use() { + backend=$1 platform=$2 + export OMARCHY_PROC_ROOT=$tmp/$platform/proc + if [[ $backend == "fake" ]]; then + export PATH="$tmp/$platform/bin:$tmp/bin:$ROOT/bin:$base_path" + else + export PATH="$tmp/$platform/bin:$tmp/real:$tmp/bin:$ROOT/bin:$base_path" + fi +} +data=$tmp/dev/data + +# The slot the key opens, or nothing. +opens() { + local device=$1 key=$2 slot k + if [[ $backend == "fake" ]]; then + while IFS=$'\t' read -r slot k; do + [[ $k == "$key" ]] && { echo "$slot"; return; } + done <"$device.slots" + else + LC_ALL=C "$REAL_CRYPTSETUP" open --test-passphrase --verbose --key-file <(printf '%s' "$key") "$device" 2>&1 | + grep -o 'Key slot [0-9]* unlocked' | grep -o '[0-9]*' || true + fi +} + +account() { + awk -F'\t' -v n="$1" '$1 == n { print $2 }' "$tmp/accounts" +} + +volume() { + local device=$1 key=$2 extra=${3:-} + echo "uuid-$(basename "$device")" >"$device.uuid" + if [[ $backend == "fake" ]]; then + : >"$device" + printf '0\t%s\n' "$key" >"$device.slots" + [[ -z $extra ]] || printf '1\t%s\n' "$extra" >>"$device.slots" + else + rm -f "$device" + truncate -s 32M "$device" + "$REAL_CRYPTSETUP" luksFormat -q --type "$backend" --pbkdf pbkdf2 --pbkdf-force-iterations 1000 "$device" <(printf '%s' "$key") + if [[ -n $extra ]]; then + "$REAL_CRYPTSETUP" luksAddKey --pbkdf pbkdf2 --pbkdf-force-iterations 1000 --key-file <(printf '%s' "$key") "$device" <(printf '%s' "$extra") + fi + fi +} + +# / on the system drive, which also holds a recovery key; a data drive beside it. +fixture() { + rm -rf "$tmp/state" "$tmp/output" "$tmp/trace" "$tmp/offered" "$tmp"/dev/* "$tmp/slot-record" "$tmp/record-fail" "$tmp/owner-slot" "$tmp/owner-query-fail" "$tmp/owner-query-garbage" "$tmp/owner-unrecorded" + unset TEST_ENROLL_AT_PROMPT TEST_ENROLL_DEVICE TEST_OPEN_FAIL TEST_CHPASSWD_FAIL TEST_CHANGE_FAIL TEST_CHANGE_PARTIAL TEST_KILL_FAIL TEST_FINDMNT_FAIL TEST_TOKEN_SLOT CRASH_ORPHAN + system=$tmp/dev/system + recovery=${1-$recovery_key} + volume "$system" "$old_password" "$recovery" + volume "$data" "$data_password" + printf 'owner\t%s\nroot\t%s\n' "$old_password" "$old_password" >"$tmp/accounts" + printf '%s\n%s\n' "$system" "$data" >"$tmp/drives" + printf '/dev/mapper/root crypt btrfs\n%s part crypto_LUKS\n/dev/fake-disk disk \n' "$system" >"$tmp/root-ancestry" + echo "$system" >"$tmp/select" + : >"$tmp/prompts" + : >"$tmp/sudo-calls" +} + +# One run of the command: an optional crash step, then answers for its prompts. +attempt() { + local crash_at=$1 + shift + if (( $# )); then printf '%s\n' "$@" >"$tmp/inputs"; else : >"$tmp/inputs"; fi + echo 0 >"$tmp/steps" + echo "$crash_at" >"$tmp/crash-at" + : >"$tmp/trace" + { + CRASH_AT=$crash_at bash -c 'TEST_PID=$$ exec bash ${TEST_TRACE:+-x} "$0"' "$ROOT/bin/omarchy-drive-password" + } >>"$tmp/output" 2>&1 +} + +consistent() { + local context=$1 password=$2 other=$old_password + [[ $password == "$old_password" ]] && other=$new_password + [[ $(account owner) == "$password" && $(account root) == "$password" ]] || + fail "$backend: $context: the login and root passwords are the disk password" "$(cat "$tmp/accounts" "$tmp/output")" + [[ -n $(opens "$system" "$password") ]] || fail "$backend: $context: the disk opens with that password" "$(cat "$tmp/output")" + [[ -z $(opens "$system" "$other") ]] || fail "$backend: $context: the other password no longer opens the disk" "$(cat "$tmp/trace" "$tmp/output")" + [[ -z $recovery || -n $(opens "$system" "$recovery") ]] || fail "$backend: $context: the recovery key still opens the disk" + [[ ! -e $journal ]] || fail "$backend: $context: the journal is gone" "$(cat "$journal")" + recorded "$context" "$password" +} + +# On Apple the boot package holds the slot the owner's password opens: recorded +# whenever the key moved. x86 records nothing and runs no extra sudo. +recorded() { + local context=$1 password=$2 slot + if [[ $platform == "apple" ]]; then + slot=$(opens "$system" "$password") + if [[ -s $tmp/slot-record ]]; then + [[ $(tail -n 1 "$tmp/slot-record") == "owner=$slot" ]] || + fail "$backend: apple: $context: the owner's slot is recorded" "$(cat "$tmp/slot-record")" + else + [[ $slot == "0" ]] || fail "$backend: apple: $context: a key that moved is recorded" "$(cat "$tmp/trace")" + fi + else + [[ ! -e $tmp/slot-record ]] && ! grep -q 'lifecycle-dispatch' "$tmp/sudo-calls" || + fail "$backend: x86: $context: no slot is recorded and no dispatch runs under sudo" + fi +} + +said() { + grep -Fq "$1" "$tmp/output" || fail "$backend: the command says: $1" "$(cat "$tmp/output")" +} + +no_secrets() { + local file + for file in "$journal" "$tmp/sudo-calls" "$tmp/prompts"; do + [[ -e $file ]] || continue + ! grep -Fq -e "$old_password" -e "$new_password" -e "$recovery_key" -e "$data_password" "$file" || + fail "$backend: $1: $(basename "$file") holds no password" "$(cat "$file")" + done +} + +# Between runs the disk and the accounts may disagree only while the journal +# says so, and the accounts never take a password the disk does not. +recoverable() { + local context=$1 login + login=$(account owner) + [[ -n $(opens "$system" "$login") || -e $journal ]] || + fail "$backend: $context: a disk that no longer opens with the login password is journaled" "$(cat "$tmp/trace")" + [[ $login == "$old_password" || -n $(opens "$system" "$login") ]] || + fail "$backend: $context: the login password changes only after the disk takes it" "$(cat "$tmp/trace")" + [[ ! -e $journal || $(stat -c %a "$journal") == "600" ]] || fail "$backend: $context: the journal is private" + no_secrets "$context" +} + +backends=(fake) +if REAL_CRYPTSETUP=$(command -v cryptsetup); then + export REAL_CRYPTSETUP + backends+=(luks2 luks1) +else + skip "cryptsetup is not installed; skipping the file-backed volume runs" +fi + +matrix=() +for backend in "${backends[@]}"; do + matrix+=("$backend x86" "$backend apple") +done + +for run_spec in "${matrix[@]}"; do + use $run_spec + + fixture + attempt 0 "$old_password" "$new_password" "$new_password" || fail "$backend: changing the system disk password succeeds" "$(cat "$tmp/output")" + consistent "uninterrupted" "$new_password" + [[ $(opens "$data" "$data_password") == "0" ]] || fail "$backend: the data drive is untouched" + first_account=$(grep -n 'chpasswd' "$tmp/trace" | head -1 | cut -d: -f1) + last_luks=$(grep -n 'cryptsetup' "$tmp/trace" | tail -1 | cut -d: -f1) + (( last_luks < first_account )) || fail "$backend: the disk key changes and is verified before any account" "$(cat "$tmp/trace")" + grep -Fq 'login password for owner and root' "$tmp/output" || fail "$backend: the command says the login password follows the system disk" + no_secrets "uninterrupted" + total_steps=$(cat "$tmp/steps") + (( total_steps >= 6 )) || fail "$backend: every change is a crash point" "$(cat "$tmp/trace")" + pass "$backend: the system disk key changes and verifies first, then the login and root passwords follow" + + fixture + if attempt 0 "not-the-password" "$new_password" "$new_password"; then fail "$backend: a wrong current password fails the command"; fi + said "That password does not open $system." + ! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal ]] || fail "$backend: a wrong current password changes nothing" + [[ -n $(opens "$system" "$old_password") && $(account owner) == "$old_password" ]] || fail "$backend: a wrong current password leaves the disk and accounts" + pass "$backend: a wrong current password is refused before anything changes" + + fixture "" + attempt 0 "$old_password" "$new_password" "$new_password" || fail "$backend: a single-slot system disk changes" "$(cat "$tmp/output")" + consistent "single slot" "$new_password" + pass "$backend: a disk with one key slot keeps it through the change" + + fixture + echo "$data" >"$tmp/select" + attempt 0 "$data_password" "$new_password" "$new_password" || fail "$backend: changing a data drive password succeeds" "$(cat "$tmp/output")" + [[ -n $(opens "$data" "$new_password") && -z $(opens "$data" "$data_password") ]] || fail "$backend: the data drive takes the new password" + [[ $(account owner) == "$old_password" && $(account root) == "$old_password" ]] || fail "$backend: a data drive leaves the login and root passwords alone" + [[ -n $(opens "$system" "$old_password") ]] || fail "$backend: a data drive change leaves the system disk alone" + ! grep -q '^chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal ]] || fail "$backend: a data drive change runs no chpasswd and keeps no journal" + ! grep -Fq 'login password' "$tmp/output" || fail "$backend: a data drive change does not mention the login password" + pass "$backend: changing a non-root encrypted drive's password leaves the login unchanged" + + # Killed after each step, the owner runs the command again and answers with a + # password that opens the disk now: the new one, or the old one while it still + # works. Each rerun is itself killed after each of its steps until one finishes. + for (( step = 1; step <= total_steps; step++ )); do + for answer in "$new_password" "$old_password"; do + fixture + if attempt "$step" "$old_password" "$new_password" "$new_password"; then fail "$backend: the run is killed at step $step"; fi + point=$(sed -n "${step}p" "$tmp/trace") + recoverable "killed after '$point'" + if [[ ! -e $journal ]]; then + if [[ $(account owner) == "$new_password" ]]; then + consistent "killed after '$point'" "$new_password" + else + consistent "killed after '$point'" "$old_password" + fi + continue + fi + [[ -n $(opens "$system" "$answer") ]] || continue + if grep -qx 'phase=accounts' "$journal" && [[ $answer == "$old_password" ]]; then + if attempt 0 "$answer"; then fail "$backend: after '$point' a confirmed change refuses the old password"; fi + [[ -e $journal ]] || fail "$backend: after '$point' a refused rerun keeps the journal" + answer=$new_password + fi + for (( again = 1; ; again++ )); do + status=0 + attempt "$again" "$answer" || status=$? + (( status == 0 )) && break + (( status == 137 )) || fail "$backend: the rerun after '$point' finishes" "$(cat "$tmp/output")" + recoverable "rerun after '$point' killed at step $again" + done + consistent "rerun with $answer after '$point'" "$answer" + no_secrets "rerun after '$point'" + done + done + pass "$backend $platform: killed after each of $total_steps steps, and each rerun after each of its own, the disk, login and root end on one password" +done + +use fake x86 + +fixture +printf '%s\n%s\n%s\n' "$system" "$data" "$data" >"$tmp/drives" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "a fresh boot finds the system disk without a blkid cache" "$(cat "$tmp/output")" +[[ $(cat "$tmp/offered") == "$system"$'\n'"$data" ]] || fail "each LUKS drive lsblk names is offered once" "$(cat "$tmp/offered")" +consistent "fresh boot" "$new_password" +pass "before root has run blkid, every LUKS drive is offered once and the system disk changes" + +fixture +printf '5\t%s\n' tpm-sealed-key >>"$system.slots" +export TEST_TOKEN_SLOT=5 +if attempt 0 "not-the-password" "$new_password" "$new_password"; then fail "an enrolled token does not stand in for the current password"; fi +said "That password does not open $system." +attempt 0 "$old_password" "$new_password" "$new_password" || fail "a disk with an enrolled token changes its password" "$(cat "$tmp/output")" +consistent "beside an enrolled token" "$new_password" +[[ $(opens "$system" tpm-sealed-key) == "5" ]] || fail "the token's slot is left alone" +pass "an enrolled token never answers for a password, and its slot is left alone" + +fixture +export TEST_CHANGE_FAIL=1 +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a failed luksChangeKey fails the command"; fi +[[ $(account owner) == "$old_password" && $(account root) == "$old_password" ]] || fail "a failed luksChangeKey leaves both accounts alone" +! grep -q '^chpasswd' "$tmp/sudo-calls" || fail "a failed luksChangeKey runs no chpasswd" +[[ -n $(opens "$system" "$old_password") && ! -e $journal ]] || fail "a failed luksChangeKey leaves the disk and no journal" +pass "a failed LUKS change leaves the login and root passwords unchanged" + +fixture +export TEST_CHANGE_PARTIAL=1 +attempt 0 "$old_password" "$new_password" "$new_password" || fail "a luksChangeKey that dies after adding the new key is finished" "$(cat "$tmp/output")" +consistent "luksChangeKey died after adding the new key" "$new_password" +pass "a luksChangeKey that dies between keys has its old key retired before the accounts change" + +fixture +export TEST_CHANGE_PARTIAL=1 TEST_KILL_FAIL=1 +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a failed retirement fails the command"; fi +[[ -e $journal && $(account owner) == "$old_password" ]] || fail "a failed retirement keeps the journal and the accounts" +unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL +if attempt 0 "$recovery_key"; then fail "an unconfirmed change refuses the recovery key"; fi +said "That password does not finish the change" +[[ -e $journal && $(account owner) == "$old_password" && -n $(opens "$system" "$recovery_key") ]] || fail "refusing the recovery key changes nothing" +attempt 0 "$new_password" || fail "the rerun retires the old key and finishes" "$(cat "$tmp/output")" +consistent "rerun after a failed retirement" "$new_password" +pass "before the change is confirmed a rerun refuses the recovery key and retires the old key" + +fixture +export TEST_CHANGE_PARTIAL=1 TEST_KILL_FAIL=1 +attempt 0 "$old_password" "$new_password" "$new_password" || true +unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL +attempt 0 "$old_password" || fail "the rerun with the old password rolls back" "$(cat "$tmp/output")" +consistent "rollback with the old password" "$old_password" +pass "a rerun with the old password removes the half-added new key" + +fixture +attempt 0 "$old_password" "$new_password" "$new_password" +chpasswd_step=$(grep -n 'chpasswd owner' "$tmp/trace" | cut -d: -f1) +fixture +attempt "$chpasswd_step" "$old_password" "$new_password" "$new_password" || true +grep -qx 'phase=accounts' "$journal" || fail "a crash before the accounts leaves the confirmed phase" "$(cat "$journal" "$tmp/trace")" +if attempt 0 "$recovery_key"; then fail "a confirmed change refuses the recovery key"; fi +said "That password does not finish the change" +[[ -e $journal && $(account owner) == "$old_password" ]] || fail "refusing the recovery key changes no account" +if attempt 0 "not-the-password"; then fail "a rerun refuses a password that opens nothing"; fi +for suffix in "" .slots .uuid; do + mv "$system$suffix" "$tmp/dev/renumbered$suffix" +done +system=$tmp/dev/renumbered +printf '/dev/mapper/root crypt btrfs\n%s part crypto_LUKS\n/dev/fake-disk disk \n' "$system" >"$tmp/root-ancestry" +attempt 0 "$new_password" || fail "the confirmed change finishes on a renumbered disk" "$(cat "$tmp/output")" +consistent "rerun after a confirmed change" "$new_password" +pass "once the new key is confirmed only its slot finishes the accounts, on the disk found by UUID" + +fixture +export TEST_OPEN_FAIL=1 +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a disk that cannot be checked fails the command"; fi +said "Could not check the password against $system." +! grep -q 'luksChangeKey' "$tmp/sudo-calls" && [[ ! -e $journal ]] || fail "a disk that cannot be checked is not changed" +pass "the current password is checked before anything changes" + +fixture +export TEST_CHPASSWD_FAIL=root +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a failed root password change fails the command"; fi +[[ $(account owner) == "$new_password" && $(account root) == "$old_password" && -e $journal ]] || + fail "a failed root password change keeps the journal" +unset TEST_CHPASSWD_FAIL +attempt 0 "$new_password" || fail "the rerun sets the root password" "$(cat "$tmp/output")" +consistent "rerun after a failed root password change" "$new_password" +pass "a failed account change is finished by the next run" + +fixture +attempt "$chpasswd_step" "$old_password" "$new_password" "$new_password" || true +export TEST_FINDMNT_FAIL=1 +if attempt 0 "$new_password"; then fail "a run that cannot find / fails"; fi +said "Could not tell which drive holds the system." +[[ -e $journal && $(account owner) == "$old_password" ]] || fail "a run that cannot find / keeps the journal and the accounts" +unset TEST_FINDMNT_FAIL +attempt 0 "$new_password" || fail "the rerun finishes once / is found" "$(cat "$tmp/output")" +consistent "rerun after a failed root lookup" "$new_password" +pass "a failed root lookup changes nothing and keeps the journal" + +fixture +attempt 0 "$old_password" "$new_password" "$new_password" +change_step=$(grep -n 'cryptsetup luksChangeKey' "$tmp/trace" | cut -d: -f1) +fixture +export CRASH_ORPHAN=1 +attempt "$change_step" "$old_password" "$new_password" "$new_password" || true +unset CRASH_ORPHAN +if attempt 0 "$old_password"; then fail "a rerun waits for a luksChangeKey the killed run left behind"; fi +attempt 0 "$new_password" || fail "the rerun finishes after the orphaned change" "$(cat "$tmp/output")" +consistent "orphaned luksChangeKey" "$new_password" +pass "a luksChangeKey that outlives its run finishes before the rerun reads the disk" + +fixture +printf '/dev/mapper/root crypt btrfs\n%s part \n/dev/fake-disk disk \n' "$system" >"$tmp/root-ancestry" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a crypt layer without a named LUKS partition fails"; fi +said "Could not tell which drive holds the system." +! grep -q 'luksChangeKey' "$tmp/sudo-calls" || fail "an unresolved crypt layer changes nothing" +pass "a crypt layer whose LUKS partition cannot be named fails closed" + +fixture +mkdir -p "${journal%/*}" +printf 'uuid=uuid-system\nold_slot=0\nslots=\nphase=luks\n' >"$journal" +if attempt 0 "$recovery_key"; then fail "a journal without slots refuses to finish"; fi +said "is unreadable" +[[ -e $journal && $(account owner) == "$old_password" && -n $(opens "$system" "$old_password") ]] || fail "an unreadable journal changes nothing" +pass "a journal without its slot list is refused rather than trusted" + +fixture +export TEST_CHANGE_PARTIAL=1 TEST_KILL_FAIL=1 +attempt 0 "$old_password" "$new_password" "$new_password" || true +unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL +printf '3\tlater-key\n' >>"$system.slots" +if attempt 0 "$new_password"; then fail "a disk that gained two slots refuses to finish"; fi +said "changed since the interrupted change" +[[ -e $journal && -n $(opens "$system" "later-key") && -n $(opens "$system" "$old_password") ]] || fail "a disk that gained two slots is left alone" +pass "a rerun never removes a slot enrolled after the interrupted change" + +fixture +mkdir -p "${journal%/*}" +exec {held}>"${journal%/*}/drive-password.lock" +flock -n "$held" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a second run waits for the first"; fi +exec {held}>&- +grep -Fq 'Another drive password change is running.' "$tmp/output" && ! grep -q 'luksChangeKey' "$tmp/sudo-calls" || + fail "a second run changes nothing while another holds the lock" +pass "two runs never interleave" + +fixture +export TEST_TRACE=1 +attempt 0 "$old_password" "$new_password" "$new_password" || fail "a traced run succeeds" "$(cat "$tmp/output")" +unset TEST_TRACE +! grep -Fq -e "$old_password" -e "$new_password" "$tmp/output" || fail "tracing never shows a password" "$(cat "$tmp/output")" +pass "bash -x does not trace passwords" + +fixture +SUDO_USER=root +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "the system disk needs a login user"; fi +said "Run omarchy-drive-password as the user who logs in" +[[ ! -s $tmp/sudo-calls && ! -e $journal && -n $(opens "$system" "$old_password") ]] || fail "without a login user nothing changes" +echo "$data" >"$tmp/select" +attempt 0 "$data_password" "$new_password" "$new_password" || fail "a data drive changes without a login user" "$(cat "$tmp/output")" +SUDO_USER=owner +pass "the system disk password changes only for a known login user" + +fixture +printf '/dev/mapper/root lvm btrfs\n/dev/mapper/cryptlvm crypt LVM2_member\n%s part crypto_LUKS\n/dev/fake-disk disk \n' "$system" >"$tmp/root-ancestry" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "an LVM-on-LUKS system disk changes" "$(cat "$tmp/output")" +consistent "LVM on LUKS" "$new_password" +pass "the system disk is found beneath an LVM volume group" + +fixture +printf '/dev/mapper/root part ext4\n/dev/fake-disk disk \n' >"$tmp/root-ancestry" +echo "$data" >"$tmp/drives" +attempt 0 "$data_password" "$new_password" "$new_password" || fail "an unencrypted root still changes a data drive" "$(cat "$tmp/output")" +[[ $(account owner) == "$old_password" && -n $(opens "$data" "$new_password") ]] || fail "an unencrypted root keeps the login password" +pass "with / unencrypted, every encrypted drive changes alone" + +fixture +mkdir -p "${journal%/*}" +printf 'uuid=uuid-elsewhere\nphase=accounts\nslot=0\n' >"$journal" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "a journal for another disk does not block a change" "$(cat "$tmp/output")" +consistent "stale journal" "$new_password" +pass "a journal for a disk that is not / is dropped" + +fixture +if attempt 0 "$old_password" ""; then fail "an empty password is refused"; fi +said "Password cannot be empty." +if attempt 0 "$old_password" "secret123" "*"; then fail "a mismatched confirmation is refused"; fi +said "Passwords do not match." +if attempt 0 "$old_password" "$old_password" "$old_password"; then fail "the current password is refused as the new one"; fi +said "The new password is the current one." +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal && $(account owner) == "$old_password" ]] || + fail "refused passwords change nothing" +pass "drive password rejects empty, mismatched and unchanged passphrases before changing anything" + +# Only the owner's slot changes the system disk. A second key an earlier setup +# added (a recovery key) is refused before anything changes, and so is a change +# the boot package cannot vouch for. +use fake apple +fixture +if attempt 0 "$recovery_key" "$new_password" "$new_password"; then fail "apple: a key in another slot is refused"; fi +said "That password opens another key slot on the system disk" +said "The system disk password did not change." +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal && ! -e $tmp/slot-record ]] || fail "apple: refusing another slot's key changes nothing" +[[ -n $(opens "$system" "$recovery_key") ]] || fail "apple: the other key still opens the disk" +consistent "another slot's key refused" "$old_password" +touch "$tmp/owner-query-fail" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "apple: a boot package that cannot name the owner's slot stops the change"; fi +said "Could not read which key slot holds the system disk password: The LUKS header has no key in the recorded owner slot 0." +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal ]] || fail "apple: an unanswered owner query changes nothing" +consistent "owner query failed" "$old_password" +rm "$tmp/owner-query-fail" +touch "$tmp/owner-query-garbage" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "apple: an owner slot that is not a number stops the change"; fi +said "named 'slot zero', which is not a key slot" +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal ]] || fail "apple: a garbled owner slot changes nothing" +rm "$tmp/owner-query-garbage" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "apple: the owner's password changes the disk" "$(cat "$tmp/output")" +consistent "owner's password after refusals" "$new_password" +pass "apple: only the owner's slot changes the system disk; another slot's key and an unanswered query change nothing" + +# A Mac set up before its boot package recorded slots has no owner slot (4). +# Its disk's only key is the owner's: the change goes through and records the +# new slot. With another key beside it the owner's can't be told, so the change +# is refused, saying how to settle it: record both, or remove the extra key. +use fake apple +fixture "" +touch "$tmp/owner-unrecorded" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "apple: an unrecorded single-key disk changes its password" "$(cat "$tmp/output")" +[[ $(cat "$tmp/slot-record") == "owner=$(opens "$system" "$new_password")" ]] || fail "apple: the owner's new slot is recorded" "$(cat "$tmp/slot-record")" +consistent "no owner slot recorded, one key" "$new_password" +step=$(grep -n 'chpasswd owner' "$tmp/trace" | head -1 | cut -d: -f1) +fixture "" +touch "$tmp/owner-unrecorded" +if attempt "$step" "$old_password" "$new_password" "$new_password"; then fail "apple: the unrecorded change is killed before the accounts"; fi +[[ -e $journal && ! -e $tmp/slot-record ]] || fail "apple: a change killed before the accounts keeps its journal" "$(cat "$tmp/trace")" +attempt 0 "$new_password" || fail "apple: the rerun finishes an unrecorded change" "$(cat "$tmp/output")" +[[ $(cat "$tmp/slot-record") == "owner=$(opens "$system" "$new_password")" ]] || fail "apple: the rerun records the owner's new slot" "$(cat "$tmp/slot-record")" +consistent "no owner slot recorded, interrupted" "$new_password" +pass "apple: with no owner slot recorded, the disk's only key changes and its new slot is recorded, after an interruption too" + +fixture +touch "$tmp/owner-unrecorded" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "apple: an unrecorded disk with two keys refuses the change"; fi +said "has keys in slots 0,1, so which one is yours can't be told" +said "record both: sudo omarchy-lifecycle-dispatch luks-slots owner=0 recovery=1" +said "sudo cryptsetup luksKillSlot $system " +said "The system disk password did not change." +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal && ! -e $tmp/slot-record ]] || fail "apple: an unrecorded two-key disk changes nothing" +"$ROOT/bin/omarchy-lifecycle-dispatch" luks-slots owner=0 recovery=1 +: >"$tmp/slot-record" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "apple: once both slots are recorded the change goes through" "$(cat "$tmp/output")" +consistent "slots recorded as the refusal said" "$new_password" +pass "apple: with no owner slot recorded, a disk with another key refuses the change and says how to record or remove it" + +fixture "" +touch "$tmp/owner-unrecorded" +export TEST_ENROLL_AT_PROMPT="Confirm new encryption password" TEST_ENROLL_DEVICE=$system +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "apple: a key enrolled during the prompts stops an unrecorded change"; fi +said "changed while you entered the new password" +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal && ! -e $tmp/slot-record ]] || fail "apple: a key enrolled during the prompts changes nothing" +pass "apple: an unrecorded disk that gains a key while the owner types is left alone" + +# A slot the boot package could not record keeps the journal: the rerun records it. +use fake apple +fixture +touch "$tmp/record-fail" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a failed record fails the command"; fi +said "Could not record the new key slot for the boot checks." +[[ -e $journal && $(account owner) == "$new_password" && ! -e $tmp/slot-record ]] || fail "a failed record keeps the journal" +rm "$tmp/record-fail" +attempt 0 "$new_password" || fail "the rerun records the slot" "$(cat "$tmp/output")" +[[ -s $tmp/slot-record ]] || fail "the rerun records the moved key's slot" +consistent "rerun after a failed record" "$new_password" +pass "apple: the change finishes only once the boot package recorded the owner's new slot" + +# An installed boot package older than luks-slots could not record the slot, so +# the system disk does not change; a data drive still does. +fixture +mv "$tmp/lifecycle/usr/lib/omarchy/mac-boot/luks-slots" "$tmp/luks-slots.off" +mkdir -p "$tmp/lifecycle/var/lib/pacman/local/omarchy-mac-boot-20260925-2" +if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "an Apple boot package without luks-slots fails the command"; fi +said "which omarchy-mac-boot 20260925-2 does not provide; update omarchy-mac-boot" +said "The system disk password did not change." +! grep -q 'luksChangeKey\|chpasswd' "$tmp/sudo-calls" && [[ ! -e $journal ]] || fail "a boot package without luks-slots changes nothing" +consistent "boot package without luks-slots" "$old_password" +echo "$data" >"$tmp/select" +attempt 0 "$data_password" "$new_password" "$new_password" || fail "a data drive changes without luks-slots" "$(cat "$tmp/output")" +pass "apple: a boot package without luks-slots stops a system disk change before it starts, naming the package" -printf 'new password\nnew password\n' >"$TEST_INPUTS" -"$ROOT/bin/omarchy-drive-password" >/dev/null +# A Mac without its boot package at all predates it: nothing checks its slots, +# so the change goes through and records nothing. +fixture +rm -r "$tmp/lifecycle/var/lib/pacman" +attempt 0 "$old_password" "$new_password" "$new_password" || fail "a Mac without the boot package changes its disk password" "$(cat "$tmp/output")" +# Checked as on x86: no slot recorded and no dispatch under sudo. +platform=x86 +consistent "no boot package" "$new_password" +platform=apple +! grep -q 'Error:' "$tmp/output" || fail "without the boot package nothing is reported" "$(cat "$tmp/output")" +mv "$tmp/luks-slots.off" "$tmp/lifecycle/usr/lib/omarchy/mac-boot/luks-slots" +pass "apple: a Mac without its boot package changes its disk password and records no slot" -[[ $(<"$TEST_STDIN") == "new password" ]] || fail "drive password passes the validated passphrase without a newline" -grep -F 'cryptsetup luksChangeKey' "$TEST_ARGS" >/dev/null || fail "drive password changes the LUKS key" -grep -Fx /dev/test-luks "$TEST_ARGS" >/dev/null || fail "drive password targets the selected drive" -pass "drive password rejects empty and mismatched passphrases and passes validated input to cryptsetup" +fixture +echo "$data" >"$tmp/select" +attempt 0 "$data_password" "$new_password" "$new_password" || fail "apple: a data drive changes" "$(cat "$tmp/output")" +[[ ! -e $tmp/slot-record && -n $(opens "$data" "$new_password") ]] || fail "apple: a data drive records no slot" +pass "apple: only the system disk's slot is recorded" diff --git a/test/shell.d/electron-gl-test.sh b/test/shell.d/electron-gl-test.sh new file mode 100644 index 000000000000..8fd70972bad4 --- /dev/null +++ b/test/shell.d/electron-gl-test.sh @@ -0,0 +1,201 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +dri="$test_tmp/dri" +mkdir -p "$dri" + +if OMARCHY_DRI_PATH="$dri" "$ROOT/bin/omarchy-hw-render-gpu"; then + fail "render GPU is absent when dri is empty" +fi +pass "render GPU is absent when dri is empty" + +touch "$dri/card1" +if OMARCHY_DRI_PATH="$dri" "$ROOT/bin/omarchy-hw-render-gpu"; then + fail "a scanout node is not a render GPU" +fi +pass "a scanout node is not a render GPU" + +touch "$dri/renderD128" +OMARCHY_DRI_PATH="$dri" "$ROOT/bin/omarchy-hw-render-gpu" || + fail "renderD128 is a render GPU" +pass "renderD128 is a render GPU" + +args=$(PATH="$ROOT/bin:$PATH" OMARCHY_DRI_PATH="$dri" \ + "$ROOT/bin/omarchy-cmd-electron-gl-args") +[[ -z $args ]] || fail "no Electron GL flags when a render GPU exists" "$args" +pass "no Electron GL flags when a render GPU exists" + +rm -f "$dri/renderD128" +args=$(PATH="$ROOT/bin:$PATH" OMARCHY_DRI_PATH="$dri" \ + "$ROOT/bin/omarchy-cmd-electron-gl-args") +[[ $args == $'--ozone-platform=wayland\n--disable-gpu' ]] || + fail "software GL flags when no render GPU" "$args" +pass "software GL flags when no render GPU" + +real="$test_tmp/real-bin" +bind="$test_tmp/bind" +mkdir -p "$bind" +printf '#!/bin/bash\nprintf "real %%s\\n" "$*"\n' >"$real" +chmod +x "$real" + +PATH="$ROOT/bin:$PATH" \ + OMARCHY_ELECTRON_GL_BIND_DIR="$bind" \ + "$ROOT/bin/omarchy-cmd-electron-gl-wrap" demo "$real" + +grep -q '^# omarchy-electron-gl-wrapper$' "$bind/demo" || + fail "wrapper is marked as an Omarchy Electron GL wrapper" +pass "wrapper is marked as an Omarchy Electron GL wrapper" + +# A repeated user finalization must not chmod an already-correct system wrapper. +stubs="$test_tmp/stubs" +mkdir -p "$stubs" +cat >"$stubs/chmod" <<'STUB' +#!/bin/bash +printf '%s\n' "$*" >>"$OMARCHY_TEST_PERMISSION_CALLS" +[[ ${OMARCHY_TEST_ALLOW_CHMOD:-0} == "1" ]] || exit 91 +exec /usr/bin/chmod "$@" +STUB +cat >"$stubs/sudo" <<'STUB' +#!/bin/bash +printf 'sudo %s\n' "$*" >>"$OMARCHY_TEST_PERMISSION_CALLS" +exit 92 +STUB +chmod +x "$stubs/chmod" "$stubs/sudo" +permission_calls="$test_tmp/permission-calls" +run_wrap() { + PATH="$stubs:$ROOT/bin:$PATH" \ + OMARCHY_TEST_PERMISSION_CALLS="$permission_calls" \ + OMARCHY_ELECTRON_GL_BIND_DIR="$bind" \ + "$ROOT/bin/omarchy-cmd-electron-gl-wrap" demo "$real" +} + +chmod 555 "$bind" +run_wrap || fail "correct wrapper needs no privilege or permission changes on repeat" +chmod 755 "$bind" +[[ ! -e $permission_calls ]] || + fail "correct wrapper must not invoke chmod or sudo" +pass "correct wrapper needs no privilege or permission changes on repeat" + +for mode in 644 775; do + chmod "$mode" "$bind/demo" + OMARCHY_TEST_ALLOW_CHMOD=1 run_wrap || + fail "wrapper repairs incorrect mode $mode" + [[ $(stat -c %a "$bind/demo") == "755" ]] || + fail "wrapper restores mode 755 from $mode" +done +pass "wrapper repairs missing execute and excessive write permissions" + +out=$(PATH="$ROOT/bin:$PATH" OMARCHY_DRI_PATH="$dri" "$bind/demo" hello) +[[ $out == "real --ozone-platform=wayland --disable-gpu hello" ]] || + fail "wrapper injects software GL flags" "$out" +pass "wrapper injects software GL flags" + +mkdir -p "$dri" +touch "$dri/renderD128" +out=$(PATH="$ROOT/bin:$PATH" OMARCHY_DRI_PATH="$dri" "$bind/demo" hello) +[[ $out == "real hello" ]] || + fail "wrapper is a no-op when a render GPU exists" "$out" +pass "wrapper is a no-op when a render GPU exists" + +wrap() { + PATH="$ROOT/bin:$PATH" OMARCHY_ELECTRON_GL_BIND_DIR="$bind" \ + "$ROOT/bin/omarchy-cmd-electron-gl-wrap" "$@" +} + +# A symlink is never an Omarchy launcher, even one aliasing the real binary. +app_real="$test_tmp/app/app" +mkdir -p "$(dirname "$app_real")" +cp "$real" "$app_real" +chmod 751 "$app_real" +app_hash=$(sha256sum "$app_real") +ln -s "$app_real" "$bind/app" +status=0 +wrap app "$app_real" 2>"$test_tmp/error" || status=$? +(( status == 3 )) || fail "a symlink to the real binary is refused as an unmanaged launcher" "status $status" +[[ -L $bind/app && $(readlink "$bind/app") == "$app_real" ]] || fail "a symlink to the real binary is preserved" +[[ $(sha256sum "$app_real") == "$app_hash" && $(stat -c %a "$app_real") == "751" ]] || + fail "a refused symlink leaves the real binary's bytes and mode alone" +pass "a symlink to the real binary is refused and preserved" + +other_real="$test_tmp/other-real" +cp "$real" "$other_real" +wrap demo "$other_real" +grep -Fxq "real=$other_real" "$bind/demo" || fail "marked wrapper can be updated to another binary" +pass "marked regular wrappers can be updated" + +printf '#!/bin/bash\nprintf custom\\n\n' >"$bind/chromium" +chmod 750 "$bind/chromium" +custom_hash=$(sha256sum "$bind/chromium") +if wrap chromium "$real" 2>"$test_tmp/error"; then + fail "custom Chromium launcher must be refused" +fi +[[ $(sha256sum "$bind/chromium") == "$custom_hash" && $(stat -c %a "$bind/chromium") == "750" ]] || + fail "custom Chromium launcher bytes and permissions are preserved" +grep -Fq 'unmanaged launcher' "$test_tmp/error" || fail "custom launcher refusal explains the conflict" +pass "custom Chromium launcher is refused and preserved" + +for link_target in "$other_real" "$test_tmp/missing" "$bind/demo"; do + ln -s "$link_target" "$bind/unknown" + if wrap unknown "$real" 2>"$test_tmp/error"; then + fail "unknown symlink must be refused" "$link_target" + fi + [[ -L $bind/unknown && $(readlink "$bind/unknown") == "$link_target" ]] || + fail "unknown symlink is preserved" "$link_target" + rm "$bind/unknown" +done +pass "unknown, dangling, and marked-wrapper symlinks are refused and preserved" + +cp "$real" "$bind/self" +self_hash=$(sha256sum "$bind/self") +if wrap self "$bind/self" 2>"$test_tmp/error"; then + fail "literal self-wrap must be refused" +fi +[[ $(sha256sum "$bind/self") == "$self_hash" ]] || fail "self-wrap preserves real executable" +ln "$app_real" "$bind/hardlink" +if wrap hardlink "$app_real" 2>"$test_tmp/error"; then + fail "hardlink self-wrap must be refused" +fi +[[ $bind/hardlink -ef $app_real ]] || fail "hardlink self-wrap preserves hardlink" +[[ $(sha256sum "$app_real") == "$app_hash" && $(stat -c %a "$app_real") == "751" ]] || + fail "hardlink self-wrap preserves binary bytes and mode" +pass "literal self-wrap and hardlinked binaries are refused" + +for invalid_name in "" . .. ../escape nested/launcher; do + if wrap "$invalid_name" "$real" 2>"$test_tmp/error"; then + fail "command name must be a basename" "$invalid_name" + fi +done +[[ ! -e $test_tmp/escape && ! -e $bind/nested ]] || fail "invalid names do not escape the launcher directory" +pass "invalid and traversing command names are refused" + +# Fail after staging has begun: an old wrapper stays, and no new launcher +# appears, until both the staged write and permissions have succeeded. +failure_stubs="$test_tmp/failure-stubs" +mkdir -p "$failure_stubs" +for command in mktemp tee chmod mv; do + printf '#!/bin/bash\nexit 93\n' >"$failure_stubs/$command" + chmod +x "$failure_stubs/$command" + for launcher in demo fresh; do + old_wrapper_hash=$(sha256sum "$bind/demo") + if PATH="$failure_stubs:$ROOT/bin:$PATH" OMARCHY_ELECTRON_GL_BIND_DIR="$bind" \ + "$ROOT/bin/omarchy-cmd-electron-gl-wrap" "$launcher" "$app_real" \ + 2>"$test_tmp/error"; then + fail "failed staging $command must be reported for $launcher" + fi + [[ $(sha256sum "$bind/demo") == "$old_wrapper_hash" ]] || fail "failed $command preserves old wrapper" + [[ ! -e $bind/fresh ]] || fail "failed $command installs no new launcher" + [[ $(sha256sum "$app_real") == "$app_hash" && $(stat -c %a "$app_real") == "751" ]] || + fail "failed $command preserves real binary bytes and mode" + if compgen -G "$bind/.omarchy-electron-gl.*" >/dev/null; then + fail "failed $command cleans up staged files" + fi + done + rm "$failure_stubs/$command" +done +pass "mktemp, write, chmod, and rename failures preserve launchers and clean up staged files" diff --git a/test/shell.d/factory-reset-dispatch-test.sh b/test/shell.d/factory-reset-dispatch-test.sh new file mode 100644 index 000000000000..734954aa02f4 --- /dev/null +++ b/test/shell.d/factory-reset-dispatch-test.sh @@ -0,0 +1,339 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +# Factory reset's boot step through the real omarchy-lifecycle-dispatch. On an +# x86 fixture the generic Limine UKI path runs and the boot-package entrypoints +# on disk never do. On an Apple fixture a fake boot package owns the factory +# root's boot chain: it prepares and verifies it before the throwaway slot is +# added, gets the key on standard input after the switch, and rolls back when +# the reset fails before the switch. Subvolumes are directories, and +# cryptsetup is a slot-table fake. +require_platform_fixtures "factory reset through lifecycle dispatch" + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +fake_platform "$tmp/x86" generic +fake_platform "$tmp/apple" apple-silicon + +# Load the production functions without self-elevation or the reset entrypoint. +awk ' + /^[a-z_]+\(\) \{/ { copying = 1 } + copying { print } + /^}/ { copying = 0 } +' "$ROOT/bin/omarchy-system-factory-reset" >"$tmp/functions" + +# The fake boot package: each entrypoint records its call, reset-commit its +# standard input, and reset-verify fails on request. +lifecycle=$tmp/lifecycle +mac_boot=$lifecycle/usr/lib/omarchy/mac-boot +mkdir -p "$mac_boot" +for operation in reset-prepare reset-verify reset-commit reset-rollback; do + cat >"$mac_boot/$operation" <>"$tmp/calls" +[[ $operation != reset-commit ]] || cat >"$tmp/committed-key" +[[ ! -e $tmp/$operation-fail ]] || { echo "$operation: the fixture refuses" >&2; exit 1; } +SH +done +chmod 755 "$mac_boot"/* +chmod -R go-w "$lifecycle" + +cat >"$tmp/reset" <<'SH' +#!/bin/bash +set -euo pipefail +source "$TMP/functions" +TOP_MNT=$TMP/top +NEXT_NAME=@omarchy-reset-next +PROVISIONING_DIR=/var/lib/omarchy/provisioning +LOG_FILE=$TMP/reset.log +DISPATCH=${TEST_DISPATCH:-$ROOT/bin/omarchy-lifecycle-dispatch} +RESET_BOOT="" +RESET_BOOT_ERROR="" +swap_done=0 + +gum() { + if [[ $1 == "input" ]]; then + printf '%s\n' "$CURRENT" + elif [[ $1 == "style" ]]; then + printf '%s\n' "${!#}" >>"$TMP/screen" + fi +} +encrypted_install() { [[ -n $DEVICE ]]; } +luks_device() { echo "$DEVICE"; } +scrub_factory_accounts() { :; } +sanitize_factory_baseline() { :; } +install_provisioning_units() { :; } +systemd-id128() { printf '%032d\n' 7; } +sync() { :; } +mountpoint() { return 0; } +mount() { echo "mount $*" >>"$TMP/calls"; } +umount() { :; } +chroot() { echo "chroot ${*:2}" >>"$TMP/calls"; } +reset_limine_config() { echo "limine menu reset" >>"$TMP/calls"; } +verify_limine_hashes() { :; } + +btrfs() { + case "$1 $2" in + "subvolume snapshot") cp -a "$3" "$4" ;; + "subvolume delete") rm -rf "${!#}" ;; + "subvolume show") return 1 ;; + esac +} + +mv() { + if [[ -e $TMP/switch-fail && $1 == "$TOP_MNT/@" ]]; then + echo "mv: the fixture refuses the switch" >&2 + return 1 + fi + if [[ -e $TMP/activate-fail && $1 == "$TOP_MNT/$NEXT_NAME" ]]; then + echo "mv: the fixture refuses the activation" >&2 + return 1 + fi + if [[ -e $TMP/restore-fail && $1 == "$TOP_MNT"/@omarchy-old-* ]]; then + echo "mv: the fixture refuses the restore" >&2 + return 1 + fi + command mv "$@" +} + +# " " per line. +cryptsetup() { + local op=$1 key_file="" slot material next + local -a args=() + shift + while (( $# )); do + case $1 in + --key-file) key_file=$2; shift 2 ;; + --token-type) shift 2 ;; + -*) shift ;; + *) args+=("$1"); shift ;; + esac + done + echo "cryptsetup $op" >>"$TMP/calls" + case $op in + open) + material=$(cat "$key_file") + if [[ -e $TMP/lookup-fail-once && $material != "$CURRENT" ]]; then + rm "$TMP/lookup-fail-once" + return 2 + fi + slot=$(awk -v m="$material" '$2 == m { print $1; exit }' "$TMP/slots") + [[ -n $slot ]] || return 2 + echo "Key slot $slot unlocked." + ;; + luksAddKey) + material=$(cat "$key_file") + awk -v m="$material" '$2 == m { found = 1 } END { exit !found }' "$TMP/slots" || return 2 + for (( next = 0; next < 32; next++ )); do + awk '{ print $1 }' "$TMP/slots" | grep -qx "$next" || break + done + printf '%s %s\n' "$next" "$(cat "${args[1]}")" >>"$TMP/slots" + ;; + luksDump) + echo "Keyslots:" + awk '{ printf " %s: luks2\n", $1 }' "$TMP/slots" + ;; + luksKillSlot) + slot=${args[1]} + awk -v s="$slot" '$1 != s' "$TMP/slots" >"$TMP/slots.next" + command mv -f "$TMP/slots.next" "$TMP/slots" + ;; + *) return 1 ;; + esac +} + +case $MODE in + owner) reset_boot_owner && echo "$RESET_BOOT" ;; + reset) + trap cleanup EXIT + stage_full_reset + ;; +esac +SH + +current_password=owner-password +device=$tmp/luks-device + +# A top-level Btrfs volume: the running root and a factory baseline whose ESP +# the generic path mounts from its fstab. +fixture() { + rm -rf "$tmp/top" "$tmp/calls" "$tmp/screen" "$tmp/reset.log" "$tmp/committed-key" "$tmp"/*-fail + mkdir -p "$tmp/top/@" "$tmp/top/@factory/etc" "$tmp/top/@factory/usr/bin" \ + "$tmp/top/@factory/usr/share/omarchy/install/provisioning" "$tmp/top/@factory/boot" + touch "$tmp/top/@/old-system" "$tmp/top/@factory/factory-system" "$tmp/esp" \ + "$tmp/top/@factory/usr/share/omarchy/install/provisioning/omarchy-provision-owner.service" + printf '#!/bin/bash\n' >"$tmp/top/@factory/usr/bin/omarchy-provision-owner" + chmod +x "$tmp/top/@factory/usr/bin/omarchy-provision-owner" + printf '%s /boot vfat defaults 0 2\n' "$tmp/esp" >"$tmp/top/@factory/etc/fstab" + : >"$device" + printf '0 %s\n' "$current_password" >"$tmp/slots" + : >"$tmp/reset.log" +} + +run() { + local mode=$1 platform=$2 + TMP=$tmp ROOT=$ROOT MODE=$mode CURRENT=$current_password DEVICE=${DEVICE-$device} TEST_DISPATCH=${TEST_DISPATCH:-} \ + OMARCHY_PROC_ROOT="$tmp/$platform/proc" OMARCHY_LIFECYCLE_ROOT=$lifecycle PATH="$tmp/$platform/bin:$PATH" \ + bash "$tmp/reset" >"$tmp/out" 2>&1 +} + +activated() { + [[ -e $tmp/top/@/factory-system && -f $tmp/top/@/var/lib/omarchy/provisioning/pending ]] && + compgen -G "$tmp/top/@omarchy-old-*/old-system" >/dev/null +} + +untouched() { + [[ -e $tmp/top/@/old-system && ! -e $tmp/top/@omarchy-reset-next ]] && [[ $(cat "$tmp/slots") == "0 $current_password" ]] +} + +# x86: the generic Limine UKI path, with boot-package entrypoints on disk that +# would record themselves if they ran. +fixture +run owner x86 && [[ $(cat "$tmp/out") == "generic" ]] || fail "x86: the reset boot step is generic" "$(cat "$tmp/out")" +run reset x86 || fail "x86: an encrypted reset stages" "$(cat "$tmp/out" "$tmp/reset.log")" +activated || fail "x86: the factory root is activated" +grep -qx 'chroot /usr/bin/limine-update' "$tmp/calls" || fail "x86: limine-update rebuilds the factory root's UKI" "$(cat "$tmp/calls")" +[[ -f $tmp/top/@/etc/omarchy/provisioning.key && -f $tmp/top/@/etc/limine-entry-tool.d/99-omarchy-provisioning-unlock.conf && + -f $tmp/top/@/etc/mkinitcpio.conf.d/99-omarchy-provisioning-key.conf ]] || fail "x86: the UKI carries the reset's auto-unlock" +[[ $(awk 'NR == 2 { print $2 }' "$tmp/slots") == "$(cat "$tmp/top/@/var/lib/omarchy/provisioning/luks-key")" ]] || + fail "x86: the throwaway slot opens with the staged key" "$(cat "$tmp/slots")" +! grep -q '^reset-' "$tmp/calls" || fail "x86: no boot-package entrypoint runs" "$(cat "$tmp/calls")" +pass "x86: dispatch is a no-op and the generic Limine UKI path resets the machine" + +# A failed activation on the generic path puts the previous root back and +# revokes the throwaway slot. The ESP already holds the factory root's UKI, so +# the screen says to rebuild the boot files rather than claiming nothing changed. +fixture +touch "$tmp/activate-fail" +if run reset x86; then fail "x86: a failed activation fails the reset"; fi +untouched || fail "x86: a failed activation puts the previous root back and revokes the throwaway slot" "$(ls "$tmp/top"; cat "$tmp/slots")" +! grep -q '^reset-' "$tmp/calls" || fail "x86: a failed activation runs no boot-package entrypoint" "$(cat "$tmp/calls")" +grep -q "run 'sudo limine-update' before rebooting" "$tmp/screen" || + fail "x86: a failed activation says the boot files need rebuilding" "$(cat "$tmp/screen")" +pass "x86: a failed activation puts the previous root back, revokes the throwaway slot and says to rebuild the boot files" + +# Apple: the boot package prepares and verifies the factory root before the +# throwaway slot exists, and gets the key only once the factory root is active. +fixture +run owner apple && [[ $(cat "$tmp/out") == "platform" ]] || fail "apple: the boot package owns the reset boot step" "$(cat "$tmp/out")" +run reset apple || fail "apple: an encrypted reset stages" "$(cat "$tmp/out" "$tmp/reset.log")" +activated || fail "apple: the factory root is activated" +expected="reset-prepare $tmp/top/@omarchy-reset-next $device +reset-verify $tmp/top/@omarchy-reset-next +cryptsetup luksAddKey +reset-commit" +[[ $(grep -v '^cryptsetup \(open\|luksDump\)' "$tmp/calls") == "$expected" ]] || + fail "apple: prepare, verify, the throwaway slot, then commit" "$(cat "$tmp/calls")" +staged=$(cat "$tmp/top/@/var/lib/omarchy/provisioning/luks-key") +[[ -n $staged && $(cat "$tmp/committed-key") == "$staged" ]] || fail "apple: reset-commit gets the staged key on standard input" +[[ $(awk 'NR == 2 { print $2 }' "$tmp/slots") == "$staged" ]] || fail "apple: the throwaway slot opens with the staged key" "$(cat "$tmp/slots")" +[[ ! -e $tmp/top/@/etc/omarchy/provisioning.key && ! -e $tmp/top/@/etc/limine-entry-tool.d/99-omarchy-provisioning-unlock.conf ]] || + fail "apple: no Limine UKI unlock is written" +! grep -q 'limine' "$tmp/calls" || fail "apple: the Limine UKI path never runs" "$(cat "$tmp/calls")" +! grep -Fq "$staged" "$tmp/reset.log" "$tmp/calls" "$tmp/out" || fail "apple: the throwaway key is never logged" +pass "apple: the boot package prepares and verifies the factory root before the throwaway slot, and gets the key after the switch" + +# A failed verification adds no credential and rolls the boot state back; the +# previous root keeps booting. +fixture +touch "$tmp/reset-verify-fail" +if run reset apple; then fail "apple: a failed verification fails the reset"; fi +[[ $(grep '^reset-' "$tmp/calls" | cut -d' ' -f1) == $'reset-prepare\nreset-verify\nreset-rollback' ]] || + fail "apple: a failed verification rolls back" "$(cat "$tmp/calls")" +! grep -q 'luksAddKey' "$tmp/calls" || fail "apple: a failed verification adds no slot" +untouched || fail "apple: a failed verification leaves the previous root and its slots" "$(cat "$tmp/slots")" +grep -q 'reset-verify: the fixture refuses' "$tmp/screen" || fail "apple: the verification's reason is shown" "$(cat "$tmp/screen")" +pass "apple: a failed verification rolls back, adds no slot and keeps the previous root" + +# A failure after the slot was added and before the switch revokes it too. +fixture +touch "$tmp/switch-fail" +if run reset apple; then fail "apple: a failed switch fails the reset"; fi +grep -q 'luksAddKey' "$tmp/calls" && grep -qx 'reset-rollback' "$tmp/calls" && ! grep -q '^reset-commit' "$tmp/calls" || + fail "apple: a failed switch rolls back without committing" "$(cat "$tmp/calls")" +untouched || fail "apple: a failed switch revokes the throwaway slot and keeps the previous root" "$(cat "$tmp/slots")" +pass "apple: a failure before the switch revokes the slot it added and rolls the boot state back" + +# The old root already moved aside when the factory root cannot take @: the old +# root goes back, and the reset is undone as if it failed before the switch. +fixture +touch "$tmp/activate-fail" +if run reset apple; then fail "apple: a failed activation fails the reset"; fi +grep -qx 'reset-rollback' "$tmp/calls" && ! grep -q '^reset-commit' "$tmp/calls" || + fail "apple: a failed activation rolls back without committing" "$(cat "$tmp/calls")" +untouched || fail "apple: a failed activation puts the previous root back at @ and revokes the throwaway slot" "$(ls "$tmp/top"; cat "$tmp/slots")" +! compgen -G "$tmp/top/@omarchy-old-*" >/dev/null || fail "apple: a failed activation leaves no renamed root behind" "$(ls "$tmp/top")" +grep -q 'the current root is back at @' "$tmp/screen" || fail "apple: a failed activation says the current root is back" "$(cat "$tmp/screen")" +pass "apple: a failed activation puts the previous root back, rolls back and revokes the slot" + +# When the old root cannot go back either, neither root is at @: both survive +# for recovery, and the screen names the rename that restores the machine. +fixture +touch "$tmp/activate-fail" "$tmp/restore-fail" +if run reset apple; then fail "apple: a failed activation and restore fails the reset"; fi +old=$(compgen -G "$tmp/top/@omarchy-old-*") || fail "apple: the previous root survives under its moved-aside name" "$(ls "$tmp/top")" +[[ -e $old/old-system && -e $tmp/top/@omarchy-reset-next/factory-system && ! -e $tmp/top/@ ]] || + fail "apple: a failed restore keeps both roots" "$(ls "$tmp/top")" +grep -qx 'reset-rollback' "$tmp/calls" && [[ $(cat "$tmp/slots") == "0 $current_password" ]] || + fail "apple: a failed restore still rolls the boot state back and revokes the slot" "$(cat "$tmp/calls" "$tmp/slots")" +grep -q "rename ${old##*/} to @" "$tmp/screen" || fail "apple: a failed restore names the recovery rename" "$(cat "$tmp/screen")" +pass "apple: a failed activation and restore keeps both roots and names the recovery rename" + +# A slot added but not confirmed is found by its key and revoked all the same. +fixture +touch "$tmp/lookup-fail-once" +if run reset apple; then fail "apple: an unconfirmed throwaway slot fails the reset"; fi +grep -qx 'reset-rollback' "$tmp/calls" && grep -q 'luksKillSlot' "$tmp/calls" || fail "apple: an unconfirmed slot is rolled back and killed" "$(cat "$tmp/calls")" +untouched || fail "apple: an unconfirmed throwaway slot is revoked" "$(cat "$tmp/slots")" +pass "apple: a throwaway slot the reset could not confirm is found by its key and revoked" + +# Once the factory root is active nothing rolls back: a failed commit is logged +# and costs the first boot one password prompt. +fixture +touch "$tmp/reset-commit-fail" +run reset apple || fail "apple: a failed commit still completes the reset" "$(cat "$tmp/out")" +activated && ! grep -qx 'reset-rollback' "$tmp/calls" || fail "apple: a failed commit keeps the factory root active" "$(cat "$tmp/calls")" +grep -q 'reset-commit: the fixture refuses' "$tmp/reset.log" || fail "apple: a failed commit is logged" "$(cat "$tmp/reset.log")" +[[ $(wc -l <"$tmp/slots") == 2 ]] || fail "apple: a failed commit keeps the throwaway slot" +pass "apple: a failed commit after the switch is logged and the reset stands" + +# An unencrypted Mac still has its boot files rebuilt and verified. +fixture +DEVICE="" run reset apple || fail "apple: an unencrypted reset stages" "$(cat "$tmp/out")" +[[ $(grep '^reset-' "$tmp/calls") == "reset-prepare $tmp/top/@omarchy-reset-next"$'\n'"reset-verify $tmp/top/@omarchy-reset-next"$'\n'"reset-commit" ]] || + fail "apple: an unencrypted reset names no LUKS device" "$(cat "$tmp/calls")" +! grep -q '^cryptsetup' "$tmp/calls" || fail "apple: an unencrypted reset touches no key slot" +pass "apple: an unencrypted reset rebuilds and verifies the boot files without a key" + +# Without the boot package, or with one that lacks a reset operation, the +# reset stops before anything is asked or changed, naming the package. +fixture +mv "$mac_boot" "$tmp/mac-boot.off" +if run owner apple; then fail "apple without the boot package: the reset refuses"; fi +grep -q 'reset-prepare on apple-silicon needs omarchy-mac-boot' "$tmp/out" || fail "apple without the boot package: the package is named" "$(cat "$tmp/out")" +mkdir -p "$mac_boot" +cp -p "$tmp/mac-boot.off/reset-prepare" "$tmp/mac-boot.off/reset-verify" "$mac_boot/" +chmod -R go-w "$lifecycle" +run owner x86 && [[ $(cat "$tmp/out") == "generic" ]] || fail "x86: a partial boot package on disk is ignored" "$(cat "$tmp/out")" +if run owner apple; then fail "apple with part of the boot package: the reset refuses"; fi +grep -q 'reset-commit on apple-silicon needs omarchy-mac-boot' "$tmp/out" || fail "apple with part of the boot package: the missing operation is named" "$(cat "$tmp/out")" +rm -rf "$mac_boot" +mv "$tmp/mac-boot.off" "$mac_boot" +pass "apple: without the boot package's reset operations, or with only some of them, the reset refuses naming the package" + +# A registration that left the reset operations optional and got only some of +# them would mix two boot paths: the reset refuses that too. +mkdir -p "$tmp/half" +cat >"$tmp/half/omarchy-lifecycle-dispatch" < "$scratch/bin/sudo" <<'STUB' #!/bin/bash case "$1" in + -n) exit "${SUDO_CACHED:-1}" ;; + -v) echo "sudo authenticated" >> "$CALL_LOG"; exit "${SUDO_V_STATUS:-0}" ;; pacman | fprintd-enroll) exec "$@" ;; + # PAM edits are logged, never made; tee's input is read and dropped. + sed | tee) echo "privileged $*" >> "$CALL_LOG"; [[ $1 != "tee" ]] || cat > /dev/null ;; *) echo "Unexpected privileged call: $*" >> "$CALL_LOG"; exit 99 ;; esac STUB @@ -45,16 +49,30 @@ case "$1" in *) printf 'pacman %s\n' "$*" >> "$CALL_LOG"; exit 99 ;; esac STUB +# Both fail unless ENROLL_STATUS or VERIFY_STATUS says otherwise, stopping +# before PAM. ENROLL_OUTPUT is what fprintd-enroll prints first. cat > "$scratch/bin/fprintd-enroll" <<'STUB' #!/bin/bash -# Stop before verification/PAM; no host authentication files may be changed. echo enroll >> "$CALL_LOG" -exit 1 +[[ -z ${ENROLL_OUTPUT:-} ]] || printf '%s\n' "$ENROLL_OUTPUT" +exit "${ENROLL_STATUS:-1}" STUB cat > "$scratch/bin/fprintd-verify" <<'STUB' #!/bin/bash echo verify >> "$CALL_LOG" -exit 1 +exit "${VERIFY_STATUS:-1}" +STUB +# fprintd on D-Bus: SCAN_TYPE is the default reader's scan type and READER_NAME +# its name; with SCAN_TYPE unset, fprintd doesn't answer. Never the host's own fprintd. +cat > "$scratch/bin/busctl" <<'STUB' +#!/bin/bash +[[ -n ${SCAN_TYPE:-} ]] || exit 1 +case "$*" in + *GetDefaultDevice*) echo 'o "/net/reactivated/Fprint/Device/0"' ;; + *"/net/reactivated/Fprint/Device/0 net.reactivated.Fprint.Device scan-type"*) echo "s \"$SCAN_TYPE\"" ;; + *"/net/reactivated/Fprint/Device/0 net.reactivated.Fprint.Device name"*) echo "s \"${READER_NAME:-Some USB reader}\"" ;; + *) exit 1 ;; +esac STUB chmod +x "$scratch/bin/"* @@ -63,8 +81,8 @@ run_setup() { if "$ROOT/bin/omarchy-setup-security-fingerprint" > "$scratch/output" 2>&1; then fail "setup stops on the simulated enrollment or installation failure" fi - if grep -q 'Unexpected privileged call' "$CALL_LOG"; then - fail "setup does not change PAM after failed enrollment" + if grep -q -e 'Unexpected privileged call' -e '^privileged ' "$CALL_LOG"; then + fail "setup does not change PAM after a failed enrollment, installation or verify" fi } @@ -98,3 +116,154 @@ pass "a failed installation stops before enrollment" HARDWARE_STATUS=1 run_setup [[ ! -s $CALL_LOG ]] || fail "missing hardware stops before package operations" pass "missing hardware performs no package operations" + +# A press sensor, such as a Mac's Touch ID, takes a touch per sample, so setup +# says to lift and touch again; a swipe sensor, or none fprintd can name, keeps +# the moving-finger instruction. +press_text="Touch the sensor, lift your finger and touch it again, and keep going until it says Enrolled. A step can take a few touches." +swipe_text="Keep moving the finger around on sensor until it says Enrolled." +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press run_setup +grep -qF "$press_text" "$scratch/output" && ! grep -qF "$swipe_text" "$scratch/output" || + fail "a press sensor is told to lift and touch again" "$(cat "$scratch/output")" +for scan_type in swipe ""; do + INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=$scan_type run_setup + grep -qF "$swipe_text" "$scratch/output" && ! grep -qF "$press_text" "$scratch/output" || + fail "a ${scan_type:-unnamed} sensor keeps the moving-finger instruction" "$(cat "$scratch/output")" +done +pass "a press sensor is told to touch again, and any other keeps the moving-finger instruction" + +# fprintd-enroll's raw results become instructions. Touch ID's duplicate check +# stage prints nothing; later stages count as steps, never touches, since one +# stage can take several. +enroll_lines=$'Using device /net/reactivated/Fprint/Device/0\nEnrolling right-index-finger finger.' +for result in stage-passed stage-passed retry-scan stage-passed unknown-error; do + enroll_lines+=$'\nEnroll result: enroll-'$result +done +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press READER_NAME='Apple secure enclave fingerprint sensor' \ + ENROLL_OUTPUT=$enroll_lines run_setup +expected=$' \u2713 Step 1 done. Keep lifting your finger and touching the sensor. + That one didn\'t read. Keep lifting your finger and touching the sensor. + \u2713 Step 2 done. Keep lifting your finger and touching the sensor. + \u2717 Enrollment stopped. If you stopped before it said Enrolled, run this again and keep going.' +[[ $(grep -E '^ ' "$scratch/output") == "$expected" ]] || + fail "enrollment results read as instructions, counting steps after the duplicate check" "$(cat "$scratch/output")" +! grep -q 'Enroll result:' "$scratch/output" || fail "no raw fprintd result reaches the owner" "$(cat "$scratch/output")" +grep -q 'Enrollment failed' "$scratch/output" || fail "a failed enrollment still fails setup" "$(cat "$scratch/output")" +pass "enrollment results read as instructions, and a failed enrollment still fails setup" + +# PAM changes only after an enrolled print verifies; a failed verify fails setup. +: > "$CALL_LOG" +if INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press ENROLL_STATUS=0 VERIFY_STATUS=1 \ + "$ROOT/bin/omarchy-setup-security-fingerprint" > "$scratch/output" 2>&1; then + fail "a failed verify fails setup" "$(cat "$scratch/output")" +fi +grep -qx verify "$CALL_LOG" && ! grep -q '^privileged ' "$CALL_LOG" || + fail "a failed verify leaves PAM alone" "$(cat "$CALL_LOG")" +: > "$CALL_LOG" +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press ENROLL_STATUS=0 VERIFY_STATUS=0 \ + "$ROOT/bin/omarchy-setup-security-fingerprint" > "$scratch/output" 2>&1 || + fail "a verified print completes setup" "$(cat "$scratch/output")" +# Only the lock-screen file is written whatever the host's /etc/pam.d holds. +verified=$(grep -nx verify "$CALL_LOG" | cut -d: -f1) +lock_pam=$(grep -n '^privileged tee /etc/pam.d/omarchy-lock-fingerprint$' "$CALL_LOG" | cut -d: -f1) +[[ -n $verified && -n $lock_pam ]] && (( verified < lock_pam )) || + fail "PAM is written after verify" "$(cat "$CALL_LOG")" +pass "PAM is written only after a print verifies, and a failed verify fails setup" + +# The verify step's results, in fprintd-verify's own format (each with " (done)" +# or " (not done)"), through the functions setup uses. +source <(sed -n '/^fprintd_chatter()/,/^}/p;/^enroll_progress()/,/^}/p;/^verify_progress()/,/^}/p' \ + "$ROOT/bin/omarchy-setup-security-fingerprint") +[[ $(printf '%s\n' 'Using device /net/reactivated/Fprint/Device/0' 'Listing enrolled fingers:' ' - #0: right-index-finger' \ + 'Verify started!' 'Verifying: right-index-finger' 'Verify result: verify-retry-scan (not done)' \ + 'Verify result: verify-finger-not-centered (not done)' 'Verify result: verify-match (done)' | + verify_progress "Touch again.") == $' That one didn\'t read. Touch again.\n Not centered: use the middle of the sensor. Touch again.\n \u2713 Matched.' ]] || + fail "verify results, retries included, read as instructions" +[[ $(printf '%s\n' 'Verify result: verify-no-match (done)' | verify_progress "Touch again.") == $' \u2717 No match.' ]] || + fail "a failed verify says so" +pass "verify results read as instructions" + +# Only Touch ID's first stage is its duplicate check. On any other reader, one +# that doesn't identify or one whose duplicate check took a touch, the first +# stage to pass is step 1. +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press ENROLL_OUTPUT=$enroll_lines run_setup +[[ $(grep -c 'Step [0-9] done' "$scratch/output") == 3 ]] || + fail "another reader's first stage is step 1" "$(cat "$scratch/output")" +INSTALLED=$'libfprint-git\nfprintd\nusbutils' ENROLL_OUTPUT=$enroll_lines run_setup +[[ $(grep -c 'Step [0-9] done' "$scratch/output") == 3 ]] || + fail "a reader fprintd can't name has its first stage counted" "$(cat "$scratch/output")" +[[ $(printf '%s\n' 'Enroll result: enroll-stage-passed' 'Enroll result: enroll-stage-passed' | enroll_progress "Again." 1) == \ + $' \u2713 Step 1 done. Again.' ]] || fail "with a duplicate check, the first stage is not a step" +pass "only a reader with a duplicate check leaves its first stage out of the steps" + +# sudo's own prompt comes first and is named as such; with sudo already +# authenticated there is no prompt to explain. +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press run_setup +grep -qF "First, confirm it's you." "$scratch/output" && grep -qx 'sudo authenticated' "$CALL_LOG" || + fail "setup names sudo's prompt and authenticates before enrolling" "$(cat "$scratch/output")" +(( $(grep -n "First, confirm it's you." "$scratch/output" | cut -d: -f1) < $(grep -n "Let's set up" "$scratch/output" | cut -d: -f1) )) || + fail "sudo's prompt comes before the enrollment instructions" "$(cat "$scratch/output")" +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press SUDO_CACHED=0 run_setup +! grep -qF "First, confirm it's you." "$scratch/output" && ! grep -qx 'sudo authenticated' "$CALL_LOG" || + fail "an authenticated sudo is not asked again" "$(cat "$scratch/output")" +pass "sudo is authenticated, and named, before the enrollment instructions" + +# What fprintd prints when it can't go on (a claimed device, no fingers) is +# shown; its routine lines are not. +[[ $(printf '%s\n' 'Using device /net/reactivated/Fprint/Device/0' \ + 'failed to claim device: GDBus.Error:net.reactivated.Fprint.Error.AlreadyInUse: Device was already claimed' | + enroll_progress "Touch again.") == ' failed to claim device: GDBus.Error:net.reactivated.Fprint.Error.AlreadyInUse: Device was already claimed' ]] || + fail "why enrollment could not start is shown" +[[ $(printf '%s\n' 'Using device /net/reactivated/Fprint/Device/0' 'No fingers enrolled for this device.' | + verify_progress "Touch again.") == ' No fingers enrolled for this device.' ]] || fail "why verify could not start is shown" +pass "fprintd's reasons for stopping reach the owner, its routine lines do not" + +# A sudo that won't authenticate stops setup with a reason, before enrolling. +INSTALLED=$'libfprint-git\nfprintd\nusbutils' SCAN_TYPE=press SUDO_V_STATUS=1 run_setup +grep -qF "Setup needs administrator rights to enroll a fingerprint." "$scratch/output" && ! grep -qx enroll "$CALL_LOG" || + fail "a failed sudo stops setup with a reason" "$(cat "$scratch/output")" +pass "a failed sudo stops setup with a reason, before enrolling" + +# Every retry fprintd 1.94.5 can send keeps enrollment going; only a failure +# says it stopped, and a result fprintd adds later is shown, not called a stop. +[[ $(printf '%s\n' 'Enrolling right-index-finger finger.' 'Enroll result: enroll-stage-passed' 'Enroll result: enroll-too-fast' \ + 'Enroll result: enroll-swipe-too-short' 'Enroll result: enroll-stage-passed' 'Enroll result: enroll-some-new-result' \ + 'Enroll result: enroll-completed' | enroll_progress "Swipe it again." 1) == $' Too fast. Swipe it again.\n Too short. Swipe it again.\n \u2713 Step 1 done. Swipe it again.\n enroll-some-new-result\n \u2713 Enrolled.' ]] || + fail "enrollment retries keep going, and an unknown result is shown" +[[ $(printf '%s\n' 'Verify result: verify-too-fast (not done)' 'Verify result: verify-some-new-retry (not done)' \ + 'Verify result: verify-disconnected (done)' | verify_progress "Swipe it again.") == $' Too fast. Swipe it again.\n That one didn\'t read. Swipe it again.\n \u2717 The check stopped.' ]] || + fail "any verify result that isn't done is a retry" +pass "every retry keeps the step going, and only a finished failure says it stopped" + +# Each of fprintd 1.94.5's enroll and verify results, one by one. +check_enroll() { + [[ $(printf 'Enroll result: %s\n' "$1" | enroll_progress "Again.") == "$2" ]] || + fail "enroll result $1 reads as: $2" "$(printf 'Enroll result: %s\n' "$1" | enroll_progress "Again.")" +} +check_enroll enroll-completed $' ✓ Enrolled.' +check_enroll enroll-retry-scan " That one didn't read. Again." +check_enroll enroll-swipe-too-short ' Too short. Again.' +check_enroll enroll-finger-not-centered ' Not centered: use the middle of the sensor. Again.' +check_enroll enroll-remove-and-retry ' Lift your finger off the sensor, then: Again.' +check_enroll enroll-too-fast ' Too fast. Again.' +check_enroll enroll-duplicate $' ✗ That finger is already enrolled.' +check_enroll enroll-data-full $' ✗ The reader has no room for another fingerprint.' +check_enroll enroll-disconnected $' ✗ The reader was disconnected.' +for result in enroll-failed enroll-unknown-error; do + check_enroll "$result" $' ✗ Enrollment stopped. If you stopped before it said Enrolled, run this again and keep going.' +done +check_verify() { + [[ $(printf 'Verify result: %s\n' "$1" | verify_progress "Again.") == "$2" ]] || + fail "verify result $1 reads as: $2" "$(printf 'Verify result: %s\n' "$1" | verify_progress "Again.")" +} +check_verify 'verify-match (done)' $' ✓ Matched.' +check_verify 'verify-no-match (done)' $' ✗ No match.' +check_verify 'verify-retry-scan (not done)' " That one didn't read. Again." +check_verify 'verify-swipe-too-short (not done)' ' Too short. Again.' +check_verify 'verify-finger-not-centered (not done)' ' Not centered: use the middle of the sensor. Again.' +check_verify 'verify-remove-and-retry (not done)' ' Lift your finger off the sensor, then: Again.' +check_verify 'verify-too-fast (not done)' ' Too fast. Again.' +for result in verify-disconnected verify-unknown-error; do + check_verify "$result (done)" $' ✗ The check stopped.' +done +pass "each enroll and verify result fprintd 1.94.5 sends reads as intended" diff --git a/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/90-omarchy-mac.conf b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/90-omarchy-mac.conf new file mode 100644 index 000000000000..0457b32d8a19 --- /dev/null +++ b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/90-omarchy-mac.conf @@ -0,0 +1,18 @@ +# Generated by the Omarchy Apple Silicon image builder. +_omarchy_asahi_hooks=() +_omarchy_asahi_added=false +for _omarchy_asahi_hook in "${HOOKS[@]}"; do + if [[ $_omarchy_asahi_hook == asahi ]]; then + _omarchy_asahi_added=true + fi + if [[ $_omarchy_asahi_hook == filesystems && $_omarchy_asahi_added == false ]]; then + _omarchy_asahi_hooks+=(asahi omarchy-vendorfw) + _omarchy_asahi_added=true + fi + _omarchy_asahi_hooks+=("$_omarchy_asahi_hook") +done +if [[ $_omarchy_asahi_added == false ]]; then + _omarchy_asahi_hooks+=(asahi omarchy-vendorfw) +fi +HOOKS=("${_omarchy_asahi_hooks[@]}") +unset _omarchy_asahi_hooks _omarchy_asahi_hook _omarchy_asahi_added diff --git a/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/91-omarchy-mac-encrypt.conf b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/91-omarchy-mac-encrypt.conf new file mode 100644 index 000000000000..306582fb00ef --- /dev/null +++ b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/91-omarchy-mac-encrypt.conf @@ -0,0 +1,90 @@ +# Insert omarchy-mac-encrypt after vendorfw/block and sd-encrypt immediately +# before filesystems, each only if that hook is absent. 90-omarchy-mac.conf +# already put asahi and omarchy-vendorfw before filesystems; this drop-in is +# sourced after it. +# +# Both are systemd initrd units: the systemd hook replaces udev (mkinitcpio's +# stock HOOKS line) and keymap/consolefont become sd-vconsole. A HOOKS line +# carrying the busybox encrypt hook belongs to a Mac unlocked by cryptdevice=, +# which sd-encrypt cannot parse: that line is left exactly as it is. +_omarchy_mac_encrypt_hooks=() +_omarchy_mac_encrypt_have_systemd=false +_omarchy_mac_encrypt_have_vconsole=false +_omarchy_mac_encrypt_have_encrypt=false +for _omarchy_mac_encrypt_hook in "${HOOKS[@]}"; do + case $_omarchy_mac_encrypt_hook in + systemd) _omarchy_mac_encrypt_have_systemd=true ;; + sd-vconsole) _omarchy_mac_encrypt_have_vconsole=true ;; + encrypt) _omarchy_mac_encrypt_have_encrypt=true ;; + esac +done +if [[ $_omarchy_mac_encrypt_have_encrypt == false ]]; then +for _omarchy_mac_encrypt_hook in "${HOOKS[@]}"; do + case $_omarchy_mac_encrypt_hook in + udev) + if [[ $_omarchy_mac_encrypt_have_systemd == false ]]; then + _omarchy_mac_encrypt_hooks+=(systemd) + _omarchy_mac_encrypt_have_systemd=true + fi + ;; + keymap|consolefont) + if [[ $_omarchy_mac_encrypt_have_vconsole == false ]]; then + _omarchy_mac_encrypt_hooks+=(sd-vconsole) + _omarchy_mac_encrypt_have_vconsole=true + fi + ;; + *) _omarchy_mac_encrypt_hooks+=("$_omarchy_mac_encrypt_hook") ;; + esac +done +if [[ $_omarchy_mac_encrypt_have_systemd == false ]]; then + if [[ ${_omarchy_mac_encrypt_hooks[0]:-} == base ]]; then + _omarchy_mac_encrypt_hooks=(base systemd "${_omarchy_mac_encrypt_hooks[@]:1}") + else + _omarchy_mac_encrypt_hooks=(systemd "${_omarchy_mac_encrypt_hooks[@]}") + fi +fi +HOOKS=("${_omarchy_mac_encrypt_hooks[@]}") +_omarchy_mac_encrypt_hooks=() +_omarchy_mac_encrypt_have_ours=false +_omarchy_mac_encrypt_have_sd=false +_omarchy_mac_encrypt_added_ours=false +_omarchy_mac_encrypt_added_sd=false +for _omarchy_mac_encrypt_hook in "${HOOKS[@]}"; do + if [[ $_omarchy_mac_encrypt_hook == omarchy-mac-encrypt ]]; then + _omarchy_mac_encrypt_have_ours=true + fi + if [[ $_omarchy_mac_encrypt_hook == sd-encrypt ]]; then + _omarchy_mac_encrypt_have_sd=true + fi +done +for _omarchy_mac_encrypt_hook in "${HOOKS[@]}"; do + if [[ $_omarchy_mac_encrypt_hook == sd-encrypt && $_omarchy_mac_encrypt_have_ours == false && + $_omarchy_mac_encrypt_added_ours == false ]]; then + _omarchy_mac_encrypt_hooks+=(omarchy-mac-encrypt) + _omarchy_mac_encrypt_added_ours=true + fi + if [[ $_omarchy_mac_encrypt_hook == filesystems ]]; then + if [[ $_omarchy_mac_encrypt_have_ours == false && $_omarchy_mac_encrypt_added_ours == false ]]; then + _omarchy_mac_encrypt_hooks+=(omarchy-mac-encrypt) + _omarchy_mac_encrypt_added_ours=true + fi + if [[ $_omarchy_mac_encrypt_have_sd == false && $_omarchy_mac_encrypt_added_sd == false ]]; then + _omarchy_mac_encrypt_hooks+=(sd-encrypt) + _omarchy_mac_encrypt_added_sd=true + fi + fi + _omarchy_mac_encrypt_hooks+=("$_omarchy_mac_encrypt_hook") +done +if [[ $_omarchy_mac_encrypt_have_ours == false && $_omarchy_mac_encrypt_added_ours == false ]]; then + _omarchy_mac_encrypt_hooks+=(omarchy-mac-encrypt) +fi +if [[ $_omarchy_mac_encrypt_have_sd == false && $_omarchy_mac_encrypt_added_sd == false ]]; then + _omarchy_mac_encrypt_hooks+=(sd-encrypt) +fi +HOOKS=("${_omarchy_mac_encrypt_hooks[@]}") +fi +unset _omarchy_mac_encrypt_hooks _omarchy_mac_encrypt_hook \ + _omarchy_mac_encrypt_have_ours _omarchy_mac_encrypt_have_sd \ + _omarchy_mac_encrypt_added_ours _omarchy_mac_encrypt_added_sd \ + _omarchy_mac_encrypt_have_systemd _omarchy_mac_encrypt_have_vconsole \ + _omarchy_mac_encrypt_have_encrypt diff --git a/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/92-omarchy-mac-hid.conf b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/92-omarchy-mac-hid.conf new file mode 100644 index 000000000000..b75e8a55d788 --- /dev/null +++ b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/92-omarchy-mac-hid.conf @@ -0,0 +1,31 @@ +# Origin: omarchy-mx-mac install/hardware/apple/fix-asahi-hid-race.sh +# Apple Silicon internal keyboard and trackpad at the sd-encrypt passphrase +# prompt. dockchannel-hid creates the HID devices; hid_apple binds the +# keyboard and hid_magicmouse the SPI trackpad. MTP machines (M2 Air, M2 Max +# j416c) use dockchannel-hid — linux-aurora has no apple-mtp module. SPI HID +# (M1 Air, M1 Pro j314s) is spi-hid-apple-of, built-in on aurora. usbhid is +# an external USB keyboard at the prompt. thunderbolt (CONFIG_USB4=m) and +# thunderbolt_apple, the Apple Silicon USB4 host router (CONFIG_USB4_APPLE_SOC=m +# on linux-aurora), bring up the USB4/Thunderbolt tunnels, so a keyboard behind +# a USB-C or Thunderbolt dock types at the prompt too (omarchy-mx-mac#86). +# +# mkinitcpio fails the whole image over a MODULES entry it cannot find, or a +# built-in it reports as "(builtin)". Each name is added only when modinfo +# returns a real path. Ending on unset keeps the exit status zero. + +for _omarchy_mac_hid_module in \ + hid_apple hid_magicmouse dockchannel-hid usbhid \ + spi-apple spi-hid-apple spi-hid-apple-of \ + apple-dockchannel apple-rtkit-helper thunderbolt thunderbolt_apple; do + _omarchy_mac_hid_path=$(modinfo -k "${KERNELVERSION:-$(uname -r)}" -F filename \ + "$_omarchy_mac_hid_module" 2>/dev/null) || continue + [[ $_omarchy_mac_hid_path == /* ]] || continue + MODULES+=("$_omarchy_mac_hid_module") +done +unset _omarchy_mac_hid_module _omarchy_mac_hid_path + +# MTP trackpad firmware (apple/tpmtfw-.bin) is not a MODULES entry. +# omarchy-vendorfw-initrd.service unpacks ESP vendorfw/firmware.cpio onto +# /lib/firmware/vendor (symlink to /vendorfw) before cryptsetup-pre.target. +# Do not FILES+= under /lib/firmware/vendor, and do not pre-create +# /vendorfw/apple in the image: that skipped ESP extraction. diff --git a/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/93-omarchy-mac-plymouth.conf b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/93-omarchy-mac-plymouth.conf new file mode 100644 index 000000000000..75cbbb50d83b --- /dev/null +++ b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/93-omarchy-mac-plymouth.conf @@ -0,0 +1,18 @@ +# Plymouth draws the disk password prompt and the boot splash, as on x86 +# Omarchy. It goes right after systemd so its initrd units order correctly, +# only when the hook is installed, and never twice. +if [[ -f /usr/lib/initcpio/install/plymouth && " ${HOOKS[*]} " != *" plymouth "* ]]; then + _omarchy_mac_plymouth_hooks=() + _omarchy_mac_plymouth_added=false + for _omarchy_mac_plymouth_hook in "${HOOKS[@]}"; do + _omarchy_mac_plymouth_hooks+=("$_omarchy_mac_plymouth_hook") + if [[ $_omarchy_mac_plymouth_hook == systemd && $_omarchy_mac_plymouth_added == false ]]; then + _omarchy_mac_plymouth_hooks+=(plymouth) + _omarchy_mac_plymouth_added=true + fi + done + if [[ $_omarchy_mac_plymouth_added == true ]]; then + HOOKS=("${_omarchy_mac_plymouth_hooks[@]}") + fi + unset _omarchy_mac_plymouth_hooks _omarchy_mac_plymouth_added _omarchy_mac_plymouth_hook +fi diff --git a/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/94-omarchy-mac-vconsole.conf b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/94-omarchy-mac-vconsole.conf new file mode 100644 index 000000000000..9547df778135 --- /dev/null +++ b/test/shell.d/fixtures/omarchy-mac-boot-mkinitcpio/94-omarchy-mac-vconsole.conf @@ -0,0 +1,46 @@ +# The disk passphrase prompt types with the owner's keyboard layout, as on +# x86 Omarchy: sd-vconsole loads KEYMAP on the console (systemd-ask-password) +# and /etc/vconsole.conf gives Plymouth its XKBLAYOUT. The aarch64 +# omarchy-settings drops upstream's omarchy_hooks.conf, so this drop-in +# carries its guard: a layout that does not type Latin letters stays out of +# the initramfs, because a Latin passphrase would be untypeable in it +# (upstream #6229). The prompt then uses the kernel's US map, which is what +# such a passphrase was typed with. +# +# A systemd HOOKS line gets sd-vconsole exactly once, after keyboard (or +# after systemd without one); keymap and consolefont are its busybox +# counterparts and never belong on such a line. A busybox line (a Mac +# unlocked by cryptdevice=, which 91 leaves alone) keeps its hooks; it only +# gets the file for Plymouth, as upstream does. +# No vconsole.conf is the kernel's US map: sd-vconsole stays, nothing to bundle. +_omarchy_mac_vconsole_latin=true +if [[ -f /etc/vconsole.conf ]]; then + _omarchy_mac_vconsole_layout=$(unset XKBLAYOUT; . /etc/vconsole.conf 2>/dev/null; printf '%s' "${XKBLAYOUT:-}") + case ${_omarchy_mac_vconsole_layout%%,*} in + af | am | ara | bd | bg | by | et | ge | gr | il | in | iq | ir | kg | kh | kz | la | lk | mk | mm | mn | mv | np | rs | ru | sy | th | tj | ua) + _omarchy_mac_vconsole_latin=false ;; + esac +fi + +if [[ " ${HOOKS[*]} " == *" systemd "* ]]; then + _omarchy_mac_vconsole_hooks=() + _omarchy_mac_vconsole_anchor=systemd + [[ " ${HOOKS[*]} " != *" keyboard "* ]] || _omarchy_mac_vconsole_anchor=keyboard + for _omarchy_mac_vconsole_hook in "${HOOKS[@]}"; do + case $_omarchy_mac_vconsole_hook in + sd-vconsole | keymap | consolefont) continue ;; + esac + _omarchy_mac_vconsole_hooks+=("$_omarchy_mac_vconsole_hook") + if [[ $_omarchy_mac_vconsole_hook == "$_omarchy_mac_vconsole_anchor" && $_omarchy_mac_vconsole_latin == true ]]; then + _omarchy_mac_vconsole_hooks+=(sd-vconsole) + _omarchy_mac_vconsole_anchor= + fi + done + HOOKS=("${_omarchy_mac_vconsole_hooks[@]}") +fi + +if [[ $_omarchy_mac_vconsole_latin == true && -f /etc/vconsole.conf ]]; then + FILES+=(/etc/vconsole.conf) +fi +unset _omarchy_mac_vconsole_latin _omarchy_mac_vconsole_layout _omarchy_mac_vconsole_hooks \ + _omarchy_mac_vconsole_anchor _omarchy_mac_vconsole_hook diff --git a/test/shell.d/fixtures/sudo-boundary-test.sh b/test/shell.d/fixtures/sudo-boundary-test.sh index 1fb123d097ca..4891811ea85b 100644 --- a/test/shell.d/fixtures/sudo-boundary-test.sh +++ b/test/shell.d/fixtures/sudo-boundary-test.sh @@ -32,6 +32,17 @@ PY copy_boundary_file bin/omarchy-security-functions copy_boundary_file bin/omarchy-update-pacman copy_boundary_file default/omarchy/sudo-no-update/sudo +copy_boundary_file install/helpers/pacman.sh +# Channel changes copy the platform's templates; the copy itself is a stand-in. +cp -r "$ROOT/default/pacman" "$SUDO_TEST_ROOT/default/" + +# Channel changes ask for the platform. x86 unless a test says otherwise, so no +# test reads the host's hardware. +cat >"$SUDO_TEST_ROOT/bin/omarchy-hw-platform" <<'STUB' +#!/bin/bash +echo "${SUDO_TEST_PLATFORM:-generic}" +STUB +chmod +x "$SUDO_TEST_ROOT/bin/omarchy-hw-platform" cat >"$SUDO_TEST_ROOT/mock/sudo" <<'STUB' #!/bin/bash @@ -132,7 +143,7 @@ case "$step" in esac STUB chmod +x "$SUDO_TEST_ROOT/bin/test-step" -for step in omarchy-update-lock omarchy-update-requires-free-space omarchy-update-confirm omarchy-update-pkg-prune omarchy-snapshot omarchy-update-stay-awake omarchy-update-dev omarchy-update-keyring omarchy-update-system-pkgs omarchy-migrate omarchy-hook omarchy-update-aur-pkgs omarchy-update-mise omarchy-update-orphan-pkgs omarchy-update-analyze-logs omarchy-update-status omarchy-update-restart omarchy-pkg-aur-accessible omarchy-notification-dismiss pacman systemd-run cp yay; do +for step in omarchy-update-lock omarchy-update-requires-free-space omarchy-update-confirm omarchy-update-pkg-prune omarchy-snapshot omarchy-update-stay-awake omarchy-update-dev omarchy-update-boot omarchy-update-keyring omarchy-update-system-pkgs omarchy-migrate omarchy-hook omarchy-update-aur-pkgs omarchy-update-mise omarchy-update-orphan-pkgs omarchy-update-analyze-logs omarchy-update-status omarchy-update-restart omarchy-pkg-aur-accessible omarchy-notification-dismiss pacman systemd-run cp yay; do ln -s test-step "$SUDO_TEST_ROOT/bin/$step" done ln -s ../bin/test-step "$SUDO_TEST_ROOT/mock/pacman" @@ -140,7 +151,7 @@ ln -s ../bin/test-step "$SUDO_TEST_ROOT/mock/pacman" reset_boundary() { : >"$SUDO_TEST_LOG" /usr/bin/rm -f "$SUDO_TEST_CACHE" - unset SUDO_TEST_FAIL_STEP SUDO_TEST_SIGNAL_STEP SUDO_TEST_SUDO_FAIL SUDO_TEST_REVOKE_FAIL SUDO_TEST_UNSUPPORTED SUDO_TEST_REMOVE_WRAPPER_STEP + unset SUDO_TEST_FAIL_STEP SUDO_TEST_SIGNAL_STEP SUDO_TEST_SUDO_FAIL SUDO_TEST_REVOKE_FAIL SUDO_TEST_UNSUPPORTED SUDO_TEST_REMOVE_WRAPPER_STEP SUDO_TEST_PLATFORM } assert_boundary_cold() { [[ ! -e $SUDO_TEST_CACHE ]] || fail "$1 left cached authorization" diff --git a/test/shell.d/hw-display-test.sh b/test/shell.d/hw-display-test.sh index e40288315b43..5e9bb1398e9a 100755 --- a/test/shell.d/hw-display-test.sh +++ b/test/shell.d/hw-display-test.sh @@ -17,8 +17,18 @@ write_backlights() { done } +# A copy of the command reads its platform file from a fixture root: none +# unless a case writes one, whatever the machine running the suite has installed. +platform_root="$tmp_dir/platform" +mkdir -p "$platform_root" "$tmp_dir/bin" +platform_root_copy "$ROOT/bin/omarchy-hw-display" "$tmp_dir/bin/omarchy-hw-display" "$platform_root" + +write_displays_conf() { + printf '%s\n' "$@" >"$platform_root/displays.conf" +} + hw_display() { - OMARCHY_BACKLIGHT_PATH="$tmp_dir/backlight" "$ROOT/bin/omarchy-hw-display" + OMARCHY_BACKLIGHT_PATH="$tmp_dir/backlight" "$tmp_dir/bin/omarchy-hw-display" } write_backlights intel_backlight @@ -48,9 +58,9 @@ pass "a T2 Mac uses gmux instead of the Touch Bar" write_backlights appletb_backlight if hw_display >/dev/null 2>&1; then - fail "the Touch Bar is never used as the display backlight" + fail "the T2 Touch Bar is never used as the display backlight" fi -pass "the Touch Bar is never used as the display backlight" +pass "the T2 Touch Bar is never used as the display backlight" write_backlights acpi_video0 amdgpu_bl0 appletb_backlight gmux_backlight intel_backlight device=$(hw_display) @@ -62,3 +72,71 @@ if hw_display >/dev/null 2>&1; then fail "no backlight device reports failure" fi pass "no backlight device reports failure" + +# An empty platform file changes nothing. +: >"$platform_root/displays.conf" +write_backlights acpi_video0 amdgpu_bl1 +device=$(hw_display) +[[ $device == "amdgpu_bl1" ]] || fail "an empty platform file keeps the candidate order" "actual: $device" +write_backlights nvidia_wmi_ec_backlight +device=$(hw_display) +[[ $device == "nvidia_wmi_ec_backlight" ]] || fail "an empty platform file keeps the fallback" "actual: $device" +pass "an empty platform file changes nothing" + +write_displays_conf '# accessory panels' 'backlight-skip accessory-pipe' 'backlight-skip *.dsi.0' 'backlight-prefer panel-bl' +write_backlights 1000.dsi.0 accessory-pipe +if hw_display >/dev/null 2>&1; then + fail "a skipped backlight is never the fallback" +fi +write_backlights 1000.dsi.0 accessory-pipe zz-other +device=$(hw_display) +[[ $device == "zz-other" ]] || fail "the fallback passes over skipped backlights" "actual: $device" +pass "a platform's skipped backlights are never the fallback" + +write_backlights 1000.dsi.0 amdgpu_bl0 panel-bl +device=$(hw_display) +[[ $device == "panel-bl" ]] || fail "a preferred backlight beats the GPU backlights" "actual: $device" +printf 'backlight-prefer panel-bl\r\n' >"$platform_root/displays.conf" +device=$(hw_display) +[[ $device == "panel-bl" ]] || fail "a file saved with CRLF line ends reads the same" "actual: $device" +write_displays_conf '# accessory panels' 'backlight-skip accessory-pipe' 'backlight-skip *.dsi.0' 'backlight-prefer panel-bl' +write_backlights gmux_backlight panel-bl +device=$(hw_display) +[[ $device == "gmux_backlight" ]] || fail "gmux still beats a preferred backlight" "actual: $device" +pass "a platform's preferred backlight follows gmux and beats the GPU backlights" + +write_displays_conf 'backlight-prefer second-bl' 'backlight-prefer first-bl' +write_backlights first-bl second-bl +device=$(hw_display) +[[ $device == "second-bl" ]] || fail "preferences are tried in file order" "actual: $device" +pass "preferences are tried in file order" + +write_displays_conf 'backlight-skip intel_backlight' 'backlight-prefer panel-bl' 'backlight-skip panel-bl' +write_backlights acpi_video0 intel_backlight panel-bl +device=$(hw_display) +[[ $device == "acpi_video0" ]] || fail "a skip beats preferred and built-in candidates" "actual: $device" +write_displays_conf 'backlight-skip gmux_backlight' +write_backlights gmux_backlight intel_backlight +device=$(hw_display) +[[ $device == "intel_backlight" ]] || fail "a skip beats gmux" "actual: $device" +pass "a skip beats preferred and built-in candidates" + +write_displays_conf 'backlight-prefer panel-*' 'backlight-prefer .' 'backlight-prefer ..' +write_backlights amdgpu_bl0 panel-bl +device=$(hw_display) +[[ $device == "amdgpu_bl0" ]] || fail "a preference is a literal name" "actual: $device" +pass "a preference is a literal name" + +# Only whole directives count: an unknown one, a path, an extra word or a +# trailing comment leaves the line out; the last line needs no newline. +printf '%s\n' 'backlight-future panel-bl' 'backlight-prefer ../panel-bl' 'backlight-prefer panel-bl extra' \ + 'backlight-skip acpi_video0 # comment' >"$platform_root/displays.conf" +printf 'backlight-prefer last-bl' >>"$platform_root/displays.conf" +write_backlights acpi_video0 last-bl panel-bl +device=$(hw_display) +[[ $device == "last-bl" ]] || fail "malformed directives are ignored and the last line is read" "actual: $device" +write_displays_conf 'backlight-skip acpi_video0 # comment' +write_backlights acpi_video0 +device=$(hw_display) +[[ $device == "acpi_video0" ]] || fail "a directive with a trailing comment is ignored" "actual: $device" +pass "malformed directives are ignored and the last line is read" diff --git a/test/shell.d/hw-fingerprint-test.sh b/test/shell.d/hw-fingerprint-test.sh index 6ae30a25b8ad..3aa8c4a9852b 100755 --- a/test/shell.d/hw-fingerprint-test.sh +++ b/test/shell.d/hw-fingerprint-test.sh @@ -34,8 +34,14 @@ write_usb_devices() { done } +# A copy of the command reads its platform file from a fixture root: none +# unless a case writes one, whatever the machine running the suite has installed. +platform_root="$tmp_dir/platform" +mkdir -p "$platform_root" "$tmp_dir/bin" +platform_root_copy "$ROOT/bin/omarchy-hw-fingerprint" "$tmp_dir/bin/omarchy-hw-fingerprint" "$platform_root" + hw_fingerprint() { - OMARCHY_USB_DEVICES_PATH="$tmp_dir/devices" "$ROOT/bin/omarchy-hw-fingerprint" + OMARCHY_USB_DEVICES_PATH="$tmp_dir/devices" "$tmp_dir/bin/omarchy-hw-fingerprint" } assert_detects() { @@ -103,3 +109,48 @@ assert_detects "a self-named reader is detected with a driver bound" write_usb_devices '1234:5678:Generic USB Device' assert_rejects "a machine with no matching USB devices detects nothing" + +# A reader the platform names: a file and the value it reads once usable, as a +# Mac's Secure Enclave reports Touch ID. No USB device takes part. +write_platform_readers() { + printf '%s\n' "$@" >"$platform_root/fingerprint-readers" +} +write_sep() { + mkdir -p "$tmp_dir/sep/$1.sep/diag" + printf '%s\n' "$2" >"$tmp_dir/sep/$1.sep/diag/touchid" +} +write_usb_devices '1234:5678:Generic USB Device' +rm -rf "$tmp_dir/sep" +write_sep 396400000 ready +write_platform_readers '# Touch ID' '' "$tmp_dir/sep/*.sep/diag/touchid ready" +assert_detects "a reader the platform names is detected once its file reads the value" + +write_sep 396400000 absent +assert_rejects "a reader the platform names is not detected while its file reads another value" + +write_sep 196400000 ready +assert_detects "any file the platform's glob matches can show the reader" + +rm -rf "$tmp_dir/sep" +assert_rejects "a reader whose file is missing is not detected" + +write_sep 396400000 ready +printf '%s ready\r\n' "$tmp_dir/sep/*.sep/diag/touchid" >"$platform_root/fingerprint-readers" +assert_detects "a fingerprint-readers file saved with CRLF line ends reads the same" + +write_platform_readers "sep/*.sep/diag/touchid ready" "$tmp_dir/sep/*.sep/diag/touchid" \ + "$tmp_dir/sep/*.sep/diag/touchid ready now" "#$tmp_dir/sep/*.sep/diag/touchid ready" +assert_rejects "relative paths, a missing value, an extra word and comments name no reader" + +# A platform file that matches nothing never hides a USB reader on the same +# machine: the USB scan still runs after it. +write_usb_devices '1234:5678:Goodix Fingerprint USB Device' +write_sep 396400000 absent +write_platform_readers "$tmp_dir/sep/*.sep/diag/touchid ready" +assert_detects "a USB reader is detected while the platform's reader is not ready" +write_platform_readers "$tmp_dir/nowhere/*/diag/touchid ready" +assert_detects "a USB reader is detected while the platform's glob matches nothing" +write_usb_devices '27c6:1234' +assert_detects "a USB reader found by vendor is detected beside a platform file" + +rm -f "$platform_root/fingerprint-readers" diff --git a/test/shell.d/hw-laptop-test.sh b/test/shell.d/hw-laptop-test.sh new file mode 100755 index 000000000000..9b4781d99dbd --- /dev/null +++ b/test/shell.d/hw-laptop-test.sh @@ -0,0 +1,141 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +stub_bin="$test_tmp/bin" +acpi_lid="$test_tmp/acpi/button/lid" +input_class="$test_tmp/input" +dmi_chassis="$test_tmp/chassis_type" +mkdir -p "$stub_bin" "$acpi_lid/LID0" "$input_class" + +busctl_log="$test_tmp/busctl.log" +cat >"$stub_bin/busctl" <<'SH' +#!/bin/bash +printf '%s\n' "$*" >>"$BUSCTL_LOG" +[[ ${OMARCHY_TEST_LID_STATE:-} != "fail" ]] || exit 1 +printf '%s\n' "${OMARCHY_TEST_LID_STATE:-}" +SH +chmod +x "$stub_bin/busctl" + +run_laptop() { + OMARCHY_DMI_CHASSIS_TYPE_PATH="$dmi_chassis" \ + OMARCHY_ACPI_LID_PATH="$acpi_lid" \ + OMARCHY_INPUT_CLASS_PATH="$input_class" \ + PATH="$stub_bin:$PATH" \ + "$ROOT/bin/omarchy-hw-laptop" +} + +run_lid_closed() { + OMARCHY_ACPI_LID_PATH="$acpi_lid" \ + OMARCHY_INPUT_CLASS_PATH="$input_class" \ + BUSCTL_LOG="$busctl_log" \ + PATH="$stub_bin:$PATH" \ + OMARCHY_TEST_LID_STATE="$1" \ + "$ROOT/bin/omarchy-hw-laptop-closed" +} + +# Input devices as sysfs shows them: a name and a switch capability bitmap +# whose last word holds SW_LID in bit 0. +input_device() { + mkdir -p "$input_class/$1/capabilities" + printf '%s\n' "$2" >"$input_class/$1/name" + printf '%s\n' "$3" >"$input_class/$1/capabilities/sw" +} + +# No ACPI lid, no lid switch, desktop chassis: a Mac mini, a Studio or a tower. +: >"$acpi_lid/LID0/state" +rm -f "$acpi_lid/LID0/state" +printf '3\n' >"$dmi_chassis" +input_device input0 "Power Button" 0 +input_device input1 "Apple SMC power/lid events" 0 +input_device input2 "MacBook Pro J416 Headphone Jack" 14 +if run_laptop; then + fail "a desktop without a lid is not classified as a laptop" +fi +pass "a desktop without a lid is not classified as a laptop, even with the SMC's power device" + +# Apple Silicon MacBook: no ACPI lid, but the SMC reports a lid switch. +input_device input1 "Apple SMC power/lid events" 1 +run_laptop || fail "an Apple SMC lid switch is classified as a laptop" +pass "an Apple SMC lid switch is classified as a laptop" + +input_device input1 "Lid Switch" "0 1" +run_laptop || fail "a lid switch in the low word of a longer bitmap is classified as a laptop" +pass "a lid switch in the low word of a longer bitmap is classified as a laptop" + +input_device input1 "Lid Switch" "1 0" +if run_laptop; then + fail "a switch bit in a higher word is not a lid" +fi +pass "a switch bit in a higher word is not a lid" + +# x86 laptop via DMI when ACPI and evdev lids are absent. +input_device input1 "Power Button" 0 +printf '9\n' >"$dmi_chassis" +run_laptop || fail "DMI laptop chassis is recognized when ACPI is absent" +pass "DMI laptop chassis is recognized when ACPI is absent" + +printf '3\n' >"$dmi_chassis" +if run_laptop; then + fail "a desktop DMI chassis is not classified as a laptop" +fi +pass "a desktop DMI chassis is not classified as a laptop" + +printf 'closed\n' >"$acpi_lid/LID0/state" +run_laptop || fail "an ACPI lid is classified as a laptop" +pass "an ACPI lid is classified as a laptop" + +# An ACPI lid answers for itself, as it always has, without asking logind over +# D-Bus: this runs in sudo's PAM stack where fingerprint login is set up. +: >"$busctl_log" +run_lid_closed 'b false' || + fail "an ACPI lid reports itself closed" +pass "an ACPI lid reports itself closed" + +printf 'open\n' >"$acpi_lid/LID0/state" +if run_lid_closed 'b true'; then + fail "an ACPI lid reports itself open" +fi +pass "an ACPI lid reports itself open" +[[ ! -s $busctl_log ]] || fail "an ACPI lid leaves logind unasked" "$(cat "$busctl_log")" +pass "an ACPI lid leaves logind unasked" + +# Without an ACPI lid, logind answers only for a lid switch it can track: it +# says "open" of a machine it tracks no lid on. +rm -f "$acpi_lid/LID0/state" +input_device input1 "Power Button" 0 +if run_lid_closed 'b true'; then + fail "a machine without a lid is never closed" +fi +[[ ! -s $busctl_log ]] || fail "a machine without a lid leaves logind unasked" "$(cat "$busctl_log")" +pass "a machine without a lid is never closed and leaves logind unasked" + +input_device input1 "Apple SMC power/lid events" 1 +run_lid_closed 'b true' || + fail "logind recognizes a closed lid switch" +pass "logind recognizes a closed lid switch" +[[ $(head -1 "$busctl_log") == "--timeout=2 "*" LidClosed" ]] || + fail "logind gets a short timeout, not busctl's 25 s" "$(cat "$busctl_log")" +pass "logind gets a short timeout, not busctl's 25 s" + +if run_lid_closed 'b false'; then + fail "logind recognizes an open lid switch" +fi +pass "logind recognizes an open lid switch" + +input_device input1 "Lid Switch" "0 1" +run_lid_closed 'b true' || + fail "a lid switch in the low word of a longer bitmap is asked about" +pass "a lid switch in the low word of a longer bitmap is asked about" + +for answer in '' 'not-a-property' 'fail'; do + if run_lid_closed "$answer"; then + fail "a logind that does not answer leaves the lid open" "$answer" + fi +done +pass "a logind that does not answer leaves the lid open" diff --git a/test/shell.d/hw-platform-test.sh b/test/shell.d/hw-platform-test.sh new file mode 100644 index 000000000000..1f05dc825fff --- /dev/null +++ b/test/shell.d/hw-platform-test.sh @@ -0,0 +1,205 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +detector="$ROOT/bin/omarchy-hw-platform" +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +for platform in apple-silicon qualcomm generic-aarch64 generic; do + fake_platform "$test_tmp/$platform" "$platform" +done + +# A privileged caller reads the live device tree and the system uname, never +# the fixture roots or PATH its environment names. Run it as root when the suite +# is root and as namespaced root otherwise. +root_runner=() +if (( EUID != 0 )); then + root_runner=(unshare --user --map-root-user) +fi +if (( EUID == 0 )) || unshare --user --map-root-user true 2>/dev/null; then + live=$("${root_runner[@]}" "$detector") || fail "root detects the live platform" + [[ $live =~ ^(apple-silicon|qualcomm|generic-aarch64|generic)$ ]] || fail "root detects the live platform" "live: $live" + for platform in apple-silicon qualcomm generic-aarch64 generic; do + fixture="$test_tmp/$platform" + if [[ -f $fixture/proc/device-tree/compatible ]]; then + mkdir -p "$fixture/sys/firmware/devicetree/base" + cp "$fixture/proc/device-tree/compatible" "$fixture/sys/firmware/devicetree/base/compatible" + fi + overridden=$(OMARCHY_PROC_ROOT="$fixture/proc" OMARCHY_SYS_ROOT="$fixture/sys" PATH="$fixture/bin:$ROOT/bin:$PATH" \ + "${root_runner[@]}" "$detector") || fail "root detects the live platform with a $platform fixture in its environment" + [[ $overridden == "$live" ]] || + fail "root ignores a $platform fixture in its environment" "live: $live +with fixture: $overridden" + done + pass "root ignores fixture roots and PATH when detecting the platform" +else + skip "no unprivileged user namespace; skipping the root override probe" +fi + +# -p in the shebang is what keeps exported functions and BASH_ENV out, so an +# ordinary Bash launch with a decoy -p argument is refused before it reads anything. +for command in omarchy-hw-platform omarchy-hw-apple-silicon; do + if /usr/bin/bash "$ROOT/bin/$command" -p >/dev/null 2>"$test_tmp/error"; then + fail "$command refuses an ordinary Bash launch" + fi + grep -Fq "Refusing an unsafe Bash startup" "$test_tmp/error" || fail "$command explains the refusal" "$(cat "$test_tmp/error")" +done +pass "the detector and the Apple predicate refuse an ordinary Bash launch with a decoy -p" + +require_platform_fixtures "the platform fixtures" + +# The four platforms every caller is written against. +for platform in apple-silicon qualcomm generic-aarch64 generic; do + fixture="$test_tmp/$platform" + actual=$(OMARCHY_PROC_ROOT="$fixture/proc" PATH="$fixture/bin:$ROOT/bin:$PATH" "$detector") || + fail "the $platform fixture is detected" + [[ $actual == "$platform" ]] || fail "the $platform fixture is detected" "actual: $actual" + + apple_status=0 + OMARCHY_PROC_ROOT="$fixture/proc" PATH="$fixture/bin:$ROOT/bin:$PATH" "$ROOT/bin/omarchy-hw-apple-silicon" || apple_status=$? + if [[ $platform == "apple-silicon" ]]; then + (( apple_status == 0 )) || fail "the Apple predicate accepts the Apple fixture" + else + (( apple_status != 0 )) || fail "the Apple predicate rejects the $platform fixture" + fi + pass "the $platform fixture is detected and the Apple predicate agrees" +done + +# Systemd and the shell run the predicate by absolute path with whatever PATH +# they have; it must use the detector shipped beside it. +fixture="$test_tmp/apple-silicon" +OMARCHY_PROC_ROOT="$fixture/proc" PATH="$fixture/bin:/usr/bin:/bin" "$ROOT/bin/omarchy-hw-apple-silicon" || + fail "the Apple predicate finds its detector without Omarchy on PATH" +pass "the Apple predicate finds its detector without Omarchy on PATH" + +stub_bin="$test_tmp/bin" +mkdir -p "$stub_bin" +cat >"$stub_bin/uname" <<'SH' +#!/bin/bash +[[ ${1:-} == -m ]] && { printf '%s\n' "${TEST_ARCH:-aarch64}"; exit 0; } +exec /usr/bin/uname "$@" +SH +chmod +x "$stub_bin/uname" + +# $1 names a case directory holding proc/ and sys/ roots; $2 is the CPU. +detect() { + local case_dir="$test_tmp/cases/$1" + TEST_ARCH="${2:-aarch64}" OMARCHY_PROC_ROOT="$case_dir/proc" OMARCHY_SYS_ROOT="$case_dir/sys" \ + PATH="$stub_bin:$ROOT/bin:$PATH" "$detector" 2>"$test_tmp/error" +} + +# $1 case, $2 proc|sys, then the compatible tokens. +write_tree() { + local case_dir="$test_tmp/cases/$1" tree="$2" file + shift 2 + mkdir -p "$case_dir/proc" "$case_dir/sys" + if [[ $tree == "proc" ]]; then + file="$case_dir/proc/device-tree/compatible" + else + file="$case_dir/sys/firmware/devicetree/base/compatible" + fi + mkdir -p "$(dirname "$file")" + printf '%s\0' "$@" >"$file" +} + +expect() { + local name="$1" machine="$2" expected="$3" description="$4" actual + actual=$(detect "$name" "$machine") || fail "$description" "$(cat "$test_tmp/error")" + [[ $actual == "$expected" ]] || fail "$description" "expected: $expected +actual: $actual" +} + +expect_contradiction() { + local name="$1" machine="$2" description="$3" + if detect "$name" "$machine" >/dev/null; then + fail "$description" + fi + grep -Fq "contradictory platform identity" "$test_tmp/error" || fail "$description explains itself" "$(cat "$test_tmp/error")" +} + +# Real boards: the M1 Pro and M2 Max test Macs, an M1 Mac mini, and the +# Snapdragon X laptops Dragon supports. +write_tree m1-pro proc apple,j314s apple,t6000 apple,arm-platform +write_tree m2-max proc apple,j416c apple,t6021 apple,arm-platform +write_tree m1-mini proc apple,j274 apple,t8103 apple,arm-platform +write_tree yoga-slim7x proc lenovo,yoga-slim7x qcom,x1e80100 +write_tree xps13-9345 proc dell,xps13-9345 qcom,x1e80100 +write_tree t14s proc lenovo,thinkpad-t14s qcom,x1e78100 qcom,x1e80100 +for board in m1-pro m2-max m1-mini; do + expect "$board" aarch64 apple-silicon "the $board device tree is Apple Silicon" +done +for board in yoga-slim7x xps13-9345 t14s; do + expect "$board" aarch64 qualcomm "the $board device tree is Qualcomm" +done +pass "real Apple and Snapdragon device trees are recognised" + +write_tree qemu-virt proc linux,dummy-virt +write_tree raspberry-pi proc raspberrypi,5-model-b brcm,bcm2712 +mkdir -p "$test_tmp/cases/acpi/proc" "$test_tmp/cases/acpi/sys" +expect qemu-virt aarch64 generic-aarch64 "a QEMU virt board is generic aarch64" +expect raspberry-pi aarch64 generic-aarch64 "a Raspberry Pi is generic aarch64" +expect acpi aarch64 generic-aarch64 "an aarch64 machine without a device tree is generic aarch64" +expect acpi x86_64 generic "an x86 machine without a device tree is generic" +pass "unknown aarch64 and x86 machines are generic" + +# Only a token's vendor prefix identifies the board; the old detector matched +# "apple," anywhere in the file. +write_tree substring proc pineapple,board acmeqcom,soc vendor,apple,x vendor,qcom,y +expect substring aarch64 generic-aarch64 "vendor names inside other tokens do not identify the board" +pass "only a token's vendor prefix identifies the board" + +# /proc/device-tree is a link into sysfs; read sysfs when it is missing. +write_tree sysfs-qualcomm sys lenovo,yoga-slim7x qcom,x1e80100 +write_tree sysfs-apple sys apple,j314s apple,t6000 apple,arm-platform +expect sysfs-qualcomm aarch64 qualcomm "sysfs identifies Qualcomm without /proc/device-tree" +expect sysfs-apple aarch64 apple-silicon "sysfs identifies Apple Silicon without /proc/device-tree" +write_tree agree proc apple,j314s apple,t6000 apple,arm-platform +write_tree agree sys apple,j314s apple,t6000 apple,arm-platform +expect agree aarch64 apple-silicon "matching proc and sysfs trees agree" +pass "sysfs is the fallback for the device tree" + +write_tree both-vendors proc apple,j314s qcom,x1e80100 +expect_contradiction both-vendors aarch64 "a device tree naming Apple and Qualcomm fails" +write_tree sources-disagree proc apple,j314s apple,t6000 apple,arm-platform +write_tree sources-disagree sys lenovo,yoga-slim7x qcom,x1e80100 +expect_contradiction sources-disagree aarch64 "proc and sysfs naming different vendors fails" +write_tree vendor-vs-none proc apple,j314s apple,t6000 apple,arm-platform +write_tree vendor-vs-none sys linux,dummy-virt +expect_contradiction vendor-vs-none aarch64 "proc naming Apple while sysfs names nobody fails" +expect_contradiction m1-pro x86_64 "an Apple device tree on an x86 CPU fails" +expect_contradiction yoga-slim7x x86_64 "a Qualcomm device tree on an x86 CPU fails" +if TEST_ARCH=x86_64 OMARCHY_PROC_ROOT="$test_tmp/cases/m1-pro/proc" PATH="$stub_bin:$ROOT/bin:$PATH" \ + "$ROOT/bin/omarchy-hw-apple-silicon" 2>/dev/null; then + fail "the Apple predicate fails closed on contradictory identity" +fi +pass "contradictory identity fails with an explanation" + +failing_uname="$test_tmp/failing-uname" +mkdir -p "$failing_uname" +printf '#!/bin/bash\nexit 1\n' >"$failing_uname/uname" +chmod +x "$failing_uname/uname" +if OMARCHY_PROC_ROOT="$test_tmp/cases/acpi/proc" PATH="$failing_uname:$PATH" "$detector" 2>"$test_tmp/error"; then + fail "an unreadable CPU architecture fails instead of guessing generic" +fi +grep -Fq "cannot read the CPU architecture" "$test_tmp/error" || fail "an unreadable CPU architecture explains itself" "$(cat "$test_tmp/error")" +pass "an unreadable CPU architecture fails instead of guessing generic" + +# Dragon's omarchy-hw-qualcomm-soc: any "qcom," token in the boot device tree. +dragon_qualcomm() { + tr '\0' '\n' <"$1" | grep '^qcom,' >/dev/null +} +for case_dir in "$test_tmp"/cases/*; do + name=$(basename "$case_dir") + compatible="$case_dir/proc/device-tree/compatible" + [[ -f $compatible && ! -f $case_dir/sys/firmware/devicetree/base/compatible ]] || continue + platform=$(detect "$name" aarch64 2>/dev/null) || continue + if dragon_qualcomm "$compatible"; then + [[ $platform == "qualcomm" ]] || fail "Qualcomm detection matches Dragon on $name" "platform: $platform" + else + [[ $platform != "qualcomm" ]] || fail "Qualcomm detection matches Dragon on $name" + fi +done +pass "Qualcomm detection matches Dragon's omarchy-hw-qualcomm-soc on every aarch64 fixture" diff --git a/test/shell.d/hw-touchpad-test.sh b/test/shell.d/hw-touchpad-test.sh new file mode 100755 index 000000000000..27a86ed96dda --- /dev/null +++ b/test/shell.d/hw-touchpad-test.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +touchpad="$ROOT/bin/omarchy-hw-touchpad" + +require_command jq + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +stub_bin="$test_tmp/bin" +input_class="$test_tmp/input" +mkdir -p "$stub_bin" + +# Hyprland's mice, in its order, from a space-separated MICE. +cat >"$stub_bin/hyprctl" <<'SH' +#!/bin/bash + +jq -n --arg names "${MICE:-}" \ + '{mice: [$names | split(" ")[] | select(. != "") | {name: .}], keyboards: []}' +SH + +# udev's properties for an event device, kept beside it in the fixture. +cat >"$stub_bin/udevadm" <<'SH' +#!/bin/bash + +cat "${!#}/udev" 2>/dev/null +SH + +chmod +x "$stub_bin"/* + +# An event device as sysfs shows it: its input device's name, and what udev +# made of it. +event_device() { + mkdir -p "$input_class/$1/device" + printf '%s\n' "$2" >"$input_class/$1/device/name" + printf '%s\n' "$3" >"$input_class/$1/udev" +} + +detected() { + MICE="$1" OMARCHY_INPUT_CLASS_PATH="$input_class" PATH="$stub_bin:$PATH" bash "$touchpad" +} + +# Apple Silicon's trackpad is named after its MTP HID interface, so neither +# word is in its name; udev still calls it a touchpad. +event_device event1 "Apple MTP keyboard" "ID_INPUT_KEYBOARD=1" +event_device event2 "Apple MTP multi-touch" "ID_INPUT_TOUCHPAD=1" +[[ $(detected "apple-mtp-multi-touch") == "apple-mtp-multi-touch" ]] || + fail "the Apple Silicon trackpad is detected" "$(detected "apple-mtp-multi-touch")" +pass "the Apple Silicon trackpad is detected" + +# omarchy-toggle-input-device passes the name straight to hl.device, so a name +# that does not match leaves the menu entry hidden and the toggle dead. +for name in "elan-touchpad" "apple-magic-trackpad-2"; do + [[ $(detected "$name") == "$name" ]] || fail "the touchpads that already worked still do" "$(detected "$name")" +done +pass "the touchpads that already worked still do" + +# Some touchscreens' mouse interfaces are called multi-touch as well. A named +# touchpad wins over one listed first, and without one it takes udev calling +# the device a touchpad, not its name. +rm -rf "$input_class" +event_device event5 "ELAN9008:00 04F3:2C82 Multi-Touch" "ID_INPUT_TOUCHSCREEN=1" +event_device event7 "SYNA2BA6:00 06CB:CEF5 Touchpad" "ID_INPUT_TOUCHPAD=1" +[[ $(detected "elan9008:00-04f3:2c82-multi-touch syna2ba6:00-06cb:cef5-touchpad") == "syna2ba6:00-06cb:cef5-touchpad" ]] || + fail "a touchscreen listed first does not take the touchpad's place" "$(detected "elan9008:00-04f3:2c82-multi-touch syna2ba6:00-06cb:cef5-touchpad")" +[[ -z $(detected "elan9008:00-04f3:2c82-multi-touch") ]] || + fail "a multi-touch touchscreen alone is no touchpad" "$(detected "elan9008:00-04f3:2c82-multi-touch")" +pass "a multi-touch touchscreen is never taken for the touchpad" + +# Hyprland turns commas into dashes as well as spaces. +event_device event9 "Acme, Inc. Precision Pad" "ID_INPUT_TOUCHPAD=1" +[[ $(detected "acme--inc.-precision-pad") == "acme--inc.-precision-pad" ]] || + fail "a touchpad named with a comma is found by Hyprland's name for it" "$(detected "acme--inc.-precision-pad")" +pass "a touchpad named with a comma is found by Hyprland's name for it" + +# Hyprland adds -1, -2 and so on to a name another device already took, as when +# the MTP interface exposes the trackpad twice; that name is the one to toggle, +# and an unrelated name that merely starts the same is not. +event_device event11 "Apple MTP multi-touch" "ID_INPUT_TOUCHPAD=1" +[[ $(detected "apple-mtp-multi-touch-1") == "apple-mtp-multi-touch-1" ]] || + fail "a touchpad under Hyprland's numbered name is found" "$(detected "apple-mtp-multi-touch-1")" +[[ -z $(detected "apple-mtp-multi-touch-extra") ]] || + fail "a name that only starts with the touchpad's is not taken for it" "$(detected "apple-mtp-multi-touch-extra")" +pass "a touchpad under Hyprland's numbered name for a duplicate is found" + +# udev's touchpad has to be one of Hyprland's mice for its name to toggle. +[[ -z $(detected "logitech-mx-master-3") ]] || + fail "a udev touchpad Hyprland does not list is not reported" "$(detected "logitech-mx-master-3")" +pass "an ordinary mouse is not mistaken for a touchpad" + +[[ -z $(detected "") ]] || + fail "no touchpad answers with an empty detection" "$(detected "")" +pass "no touchpad answers with an empty detection" diff --git a/test/shell.d/hyprland-monitor-removal-test.sh b/test/shell.d/hyprland-monitor-removal-test.sh new file mode 100755 index 000000000000..2478d89f3f1b --- /dev/null +++ b/test/shell.d/hyprland-monitor-removal-test.sh @@ -0,0 +1,188 @@ +#!/bin/bash + +source "$(dirname "${BASH_SOURCE[0]}")/base-test.sh" + +require_command lua + +grep -Fx 'require("default.hypr.monitor-removal")' "$ROOT/default/hypr/omarchy.lua" >/dev/null || + fail "the Omarchy Hyprland config loads the monitor removal handler" +pass "the Omarchy Hyprland config loads the monitor removal handler" + +# Replays the event order of Hyprland 0.56.2's CMonitor::onDisconnect: focus is +# warped to the first remaining monitor, and monitor.focused fires while the +# monitor being left is still the active one (FocusState.cpp rawMonitorFocus). +# The removed monitor's workspaces then move over hidden and monitor.removed +# fires, all in one event-loop turn. hl.timer callbacks only run once that turn +# is over. +OMARCHY_PATH="$ROOT" lua - <<'LUA' || fail "a monitor removal keeps the focused workspace in front" +local handlers, timers, dispatched = {}, {}, {} +local monitors, focused, workspaces, after_dispatch + +local function reset() + monitors = { + ["eDP-1"] = { name = "eDP-1", active = { id = 1, name = "1" } }, + ["USB-2"] = { name = "USB-2", active = { id = 5, name = "5" } }, + } + focused = "eDP-1" + workspaces = { ["1"] = true, ["5"] = true, ["name:notes"] = true } + dispatched = {} + after_dispatch = nil +end + +-- Like rawMonitorFocus, which returns early when the monitor is unchanged. +local function focus_moves(to, assign_first) + if focused == to then return end + if assign_first then focused = to end + handlers["monitor.focused"]({ name = to }) + focused = to +end + +local function switch_workspace(id) + monitors[focused].active = { id = id, name = tostring(id) } + workspaces[tostring(id)] = true + handlers["workspace.active"](monitors[focused].active) +end + +hl = { + on = function(event, callback) + handlers[event] = callback + end, + timer = function(callback, opts) + assert(opts.type == "oneshot" and opts.timeout > 0) + table.insert(timers, callback) + end, + get_active_monitor = function() + return monitors[focused] + end, + get_active_workspace = function(monitor) + return (monitor or monitors[focused]).active + end, + get_active_special_workspace = function(monitor) + return (monitor or monitors[focused]).special + end, + get_workspace = function(selector) + return workspaces[selector] and {} or nil + end, + dispatch = function(action) + table.insert(dispatched, action.workspace) + -- Focusing a workspace on another monitor moves monitor focus too. + focus_moves("eDP-1") + if after_dispatch then after_dispatch() end + end, + dsp = { + focus = function(args) + return args + end, + }, +} + +local function turn_ends() + while #timers > 0 do + local pending = timers + timers = {} + for _, callback in ipairs(pending) do + callback() + end + end +end + +-- Unplug USB-2 the way Hyprland does: the warp goes to eDP-1, first in its +-- monitor list, wherever focus was. +local function unplug(opts) + opts = opts or {} + local was_on_it = focused == "USB-2" + focus_moves("eDP-1", opts.assign_first) + if was_on_it then + -- Moving the active workspace off leaves the dying monitor a placeholder. + handlers["workspace.active"]({ id = 2, name = "2" }) + end + monitors["USB-2"] = nil + handlers["monitor.removed"]({ name = "USB-2" }) + if opts.then_focus then + focus_moves(opts.then_focus) + end + if opts.then_switch then + switch_workspace(opts.then_switch) + end + turn_ends() +end + +dofile(os.getenv("OMARCHY_PATH") .. "/default/hypr/bootstrap.lua") +require("default.hypr.monitor-removal") + +reset() +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 1 and dispatched[1] == "5", "the workspace you were on comes to the front of the built-in panel") + +reset() +unplug() +assert(#dispatched == 0, "unplugging a display you were not on changes nothing") + +reset() +focus_moves("USB-2"); turn_ends() +focus_moves("eDP-1"); turn_ends() +unplug() +assert(#dispatched == 0, "an earlier focus change does not switch the workspace later") + +reset() +monitors["HDMI-A-1"] = { name = "HDMI-A-1", active = { id = 3, name = "3" } } +focus_moves("USB-2"); turn_ends() +unplug({ then_focus = "HDMI-A-1" }) +assert(#dispatched == 0, "a focus change after the removal wins over the restore") + +reset() +focus_moves("USB-2"); turn_ends() +unplug({ then_switch = 9 }) +assert(#dispatched == 0, "a workspace switch after the removal wins over the restore") + +reset() +monitors["HDMI-A-1"] = { name = "HDMI-A-1", active = { id = 7, name = "7" } } +workspaces["7"] = true +focus_moves("HDMI-A-1"); turn_ends() +unplug() +assert(#dispatched == 1 and dispatched[1] == "7", "unplugging another display leaves focus on the third one") + +reset() +workspaces["5"] = nil +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 0, "an empty workspace, gone once hidden, is not recreated") + +reset() +monitors["USB-2"].special = { id = -98, name = "special:scratchpad" } +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 0, "an open scratchpad is left in front") + +reset() +monitors["USB-2"].active = { id = -1337, name = "notes" } +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 1 and dispatched[1] == "name:notes", "a named workspace is focused by name") + +-- The restore's own focus change does not arm another one: here it leaves a +-- third monitor, which is then removed in the same turn. +reset() +monitors["HDMI-A-1"] = { name = "HDMI-A-1", active = { id = 3, name = "3" } } +workspaces["3"] = true +focus_moves("USB-2"); turn_ends() +focus_moves("eDP-1") +monitors["USB-2"] = nil +handlers["monitor.removed"]({ name = "USB-2" }) +focused = "HDMI-A-1" +after_dispatch = function() + monitors["HDMI-A-1"] = nil + handlers["monitor.removed"]({ name = "HDMI-A-1" }) +end +turn_ends() +assert(#dispatched == 1 and dispatched[1] == "5", "restoring focus does not queue a second restore") + +-- A Hyprland that records focus before announcing it leaves nothing to restore +-- from, so the handler stays out of the way rather than guessing. +reset() +focus_moves("USB-2"); turn_ends() +unplug({ assign_first = true }) +assert(#dispatched == 0, "a changed event order degrades to Hyprland's own behaviour") +LUA +pass "a monitor removal keeps the focused workspace in front" diff --git a/test/shell.d/image-deferred-hardware-test.sh b/test/shell.d/image-deferred-hardware-test.sh new file mode 100644 index 000000000000..964e0384db63 --- /dev/null +++ b/test/shell.d/image-deferred-hardware-test.sh @@ -0,0 +1,648 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +# Root never reads the fixture root, so as root the build and first boot below +# would act on this machine. A skip is a pass. +if (( EUID == 0 )); then + skip "running as root, where image setup ignores fixture roots; skipping" + exit 0 +fi + +umask 022 +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +unit_name=omarchy-provision-hardware.service +unit_source="$ROOT/install/provisioning/$unit_name" + +# omarchy-apply-hardware refuses anyone but root, and root never reads the +# fixture root. Run a copy whose only change is that check, so a normal user +# can build an image into a fixture root. +apply_hardware="$test_tmp/omarchy-apply-hardware" +sed 's/^if (( EUID != 0 )); then$/if false; then/' "$ROOT/bin/omarchy-apply-hardware" >"$apply_hardware" +chmod +x "$apply_hardware" +grep -q '^if false; then$' "$apply_hardware" || fail "the test copy of omarchy-apply-hardware drops only its root check" + +# Commands a hardware leaf would reach for. Any call means a leaf ran. +stub_bin="$test_tmp/stub-bin" +mkdir -p "$stub_bin" +for command in sudo pacman pacman-key systemctl lspci omarchy-pkg-add modinfo; do + cat >"$stub_bin/$command" <<'SH' +#!/bin/bash +printf '%s %s\n' "${0##*/}" "$*" >>"$LEAF_CALLS" +exit 1 +SH +done +stub_rebuild() { + cat >"$stub_bin/$1" <<'SH' +#!/bin/bash +printf '%s %s\n' "${0##*/}" "$*" >>"$REBUILDS" +SH + chmod +x "$stub_bin/$1" +} +stub_rebuild mkinitcpio +# Only the stubs count as present, so a machine with Limine never runs its own +# limine-mkinitcpio here. +cat >"$stub_bin/omarchy-cmd-present" <"$fixture/install/hardware/all.sh" <<'SH' +run_logged "$OMARCHY_INSTALL/hardware/a.sh" +run_logged "$OMARCHY_INSTALL/hardware/apple/b.sh" +run_logged "$OMARCHY_INSTALL/hardware/c.sh" +SH +cat >"$fixture/install/hardware/a.sh" <<'SH' +printf 'a user=%s path=%s\n' "${OMARCHY_INSTALL_USER-unset}" "$OMARCHY_PATH" >>"$RUNS" +mkdir -p "$OMARCHY_IMAGE_ROOT/etc/mkinitcpio.conf.d" +echo 'MODULES+=(a)' >"$OMARCHY_IMAGE_ROOT/etc/mkinitcpio.conf.d/a.conf" +SH +cat >"$fixture/install/hardware/apple/b.sh" <<'SH' +mkdir -p "$OMARCHY_IMAGE_ROOT/etc/mkinitcpio.conf.d" +echo 'MODULES+=(b)' >"$OMARCHY_IMAGE_ROOT/etc/mkinitcpio.conf.d/b.conf" +[[ ! -e $FAIL_B ]] || return 1 +echo b >>"$RUNS" +SH +cat >"$fixture/install/hardware/c.sh" <<'SH' +echo c >>"$RUNS" +[[ ! -e $REQUEST_REBUILD ]] || touch "$OMARCHY_IMAGE_BOOT_REBUILD" +SH +export RUNS="$test_tmp/runs" FAIL_B="$test_tmp/fail-b" REQUEST_REBUILD="$test_tmp/request-rebuild" + +new_root() { + local root="$test_tmp/root-$1" + rm -rf "$root" + mkdir -p "$root/var/log" + printf '%s\n' "$root" +} + +write_manifest() { + local root=$1 body=${2:-$'format=1\nplatform=apple-silicon\n'} + mkdir -p "$root/var/lib/omarchy/image" + chmod 0755 "$root/var/lib/omarchy/image" + printf '%s' "$body" >"$root/var/lib/omarchy/image/target" + chmod 0644 "$root/var/lib/omarchy/image/target" +} + +build() { + local root=$1 omarchy=${2:-$fixture} + OMARCHY_IMAGE_ROOT="$root" OMARCHY_PATH="$omarchy" OMARCHY_INSTALL_LOG_FILE="$root/var/log/omarchy-install.log" \ + PATH="$base_path" "$apply_hardware" --defer-provisioning +} + +first_boot() { + local root=$1 + OMARCHY_IMAGE_ROOT="$root" OMARCHY_PATH="$fixture" OMARCHY_PROC_ROOT="$test_tmp/hw/proc" \ + PATH="$base_path" "$ROOT/bin/omarchy-provision-hardware" +} + +reset_logs() { + rm -f "$RUNS" "$LEAF_CALLS" "$REBUILDS" "$FAIL_B" +} + +queue_of() { + cat "$1/var/lib/omarchy/image/deferred-steps" +} + +# --- The build --------------------------------------------------------------- + +# The real hardware setup: every leaf is queued in order and none of them runs. +reset_logs +root=$(new_root real) +write_manifest "$root" +output=$(build "$root" "$ROOT") || fail "an image build defers the real hardware setup" "$output" +expected=$(sed -n 's|^run_logged "\$OMARCHY_INSTALL/\(hardware/[^"]*\)"$|install/\1|p' "$ROOT/install/hardware/all.sh") +[[ -n $expected && $(queue_of "$root") == "$expected" ]] || + fail "an image build queues every hardware leaf in order" "expected: +$expected +queued: +$(queue_of "$root")" +[[ ! -e $LEAF_CALLS ]] || fail "an image build runs no hardware leaf" "$(cat "$LEAF_CALLS")" +[[ $output == *"Image build for apple-silicon: deferred $(wc -l <<<"$expected") hardware steps to first boot"* ]] || + fail "an image build reports what it deferred" "$output" +pass "an image build queues every hardware leaf in order and runs none of them" + +[[ $(<"$unit_source") == "$(<"$root/etc/systemd/system/$unit_name")" ]] || + fail "an image build installs the first-boot hardware service" +[[ $(readlink "$root/etc/systemd/system/multi-user.target.wants/$unit_name") == "/etc/systemd/system/$unit_name" ]] || + fail "an image build enables the first-boot hardware service" +[[ -f $root/var/lib/omarchy/image/target && ! -e $root/var/lib/omarchy/image/target.booted ]] || + fail "an image build leaves its manifest for the first boot" +[[ $(stat -c '%a' "$root/var/lib/omarchy/image/deferred-steps") == "644" ]] || + fail "the queue is readable and only its owner writes it" +pass "an image build arms the first-boot hardware service" + +before=$(queue_of "$root") +build "$root" "$ROOT" >/dev/null || fail "an image build can run hardware setup again" +[[ $(queue_of "$root") == "$before" ]] || fail "running hardware setup again in the build queues the same steps" +pass "running hardware setup again in the build queues the same steps" + +# Without a manifest nothing is deferred: the leaves run as they always did. +reset_logs +root=$(new_root live) +build "$root" >/dev/null || fail "hardware setup without a manifest runs" +[[ $(cat "$RUNS") == $'a user= path='"$fixture"$'\nb\nc' ]] || fail "hardware setup without a manifest runs every leaf" "$(cat "$RUNS")" +[[ ! -e $root/var/lib/omarchy/image && ! -e $root/etc/systemd/system/$unit_name ]] || + fail "hardware setup without a manifest queues and arms nothing" +pass "hardware setup without a manifest runs every leaf and defers nothing" + +# Hardware setup that has to wait for a first boot in progress decides only +# once it has the lock: by then the manifest is retired and the leaves run here. +reset_logs +root=$(new_root race) +write_manifest "$root" +( + exec {lock}>>"$root/var/lib/omarchy/image/lock" + flock "$lock" + touch "$test_tmp/locked" + until [[ -e $test_tmp/release || ! -d $test_tmp ]]; do sleep 0.1; done + mv "$root/var/lib/omarchy/image/target" "$root/var/lib/omarchy/image/target.booted" +) & +first_boot_pid=$! +until [[ -e $test_tmp/locked ]]; do sleep 0.1; done +build "$root" >/dev/null & +setup_pid=$! +sleep 1 +kill -0 "$setup_pid" 2>/dev/null || fail "hardware setup waits while a first boot holds the lock" +touch "$test_tmp/release" +wait "$first_boot_pid" +wait "$setup_pid" || fail "hardware setup runs after waiting for a first boot" +[[ $(cat "$RUNS") == $'a user= path='"$fixture"$'\nb\nc' && ! -e $root/var/lib/omarchy/image/deferred-steps ]] || + fail "hardware setup that waited for a first boot runs its leaves instead of queueing them" +pass "hardware setup waits for a first boot in progress and then runs on the machine" + +# A manifest that is not unambiguously root's own stops hardware setup outright. +check_refused() { + local description=$1 root=$2 + reset_logs + if build "$root" >/dev/null 2>&1; then + fail "hardware setup refuses $description" + fi + [[ ! -e $RUNS && ! -e $root/var/lib/omarchy/image/deferred-steps && ! -e $root/etc/systemd/system/$unit_name ]] || + fail "hardware setup refuses $description without running, queueing or arming anything" + pass "hardware setup refuses $description" +} + +root=$(new_root format) +write_manifest "$root" $'format=2\nplatform=apple-silicon\n' +check_refused "a manifest of an unknown format" "$root" + +root=$(new_root platform) +write_manifest "$root" $'format=1\nplatform=apple\n' +check_refused "a manifest naming an unknown platform" "$root" + +root=$(new_root malformed) +write_manifest "$root" $'format=1\napple-silicon\n' +check_refused "a malformed manifest" "$root" + +root=$(new_root writable) +write_manifest "$root" +chmod 0666 "$root/var/lib/omarchy/image/target" +check_refused "a manifest others can write" "$root" + +root=$(new_root writable-dir) +write_manifest "$root" +chmod 0777 "$root/var/lib/omarchy/image" +check_refused "a manifest in a directory others can write" "$root" + +root=$(new_root symlink) +write_manifest "$root" +mv "$root/var/lib/omarchy/image/target" "$root/var/lib/omarchy/image/real-target" +ln -s real-target "$root/var/lib/omarchy/image/target" +check_refused "a symlinked manifest" "$root" + +# --- The first boot ---------------------------------------------------------- + +reset_logs +root=$(new_root fresh) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +[[ $(queue_of "$root") == $'install/hardware/a.sh\ninstall/hardware/apple/b.sh\ninstall/hardware/c.sh' ]] || + fail "the fixture image queues its three leaves" "$(queue_of "$root")" + +output=$(first_boot "$root") || fail "the first boot finishes the deferred hardware setup" "$output" +[[ $(cat "$RUNS") == $'a user= path='"$fixture"$'\nb\nc' ]] || + fail "the first boot runs each deferred leaf once, in order, with no install user" "$(cat "$RUNS")" +[[ $output == *"First boot of an image built for apple-silicon, on apple-silicon hardware"* ]] || + fail "the first boot reports the image target and the live platform" "$output" +[[ ! -e $root/var/lib/omarchy/image/target && -f $root/var/lib/omarchy/image/target.booted ]] || + fail "the first boot retires the build manifest" +[[ ! -e $root/var/lib/omarchy/image/deferred-steps ]] || fail "the first boot empties the queue" +[[ ! -L $root/etc/systemd/system/multi-user.target.wants/$unit_name && -f $root/etc/systemd/system/$unit_name ]] || + fail "the first boot disables its service and keeps the unit, so a start job already queued is skipped rather than failed" +[[ $(cat "$REBUILDS") == "mkinitcpio -P" ]] || + fail "the first boot rebuilds the initramfs once after a leaf changed it" "$(cat "$REBUILDS" 2>/dev/null)" +[[ ! -e $root/var/lib/omarchy/image/initramfs-inputs ]] || fail "the first boot clears the rebuild it owed" +grep -q 'Completed: .*/install/hardware/c.sh' "$root/var/log/omarchy-install.log" || + fail "the first boot logs each step to the install log" +pass "the first boot runs the deferred hardware setup with no command from the owner" + +rm -f "$RUNS" "$REBUILDS" +output=$(first_boot "$root") || fail "running the first-boot hardware setup again succeeds" +[[ -z $output && ! -e $RUNS && ! -e $REBUILDS ]] || + fail "running the first-boot hardware setup again does nothing" "$output" +pass "running the first-boot hardware setup again does nothing" + +# The live platform is asked once the manifest is retired, so it is the +# hardware's even where the detector cannot tell a booted root (this fixture +# runs no systemd): an Apple Silicon image booted in a VM reports generic aarch64. +fake_platform "$test_tmp/vm" generic-aarch64 +reset_logs +root=$(new_root vm) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds for a VM boot" +output=$(OMARCHY_IMAGE_ROOT="$root" OMARCHY_PATH="$fixture" OMARCHY_PROC_ROOT="$test_tmp/vm/proc" \ + PATH="$test_tmp/vm/bin:$stub_bin:$ROOT/bin:/usr/local/bin:/usr/bin:/bin" "$ROOT/bin/omarchy-provision-hardware") || + fail "the first boot on other hardware finishes" "$output" +[[ $output == *"First boot of an image built for apple-silicon, on generic-aarch64 hardware"* ]] || + fail "the first boot reports the hardware it runs on, not the image target" "$output" +pass "the first boot reports the hardware it runs on, not the image target" + +# A failed step stays queued with everything after it; the next boot resumes there. +reset_logs +root=$(new_root resume) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +# A repository without a sync database, which the image never has: the failed +# step is not retried after a fetch. +mkdir -p "$root/etc" +printf '[options]\nArchitecture = auto\n\n[core]\nServer = https://mirror.invalid/$arch/$repo\n' >"$root/etc/pacman.conf" +touch "$FAIL_B" +status=0 +output=$(first_boot "$root" 2>&1) || status=$? +(( status == 75 )) || fail "a failed step exits 75, so a platform first boot can tell it from a refusal" "status $status: $output" +[[ $output == *"Deferred hardware step failed: install/hardware/apple/b.sh"* ]] || + fail "the first boot names the failed step" "$output" +[[ $(cat "$RUNS") == "a user= path=$fixture" ]] || fail "the first boot stops at the failed step" "$(cat "$RUNS")" +[[ $(queue_of "$root") == $'install/hardware/apple/b.sh\ninstall/hardware/c.sh' ]] || + fail "the failed step and those after it stay queued" "$(queue_of "$root")" +[[ -f $root/var/lib/omarchy/image/target.booted && ! -e $root/var/lib/omarchy/image/target ]] || + fail "the machine stops being an image build once its first boot starts" +[[ -L $root/etc/systemd/system/multi-user.target.wants/$unit_name && -e $root/var/lib/omarchy/image/initramfs-inputs && ! -e $REBUILDS ]] || + fail "the service stays armed and the initramfs rebuild stays owed until the queue is empty" +[[ ! -e $LEAF_CALLS ]] || fail "a failed step fetches no package databases" "$(cat "$LEAF_CALLS")" +pass "a failed step stays queued with the steps after it" + +rm -f "$FAIL_B" "$RUNS" +stub_rebuild limine-mkinitcpio +output=$(first_boot "$root") || fail "the next boot finishes the deferred hardware setup" "$output" +[[ $(cat "$RUNS") == $'b\nc' ]] || fail "the next boot resumes at the failed step" "$(cat "$RUNS")" +[[ $(cat "$REBUILDS") == "limine-mkinitcpio " ]] || + fail "the next boot rebuilds the initramfs the earlier boot owed, through Limine when present" "$(cat "$REBUILDS" 2>/dev/null)" +[[ ! -e $root/var/lib/omarchy/image/deferred-steps && ! -L $root/etc/systemd/system/multi-user.target.wants/$unit_name ]] || + fail "the next boot disarms the service" +pass "the next boot resumes at the failed step and finishes" + +# The step that changed the initramfs inputs is the one that failed: its rerun +# changes nothing more, and the rebuild is still owed. +reset_logs +rm -f "$stub_bin/limine-mkinitcpio" +root=$(new_root rebuild) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +printf '%s\n' install/hardware/apple/b.sh install/hardware/c.sh >"$root/var/lib/omarchy/image/deferred-steps" +touch "$FAIL_B" +first_boot "$root" >/dev/null 2>&1 && fail "the first boot reports the failed step" +rm -f "$FAIL_B" +first_boot "$root" >/dev/null || fail "the next boot finishes the deferred hardware setup" +[[ $(cat "$RUNS") == $'b\nc' && $(cat "$REBUILDS" 2>/dev/null) == "mkinitcpio -P" ]] || + fail "a step that changed the initramfs and then failed still gets it rebuilt" "$(cat "$REBUILDS" 2>/dev/null)" +pass "a step that changed the initramfs and then failed still gets it rebuilt" + +# A step that only adds to the kernel command line Limine's entry tool builds +# (as intel/fred.sh does) gets the rebuild too. +reset_logs +root=$(new_root cmdline) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +cat >"$fixture/install/hardware/cmdline.sh" <<'SH' +mkdir -p "$OMARCHY_IMAGE_ROOT/etc/limine-entry-tool.d" +echo 'KERNEL_CMDLINE[default]+=" example=on"' >"$OMARCHY_IMAGE_ROOT/etc/limine-entry-tool.d/example.conf" +SH +printf '%s\n' install/hardware/cmdline.sh >"$root/var/lib/omarchy/image/deferred-steps" +first_boot "$root" >/dev/null || fail "a step that changes the kernel command line finishes" +[[ $(cat "$REBUILDS" 2>/dev/null) == "mkinitcpio -P" ]] || + fail "a step that only changed Limine's kernel command line gets the rebuild" "$(cat "$REBUILDS" 2>/dev/null)" +rm -f "$fixture/install/hardware/cmdline.sh" +pass "a step that only changed Limine's kernel command line gets the rebuild" + +# --- The pacman keyring ------------------------------------------------------- + +# Install finalization makes the pacman keyring on Qualcomm and generic aarch64. +# An image build asks for it instead, so every machine flashed from the image +# makes its own master key on its first boot, before any step installs a package. +keyring_bin="$test_tmp/keyring-bin" +mkdir -p "$keyring_bin" "$fixture/install/post-install" +# Finalization's repositories are pacman-templates-test.sh's; here they land in +# the root under test, never the host's /etc. +sed 's|/etc/pacman|$OMARCHY_IMAGE_ROOT/etc/pacman|g' "$ROOT/install/post-install/pacman.sh" >"$fixture/install/post-install/pacman.sh" +cp "$ROOT/install/helpers/pacman.sh" "$fixture/install/helpers/pacman.sh" +mkdir -p "$fixture/default" +cp -r "$ROOT/default/pacman" "$fixture/default/" +: >"$fixture/install/hardware/pacman.sh" +for command in pacman-key omarchy-pkg-add; do + cat >"$keyring_bin/$command" <<'SH' +#!/bin/bash +printf '%s %s\n' "${0##*/}" "$*" >>"$KEYRING" +[[ ${0##*/} != "pacman-key" || ! -e $KEYRING_FAIL ]] +SH +done +printf '#!/bin/bash\necho "$PLATFORM"\n' >"$keyring_bin/omarchy-hw-platform" +chmod +x "$keyring_bin"/* +export KEYRING="$test_tmp/keyring" KEYRING_FAIL="$test_tmp/keyring-fail" +request=var/lib/omarchy/image/pacman-keyring + +finalize() { + mkdir -p "$1/etc/pacman.d" + OMARCHY_IMAGE_ROOT="$1" OMARCHY_PATH="$fixture" OMARCHY_INSTALL="$fixture/install" PLATFORM=$2 \ + PATH="$keyring_bin:$base_path" bash -e -c 'source "$1"' bash "$fixture/install/post-install/pacman.sh" +} + +keyring_boot() { + OMARCHY_IMAGE_ROOT="$1" OMARCHY_PATH="$fixture" OMARCHY_PROC_ROOT="$test_tmp/hw/proc" \ + PATH="$keyring_bin:$base_path" "$ROOT/bin/omarchy-provision-hardware" +} + +for platform in qualcomm generic-aarch64; do + rm -f "$KEYRING" + root=$(new_root "install-$platform") + finalize "$root" "$platform" || fail "$platform: install finalization succeeds" + [[ $(cat "$KEYRING") == $'omarchy-pkg-add archlinuxarm-keyring\npacman-key --init\npacman-key --populate' ]] || + fail "$platform: an install makes its pacman keyring in finalization, as before" "$(cat "$KEYRING")" + [[ ! -e $root/$request ]] || fail "$platform: an install requests no first-boot keyring" + + rm -f "$KEYRING" + root=$(new_root "image-$platform") + write_manifest "$root" $'format=1\nplatform='"$platform"$'\n' + build "$root" >/dev/null || fail "$platform: the fixture image builds" + finalize "$root" "$platform" || fail "$platform: image finalization succeeds" + [[ $(cat "$KEYRING") == "omarchy-pkg-add archlinuxarm-keyring" ]] || + fail "$platform: an image build makes no pacman keyring" "$(cat "$KEYRING")" + [[ -f $root/$request ]] || fail "$platform: an image build asks its first boot for the keyring" +done +for platform in apple-silicon generic; do + rm -f "$KEYRING" + root=$(new_root "image-$platform") + write_manifest "$root" $'format=1\nplatform='"$platform"$'\n' + build "$root" >/dev/null || fail "$platform: the fixture image builds" + finalize "$root" "$platform" || fail "$platform: image finalization succeeds" + finalize "$(new_root "install-$platform")" "$platform" || fail "$platform: install finalization succeeds" + [[ ! -e $KEYRING && ! -e $root/$request ]] || fail "$platform: finalization leaves the keyring alone" "$(cat "$KEYRING" 2>/dev/null)" +done +pass "an image build on Qualcomm and generic aarch64 leaves the pacman keyring to the first boot" + +reset_logs +rm -f "$KEYRING" +root=$(new_root image-apple-silicon) +write_manifest "$root" +build "$root" >/dev/null || fail "the Mac fixture image builds" +keyring_boot "$root" >/dev/null || fail "a Mac image's first boot finishes" +[[ ! -e $KEYRING ]] || fail "a first boot nobody asked for a keyring leaves the keyring alone" "$(cat "$KEYRING")" +pass "a first boot nobody asked for a keyring leaves the keyring alone" + +reset_logs +root=$test_tmp/root-image-qualcomm +mkdir -p "$root/etc/pacman.d/gnupg" +touch "$root/etc/pacman.d/gnupg/build-master-key" "$KEYRING_FAIL" +status=0 +output=$(KEYRING=$RUNS keyring_boot "$root" 2>&1) || status=$? +(( status == 75 )) || fail "a keyring that can't be made exits 75" "status $status: $output" +[[ $output == *"could not create the pacman keyring"* ]] || fail "the first boot says the keyring failed" "$output" +[[ $(cat "$RUNS") == "pacman-key --gpgdir $root/etc/pacman.d/gnupg --init" ]] || + fail "no step runs before the keyring exists" "$(cat "$RUNS")" +[[ -f $root/$request && $(queue_of "$root") == $'install/hardware/a.sh\ninstall/hardware/apple/b.sh\ninstall/hardware/c.sh' ]] || + fail "the keyring request and the whole queue stay for the next boot" +pass "a keyring that can't be made stays requested and holds back every step" + +rm -f "$KEYRING_FAIL" "$RUNS" +mkdir -p "$root/etc/pacman.d/gnupg" +touch "$root/etc/pacman.d/gnupg/build-master-key" +output=$(KEYRING=$RUNS keyring_boot "$root") || fail "the next boot makes the keyring and finishes" "$output" +[[ $(cat "$RUNS") == "pacman-key --gpgdir $root/etc/pacman.d/gnupg --init"$'\n'"pacman-key --gpgdir $root/etc/pacman.d/gnupg --populate"$'\na user= path='"$fixture"$'\nb\nc' ]] || + fail "the first boot makes the keyring, then runs the steps" "$(cat "$RUNS")" +[[ ! -e $root/etc/pacman.d/gnupg/build-master-key ]] || fail "the first boot replaces a keyring the build left" +[[ ! -e $root/$request && ! -e $root/var/lib/omarchy/image/deferred-steps ]] || fail "the first boot clears the keyring request" +pass "the first boot replaces any build keyring with the machine's own before the steps run" + +# A keyring request with nothing queued (a build whose hardware setup queued no +# step) still gets its keyring. +reset_logs +rm -f "$RUNS" +root=$test_tmp/root-keyring-only +mkdir -p "$root/var/lib/omarchy/image" +install -m 0644 /dev/null "$root/$request" +output=$(KEYRING=$RUNS keyring_boot "$root") || fail "a first boot with only a keyring request finishes" "$output" +[[ $(cat "$RUNS") == "pacman-key --gpgdir $root/etc/pacman.d/gnupg --init"$'\n'"pacman-key --gpgdir $root/etc/pacman.d/gnupg --populate" && ! -e $root/$request ]] || + fail "a keyring request is honoured with no queued step" "$(cat "$RUNS" 2>/dev/null)" +grep -qx 'ConditionPathExists=|/var/lib/omarchy/image/pacman-keyring' "$ROOT/install/provisioning/omarchy-provision-hardware.service" || + fail "the first-boot unit also starts for a keyring request alone" +pass "a keyring request with no queued step still gets the machine's own keyring" + +# A leaf a later Omarchy no longer ships is dropped; a queue entry outside the +# hardware setup stops the run before anything runs. +reset_logs +root=$(new_root gone) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +printf '%s\n' install/hardware/removed.sh install/hardware/c.sh >"$root/var/lib/omarchy/image/deferred-steps" +output=$(first_boot "$root") || fail "a step no longer shipped does not block the rest" "$output" +[[ $output == *"Skipping deferred hardware step install/hardware/removed.sh"* && $(cat "$RUNS") == "c" ]] || + fail "a step no longer shipped is skipped and the rest run" "$output" +pass "a deferred step Omarchy no longer ships is skipped" + +# A step that would build the boot image itself asks for the rebuild after the +# last step instead: one rebuild, even when no +# initramfs input changed, and the request outlives a run that stopped early. +reset_logs +root=$(new_root request) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +printf '%s\n' install/hardware/c.sh >"$root/var/lib/omarchy/image/deferred-steps" +first_boot "$root" >/dev/null || fail "a step that asks for nothing finishes" +[[ ! -e $REBUILDS ]] || fail "no rebuild without a changed input or a request" "$(cat "$REBUILDS")" +reset_logs +root=$(new_root request) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +printf '%s\n' install/hardware/c.sh >"$root/var/lib/omarchy/image/deferred-steps" +touch "$REQUEST_REBUILD" +first_boot "$root" >/dev/null || fail "a step that asks for the rebuild finishes" +rm -f "$REQUEST_REBUILD" +[[ $(cat "$REBUILDS" 2>/dev/null) == "mkinitcpio -P" && ! -e $root/var/lib/omarchy/image/boot-rebuild ]] || + fail "the request alone, with no changed input, gets one rebuild and is then cleared" "$(cat "$REBUILDS" 2>/dev/null)" +reset_logs +root=$(new_root request) +write_manifest "$root" +build "$root" >/dev/null || fail "the fixture image builds" +printf '%s\n' install/hardware/c.sh install/hardware/apple/b.sh >"$root/var/lib/omarchy/image/deferred-steps" +rm -f "$root/etc/mkinitcpio.conf.d/b.conf" +touch "$REQUEST_REBUILD" "$FAIL_B" +first_boot "$root" >/dev/null 2>&1 && fail "the first boot reports the failed step" +[[ -f $root/var/lib/omarchy/image/boot-rebuild && ! -e $REBUILDS ]] || + fail "a requested rebuild waits for the queue to empty, and stays requested" +rm -f "$FAIL_B" "$REQUEST_REBUILD" +first_boot "$root" >/dev/null || fail "the next boot finishes the deferred hardware setup" +[[ $(cat "$REBUILDS" 2>/dev/null) == "mkinitcpio -P" && ! -e $root/var/lib/omarchy/image/boot-rebuild ]] || + fail "the requested rebuild runs once after the last step and is then cleared" "$(cat "$REBUILDS" 2>/dev/null)" +pass "a step can ask for the one rebuild after the last step" + +for entry in /etc/passwd install/hardware/../../bin/x.sh install/login/sddm.sh; do + reset_logs + root=$(new_root unsafe) + write_manifest "$root" + build "$root" >/dev/null || fail "the fixture image builds" + printf '%s\n' "$entry" install/hardware/c.sh >"$root/var/lib/omarchy/image/deferred-steps" + status=0 + first_boot "$root" >/dev/null 2>&1 || status=$? + (( status == 1 )) || fail "the first boot refuses the queue entry $entry with status 1, not a step failure's 75" "status $status" + [[ ! -e $RUNS && $(head -n 1 "$root/var/lib/omarchy/image/deferred-steps") == "$entry" ]] || + fail "the first boot refuses the queue entry $entry before running anything" +done +pass "the first boot runs only hardware setup leaves from its queue" + +reset_logs +root=$(new_root nothing) +output=$(first_boot "$root") || fail "the first-boot hardware setup succeeds with nothing deferred" +[[ -z $output && ! -e $root/var/lib/omarchy ]] || fail "the first-boot hardware setup does nothing with nothing deferred" "$output" +pass "with nothing deferred the first-boot hardware setup does nothing" + +if env -u OMARCHY_IMAGE_ROOT OMARCHY_PATH="$fixture" PATH="$base_path" "$ROOT/bin/omarchy-provision-hardware" >/dev/null 2>&1; then + fail "the first-boot hardware setup refuses a normal user" +fi +pass "the first-boot hardware setup refuses a normal user" + +# The service fires on the queue this helper writes and hands the machine to +# owner setup and the login screen only after it. +grep -qx 'ConditionPathExists=|/var/lib/omarchy/image/deferred-steps' "$unit_source" && + grep -qx 'ExecStart=/usr/bin/omarchy-provision-hardware' "$unit_source" && + grep -qx 'Before=omarchy-provision-owner.service display-manager.service' "$unit_source" && + grep -qx 'WantedBy=multi-user.target' "$unit_source" || + fail "the first-boot hardware service runs on the queue before owner setup and the login screen" +pass "the first-boot hardware service runs on the queue before owner setup and the login screen" + +# --- Bluetooth on the first boot --------------------------------------------- + +# The adapter brings up bluetooth.target before the first boot runs the +# Bluetooth leaf, and a target never starts a unit enabled after its start job +# was made, so that boot has to start bluetooth.service itself. Nothing else +# does: an install or a rerun of hardware setup leaves the service as it finds it. +bt_fixture="$test_tmp/omarchy-bluetooth" +mkdir -p "$bt_fixture/install/helpers" "$bt_fixture/install/provisioning" "$bt_fixture/install/hardware" +cp "$ROOT/install/helpers/logging.sh" "$ROOT/install/helpers/image-target.sh" "$bt_fixture/install/helpers/" +cp "$unit_source" "$bt_fixture/install/provisioning/" +cp "$ROOT/install/hardware/bluetooth.sh" "$bt_fixture/install/hardware/" +cp "$fixture/install/hardware/c.sh" "$bt_fixture/install/hardware/" +printf 'run_logged "$OMARCHY_INSTALL/hardware/%s"\n' bluetooth.sh c.sh >"$bt_fixture/install/hardware/all.sh" + +bt_bin="$test_tmp/bt-bin" +mkdir -p "$bt_bin" +cat >"$bt_bin/systemctl" <<'SH' +#!/bin/bash +printf '%s\n' "$*" >>"$SYSTEMCTL_CALLS" +case $* in + "start --no-block bluetooth.service") [[ ! -e $BT_START_REFUSED ]] ;; + *) exit 0 ;; +esac +SH +chmod +x "$bt_bin/systemctl" +export SYSTEMCTL_CALLS="$test_tmp/systemctl-calls" BT_START_REFUSED="$test_tmp/bt-start-refused" +bt_path="$bt_bin:$base_path" + +bt_image() { + local root + root=$(new_root "bt-$1") + write_manifest "$root" + OMARCHY_IMAGE_ROOT="$root" OMARCHY_PATH="$bt_fixture" OMARCHY_INSTALL_LOG_FILE="$root/var/log/omarchy-install.log" \ + PATH="$bt_path" "$apply_hardware" --defer-provisioning >/dev/null || fail "the Bluetooth fixture image builds" + printf '%s\n' "$root" +} + +bt_first_boot() { + rm -f "$SYSTEMCTL_CALLS" "$RUNS" + OMARCHY_IMAGE_ROOT="$1" OMARCHY_PATH="$bt_fixture" OMARCHY_PROC_ROOT="$test_tmp/hw/proc" \ + PATH="$bt_path" "$ROOT/bin/omarchy-provision-hardware" >/dev/null 2>&1 +} + +started=$'enable bluetooth.service\nstart --no-block bluetooth.service' + +reset_logs +rm -f "$BT_START_REFUSED" "$SYSTEMCTL_CALLS" +root=$(bt_image started) +[[ ! -e $SYSTEMCTL_CALLS ]] || fail "an image build touches no Bluetooth unit" "$(cat "$SYSTEMCTL_CALLS")" +bt_first_boot "$root" || fail "the first boot finishes the Bluetooth leaf" +[[ $(<"$SYSTEMCTL_CALLS") == "$started" ]] || + fail "the first boot enables Bluetooth and starts it without waiting for the job" "$(<"$SYSTEMCTL_CALLS")" +[[ $(cat "$RUNS") == c && ! -e $root/var/lib/omarchy/image/deferred-steps ]] || + fail "the first boot goes on past the Bluetooth leaf" +pass "the first boot enables Bluetooth and starts it in the same session" + +touch "$BT_START_REFUSED" +root=$(bt_image refused) +bt_first_boot "$root" || fail "a refused Bluetooth start does not fail the first boot" +[[ $(<"$SYSTEMCTL_CALLS") == "$started" && $(cat "$RUNS") == c && ! -e $root/var/lib/omarchy/image/deferred-steps ]] || + fail "a refused Bluetooth start leaves the step done and the queue going" "$(<"$SYSTEMCTL_CALLS")" +pass "a refused Bluetooth start does not fail the first boot" +rm -f "$BT_START_REFUSED" + +# A later boot that retries the queue is still the machine's own first setup. +root=$(bt_image retry) +mv "$root/var/lib/omarchy/image/target" "$root/var/lib/omarchy/image/target.booted" +bt_first_boot "$root" || fail "a retried queue finishes" +[[ $(<"$SYSTEMCTL_CALLS") == "$started" ]] || + fail "a retried queue starts Bluetooth like the first run" "$(<"$SYSTEMCTL_CALLS")" +pass "a retried queue starts Bluetooth like the first run" + +# Hardware setup outside the first boot, even with the marker inherited. +rm -f "$SYSTEMCTL_CALLS" "$RUNS" +root=$(new_root bt-live) +OMARCHY_IMAGE_DEFERRED_HARDWARE=1 OMARCHY_IMAGE_ROOT="$root" OMARCHY_PATH="$bt_fixture" \ + OMARCHY_INSTALL_LOG_FILE="$root/var/log/omarchy-install.log" PATH="$bt_path" "$apply_hardware" --defer-provisioning >/dev/null || + fail "hardware setup without a manifest runs the Bluetooth leaf" +[[ $(<"$SYSTEMCTL_CALLS") == "enable bluetooth.service" && $(cat "$RUNS") == c ]] || + fail "hardware setup outside the first boot only enables Bluetooth" "$(<"$SYSTEMCTL_CALLS")" +pass "hardware setup outside the first boot only enables Bluetooth, as an install always has" + +# --- Root ignores the fixture root ------------------------------------------ + +# As namespaced root the fixture root in the environment is ignored: the +# fixture image is not built into, and hardware setup runs as on any live system. +if [[ -e /var/lib/omarchy/image ]]; then + pass "this machine keeps image state of its own; skipping the root override probe" +elif unshare --user --map-root-user true 2>/dev/null; then + root_runner=(unshare --user --map-root-user) + + reset_logs + root=$(new_root root-build) + write_manifest "$root" + OMARCHY_IMAGE_ROOT="$root" OMARCHY_PATH="$fixture" OMARCHY_INSTALL_LOG_FILE="$root/var/log/omarchy-install.log" \ + PATH="$base_path" "${root_runner[@]}" "$ROOT/bin/omarchy-apply-hardware" --defer-provisioning >/dev/null || + fail "root runs hardware setup with a fixture root in its environment" + [[ $(cat "$RUNS") == $'a user= path='"$fixture"$'\nb\nc' && ! -e $root/var/lib/omarchy/image/deferred-steps ]] || + fail "root ignores a fixture manifest named by its environment" + + # Both commands take every path from the helper, which as root answers with + # the live ones whatever the environment says. + resolved=$(OMARCHY_IMAGE_ROOT="$root" "${root_runner[@]}" bash -c \ + 'source "$1"; omarchy_image_init && printf "%s\n" "$omarchy_image_manifest" "$omarchy_image_queue" "$omarchy_image_systemd_dir"' \ + bash "$ROOT/install/helpers/image-target.sh") || fail "root resolves the image paths" + [[ $resolved == $'/var/lib/omarchy/image/target\n/var/lib/omarchy/image/deferred-steps\n/etc/systemd/system' ]] || + fail "root resolves the live image paths whatever its environment names" "$resolved" + pass "root ignores fixture roots in its environment" +else + pass "no unprivileged user namespace; skipping the root override probe" +fi diff --git a/test/shell.d/image-target-platform-test.sh b/test/shell.d/image-target-platform-test.sh new file mode 100644 index 000000000000..9ac03f91d39a --- /dev/null +++ b/test/shell.d/image-target-platform-test.sh @@ -0,0 +1,421 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +# omarchy-hw-platform answers with the image-target manifest while a root is +# being built, and with the hardware once it boots. Each world below is what one +# detector run sees: image/ is the root, proc/ its /proc (the host's device tree +# and PID 1), bin/ a uname for the CPU it runs on. + +detector="$ROOT/bin/omarchy-hw-platform" +umask 022 +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +apple_manifest=$'format=1\nplatform=apple-silicon\n' + +# $1 world, $2 CPU, then the host's device-tree tokens (none: no device tree). +world() { + local dir="$test_tmp/worlds/$1" cpu=$2 + shift 2 + rm -rf "$dir" + mkdir -p "$dir/image" "$dir/proc" "$dir/bin" "$dir/host" + if (( $# )); then + mkdir -p "$dir/proc/device-tree" + printf '%s\0' "$@" >"$dir/proc/device-tree/compatible" + fi + cat >"$dir/bin/uname" <