nixconf

nixos dots
git clone git://popovic.xyz/nixos.config.git
Log | Files | Refs

niri-resume-hook.nix (2574B)


      1 { config, pkgs, ... }:
      2 
      3 let
      4   user = "mika";
      5   uid = toString config.users.users.${user}.uid;
      6 
      7   niriWakeupMonitors = pkgs.writeShellScriptBin "niri-wakeup-monitors" ''
      8     #!${pkgs.bash}/bin/bash
      9     set -euo pipefail
     10 
     11     mon_n="BOE 0x0BCA Unknown"
     12     mon_l="PNP(BNQ) BenQ GL2760 H3E04203019"
     13     mon_r="PNP(BNQ) BenQ GL2760 SCF04101019"
     14 
     15     niri="${pkgs.niri}/bin/niri"
     16     grep="${pkgs.gnugrep}/bin/grep"
     17     wc="${pkgs.coreutils}/bin/wc"
     18     tr="${pkgs.coreutils}/bin/tr"
     19     seq="${pkgs.coreutils}/bin/seq"
     20 
     21     active_connection() {
     22       count=$("$niri" msg outputs | "$grep" -E "H3E04203019|SCF04101019" | "$wc" -l | "$tr" -d ' ')
     23       [ "$count" -eq 2 ]
     24     }
     25 
     26     move_workspaces() {
     27       local monitor="$1"
     28       local direction="$2"
     29 
     30       for nw in $("$seq" 1 9); do
     31         "$niri" msg action \
     32           move-workspace-to-monitor \
     33           --reference "$direction$nw" \
     34           "$monitor"
     35       done
     36 
     37       "$niri" msg action focus-monitor "$monitor"
     38 
     39       for nw in $("$seq" 1 9); do
     40         "$niri" msg action \
     41           move-workspace-to-index \
     42           --reference "$direction$nw" \
     43           "$nw"
     44       done
     45     }
     46 
     47     mulmon_activate() {
     48       "$niri" msg output "$mon_l" on
     49       "$niri" msg output "$mon_r" on
     50       move_workspaces "$mon_l" "l"
     51       move_workspaces "$mon_r" "r"
     52       "$niri" msg output "$mon_n" off
     53     }
     54 
     55     mulmon_deactivate() {
     56       "$niri" msg output "$mon_n" on
     57       move_workspaces "$mon_n" "r"
     58       move_workspaces "$mon_n" "l"
     59       "$niri" msg output "$mon_l" off
     60       "$niri" msg output "$mon_r" off
     61     }
     62 
     63     if active_connection; then
     64       mulmon_activate
     65     else
     66       mulmon_deactivate
     67     fi
     68   '';
     69 in
     70 {
     71   environment.systemPackages = [ niriWakeupMonitors ];
     72 
     73   environment.etc."systemd/system-sleep/niri-wakeup-monitors" = {
     74     mode = "0755";
     75     text = ''
     76       #!${pkgs.bash}/bin/bash
     77       set -euo pipefail
     78 
     79       # args: pre|post  suspend|hibernate|hybrid-sleep|suspend-then-hibernate
     80       if [ "''${1:-}" = "post" ]; then
     81         exec ${pkgs.util-linux}/bin/runuser -u ${user} -- \
     82           env XDG_RUNTIME_DIR=/run/user/${uid} \
     83               DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
     84               ${pkgs.bash}/bin/bash -lc '
     85                 # wait briefly for niri IPC to be ready after resume
     86                 for i in $(seq 1 30); do
     87                   ${pkgs.niri}/bin/niri msg -q outputs >/dev/null 2>&1 && break
     88                   sleep 0.2
     89                 done
     90                 ${niriWakeupMonitors}/bin/niri-wakeup-monitors
     91               '
     92       fi
     93     '';
     94   };
     95 }