From 09f97244dff32482d4b49cc0a8ae0ba85e495a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 30 Jul 2026 06:44:34 +0300 Subject: [PATCH] refactor(niri): declare app workspaces, drop reaper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The app workspaces were named at runtime and workspace_reaper.sh un-named them once empty so niri would reap them. That daemon died mid-session: it ran a 4-fork reap() inline inside `while read` off `niri msg -j event-stream`, an event burst outran it, and niri disconnected the client for "reading events too slowly" — pipe EOF, script exit, no restart. The session then ran unreaped and left `github` named, empty and immortal. Declare the eight workspaces instead. Immortal is the point: nothing has to create, name or clean them, so "is the app running" is just "does its workspace hold a window" — one stateless query per keypress, no daemon, no marker files, no reaper to die. The empty-workspace objection that blocked this design was already moot: workspace_nav.sh filters `.name == null`, so number and directional navigation skip named workspaces, and waybar runs niri/workspaces with current-only. They show up in the overview only. No open-on-output — KDL has no variables and monitor names are kept to the `output` blocks. An explicit move-workspace-to-monitor updates a named workspace's original output, so nothing snaps back on the config reload that every wallpaper change triggers. --- .config/niri/config.kdl | 38 +++++++--- .config/scripts/niri/app_workspaces.sh | 21 ------ .config/scripts/niri/workspace_app.sh | 76 ++++++++++--------- .config/scripts/niri/workspace_reaper.sh | 94 ------------------------ 4 files changed, 65 insertions(+), 164 deletions(-) delete mode 100644 .config/scripts/niri/app_workspaces.sh delete mode 100755 .config/scripts/niri/workspace_reaper.sh diff --git a/.config/niri/config.kdl b/.config/niri/config.kdl index cf21b69..da3073e 100644 --- a/.config/niri/config.kdl +++ b/.config/niri/config.kdl @@ -126,7 +126,6 @@ environment { // starts them. Hyprland ignored that target, which is why it needed exec-once. // The sleeps preserve Hyprland's ordering (tray applets after waybar). spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/wallpaper_restore.sh" -spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/workspace_reaper.sh" spawn-at-startup "sh" "-c" "bash ~/.config/scripts/shared/song_detail_writer.sh" // waybar needs its own config here: the shared one uses hyprland/workspaces // and hyprland/window, which do nothing under niri. config-niri swaps those @@ -167,11 +166,28 @@ window-rule { open-floating true } -// The app workspaces (code, github, ...) are deliberately NOT declared here. -// A declared `workspace "name"` node is immortal, which left eight empty -// workspaces lying around. workspace_app.sh names a fresh workspace when it -// launches the app, and workspace_reaper.sh un-names it once it empties so niri -// reaps it. Names live in ~/.config/scripts/niri/app_workspaces.sh. +// The app workspaces. A declared `workspace "name"` node is immortal — which is +// exactly the point: nothing has to create, name or clean them up, so there is +// no reaper daemon and no marker files. They were briefly named at runtime +// instead, and the reaper that un-named them died mid-session (niri dropped its +// IPC event stream for "reading events too slowly") leaving empty named +// workspaces behind — the failure this replaces. +// +// The empties are only visible in the overview: workspace_nav.sh filters on +// `.name == null`, so number and directional navigation skip them, and waybar +// runs niri/workspaces with current-only. +// +// No open-on-output on purpose — KDL has no variables and monitor names are kept +// to the `output` blocks and the monitor binds. They start on the first output +// and workspace_app.sh pulls an empty one to the monitor in use on launch. +workspace "code" +workspace "github" +workspace "whatsapp" +workspace "discord" +workspace "deezer" +workspace "youtube" +workspace "mail" +workspace "signal" binds { // ─── launchers (Alt trigger, Hyprland $my_trigger) ─────────────────────── @@ -193,13 +209,15 @@ binds { // ─── app workspaces ───────────────────────────────────────────────────── // workspace_app.sh emulates Hyprland's on-created-empty, which niri lacks. - // An empty workspace moves to the monitor you are on and the app launches - // there; once it holds windows the key only moves focus to it. webapp.sh - // builds the Chromium web-app command from a URL + profile id. + // The workspaces are declared above, so "is the app running" is just "does + // its workspace hold a window". Empty -> the workspace moves to the monitor + // you are on and the app launches there; holding windows -> the key only + // moves focus to it, leaving it on its own monitor. webapp.sh builds the + // Chromium web-app command from a URL + profile id. Alt+C hotkey-overlay-title="Workspace: code" { spawn-sh "~/.config/scripts/niri/workspace_app.sh code zed"; } Alt+G hotkey-overlay-title="Workspace: github" { spawn-sh "~/.config/scripts/niri/workspace_app.sh github ~/.config/scripts/shared/webapp.sh https://github.com/ FCF18Wmt"; } Alt+W hotkey-overlay-title="Workspace: whatsapp" { spawn-sh "~/.config/scripts/niri/workspace_app.sh whatsapp ~/.config/scripts/shared/webapp.sh --mullvad https://web.whatsapp.com/ aW2IMaZ8"; } - Alt+D hotkey-overlay-title="Workspace: discord" { spawn-sh "~/.config/scripts/niri/workspace_app.sh discord flatpak run dev.vencord.Vesktop"; } + Alt+D hotkey-overlay-title="Workspace: discord" { spawn-sh "~/.config/scripts/niri/workspace_app.sh discord flatpak run org.equicord.equibop"; } Alt+S hotkey-overlay-title="Workspace: deezer" { spawn-sh "~/.config/scripts/niri/workspace_app.sh deezer ~/.config/scripts/shared/webapp.sh --mullvad https://deezer.com/ VjW8vTQ3"; } Alt+Y hotkey-overlay-title="Workspace: youtube" { spawn-sh "~/.config/scripts/niri/workspace_app.sh youtube mullvad-exclude flatpak run io.freetubeapp.FreeTube"; } Alt+P hotkey-overlay-title="Workspace: mail" { spawn-sh "~/.config/scripts/niri/workspace_app.sh mail ~/.config/scripts/shared/webapp.sh https://mail.proton.me/ eIpzVtL8"; } diff --git a/.config/scripts/niri/app_workspaces.sh b/.config/scripts/niri/app_workspaces.sh deleted file mode 100644 index abe1853..0000000 --- a/.config/scripts/niri/app_workspaces.sh +++ /dev/null @@ -1,21 +0,0 @@ -# Names of the app workspaces, sourced by the niri workspace scripts. -# Not executable on purpose. -# -# These are NOT declared in config.kdl: a declared `workspace "name"` node makes -# niri keep that workspace alive forever, which left eight empty workspaces -# sitting around. Instead workspace_app.sh names a fresh workspace on launch and -# workspace_reaper.sh un-names it once it empties, letting niri reap it. -# -# Only names in this list are ever un-named, so a workspace you name by hand is -# left alone. - -APP_WORKSPACES=(code github whatsapp discord deezer youtube mail signal) - -# True if $1 is one of the app workspaces. -is_app_workspace() { - local candidate="$1" name - for name in "${APP_WORKSPACES[@]}"; do - [ "$name" = "$candidate" ] && return 0 - done - return 1 -} diff --git a/.config/scripts/niri/workspace_app.sh b/.config/scripts/niri/workspace_app.sh index 59e5b2b..d215294 100755 --- a/.config/scripts/niri/workspace_app.sh +++ b/.config/scripts/niri/workspace_app.sh @@ -1,24 +1,23 @@ #!/usr/bin/env bash -# Emulates Hyprland's `workspace = N, on-created-empty: ` for niri. +# Focus an app workspace, launching the app the first time. # # usage: workspace_app.sh [args...] # workspace_app.sh --move # # Behaviour: -# * workspace already exists (app is running) -> just focus it. It stays on the +# * workspace holds windows (app is running) -> just focus it. It stays on the # monitor it was launched on; the key moves FOCUS there rather than dragging # the workspace to the monitor you happen to be on. -# * workspace does not exist -> take the trailing empty workspace of the -# focused monitor, name it, and launch the app there. +# * workspace is empty (app not running) -> pull it to the focused monitor, +# focus it, and launch the app there. # -# The workspaces are deliberately NOT declared in config.kdl: a declared -# `workspace "name"` node is immortal, so eight empty workspaces would always be -# present. Naming happens here and workspace_reaper.sh un-names an app workspace -# once it empties, after which niri reaps it. +# The workspaces are DECLARED in config.kdl, so they always exist and niri never +# reaps them. That is what removed the naming step, the grace marker files and +# workspace_reaper.sh: with a permanent workspace, "is the app running" is just +# "does its workspace hold a window", which is one stateless query per keypress +# instead of a daemon following the IPC event stream. set -euo pipefail -. "$(dirname "$(realpath "$0")")/app_workspaces.sh" - mode="focus" if [ "${1:-}" = "--move" ]; then mode="move" @@ -29,47 +28,46 @@ ws="${1:?usage: workspace_app.sh [--move] [command...]}" shift || true ws_json=$(niri msg -j workspaces) -exists=$(jq -r --arg w "$ws" '[.[] | select(.name == $w)] | length' <<<"$ws_json") - -if [ "$exists" = "0" ]; then - # Claim the last empty unnamed workspace on this monitor. niri always keeps - # one at the end of every output, so there is something to claim. - output=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") - win_json=$(niri msg -j windows) - idx=$(jq -r --arg o "$output" --argjson w "$win_json" ' - [ .[] - | select(.output == $o and .name == null) - | . as $ws - | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) - ] | last | .idx // empty' <<<"$ws_json") - - if [ -z "$idx" ]; then - notify-send "workspace_app" "No empty workspace free on $output for '$ws'" - exit 1 - fi - - # A freshly named workspace is still empty until the app maps its window, - # and workspace_reaper.sh would un-name it in that gap. The marker tells the - # reaper to leave this one alone for a while; see GRACE_SECONDS there. - mkdir -p "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" - : > "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace/$ws" - - niri msg action set-workspace-name --workspace "$idx" "$ws" >/dev/null +# Resolved before the --move branch so both modes fail loudly rather than letting +# niri exit non-zero into a keybind, where nothing would be shown at all. +# Two ids means two workspaces carry the name — a leftover runtime name next to +# the declared one — which every jq below would then choke on. +mapfile -t ids < <(jq -r --arg w "$ws" '.[] | select(.name == $w) | .id' <<<"$ws_json") +if [ "${#ids[@]}" -eq 0 ]; then + notify-send "workspace_app" "No workspace named '$ws' — is it declared in config.kdl?" + exit 1 +fi +if [ "${#ids[@]}" -gt 1 ]; then + notify-send "workspace_app" "Several workspaces named '$ws' — un-name the leftover one" + exit 1 fi +id="${ids[0]}" if [ "$mode" = "move" ]; then niri msg action move-column-to-workspace "$ws" >/dev/null exit 0 fi +count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') + +if [ "$count" -eq 0 ]; then + # Empty, so the app is not running: bring the workspace to the monitor in use + # before focusing, so the app opens where you are. move-workspace-to-monitor + # takes --reference; the positional argument is the OUTPUT. + focused=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") + home=$(jq -r --arg w "$ws" '.[] | select(.name == $w) | .output' <<<"$ws_json") + # || true: a failed move must not take the focus and the launch down with it + # under `set -e`. Worst case the app opens on the workspace's own monitor. + if [ -n "$focused" ] && [ "$focused" != "$home" ]; then + niri msg action move-workspace-to-monitor --reference "$ws" "$focused" >/dev/null || true + fi +fi + niri msg action focus-workspace "$ws" >/dev/null # Launch only when the workspace holds nothing, so a second press is "go there" # rather than a duplicate app. -id=$(niri msg -j workspaces | jq -r --arg w "$ws" '.[] | select(.name == $w) | .id') -count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') - if [ "$count" -eq 0 ] && [ "$#" -gt 0 ]; then setsid "$@" >/dev/null 2>&1 & fi diff --git a/.config/scripts/niri/workspace_reaper.sh b/.config/scripts/niri/workspace_reaper.sh deleted file mode 100755 index d0cf0e1..0000000 --- a/.config/scripts/niri/workspace_reaper.sh +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env bash -# Un-names app workspaces once they are empty, so niri reaps them. -# -# Runs for the whole session (spawn-at-startup in config.kdl). Follows -# `niri msg event-stream` rather than polling, so it is idle unless windows or -# workspaces actually change. -# -# Why: a named workspace is never reaped, whether the name came from a config -# `workspace "..."` node or from set-workspace-name at runtime. The app -# workspaces are named on launch by workspace_app.sh; without this they would -# pile up empty exactly as the declared ones did. -# -# Only names listed in app_workspaces.sh are touched, so hand-named workspaces -# survive. -set -uo pipefail - -. "$(dirname "$(realpath "$0")")/app_workspaces.sh" - -# A workspace named by workspace_app.sh sits empty until the app maps its window, -# and reaping it in that gap would drop the name the app was launched under. The -# marker file it leaves means "not populated yet, hands off". -# -# The marker is cleared as soon as the workspace holds a window, so the NEXT time -# it goes empty it is reaped at once. A plain timer would not do: if the app is -# closed while the timer is still running, that close event gets skipped and no -# further event arrives to retry. -# -# GRACE_SECONDS is only a backstop for a launch that never produced a window at -# all (crash, wrong command). Flatpak cold starts are slow, hence the size. -GRACE_SECONDS=60 -GRACE_DIR="${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" - -in_grace() { - local marker="$GRACE_DIR/$1" age - [ -f "$marker" ] || return 1 - age=$(( $(date +%s) - $(stat -c %Y "$marker") )) - if [ "$age" -lt "$GRACE_SECONDS" ]; then - return 0 - fi - rm -f "$marker" - return 1 -} - -reap() { - local ws_json win_json name - ws_json=$(niri msg -j workspaces 2>/dev/null) || return 0 - win_json=$(niri msg -j windows 2>/dev/null) || return 0 - - # Any named workspace that now holds windows has served its purpose: drop the - # marker so emptying it later reaps immediately. - while read -r name; do - [ -n "$name" ] && rm -f "$GRACE_DIR/$name" - done < <(jq -r --argjson w "$win_json" ' - .[] - | select(.name != null) - | . as $ws - | select(([$w[] | select(.workspace_id == $ws.id)] | length) > 0) - | .name - ' <<<"$ws_json") - - # Named workspaces holding no windows. - while read -r name; do - [ -n "$name" ] || continue - is_app_workspace "$name" || continue - in_grace "$name" && continue - # The reference is POSITIONAL here. set-workspace-name takes --workspace - # and move-workspace-to-monitor takes --reference, but this one does not. - niri msg action unset-workspace-name "$name" >/dev/null 2>&1 || true - done < <(jq -r --argjson w "$win_json" ' - .[] - | select(.name != null) - | . as $ws - | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) - | .name - ' <<<"$ws_json") -} - -# Sweep once at startup: workspaces can be left named and empty by a previous -# session, a config reload, or a crash of this script. -reap - -# Any window or workspace event. Matching loosely on purpose: niri emits -# WindowOpenedOrChanged (singular) when a window maps and WindowsChanged (plural) -# only for the bulk snapshot, so a narrow list missed the moment a workspace -# became populated — which is exactly when the grace marker must be dropped. -# Ignored by omission: ConfigLoaded, KeyboardLayoutsChanged, OverviewOpenedOrClosed, -# CastsChanged. -niri msg -j event-stream 2>/dev/null | while read -r line; do - case "$line" in - *Window* | *Workspace*) - reap - ;; - esac -done