diff --git a/hosts/fwbook.nix b/hosts/fwbook.nix index 085110e..8e597e2 100644 --- a/hosts/fwbook.nix +++ b/hosts/fwbook.nix @@ -46,6 +46,10 @@ in }; }; }; + common.ssh-notify = { + enable = true; + role = "client"; + }; }; networkDrives = { enable = false; diff --git a/modules/nixos/common/default.nix b/modules/nixos/common/default.nix index c7dcdfc..4f540e5 100644 --- a/modules/nixos/common/default.nix +++ b/modules/nixos/common/default.nix @@ -8,6 +8,7 @@ ./user.nix ./fonts.nix ./ssh-server.nix + ./ssh-notify.nix ./qemu.nix ./pritunl ]; diff --git a/modules/nixos/common/ssh-notify.nix b/modules/nixos/common/ssh-notify.nix new file mode 100644 index 0000000..c96ee31 --- /dev/null +++ b/modules/nixos/common/ssh-notify.nix @@ -0,0 +1,95 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.skyg.nixos.common.ssh-notify; + socketPath = "/tmp/ssh-notify.sock"; + + # Detect if this host is acting as the notification client (the machine you sit in front of) + isClient = + cfg.role == "client" + || (cfg.role == "auto" && (config.skyg.nixos.desktop.enable or false)); + + isServer = + cfg.role == "server" + || (cfg.role == "auto" && !isClient); + + realNotifySend = "${pkgs.libnotify}/bin/notify-send"; + + # Thin wrapper installed in PATH. On servers (when SSH'd) it forwards via the socket. + notifyWrapper = pkgs.writeShellScriptBin "notify-send" '' + #!${pkgs.runtimeShell} + set -euo pipefail + + if [ -n "''${SSH_CONNECTION:-}" ] && [ -S "${socketPath}" ]; then + host="$(hostname -s 2>/dev/null || hostname)" + # Send two lines: prefixed title + body (or remaining args joined) + # This keeps the protocol simple; complex notify-send flags are passed through as-is to the real binary on the client. + printf '[%s] ' "$host" + printf '%s ' "$@" + printf '\n' + # For body we just send the same line for now; user can refine later if needed. + # The listener below will treat the received line as both title and body (notify-send accepts that). + printf '%s\n' "$*" + else + exec ${realNotifySend} "$@" + fi + ''; +in +{ + options.skyg.nixos.common.ssh-notify = { + enable = lib.mkEnableOption + "forward libnotify messages from SSH sessions back to the client desktop (prefixed with hostname)"; + + role = lib.mkOption { + type = lib.types.enum [ "auto" "client" "server" ]; + default = "auto"; + description = '' + auto = client on hosts with skyg.nixos.desktop.enable, server otherwise + client = force desktop listener + RemoteForward + server = force wrapper only + ''; + }; + }; + + config = lib.mkIf cfg.enable { + # Always provide socat + real libnotify + our wrapper (wrapper shadows notify-send when earlier in PATH) + environment.systemPackages = [ + pkgs.socat + pkgs.libnotify + notifyWrapper + ]; + + # Client: ask SSH to forward the notification socket back to us on every connection + programs.ssh.extraConfig = lib.mkIf isClient '' + # Forward remote notify-send traffic to the local listener + RemoteForward ${socketPath} ${socketPath} + ''; + + # Client listener: receives messages over the forwarded socket and shows them locally + systemd.user.services.ssh-notify-listener = lib.mkIf isClient { + description = "Receive notify-send from remote hosts over SSH and display locally"; + after = [ "default.target" ]; + wantedBy = [ "default.target" ]; + serviceConfig = { + ExecStart = let + listenerScript = pkgs.writeShellScript "ssh-notify-listener" '' + set -euo pipefail + # Remove stale socket if present + rm -f ${socketPath} + # Listen and for each connection read two lines (title line, body line) + # and invoke the real notify-send. We keep it running via systemd Restart. + while true; do + ${pkgs.socat}/bin/socat \ + UNIX-LISTEN:${socketPath},fork,mode=0666 \ + SYSTEM:'read title; read body; ${realNotifySend} "$title" "$body" || true' + done + ''; + in "${listenerScript}"; + Restart = "always"; + RestartSec = 2; + }; + }; + + # On servers we rely purely on the wrapper; no extra services needed. + }; +} diff --git a/modules/nixos/common/ssh-server.nix b/modules/nixos/common/ssh-server.nix index b73359f..8b42065 100644 --- a/modules/nixos/common/ssh-server.nix +++ b/modules/nixos/common/ssh-server.nix @@ -14,6 +14,10 @@ in }; }; config = lib.mkIf cfg.enable { + skyg.nixos.common.ssh-notify = { + enable = true; + role = "server"; + }; services.openssh = { enable = true; openFirewall = true;