From 5133b7ebfc13eda58bf54cf2c1ac5b73c2ccf237 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 5 Dec 2025 09:27:51 +0100 Subject: ... --- accounts/gkleen@sif/niri.nix | 978 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 978 insertions(+) create mode 100644 accounts/gkleen@sif/niri.nix (limited to 'accounts/gkleen@sif/niri.nix') diff --git a/accounts/gkleen@sif/niri.nix b/accounts/gkleen@sif/niri.nix new file mode 100644 index 00000000..d4b77d9c --- /dev/null +++ b/accounts/gkleen@sif/niri.nix @@ -0,0 +1,978 @@ +{ config, hostConfig, pkgs, lib, flakeInputs, ... }: +let + cfg = config.programs.niri; + + kdl = flakeInputs.niri-flake.lib.kdl; + sleaf = name: arg: kdl.node name [arg] []; + + niri = cfg.package; + terminal = lib.getExe config.programs.kitty.package; + + focus_or_spawn = pkgs.writeShellApplication { + name = "focus-or-spawn"; + runtimeInputs = [ niri pkgs.gojq pkgs.gnugrep pkgs.socat ]; + text = '' + window_select="$1" + shift + workspace_name="$1" + shift + + workspaces_json="$(niri msg -j workspaces)" + workspace_output="$(jq -r --arg workspace_name "$workspace_name" '.[] | select(.name == $workspace_name) | .output' <<<"$workspaces_json")" + # active_workspace="$(jq -r --arg workspace_output "$workspace_output" '.[] | select(.output == $workspace_output and .is_active) | .id' <<<"$workspaces_json")" + active_output="$(jq -r '.[] | select(.is_focused) | .output' <<<"$workspaces_json")" + if [[ $workspace_output != "$active_output" ]]; then + niri msg action move-workspace-to-monitor --reference "$workspace_name" "$active_output" + # socat STDIO "$NIRI_SOCKET" <<<'{"Action":{"FocusWorkspace":{"reference":{"Id":'"''${active_workspace}"'}}}}' + # niri msg action move-workspace-to-index --reference "$workspace_name" 1 + fi + + while IFS=$'\n' read -r window_json; do + if [[ -n $(jq -c "$window_select" <<<"$window_json") ]]; then + if jq -e '.is_focused' <<<"$window_json" >/dev/null; then + niri msg action focus-workspace-previous + else + if [[ $(jq -r --arg workspace_name "$workspace_name" 'map(select(.name == $workspace_name)) | .[0].is_focused' <<<"$workspaces_json") != "true" ]] && [[ $(jq -r --arg workspace_name "$workspace_name" 'map(select(.name == $workspace_name)) | .[0].id' <<<"$workspaces_json") = $(jq -r '.workspace_id' <<<"$window_json") ]]; then + niri msg action focus-workspace "$workspace_name" + else + niri msg action focus-window --id "$(jq -r '.id' <<<"$window_json")" + fi + fi + exit 0 + fi + done < <(niri msg -j windows | jq -c '.[]') + + exec "$@" + ''; + }; + focus-or-spawn-action = config.lib.niri.actions.spawn (lib.getExe focus_or_spawn); + + with_adjacent_workspace = pkgs.writeShellApplication { + name = "with-adjacent-workspace"; + runtimeInputs = [ niri pkgs.gojq pkgs.socat ]; + text = '' + blacklist="$1" + shift + direction="$1" + shift + action="$1" + shift + + workspaces_json="$(niri msg -j workspaces)" + active_workspace="$(jq -r '.[] | select(.is_focused) | .id' <<<"$workspaces_json")" + workspace_output="$(jq -r --arg active_workspace "$active_workspace" '.[] | select(.id == ($active_workspace | tonumber)) | .output' <<<"$workspaces_json")" + workspace_idx="$(jq -r '.[] | select(.is_focused) | .idx' <<<"$workspaces_json")" + + jq_script='map(select(' + case "$direction" in + down) + # shellcheck disable=SC2016 + jq_script=''${jq_script}'.idx > ($workspace_idx | tonumber)';; + up) + # shellcheck disable=SC2016 + jq_script=''${jq_script}'.idx < ($workspace_idx | tonumber)';; + esac + # shellcheck disable=SC2016 + jq_script=''${jq_script}' and .output == $workspace_output and ((.name == null) or (.name | test($blacklist) | not)))) | sort_by(.idx)' + [[ $direction == "up" ]] && jq_script=''${jq_script}' | reverse' + jq_script=''${jq_script}' | .[0]' + + workspace_json=$(jq -c --arg blacklist "$blacklist" --arg workspace_output "$workspace_output" --arg workspace_idx "$workspace_idx" "$jq_script" <<<"$workspaces_json") + [[ -n $workspace_json && $workspace_json != null ]] || exit 0 + jq --arg active_workspace "$active_workspace" -c "$action" <<<"$workspace_json" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET" + ''; + }; + with-adjacent-workspace-action = config.lib.niri.actions.spawn (lib.getExe with_adjacent_workspace) "^${lib.concatMapStringsSep "|" ({ name, ...}: name) cfg.scratchspaces}$"; + focus-adjacent-workspace = direction: with-adjacent-workspace-action direction ''{"Action":{"FocusWorkspace":{"reference":{"Id": .id}}}}''; + move-column-to-adjacent-workspace = direction: with-adjacent-workspace-action direction ''{"Action":{"MoveColumnToWorkspace":{"reference":{"Id": .id}, "focus": true}}}''; + + with_unnamed_workspace = pkgs.writeShellApplication { + name = "with-unnamed-workspace"; + runtimeInputs = [ niri pkgs.gojq pkgs.socat ]; + text = '' + action="$1" + shift + + workspaces_json="$(niri msg -j workspaces)" + active_output="$(jq -r '.[] | select(.is_focused) | .output' <<<"$workspaces_json")" + active_workspace="$(jq -r '.[] | select(.is_focused) | .id' <<<"$workspaces_json")" + + history_json="$(socat STDIO UNIX-CONNECT:"$XDG_RUNTIME_DIR"/niri-workspace-history.sock)" + workspace_json="$(jq -c --arg active_output "$active_output" --argjson history "$history_json" 'map(select(.output == $active_output and .name == null)) | map({"value": ., "history_idx": ((. as $workspace | ($history[$active_output] | index($workspace | .id))) as $active_idx | if $active_idx then $active_idx else ($history[$active_output] | length) + 1 end)}) | sort_by(.history_idx, .value.idx) | map(.value) | .[0]' <<<"$workspaces_json")" + [[ -n $workspace_json && $workspace_json != null ]] || exit 0 + jq --arg active_workspace "$active_workspace" -c "$action" <<<"$workspace_json" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET" + ''; + }; + with-unnamed-workspace-action = config.lib.niri.actions.spawn (lib.getExe with_unnamed_workspace); + + with_empty_unnamed_workspace = pkgs.writeShellApplication { + name = "with-empty-unnamed-workspace"; + runtimeInputs = [ niri pkgs.gojq pkgs.socat ]; + text = '' + action="$1" + shift + + workspaces_json="$(niri msg -j workspaces)" + active_output="$(jq '.[] | select(.is_focused) | .output' <<<"$workspaces_json")" + target_workspace_id="$(jq --argjson active_output "$active_output" 'map(select(.active_window_id == null and .name == null and .output == $active_output)) | sort_by(.idx) | .[0].id' <<<"$workspaces_json")" + jq --argjson workspace_id "$target_workspace_id" -nc "$action" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET" + ''; + }; + with-empty-unnamed-workspace-action = config.lib.niri.actions.spawn (lib.getExe with_empty_unnamed_workspace); + + with_select_window = pkgs.writeShellApplication { + name = "with-select-window"; + runtimeInputs = [ niri pkgs.gojq pkgs.socat config.programs.fuzzel.package pkgs.gawk ]; + text = '' + window_select="$1" + shift + action="$1" + shift + + windows_json="$(niri msg -j windows)" + active_workspace="$(jq -r '.[] | select(.is_focused) | .workspace_id' <<<"$windows_json")" + window_ix="$(gojq -r --arg active_workspace "$active_workspace" '.[] | select('"$window_select"') | "\(.title)\u0000icon\u001f\(.app_id)"' <<<"$windows_json" | fuzzel --width=60 --log-level=warning --dmenu --index)" + # shellcheck disable=SC2016 + window_json="$(gojq -rc --arg active_workspace "$active_workspace" --arg window_ix "$window_ix" 'map(select('"$window_select"')) | .[($window_ix | tonumber)]' <<<"$windows_json")" + + [[ -z "$window_json" ]] && exit 1 + + jq -c "$action" <<<"$window_json" | socat STDIO "$NIRI_SOCKET" + ''; + }; + with-select-window-action = config.lib.niri.actions.spawn (lib.getExe with_select_window); + + with_predicate_window = pred: pkgs.writeShellApplication { + name = "with-predicate-window"; + runtimeInputs = [ niri pkgs.gojq pkgs.socat ]; + text = '' + action="$1" + shift + + windows_json="$(niri msg -j windows)" + window_json="$(gojq -rc 'map(select(${pred})) | .[0]' <<<"$windows_json")" + + [[ -z "$window_json" || $window_json = "null" ]] && exit 1 + + jq -c "$action" <<<"$window_json" | socat STDIO "$NIRI_SOCKET" + ''; + }; + + with-urgent-window-action = config.lib.niri.actions.spawn (lib.getExe (with_predicate_window ".is_urgent")); + with-focused-window-action = config.lib.niri.actions.spawn (lib.getExe (with_predicate_window ".is_focused")); +in { + options = { + programs.niri.scratchspaces = lib.mkOption { + type = lib.types.listOf (lib.types.submodule ({ config, ... }: { + options = { + name = lib.mkOption { + type = lib.types.str; + }; + match = lib.mkOption { + type = lib.types.listOf (lib.types.attrsOf kdl.types.kdl-args); + default = []; + }; + exclude = lib.mkOption { + type = lib.types.listOf (lib.types.attrsOf kdl.types.kdl-args); + default = []; + }; + windowRuleExtra = lib.mkOption { + type = kdl.types.kdl-nodes; + default = []; + }; + key = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + }; + moveKey = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = let + keys = lib.splitString "+" config.key; + defMoveKey = lib.concatStringsSep "+" (lib.flatten [ + (lib.take (lib.length keys - 1) keys) + ["Shift"] + (lib.takeEnd 1 keys) + ]); + in if config.key == null then null else defMoveKey; + }; + spawn = lib.mkOption { + type = lib.types.nullOr (lib.types.listOf lib.types.str); + default = null; + }; + app-id = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + }; + selector = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + }; + }; + + config = lib.mkMerge [ + (lib.mkIf (config.app-id != null) { + match = lib.mkDefault [ { app-id = "^${lib.escapeRegex config.app-id}$"; } ]; + selector = lib.mkDefault "select(.app_id == \"${config.app-id}\")"; + }) + ]; + })); + default = []; + }; + }; + + config = { + home.packages = [ pkgs.xwayland-satellite-unstable ]; + + systemd.user.sockets.niri-workspace-history = { + Socket = { + ListenStream = "%t/niri-workspace-history.sock"; + SocketMode = "0600"; + }; + }; + systemd.user.services.niri-workspace-history = { + Unit = { + BindsTo = [ "niri.service" ]; + After = [ "niri.service" ]; + }; + Install = { + WantedBy = [ "niri.service" ]; + }; + Service = { + Type = "simple"; + Sockets = [ "niri-workspace-history.socket" ]; + ExecStart = pkgs.writers.writePython3 "niri-workspace-history" { flakeIgnore = ["E501"]; } '' + import os + import socket + import json + # import sys + from collections import defaultdict + from threading import Thread, Lock + from socketserver import StreamRequestHandler, ThreadingTCPServer + from contextlib import contextmanager + from io import TextIOWrapper + + + @contextmanager + def detaching(thing): + try: + yield thing + finally: + thing.detach() + + + workspace_history = defaultdict(list) + history_lock = Lock() + + + def monitor_niri(): + workspaces = list() + + def focus_workspace(output, workspace): + with history_lock: + workspace_history[output] = [workspace] + [ws for ws in workspace_history[output] if ws != workspace] + # print(json.dumps(workspace_history), file=sys.stderr) + + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(os.environ["NIRI_SOCKET"]) + sock.send(b"\"EventStream\"\n") + for line in sock.makefile(buffering=1, encoding='utf-8'): + if line_json := json.loads(line): + if "WorkspacesChanged" in line_json: + workspaces = line_json["WorkspacesChanged"]["workspaces"] + for ws in workspaces: + if ws["is_focused"]: + focus_workspace(ws["output"], ws["id"]) + if "WorkspaceActivated" in line_json: + for ws in workspaces: + if ws["id"] != line_json["WorkspaceActivated"]["id"]: + continue + focus_workspace(ws["output"], ws["id"]) + break + + + class RequestHandler(StreamRequestHandler): + def handle(self): + with detaching(TextIOWrapper(self.wfile, encoding='utf-8', write_through=True)) as out: + with history_lock: + json.dump(workspace_history, out) + + + class Server(ThreadingTCPServer): + def __init__(self): + ThreadingTCPServer.__init__(self, ("", 8000), RequestHandler, bind_and_activate=False) + self.socket = socket.fromfd(3, self.address_family, self.socket_type) + + + def run_server(): + with Server() as server: + server.serve_forever() + + + niri = Thread(target=monitor_niri) + niri.daemon = True + niri.start() + + server_thread = Thread(target=run_server) + server_thread.daemon = True + server_thread.start() + + while True: + server_thread.join(timeout=0.5) + niri.join(timeout=0.5) + + if not (niri.is_alive() and server_thread.is_alive()): + break + ''; + }; + }; + systemd.user.services.niri-workspace-sort = { + Unit = { + BindsTo = [ "niri.service" ]; + After = [ "niri.service" ]; + }; + Install = { + WantedBy = [ "niri.service" ]; + }; + Service = { + Type = "simple"; + ExecStart = pkgs.writers.writePython3 "niri-workspace-sort" { flakeIgnore = ["E501"]; } '' + import os + import sys + import socket + import json + + outputs = None + only = {'HDMI-A-1': {'bmr'}, 'eDP-1': {'vid'}} + + + class Niri(socket.socket): + def __init__(self): + super().__init__(socket.AF_UNIX, socket.SOCK_STREAM) + super().connect(os.environ["NIRI_SOCKET"]) + self.fh = super().makefile(mode='rw', buffering=1, encoding='utf-8') + + def cmd(self, obj): + print(json.dumps(obj, separators=(',', ':')), flush=True, file=self.fh) + + def event_stream(self): + self.cmd("EventStream") + return self.fh + + + with Niri() as niri, Niri().event_stream() as niri_stream: + for line in niri_stream: + workspaces = None + if line_json := json.loads(line): + if "WorkspacesChanged" in line_json: + workspaces = line_json["WorkspacesChanged"]["workspaces"] + + if workspaces is None: + continue + + old_outputs = outputs + outputs = {ws["output"] for ws in workspaces} + if old_outputs is None: + print("Initial outputs: {}".format(outputs), file=sys.stderr) + continue + + new_outputs = outputs - old_outputs + if not new_outputs: + continue + print("New outputs: {}".format(new_outputs), file=sys.stderr) + + relevant_workspaces = list(filter(lambda ws: (ws["name"] is not None) or (ws["active_window_id"] is not None), workspaces)) + target_output = next(iter(outputs - set(only.keys()))) + if not target_output: + continue + for ws in relevant_workspaces: + ws_ident = ws["name"] if ws["name"] is not None else (ws["output"], ws["idx"]) + if ws["output"] not in set(only.keys()): + continue + if ws_ident in only[ws["output"]]: + continue + + print("{} -> {}".format(ws_ident, target_output), file=sys.stderr) + niri.cmd({"Action": {"MoveWorkspaceToMonitor": {"reference": {"Id": ws["id"]}, "output": target_output}}}) + ''; + Restart = "on-failure"; + RestartSec = 10; + }; + }; + + programs.niri.scratchspaces = [ + { name = "pwctl"; + key = "Mod+Control+A"; + spawn = ["pwvucontrol"]; + app-id = "com.saivert.pwvucontrol"; + } + { name = "kpxc"; + exclude = [ + { title = "^Unlock Database.*"; } + { title = "^Access Request.*"; } + { title = ".*Passkey credentials$"; } + ]; + windowRuleExtra = with kdl; [ + (sleaf "open-focused" false) + ]; + key = "Mod+Control+P"; + app-id = "org.keepassxc.KeePassXC"; + spawn = [ "keepassxc" ]; + } + { name = "bmgr"; + key = "Mod+Control+B"; + app-id = ".blueman-manager-wrapped"; + spawn = [ "blueman-manager" ]; + } + { name = "term"; + key = "Mod+Control+Return"; + app-id = "kitty-scratch"; + spawn = [ "kitty" "--app-id" "kitty-scratch" ]; + } + { name = "edit"; + match = [ { title = "^scratch$"; app-id = "^emacs$"; } ]; + key = "Mod+Control+E"; + selector = "select(.app_id == \"emacs\" and .title == \"scratch\")"; + spawn = [ "emacsclient" "-c" "--frame-parameters=(quote (name . \"scratch\"))" ]; + } + { name = "eff"; + key = "Mod+Control+O"; + app-id = "com.github.wwmm.easyeffects"; + spawn = [ "easyeffects" ]; + } + { name = "time"; + key = "Mod+Control+K"; + app-id = "chrome-kimai.yggdrasil.li__-Default"; + spawn = [ (toString (pkgs.resholve.writeScript "kimai" { + interpreter = pkgs.runtimeShell; + inputs = [ pkgs.dex ]; + execer = [ "cannot:${lib.getExe pkgs.dex}" ]; + } '' + exec dex $HOME/.local/state/nix/profile/share/applications/kimai.desktop + '')) ]; + windowRuleExtra = with kdl; [ + (sleaf "block-out-from" "screencast") + ]; + } + ]; + programs.niri.config = + let + inherit (kdl) node plain leaf flag; + optional-node = cond: v: + if cond + then v + else null; + opt-props = lib.filterAttrs (lib.const (value: value != null)); + normalize-nodes = nodes: lib.remove null (lib.flatten nodes); + in + normalize-nodes [ + (flag "prefer-no-csd") + + (sleaf "screenshot-path" "~/screenshots/%Y-%m-%dT%H:%M:%S.png") + + (plain "hotkey-overlay" [ + (flag "skip-at-startup") + ]) + + (plain "input" [ + (plain "keyboard" [ + (sleaf "repeat-delay" 300) + (sleaf "repeat-rate" 50) + + (plain "xkb" [ + (sleaf "layout" "us,us") + (sleaf "variant" "dvp,") + (sleaf "options" "compose:caps,grp:win_space_toggle") + ]) + ]) + + (flag "workspace-auto-back-and-forth") + # (sleaf "focus-follows-mouse" {}) + # (flag "warp-mouse-to-focus") + + # (plain "touchpad" [ (flag "off") ]) + (plain "trackball" [ + (sleaf "scroll-method" "on-button-down") + (sleaf "scroll-button" 278) + ]) + (plain "touch" [ + (sleaf "map-to-output" "eDP-1") + ]) + ]) + + (plain "gestures" [ + (plain "hot-corners" [(flag "off")]) + ]) + + (plain "environment" (lib.mapAttrsToList sleaf { + NIXOS_OZONE_WL = "1"; + QT_QPA_PLATFORM = "wayland"; + QT_WAYLAND_DISABLE_WINDOWDECORATION = "1"; + GDK_BACKEND = "wayland"; + SDL_VIDEODRIVER = "wayland"; + DISPLAY = ":0"; + ELECTRON_OZONE_PLATFORM_HINT = "auto"; + SSH_ASKPASS_REQUIRE = "prefer"; + SSH_ASKPASS = lib.getExe pkgs.kdePackages.ksshaskpass; + SUDO_ASKPASS = lib.getExe pkgs.kdePackages.ksshaskpass; + })) + + (node "output" ["eDP-1"] [ + (sleaf "scale" 1.5) + (sleaf "position" { x = 0; y = 0; }) + ]) + (node "output" ["Ancor Communications Inc ASUS PB287Q 0x0000DD9B"] [ + (sleaf "scale" 1.5) + (sleaf "position" { x = 2560; y = 0; }) + ]) + (node "output" ["HP Inc. HP 727pu CN4417143K"] [ + (sleaf "mode" "2560x1440@119.998") + (sleaf "scale" 1) + (sleaf "position" { x = 2560; y = 0; }) + (flag "variable-refresh-rate") + ]) + + (plain "debug" [ + (sleaf "render-drm-device" "/dev/dri/by-path/pci-0000:00:02.0-render") + ]) + + (plain "animations" [ + (sleaf "slowdown" 0.5) + (plain "workspace-switch" [(flag "off")]) + ]) + + (plain "layout" [ + (sleaf "gaps" 8) + (plain "struts" [ + (sleaf "left" 26) + (sleaf "right" 26) + (sleaf "top" 0) + (sleaf "bottom" 0) + ]) + (plain "border" [ + (sleaf "width" 2) + (sleaf "active-gradient" { + from = "hsla(195 100% 45% 1)"; + to = "hsla(155 100% 37.5% 1)"; + angle = 29; + relative-to = "workspace-view"; + }) + (sleaf "inactive-gradient" { + from = "hsla(0 0% 27.7% 1)"; + to = "hsla(0 0% 23% 1)"; + angle = 29; + relative-to = "workspace-view"; + }) + ]) + (plain "focus-ring" [ + (flag "off") + ]) + + (plain "preset-column-widths" (map (prop: sleaf "proportion" prop) [ + (1. / 4.) (1. / 3.) (1. / 2.) (2. / 3.) (3. / 4.) (1.) + ])) + (plain "default-column-width" [ (sleaf "proportion" (1. / 2.)) ]) + (plain "preset-window-heights" (map (prop: sleaf "proportion" prop) [ + (1. / 3.) (1. / 2.) (2. / 3.) (1.) + ])) + + (flag "always-center-single-column") + + (plain "tab-indicator" [ + (sleaf "gap" 4) + (sleaf "width" 8) + (sleaf "gaps-between-tabs" 4) + (flag "place-within-column") + (sleaf "length" { total-proportion = 1.; }) + (sleaf "active-gradient" { + from = "hsla(195 100% 60% 0.75)"; + to = "hsla(155 100% 50% 0.75)"; + angle = 29; + relative-to = "workspace-view"; + }) + (sleaf "inactive-gradient" { + from = "hsla(0 0% 42% 0.66)"; + to = "hsla(0 0% 35% 0.66)"; + angle = 29; + relative-to = "workspace-view"; + }) + ]) + ]) + + (plain "cursor" [ + (flag "hide-when-typing") + ]) + + (map (name: + (node "workspace" [name] [ + (sleaf "open-on-output" "eDP-1") + ]) + ) (map ({name, ...}: name) cfg.scratchspaces)) + (map (name: + (sleaf "workspace" name) + ) ["comm" "web" "vid" "bmr"]) + + (plain "window-rule" [ + (sleaf "clip-to-geometry" true) + ]) + + (plain "window-rule" [ + (sleaf "match" { is-floating = true; }) + (sleaf "geometry-corner-radius" 8) + (plain "shadow" [ (flag "on") ]) + ]) + + (plain "window-rule" [ + (sleaf "match" { app-id = "^org\\.keepassxc\\.KeePassXC$"; }) + (sleaf "block-out-from" "screencast") + ]) + (plain "window-rule" (normalize-nodes [ + (map (title: + (sleaf "match" { app-id = "^org\\.keepassxc\\.KeePassXC$"; inherit title; }) + ) ["^Unlock Database.*" "^Access Request.*" ".*Passkey credentials$" "Browser Access Request$"]) + (sleaf "open-focused" true) + (sleaf "open-floating" true) + ])) + + (map ({ name, match, exclude, windowRuleExtra, ... }: + (optional-node (match != []) (plain "window-rule" (normalize-nodes [ + (map (sleaf "match") match) + (map (sleaf "exclude") exclude) + (sleaf "open-on-workspace" name) + (sleaf "open-maximized" true) + windowRuleExtra + ]))) + ) cfg.scratchspaces) + + (plain "window-rule" [ + (sleaf "match" { app-id = "^emacs$"; }) + (sleaf "match" { app-id = "^firefox$"; }) + (plain "default-column-width" [(sleaf "proportion" (2. / 3.))]) + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^kitty$"; }) + (sleaf "match" { app-id = "^kitty-play$"; }) + (plain "default-column-width" [(sleaf "proportion" (1. / 3.))]) + ]) + + (plain "window-rule" [ + (sleaf "match" { app-id = "^thunderbird$"; }) + (sleaf "match" { app-id = "^Element$"; }) + (sleaf "match" { app-id = "^chrome-web\.openrainbow\.com__-Default$"; }) + (sleaf "open-on-workspace" "comm") + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^firefox$"; }) + (sleaf "open-on-workspace" "web") + (sleaf "open-maximized" true) + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^mpv$"; }) + (sleaf "open-on-workspace" "vid") + (plain "default-column-width" [(sleaf "proportion" 1.)]) + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^kitty-play$"; }) + (sleaf "open-on-workspace" "vid") + (sleaf "open-focused" false) + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^chrome-audiobookshelf\.yggdrasil\.li__-Default$"; }) + (sleaf "match" { app-id = "^YouTube Music Desktop App$"; }) + (sleaf "open-on-workspace" "vid") + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^pdfpc$"; }) + (plain "default-column-width" [(sleaf "proportion" 1.)]) + ]) + (plain "window-rule" [ + (sleaf "match" { app-id = "^pdfpc$"; title = "^.*presentation.*$"; }) + (plain "default-column-width" [(sleaf "proportion" 1.)]) + (sleaf "open-fullscreen" true) + (sleaf "open-on-workspace" "bmr") + (sleaf "open-focused" false) + ]) + (plain "window-rule" (normalize-nodes [ + (map (sleaf "match") [ + { app-id = "^Gimp-"; title = "^Quit GIMP$"; } + { app-id = "^org\\.kde\\.polkit-kde-authentication-agent-1$"; } + { app-id = "^xdg-desktop-portal-gtk$"; } + ]) + (sleaf "open-floating" true) + ])) + (plain "window-rule" [ + (sleaf "match" { app-id = "^org\\.pwmt\\.zathura$"; }) + (sleaf "match" { app-id = "^evince$"; }) + (sleaf "match" { app-id = "^org\\.gnome\\.Papers$"; }) + (sleaf "default-column-display" "tabbed") + ]) + + (plain "layer-rule" [ + (sleaf "match" { namespace = "^notifications$"; }) + (sleaf "match" { namespace = "^bar$"; }) + (sleaf "match" { namespace = "^launcher$"; }) + (sleaf "block-out-from" "screencast") + ]) + + (plain "binds" + (let + bind = name: cfg: node name [(lib.removeAttrs cfg ["action"])] (lib.mapAttrsToList leaf (lib.removeAttrs cfg.action ["__functor"])); + in + normalize-nodes [ + (lib.mapAttrsToList bind (with config.lib.niri.actions; { + "Mod+Slash".action = show-hotkey-overlay; + + "Mod+Return".action = spawn terminal; + "Mod+Shift+Return".action = + let + nushellKitty = pkgs.symlinkJoin { + name = "nushell-kitty"; + paths = [ config.programs.kitty.package ]; + buildInputs = [ pkgs.makeWrapper ]; + postBuild = '' + wrapProgram $out/bin/kitty \ + --add-flags "--config ${pkgs.writeText "kitty.conf" '' + include $HOME/${config.xdg.configFile."kitty/kitty.conf".target} + shell ${lib.getExe config.programs.nushell.package} + ''}" + ''; + }; + in spawn (lib.getExe' nushellKitty "kitty"); + "Mod+Q".action = close-window; + "Mod+O".action = spawn (lib.getExe config.programs.fuzzel.package); + "Mod+Shift+O".action = spawn (lib.getExe config.programs.fuzzel.package) "--list-executables-in-path"; + + "Mod+Alt+E".action = spawn (lib.getExe' config.services.emacs.package "emacsclient") "-c"; + "Mod+Alt+Y".action = spawn (lib.getExe (pkgs.writeShellApplication { + name = "queue-yt-dlp"; + runtimeInputs = with pkgs; [ wl-clipboard-rs socat ]; + text = '' + socat STDIO UNIX-CONNECT:"$XDG_RUNTIME_DIR"/yt-dlp.sock <<<$'{ "urls": ["'"$(wl-paste)"$'"] }' + ''; + })); + "Mod+Alt+L".action = spawn (lib.getExe (pkgs.writeShellApplication { + name = "queue-yt-dlp"; + runtimeInputs = with pkgs; [ wl-clipboard-rs config.programs.kitty.package ]; + text = '' + exec -- kitty --app-id kitty-play --directory "$HOME"/media mpv "$(wl-paste)" + ''; + })); + "Mod+Alt+M".action = spawn (lib.getExe' pkgs.screen-message "sm") "-n" "Fira Mono" "-a" "1" "-f" "#fff" "-b" "#000"; + + "Mod+U".action = spawn (lib.getExe (pkgs.writeShellApplication { + name = "qalc-fuzzel"; + runtimeInputs = with pkgs; [ wl-clipboard-rs libqalculate config.programs.fuzzel.package coreutils findutils libnotify gnugrep ]; + text = '' + RESULTS_DIR="$HOME/.cache/qalc-fuzzel" + prev() { + FOUND=false + while IFS= read -r line; do + [[ -n "$line" ]] || continue + FOUND=true + echo "$line" + done < <(export LC_ALL=C.UTF-8; echo; find "$RESULTS_DIR" -type f -printf $'%T@ %p\n' | sort -n | cut -d' ' -f2- | xargs -r cat) + $FOUND || echo + } + FUZZEL_RES=$(prev | fuzzel --dmenu --prompt "qalc> " --width=60) || exit $? + if [[ "$FUZZEL_RES" =~ .*\ =\ .* ]]; then + QALC_RES="$FUZZEL_RES" + QALC_RET=0 + else + QALC_RES=$(qalc -set "autocalc off" "$FUZZEL_RES" 2>&1) + QALC_RET=$? + fi + [[ -n "$QALC_RES" ]] || exit 1 + EXISTING=false + set +o pipefail + grep -Fxrl "$QALC_RES" "$RESULTS_DIR" | xargs -r touch + [[ ''${PIPESTATUS[0]} -eq 0 ]] && EXISTING=true + set -o pipefail + if [[ $QALC_RET -eq 0 ]] && ! $EXISTING; then + set +o pipefail + RES_FILE="$RESULTS_DIR"/$(date -uIs).$(tr -Cd 'a-zA-Z0-9' "$RES_FILE" <<<"$QALC_RES" + fi + [[ "$QALC_RES" =~ .*\ =\ (.*) ]] && QALC_RES="''${BASH_REMATCH[1]}" + [[ $QALC_RET -eq 0 ]] && wl-copy "$QALC_RES" + notify-send "$QALC_RES" + ''; + })); + "Mod+Shift+U".action = + let + qalcKitty = pkgs.symlinkJoin { + name = "qalc-kitty"; + paths = [ config.programs.kitty.package ]; + buildInputs = [ pkgs.makeWrapper ]; + postBuild = '' + wrapProgram $out/bin/kitty \ + --add-flags "--config ${pkgs.writeText "kitty.conf" '' + include $HOME/${config.xdg.configFile."kitty/kitty.conf".target} + shell ${lib.getExe pkgs.libqalculate} + ''}" + ''; + }; + in spawn (lib.getExe' qalcKitty "kitty"); + "Mod+E".action = spawn (lib.getExe (pkgs.writeShellApplication { + name = "emoji-fuzzel"; + runtimeInputs = with pkgs; [ config.programs.fuzzel.package wtype wl-clipboard-rs ]; + text = '' + FUZZEL_RES=$(fuzzel --dmenu --prompt "emoji> " --cache "$HOME"/.cache/fuzzel-emoji --width=60 <"$HOME"/.local/share/emoji-data/list.txt) || exit $? + [[ -n "$FUZZEL_RES" ]] || exit 1 + wl-copy "$(cut -d ':' -f 1 <<<"$FUZZEL_RES" | tr -d '\n')" && wtype -k XF86Paste + ''; + })); + "Print".action = kdl.magic-leaf "screenshot"; + "Control+Print".action = kdl.magic-leaf "screenshot-window"; + "Shift+Print".action = kdl.magic-leaf "screenshot-screen"; + "Mod+B".action = with-select-window-action ".workspace_id == ($active_workspace | tonumber)" "{\"Action\":{\"FocusWindow\":{\"id\": .id}}}"; + "Mod+Shift+B".action = with-select-window-action "true" "{\"Action\":{\"FocusWindow\":{\"id\": .id}}}"; + + "Mod+Escape" = { + allow-inhibiting = false; + action = toggle-keyboard-shortcuts-inhibit; + }; + + "Mod+H".action = focus-column-left; + "Mod+T".action = focus-window-down; + "Mod+N".action = focus-window-up; + "Mod+S".action = focus-column-right; + + "Mod+Shift+H".action = move-column-left; + "Mod+Shift+T".action = move-window-down; + "Mod+Shift+N".action = move-window-up; + "Mod+Shift+S".action = move-column-right; + + "Mod+Control+H".action = focus-monitor-left; + "Mod+Control+T".action = focus-monitor-down; + "Mod+Control+N".action = focus-monitor-up; + "Mod+Control+S".action = focus-monitor-right; + + "Mod+Shift+Control+H".action = move-workspace-to-monitor-left; + "Mod+Shift+Control+T".action = move-workspace-to-monitor-down; + "Mod+Shift+Control+N".action = move-workspace-to-monitor-up; + "Mod+Shift+Control+S".action = move-workspace-to-monitor-right; + + "Mod+G".action = focus-adjacent-workspace "down"; + "Mod+C".action = focus-adjacent-workspace "up"; + + "Mod+Shift+G".action = move-column-to-adjacent-workspace "down"; + "Mod+Shift+C".action = move-column-to-adjacent-workspace "up"; + + "Mod+Shift+Control+G".action = move-workspace-down; + "Mod+Shift+Control+C".action = move-workspace-up; + + "Mod+ParenLeft".action = focus-workspace "comm"; + "Mod+Shift+ParenLeft".action = kdl.magic-leaf "move-column-to-workspace" "comm"; + + "Mod+ParenRight".action = focus-workspace "web"; + "Mod+Shift+ParenRight".action = kdl.magic-leaf "move-column-to-workspace" "web"; + + "Mod+BraceRight".action = focus-workspace "read"; + "Mod+Shift+BraceRight".action = kdl.magic-leaf "move-column-to-workspace" "read"; + + "Mod+BraceLeft".action = focus-workspace "mon"; + "Mod+Shift+BraceLeft".action = kdl.magic-leaf "move-column-to-workspace" "mon"; + + "Mod+Asterisk".action = focus-workspace "vid"; + "Mod+Shift+Asterisk".action = kdl.magic-leaf "move-column-to-workspace" "vid"; + + "Mod+Plus".action = with-unnamed-workspace-action ''{"Action":{"FocusWorkspace":{"reference":{"Id": .id}}}}''; + "Mod+Shift+Plus".action = with-unnamed-workspace-action ''{"Action":{"MoveColumnToWorkspace":{"reference":{"Id": .id}, "focus": true}}}''; + + "Mod+M".action = consume-or-expel-window-left; + "Mod+W".action = consume-or-expel-window-right; + + "Mod+Shift+M".action = toggle-column-tabbed-display; + + "Mod+R".action = switch-preset-column-width; + "Mod+Shift+R".action = maximize-column; + "Mod+Shift+Ctrl+R".action = switch-preset-window-height; + "Mod+F".action = center-column; + "Mod+Shift+F".action = toggle-windowed-fullscreen; + "Mod+Ctrl+Shift+F".action = fullscreen-window; + + "Mod+V".action = switch-focus-between-floating-and-tiling; + "Mod+Shift+V".action = toggle-window-floating; + + "Mod+Left".action = set-column-width "-10%"; + "Mod+Down".action = set-window-height "-10%"; + "Mod+Up".action = set-window-height "+10%"; + "Mod+Right".action = set-column-width "+10%"; + + "Mod+Shift+Z" = { + action = power-off-monitors; + allow-when-locked = true; + }; + "Mod+Shift+E".action = quit; + + # "Mod+Semicolon".action = spawn makoctl "dismiss" "--group"; + # "Mod+Shift+Semicolon".action = spawn makoctl "dismiss" "--all"; + # "Mod+Period".action = spawn makoctl "menu" "--" (lib.getExe config.programs.fuzzel.package) "--dmenu"; + # "Mod+Comma".action = spawn makoctl "restore"; + + "Mod+Control+W".action = with-empty-unnamed-workspace-action "{\"Action\":{\"FocusWorkspace\":{\"reference\":{\"Id\": $workspace_id}}}}"; + "Mod+Control+Shift+W".action = with-empty-unnamed-workspace-action "{\"Action\":{\"MoveColumnToWorkspace\":{\"reference\":{\"Id\": $workspace_id}, \"focus\": true}}}"; + + "Mod+X".action = set-dynamic-cast-window; + "Mod+Shift+X".action = set-dynamic-cast-monitor; + "Mod+Control+Shift+X".action = clear-dynamic-cast-target; + + "Mod+D".action = with-urgent-window-action "{\"Action\":{\"FocusWindow\":{\"id\": .id}}}"; + "Mod+Shift+D".action = with-focused-window-action "{\"Action\":{\"UnsetUrgent\":{\"id\": .id}}}"; + + "Mod+K".action = spawn (lib.getExe' pkgs.worktime "worktime-ui"); + "Mod+Shift+K".action = spawn (lib.getExe' pkgs.worktime "worktime-stop"); + })) + (lib.mapAttrsToList (name: cfg: node name [(lib.removeAttrs cfg ["action"])] [cfg.action]) (let + shell = obj: leaf "send-unix" [ + { path = ''''${XDG_RUNTIME_DIR}/shell.sock''; } + (builtins.toJSON obj + "\n") + ]; + in { + "XF86AudioRaiseVolume" = { + allow-when-locked = true; + action = shell { Volume.volume = "up"; }; + }; + "XF86AudioLowerVolume" = { + allow-when-locked = true; + action = shell { Volume.volume = "down"; }; + }; + "XF86AudioMute" = { + allow-when-locked = true; + action = shell { Volume.muted = "toggle"; }; + }; + "XF86AudioMicMute" = { + allow-when-locked = true; + action = shell { Volume."mic-muted" = "toggle"; }; + }; + "XF86MonBrightnessUp" = { + action = shell { Brightness = "up"; }; + allow-when-locked = true; + }; + "XF86MonBrightnessDown" = { + action = shell { Brightness = "down"; }; + allow-when-locked = true; + }; + "Mod+Shift+L".action = shell { LockSession = {}; }; + "Mod+Shift+Minus" = { + action = shell { Suspend = {}; }; + allow-when-locked = true; + }; + "Mod+Shift+Control+Minus" = { + action = shell { Hibernate = {}; }; + allow-when-locked = true; + }; + "Mod+Shift+P" = { + action = shell { Mpris = { PauseAll = {}; }; }; + allow-when-locked = true; + }; + "Mod+Semicolon".action = shell { Notifications = { DismissGroup = {}; }; }; + "Mod+Shift+Semicolon".action = shell { Notifications = { DismissAll = {}; }; }; + })) + (map ({ name, selector, spawn, key, ...}: if key != null && selector != null && spawn != null then bind key { action = focus-or-spawn-action selector name spawn; } else null) cfg.scratchspaces) + (map ({ name, moveKey, ...}: if moveKey != null then bind moveKey { action = kdl.magic-leaf "move-column-to-workspace" name; } else null) cfg.scratchspaces) + ] + )) + ]; + }; +} -- cgit v1.2.3