summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/niri/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/gkleen@sif/niri/default.nix')
-rw-r--r--accounts/gkleen@sif/niri/default.nix185
1 files changed, 159 insertions, 26 deletions
diff --git a/accounts/gkleen@sif/niri/default.nix b/accounts/gkleen@sif/niri/default.nix
index 2e9b6909..7e187c84 100644
--- a/accounts/gkleen@sif/niri/default.nix
+++ b/accounts/gkleen@sif/niri/default.nix
@@ -2,11 +2,10 @@
2let 2let
3 niri = config.programs.niri.package; 3 niri = config.programs.niri.package;
4 terminal = lib.getExe config.programs.kitty.package; 4 terminal = lib.getExe config.programs.kitty.package;
5 lightctl = lib.getExe' config.services.avizo.package "lightctl";
6 volumectl = lib.getExe' config.services.avizo.package "volumectl";
7 makoctl = lib.getExe' config.services.mako.package "makoctl"; 5 makoctl = lib.getExe' config.services.mako.package "makoctl";
8 loginctl = lib.getExe' hostConfig.systemd.package "loginctl"; 6 loginctl = lib.getExe' hostConfig.systemd.package "loginctl";
9 systemctl = lib.getExe' hostConfig.systemd.package "systemctl"; 7 systemctl = lib.getExe' hostConfig.systemd.package "systemctl";
8 swayosd-client = lib.getExe' config.services.swayosd.package "swayosd-client";
10 9
11 focus_or_spawn = pkgs.writeShellApplication { 10 focus_or_spawn = pkgs.writeShellApplication {
12 name = "focus-or-spawn"; 11 name = "focus-or-spawn";
@@ -19,17 +18,21 @@ let
19 18
20 workspaces_json="$(niri msg -j workspaces)" 19 workspaces_json="$(niri msg -j workspaces)"
21 workspace_output="$(jq -r --arg workspace_name "$workspace_name" '.[] | select(.name == $workspace_name) | .output' <<<"$workspaces_json")" 20 workspace_output="$(jq -r --arg workspace_name "$workspace_name" '.[] | select(.name == $workspace_name) | .output' <<<"$workspaces_json")"
22 active_workspace="$(jq -r --arg workspace_output "$workspace_output" '.[] | select(.output == $workspace_output and .is_active) | .id' <<<"$workspaces_json")" 21 # active_workspace="$(jq -r --arg workspace_output "$workspace_output" '.[] | select(.output == $workspace_output and .is_active) | .id' <<<"$workspaces_json")"
23 active_output="$(jq -r '.[] | select(.is_focused) | .output' <<<"$workspaces_json")" 22 active_output="$(jq -r '.[] | select(.is_focused) | .output' <<<"$workspaces_json")"
24 if [[ $workspace_output != "$active_output" ]]; then 23 if [[ $workspace_output != "$active_output" ]]; then
25 niri msg action move-workspace-to-monitor --output "$active_output" "$workspace_name" 24 niri msg action move-workspace-to-monitor --reference "$workspace_name" "$active_output"
26 socat STDIO "$NIRI_SOCKET" <<<'{"Action":{"FocusWorkspace":{"reference":{"Id":'"''${active_workspace}"'}}}}' 25 # socat STDIO "$NIRI_SOCKET" <<<'{"Action":{"FocusWorkspace":{"reference":{"Id":'"''${active_workspace}"'}}}}'
27 niri msg action move-workspace-to-index --index 1 "$workspace_name" 26 # niri msg action move-workspace-to-index --reference "$workspace_name" 1
28 fi 27 fi
29 28
30 while IFS=$'\n' read -r window_json; do 29 while IFS=$'\n' read -r window_json; do
31 if [[ -n $(jq -c "$window_select" <<<"$window_json") ]]; then 30 if [[ -n $(jq -c "$window_select" <<<"$window_json") ]]; then
32 niri msg action focus-window --id "$(jq -r '.id' <<<"$window_json")" 31 if jq -e '.is_focused' <<<"$window_json" >/dev/null; then
32 niri msg action focus-workspace-previous
33 else
34 niri msg action focus-window --id "$(jq -r '.id' <<<"$window_json")"
35 fi
33 exit 0 36 exit 0
34 fi 37 fi
35 done < <(niri msg -j windows | jq -c '.[]') 38 done < <(niri msg -j windows | jq -c '.[]')
@@ -75,7 +78,7 @@ let
75 jq --arg active_workspace "$active_workspace" -c "$action" <<<"$workspace_json" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET" 78 jq --arg active_workspace "$active_workspace" -c "$action" <<<"$workspace_json" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET"
76 ''; 79 '';
77 }; 80 };
78 with-adjacent-workspace-action = config.lib.niri.actions.spawn (lib.getExe with_adjacent_workspace) "^pwctl|kpxc|bmgr|edit|term$"; 81 with-adjacent-workspace-action = config.lib.niri.actions.spawn (lib.getExe with_adjacent_workspace) "^pwctl|eff|kpxc|bmgr|edit|term$";
79 focus-adjacent-workspace = direction: with-adjacent-workspace-action direction ''{"Action":{"FocusWorkspace":{"reference":{"Id": .id}}}}''; 82 focus-adjacent-workspace = direction: with-adjacent-workspace-action direction ''{"Action":{"FocusWorkspace":{"reference":{"Id": .id}}}}'';
80 move-column-to-adjacent-workspace = direction: with-adjacent-workspace-action direction ''{"Action":{"MoveColumnToWorkspace":{"reference":{"Id": .id}}}}''; 83 move-column-to-adjacent-workspace = direction: with-adjacent-workspace-action direction ''{"Action":{"MoveColumnToWorkspace":{"reference":{"Id": .id}}}}'';
81 84
@@ -90,7 +93,8 @@ let
90 active_output="$(jq -r '.[] | select(.is_focused) | .output' <<<"$workspaces_json")" 93 active_output="$(jq -r '.[] | select(.is_focused) | .output' <<<"$workspaces_json")"
91 active_workspace="$(jq -r '.[] | select(.is_focused) | .id' <<<"$workspaces_json")" 94 active_workspace="$(jq -r '.[] | select(.is_focused) | .id' <<<"$workspaces_json")"
92 95
93 workspace_json="$(jq -c --arg active_output "$active_output" 'map(select(.output == $active_output and .name == null)) | sort_by(.idx) | .[0]' <<<"$workspaces_json")" 96 history_json="$(socat STDIO UNIX-CONNECT:"$XDG_RUNTIME_DIR"/niri-workspace-history.sock)"
97 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")"
94 [[ -n $workspace_json && $workspace_json != null ]] || exit 0 98 [[ -n $workspace_json && $workspace_json != null ]] || exit 0
95 jq --arg active_workspace "$active_workspace" -c "$action" <<<"$workspace_json" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET" 99 jq --arg active_workspace "$active_workspace" -c "$action" <<<"$workspace_json" | tee /dev/stderr | socat STDIO "$NIRI_SOCKET"
96 ''; 100 '';
@@ -122,6 +126,7 @@ in {
122 imports = [ 126 imports = [
123 ./waybar.nix 127 ./waybar.nix
124 ./mako.nix 128 ./mako.nix
129 ./swayosd.nix
125 ]; 130 ];
126 131
127 config = { 132 config = {
@@ -156,6 +161,113 @@ in {
156 ]; 161 ];
157 }; 162 };
158 163
164 systemd.user.sockets.niri-workspace-history = {
165 Socket = {
166 ListenStream = "%t/niri-workspace-history.sock";
167 SocketMode = "0600";
168 };
169 };
170 systemd.user.services.niri-workspace-history = {
171 Unit = {
172 BindsTo = [ "niri.service" ];
173 After = [ "niri.service" ];
174 };
175 Install = {
176 WantedBy = [ "niri.service" ];
177 };
178 Service = {
179 Type = "simple";
180 Sockets = [ "niri-workspace-history.socket" ];
181 ExecStart = pkgs.writers.writePython3 "niri-workspace-history" {} ''
182 import os
183 import socket
184 import json
185 import sys
186 from collections import defaultdict
187 from threading import Thread, Lock
188 from socketserver import StreamRequestHandler, ThreadingTCPServer
189 from contextlib import contextmanager
190 from io import TextIOWrapper
191
192
193 @contextmanager
194 def detaching(thing):
195 try:
196 yield thing
197 finally:
198 thing.detach()
199
200
201 workspace_history = defaultdict(list)
202 history_lock = Lock()
203
204
205 def monitor_niri():
206 workspaces = list()
207
208 def focus_workspace(output, workspace):
209 global workspace_history, history_lock
210
211 with history_lock:
212 workspace_history[output] = [workspace] + [ws for ws in workspace_history[output] if ws != workspace] # noqa: E501
213 print(json.dumps(workspace_history), file=sys.stderr)
214
215 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
216 sock.connect(os.environ["NIRI_SOCKET"])
217 sock.send(b"\"EventStream\"\n")
218 for line in sock.makefile(buffering=1, encoding='utf-8'):
219 if line_json := json.loads(line):
220 if "WorkspacesChanged" in line_json:
221 workspaces = line_json["WorkspacesChanged"]["workspaces"]
222 for ws in workspaces:
223 if ws["is_focused"]:
224 focus_workspace(ws["output"], ws["id"])
225 if "WorkspaceActivated" in line_json:
226 for ws in workspaces:
227 if ws["id"] != line_json["WorkspaceActivated"]["id"]:
228 continue
229 focus_workspace(ws["output"], ws["id"])
230 break
231
232
233 class RequestHandler(StreamRequestHandler):
234 def handle(self):
235 global workspace_history, history_lock
236
237 with detaching(TextIOWrapper(self.wfile, encoding='utf-8', write_through=True)) as out: # noqa: E501
238 with history_lock:
239 json.dump(workspace_history, out)
240
241
242 class Server(ThreadingTCPServer):
243 def __init__(self):
244 ThreadingTCPServer.__init__(self, ("", 8000), RequestHandler, bind_and_activate=False) # noqa: E501
245 self.socket = socket.fromfd(3, self.address_family, self.socket_type)
246
247
248 def run_server():
249 with Server() as server:
250 server.serve_forever()
251
252
253 niri = Thread(target=monitor_niri)
254 niri.daemon = True
255 niri.start()
256
257 server_thread = Thread(target=run_server)
258 server_thread.daemon = True
259 server_thread.start()
260
261 while True:
262 server_thread.join(timeout=0.5)
263 niri.join(timeout=0.5)
264
265 if not (niri.is_alive() and server_thread.is_alive()):
266 break
267 '';
268 };
269 };
270
159 programs.niri.settings = { 271 programs.niri.settings = {
160 prefer-no-csd = true; 272 prefer-no-csd = true;
161 screenshot-path = "${config.home.homeDirectory}/screenshots"; 273 screenshot-path = "${config.home.homeDirectory}/screenshots";
@@ -163,10 +275,15 @@ in {
163 hotkey-overlay.skip-at-startup = true; 275 hotkey-overlay.skip-at-startup = true;
164 276
165 input = { 277 input = {
166 keyboard.xkb = { 278 keyboard = {
167 layout = "us,us"; 279 repeat-delay = 300;
168 variant = "dvp,"; 280 repeat-rate = 50;
169 options = "compose:caps,grp:win_space_toggle"; 281
282 xkb = {
283 layout = "us,us";
284 variant = "dvp,";
285 options = "compose:caps,grp:win_space_toggle";
286 };
170 }; 287 };
171 288
172 workspace-auto-back-and-forth = true; 289 workspace-auto-back-and-forth = true;
@@ -202,6 +319,11 @@ in {
202 319
203 debug.render-drm-device = "/dev/dri/by-path/pci-0000:00:02.0-render"; 320 debug.render-drm-device = "/dev/dri/by-path/pci-0000:00:02.0-render";
204 321
322 animations = {
323 slowdown = 0.5;
324 workspace-switch = null;
325 };
326
205 layout = { 327 layout = {
206 gaps = 8; 328 gaps = 8;
207 struts = { left = 0; right = 0; top = 0; bottom = 0; }; 329 struts = { left = 0; right = 0; top = 0; bottom = 0; };
@@ -255,6 +377,7 @@ in {
255 "003" = { name = "bmgr"; open-on-output = "eDP-1"; }; 377 "003" = { name = "bmgr"; open-on-output = "eDP-1"; };
256 "004" = { name = "term"; open-on-output = "eDP-1"; }; 378 "004" = { name = "term"; open-on-output = "eDP-1"; };
257 "005" = { name = "edit"; open-on-output = "eDP-1"; }; 379 "005" = { name = "edit"; open-on-output = "eDP-1"; };
380 "006" = { name = "eff"; open-on-output = "eDP-1"; };
258 "101".name = "comm"; 381 "101".name = "comm";
259 "102".name = "web"; 382 "102".name = "web";
260 # "104".name = "read"; 383 # "104".name = "read";
@@ -264,19 +387,25 @@ in {
264 }; 387 };
265 388
266 window-rules = [ 389 window-rules = [
267 # { 390 {
268 # geometry-corner-radius = 391 matches = [ { is-floating = true; } ];
269 # let 392 geometry-corner-radius =
270 # allCorners = r: { bottom-left = r; bottom-right = r; top-left = r; top-right = r; }; 393 let
271 # in allCorners 4.; 394 allCorners = r: { bottom-left = r; bottom-right = r; top-left = r; top-right = r; };
272 # clip-to-geometry = true; 395 in allCorners 8.;
273 # } 396 clip-to-geometry = true;
397 }
274 { 398 {
275 matches = [ { app-id = "^com\.saivert\.pwvucontrol$"; } ]; 399 matches = [ { app-id = "^com\.saivert\.pwvucontrol$"; } ];
276 open-on-workspace = "pwctl"; 400 open-on-workspace = "pwctl";
277 open-maximized = true; 401 open-maximized = true;
278 } 402 }
279 { 403 {
404 matches = [ { app-id = "^com\.github\.wwmm\.easyeffects$"; } ];
405 open-on-workspace = "eff";
406 open-maximized = true;
407 }
408 {
280 matches = [ { app-id = "^\.blueman-manager-wrapped$"; } ]; 409 matches = [ { app-id = "^\.blueman-manager-wrapped$"; } ];
281 open-on-workspace = "bmgr"; 410 open-on-workspace = "bmgr";
282 open-maximized = true; 411 open-maximized = true;
@@ -303,6 +432,7 @@ in {
303 { app-id = "^org\.keepassxc\.KeePassXC$"; title = ".*Passkey credentials$"; } 432 { app-id = "^org\.keepassxc\.KeePassXC$"; title = ".*Passkey credentials$"; }
304 ]; 433 ];
305 open-focused = true; 434 open-focused = true;
435 open-floating = true;
306 } 436 }
307 { 437 {
308 matches = [ { app-id = "^kitty-scratch$"; } ]; 438 matches = [ { app-id = "^kitty-scratch$"; } ];
@@ -332,6 +462,7 @@ in {
332 matches = [ 462 matches = [
333 { app-id = "^thunderbird$"; } 463 { app-id = "^thunderbird$"; }
334 { app-id = "^Element$"; } 464 { app-id = "^Element$"; }
465 { app-id = "^Rainbow$"; }
335 ]; 466 ];
336 open-on-workspace = "comm"; 467 open-on-workspace = "comm";
337 } 468 }
@@ -380,6 +511,7 @@ in {
380 matches = [ 511 matches = [
381 { app-id = "^Gimp-"; title = "^Quit GIMP$"; } 512 { app-id = "^Gimp-"; title = "^Quit GIMP$"; }
382 { app-id = "^org\.kde\.polkit-kde-authentication-agent-1$"; } 513 { app-id = "^org\.kde\.polkit-kde-authentication-agent-1$"; }
514 { app-id = "^xdg-desktop-portal-gtk$"; }
383 ]; 515 ];
384 open-floating = true; 516 open-floating = true;
385 } 517 }
@@ -570,27 +702,27 @@ in {
570 }; 702 };
571 703
572 "XF86MonBrightnessUp" = { 704 "XF86MonBrightnessUp" = {
573 action = spawn lightctl "-d" "-e4" "-n1" "up"; 705 action = spawn swayosd-client "--brightness" "raise";
574 allow-when-locked = true; 706 allow-when-locked = true;
575 }; 707 };
576 "XF86MonBrightnessDown" = { 708 "XF86MonBrightnessDown" = {
577 action = spawn lightctl "-d" "-e4" "-n1" "down"; 709 action = spawn swayosd-client "--brightness" "lower";
578 allow-when-locked = true; 710 allow-when-locked = true;
579 }; 711 };
580 "XF86AudioRaiseVolume" = { 712 "XF86AudioRaiseVolume" = {
581 action = spawn volumectl "-d" "-u" "up"; 713 action = spawn swayosd-client "--output-volume" "raise";
582 allow-when-locked = true; 714 allow-when-locked = true;
583 }; 715 };
584 "XF86AudioLowerVolume" = { 716 "XF86AudioLowerVolume" = {
585 action = spawn volumectl "-d" "-u" "down"; 717 action = spawn swayosd-client "--output-volume" "lower";
586 allow-when-locked = true; 718 allow-when-locked = true;
587 }; 719 };
588 "XF86AudioMute" = { 720 "XF86AudioMute" = {
589 action = spawn volumectl "-d" "toggle-mute"; 721 action = spawn swayosd-client "--output-volume" "mute-toggle";
590 allow-when-locked = true; 722 allow-when-locked = true;
591 }; 723 };
592 "XF86AudioMicMute" = { 724 "XF86AudioMicMute" = {
593 action = spawn volumectl "-d" "-m" "toggle-mute"; 725 action = spawn swayosd-client "--input-volume" "mute-toggle";
594 allow-when-locked = true; 726 allow-when-locked = true;
595 }; 727 };
596 728
@@ -600,6 +732,7 @@ in {
600 "Mod+Comma".action = spawn makoctl "restore"; 732 "Mod+Comma".action = spawn makoctl "restore";
601 733
602 "Mod+Control+A".action = focus-or-spawn-action-app_id "com.saivert.pwvucontrol" "pwctl" "pwvucontrol"; 734 "Mod+Control+A".action = focus-or-spawn-action-app_id "com.saivert.pwvucontrol" "pwctl" "pwvucontrol";
735 "Mod+Control+O".action = focus-or-spawn-action-app_id "com.github.wwmm.easyeffects" "eff" "easyeffects";
603 "Mod+Control+P".action = focus-or-spawn-action-app_id "org.keepassxc.KeePassXC" "kpxc" "keepassxc"; 736 "Mod+Control+P".action = focus-or-spawn-action-app_id "org.keepassxc.KeePassXC" "kpxc" "keepassxc";
604 "Mod+Control+B".action = focus-or-spawn-action-app_id ".blueman-manager-wrapped" "bmgr" "blueman-manager"; 737 "Mod+Control+B".action = focus-or-spawn-action-app_id ".blueman-manager-wrapped" "bmgr" "blueman-manager";
605 "Mod+Control+Return".action = focus-or-spawn-action-app_id "kitty-scratch" "term" "kitty" "--app-id" "kitty-scratch"; 738 "Mod+Control+Return".action = focus-or-spawn-action-app_id "kitty-scratch" "term" "kitty" "--app-id" "kitty-scratch";