refactor of ssh-notify
This commit is contained in:
1 parent
466080ead9
commit
a781b759f4
8 files changed
+341
-58
No files matched your search
@@ -0,0 +1,77 @@
|
||||
{ lib
|
||||
, stdenvNoCC
|
||||
, replaceVars
|
||||
, makeWrapper
|
||||
, nushell
|
||||
, socat
|
||||
, libnotify
|
||||
}:
|
||||
|
||||
# Forwards `notify-send` calls made in an SSH session back to a notification
|
||||
# daemon on the client desktop, instead of failing with
|
||||
# `GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown` (no daemon exists on
|
||||
# a headless host). Ties into `modules/nixos/common/ssh-notify.nix`, which
|
||||
# wires up the `RemoteForward`/`SetEnv` plumbing that gives each concurrent
|
||||
# SSH session its own per-connection socket on the server side.
|
||||
#
|
||||
# Three entry points, installed as separate binaries:
|
||||
# - `notify-send` -- server-side wrapper, shadows the real binary
|
||||
# - `ssh-notify-listener` -- client-side, one persistent listen socket
|
||||
# - `ssh-notify-handle-connection` -- client-side, one JSON payload per call
|
||||
#
|
||||
# The actual socket I/O is delegated to `socat`; the nushell scripts own the
|
||||
# typed payload encoding/decoding and control flow.
|
||||
|
||||
let
|
||||
realNotifySend = "${libnotify}/bin/notify-send";
|
||||
in
|
||||
stdenvNoCC.mkDerivation {
|
||||
pname = "ssh-notify";
|
||||
version = "0.1.0";
|
||||
|
||||
src = ./.;
|
||||
|
||||
nativeBuildInputs = [ makeWrapper ];
|
||||
|
||||
dontUnpack = true;
|
||||
dontBuild = true;
|
||||
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
|
||||
mkdir -p $out/bin
|
||||
|
||||
install -m755 ${replaceVars ./notify-send.nu {
|
||||
real_notify_send = realNotifySend;
|
||||
}} $out/bin/.notify-send-wrapped.nu
|
||||
|
||||
install -m755 ${replaceVars ./ssh-notify-handle-connection.nu {
|
||||
real_notify_send = realNotifySend;
|
||||
}} $out/bin/ssh-notify-handle-connection.nu
|
||||
|
||||
install -m755 ${replaceVars ./ssh-notify-listener.nu {
|
||||
handle_connection = "$out/bin/ssh-notify-handle-connection.nu";
|
||||
}} $out/bin/.ssh-notify-listener-wrapped.nu
|
||||
|
||||
makeWrapper ${nushell}/bin/nu $out/bin/notify-send \
|
||||
--add-flags "$out/bin/.notify-send-wrapped.nu" \
|
||||
--prefix PATH : ${lib.makeBinPath [ socat ]}
|
||||
|
||||
makeWrapper ${nushell}/bin/nu $out/bin/ssh-notify-listener \
|
||||
--add-flags "$out/bin/.ssh-notify-listener-wrapped.nu" \
|
||||
--prefix PATH : ${lib.makeBinPath [ socat nushell ]}
|
||||
|
||||
chmod +x $out/bin/ssh-notify-handle-connection.nu
|
||||
|
||||
runHook postInstall
|
||||
'';
|
||||
|
||||
meta = {
|
||||
description = "Forwards notify-send from SSH sessions back to the client desktop, one socket per concurrent connection";
|
||||
homepage = "https://github.com/skykanin/nixos-setup";
|
||||
license = lib.licenses.mit;
|
||||
sourceProvenance = [ lib.sourceTypes.fromSource ];
|
||||
platforms = lib.platforms.linux;
|
||||
mainProgram = "notify-send";
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env nu
|
||||
# Drop-in `notify-send` replacement.
|
||||
#
|
||||
# - Local session (no $SSH_CONNECTION): passes through to the real libnotify
|
||||
# `notify-send` unchanged.
|
||||
# - SSH session with a live per-connection forward ($SSH_NOTIFY_SOCK set by
|
||||
# the client's `SetEnv`, and the socket file present): encodes the message
|
||||
# as JSON and hands it to `socat` to relay over the forwarded Unix socket
|
||||
# to the listener on the client desktop.
|
||||
# - SSH session with no forward available (client didn't request one, or the
|
||||
# remote bind failed): no-op. There is no notification daemon to call on a
|
||||
# headless host, so falling through to the real binary would just reproduce
|
||||
# the `ServiceUnknown` D-Bus error this wrapper exists to avoid.
|
||||
#
|
||||
# @real_notify_send@ is substituted at build time with the libnotify binary's
|
||||
# store path.
|
||||
|
||||
def main [title: string, body: string = "", ...rest: string]: nothing -> nothing {
|
||||
let ssh_connection = ($env.SSH_CONNECTION? | default "")
|
||||
let notify_sock = ($env.SSH_NOTIFY_SOCK? | default "")
|
||||
|
||||
if ($ssh_connection | is-empty) {
|
||||
^@real_notify_send@ $title $body ...$rest
|
||||
return
|
||||
}
|
||||
|
||||
if ($notify_sock | is-empty) or (not ($notify_sock | path exists)) {
|
||||
return
|
||||
}
|
||||
|
||||
let host = (^hostname -s | str trim)
|
||||
let payload = { host: $host, title: $title, body: $body } | to json --raw
|
||||
|
||||
try {
|
||||
$payload | ^socat - $"UNIX-CONNECT:($notify_sock)"
|
||||
} catch {
|
||||
# Best-effort: a race where the forward closed between the exists
|
||||
# check and the connect attempt isn't worth surfacing to the caller.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env nu
|
||||
# Invoked by `socat EXEC:` once per accepted connection on the listener
|
||||
# socket, with stdin/stdout wired to that connection. Reads the JSON payload
|
||||
# written by notify-send.nu and shows a real desktop notification.
|
||||
#
|
||||
# @real_notify_send@ is substituted at build time with the libnotify binary's
|
||||
# store path.
|
||||
|
||||
def main [] {
|
||||
let raw = (^cat | str trim)
|
||||
|
||||
if ($raw | is-empty) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
let payload = ($raw | from json)
|
||||
^@real_notify_send@ $"[($payload.host)] ($payload.title)" $payload.body
|
||||
} catch {
|
||||
# Malformed/partial payload (e.g. a stray connection) -- ignore.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env nu
|
||||
# Client-side (desktop) listener. Runs one persistent `socat` listen socket
|
||||
# at a fixed, per-user path. This can stay fixed (unlike the per-connection
|
||||
# *remote*-side socket) because `fork` mode lets it accept an unbounded
|
||||
# number of connections over its lifetime -- one per `notify-send` call from
|
||||
# any concurrently forwarded SSH session.
|
||||
#
|
||||
# Each accepted connection is handed off to `ssh-notify-handle-connection`,
|
||||
# which reads one JSON payload and turns it into a real desktop notification.
|
||||
#
|
||||
# @handle_connection@ is substituted at build time with that script's store
|
||||
# path.
|
||||
|
||||
def main [] {
|
||||
let uid = (^id -u | str trim)
|
||||
let dir = $"/run/user/($uid)/ssh-notify"
|
||||
mkdir $dir
|
||||
let sock = $"($dir)/listener.sock"
|
||||
|
||||
# /run/user/<uid> is tmpfs and owned solely by this user, so unlike the
|
||||
# old shared /tmp path there's no cross-boot/cross-owner rm race here.
|
||||
rm -f $sock
|
||||
|
||||
^socat $"UNIX-LISTEN:($sock),fork,mode=0666" $"EXEC:@handle_connection@"
|
||||
}
|
||||
Reference in new issue
Block a user