This commit is contained in:
atagen 2026-02-15 11:55:28 +11:00
commit f7d47d6c80
3 changed files with 107 additions and 0 deletions

7
flake.lock generated Normal file
View file

@ -0,0 +1,7 @@
{
"nodes": {
"root": {}
},
"root": "root",
"version": 7
}

36
flake.nix Normal file
View file

@ -0,0 +1,36 @@
{
description = "Passes focused Niri window's PID to System76 Scheduler.";
outputs = _: {
nixosModules.default =
{
config,
lib,
pkgs,
...
}:
{
options.services.niri-s76-bridge = {
enable = lib.mkEnableOption "niri-s76-bridge service";
};
config = lib.mkIf config.services.niri-s76-bridge.enable {
systemd.user.services.niri-s76-bridge = {
description = "Bridge between niri and System76 Scheduler";
after = [ "graphical-session.target" ];
partOf = [ "graphical-session.target" ];
wantedBy = [ "graphical-session.target" ];
path = [ config.programs.niri.package ];
serviceConfig = {
Type = "simple";
ExecStart = "${lib.getExe pkgs.nushell} -n ${./niri-s76-bridge.nu}";
Restart = "on-failure";
RestartSec = "5";
};
};
};
};
};
}

64
niri-s76-bridge.nu Executable file
View file

@ -0,0 +1,64 @@
def main [] {
def notify-scheduler [proc_id: int] {
print $"Forwarding PID ($proc_id) to scheduler"
busctl call com.system76.Scheduler /com/system76/Scheduler com.system76.Scheduler SetForegroundProcess u $"($proc_id)"
}
print "niri-s76-bridge started"
niri msg -j event-stream | lines | reduce --fold [] { |line, windows|
let event = $line | from json
let event_type = $event | columns | first
match $event_type {
"WindowsChanged" => {
mut acc = $windows
for w in $event.WindowsChanged.windows {
if $w.pid != null and $w.is_focused {
notify-scheduler $w.pid
}
if $w.pid != null {
let existing = $acc | where id == $w.id
if ($existing | is-empty) {
$acc = ($acc | append {id: $w.id, pid: $w.pid})
}
}
}
$acc
}
"WindowOpenedOrChanged" => {
let w = $event.WindowOpenedOrChanged.window
if $w.is_focused and $w.pid != null {
notify-scheduler $w.pid
}
if $w.pid != null {
let existing = $windows | where id == $w.id
if ($existing | is-empty) {
$windows | append {id: $w.id, pid: $w.pid}
} else {
$windows
}
} else {
$windows
}
}
"WindowFocusChanged" => {
let focused_id = $event.WindowFocusChanged.id
if $focused_id != null {
let match = $windows | where id == $focused_id
if not ($match | is-empty) {
let focused_pid = $match.0.pid
if $focused_pid != null {
notify-scheduler $focused_pid
}
}
}
$windows
}
"WindowClosed" => {
$windows | where id != $event.WindowClosed.id
}
_ => $windows
}
}
}