diff --git a/modules/core/default.nix b/modules/core/default.nix index 27f6ee4..c9aa00a 100644 --- a/modules/core/default.nix +++ b/modules/core/default.nix @@ -29,6 +29,8 @@ niri-touchscreen-gestures = final.callPackage ../../pkgs/niri-touchscreen-gestures { }; # Buzz Desktop AppImage wrapper, see pkgs/buzz-desktop. buzz-desktop = final.callPackage ../../pkgs/buzz-desktop { }; + # Per-connection SSH notification forwarder, see pkgs/ssh-notify. + ssh-notify = final.callPackage ../../pkgs/ssh-notify { }; # Claude Desktop (Linux), see pkgs/claude-desktop. claude-desktop = claude-desktop.packages.${final.stdenv.hostPlatform.system}.default; # herdr (herdrdev/herdr) — see flake.nix input. diff --git a/modules/nixos/common/ssh-notify.nix b/modules/nixos/common/ssh-notify.nix index 31f57d8..b84a576 100644 --- a/modules/nixos/common/ssh-notify.nix +++ b/modules/nixos/common/ssh-notify.nix @@ -2,7 +2,23 @@ let cfg = config.skyg.nixos.common.ssh-notify; - socketPath = "/tmp/ssh-notify.sock"; + + # Client-side listen socket. Fixed/shared is fine here: socat's `fork` + # mode accepts unbounded connections over this path's lifetime, one per + # `notify-send` call from any concurrently forwarded SSH session. + # + # NOTE: assumes a single interactive uid (1000) on the desktop host, same + # as the rest of this single-user setup. + localSocketPath = "/run/user/1000/ssh-notify/listener.sock"; + + # Remote-side (server) forwarded socket. Uses OpenSSH's %C token (hash of + # local host/port/user + remote host/port/user, requires OpenSSH >= 8.9) + # so each concurrent SSH connection gets its own bind path on the server — + # this is what the old shared /tmp/ssh-notify.sock path got wrong: only + # one concurrent session could ever bind it, and every other session's + # `RemoteForward` silently failed ("remote port forwarding failed for + # listen path ..."), falling back to a broken direct D-Bus notify-send. + remoteSocketPath = "/tmp/ssh-notify-%C.sock"; # Detect if this host is acting as the notification client (the machine you sit in front of) isClient = @@ -12,28 +28,6 @@ let 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 = { @@ -52,41 +46,29 @@ in }; 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 - ]; + environment.systemPackages = [ pkgs.ssh-notify ]; - # Client: ask SSH to forward the notification socket back to us on every connection + # Client: ask SSH to forward this connection's notification socket back + # to the shared local listener, and tell the server-side wrapper (via + # SetEnv, expanded here with the same %C the RemoteForward bind uses) + # which socket to connect to. programs.ssh.extraConfig = lib.mkIf isClient '' - # Forward remote notify-send traffic to the local listener - RemoteForward ${socketPath} ${socketPath} + RemoteForward ${remoteSocketPath} ${localSocketPath} + SetEnv SSH_NOTIFY_SOCK=${remoteSocketPath} ''; - # Client listener: receives messages over the forwarded socket and shows them locally + # Server: accept the SSH_NOTIFY_SOCK env var forwarded by the client + # above (SSH only passes through an allow-listed set by default). + services.openssh.settings.AcceptEnv = lib.mkIf isServer [ "SSH_NOTIFY_SOCK" ]; + + # Client listener: one persistent socket, forked per connection, that + # turns each incoming JSON payload into a real local notification. 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}"; + ExecStart = "${pkgs.ssh-notify}/bin/ssh-notify-listener"; Restart = "always"; RestartSec = 2; }; diff --git a/modules/nixos/server/services/ds4.nix b/modules/nixos/server/services/ds4.nix index a4ed888..1910c46 100644 --- a/modules/nixos/server/services/ds4.nix +++ b/modules/nixos/server/services/ds4.nix @@ -20,13 +20,30 @@ let "--port ${toString port}" (optionalString (ctx != null) "--ctx ${toString ctx}") + (optionalString (tokens != null) "--tokens ${toString tokens}") (optionalString (threads != null) "--threads ${toString threads}") - (optionalString (batchSize != null) "--batch-size ${toString batchSize}") + (optionalString (power != null) "--power ${toString power}") + (optionalString (prefillChunk != null) "--prefill-chunk ${toString prefillChunk}") + (optionalString (batchedSession != null) "--batched-session ${toString batchedSession}") + (optionalString (mixedPrefillQuantum != null) "--mixed-prefill-quantum ${toString mixedPrefillQuantum}") + (optionalString (vision != null) "--vision ${escapeShellArg vision}") + (optionalString (trace != null) "--trace ${escapeShellArg trace}") (optionalString (kvDiskDir != null) "--kv-disk-dir ${escapeShellArg kvDiskDir}") (optionalString (kvDiskSpaceMb != null) "--kv-disk-space-mb ${toString kvDiskSpaceMb}") - (optionalString noKvOffload "--no-kv-offload") + (optionalString (kvCacheMinTokens != null) "--kv-cache-min-tokens ${toString kvCacheMinTokens}") + (optionalString (kvCacheColdMaxTokens != null) "--kv-cache-cold-max-tokens ${toString kvCacheColdMaxTokens}") + (optionalString (kvCacheContinuedIntervalTokens != null) "--kv-cache-continued-interval-tokens ${toString kvCacheContinuedIntervalTokens}") + (optionalString (kvCacheBoundaryTrimTokens != null) "--kv-cache-boundary-trim-tokens ${toString kvCacheBoundaryTrimTokens}") + (optionalString (kvCacheBoundaryAlignTokens != null) "--kv-cache-boundary-align-tokens ${toString kvCacheBoundaryAlignTokens}") + (optionalString kvCacheRejectDifferentQuant "--kv-cache-reject-different-quant") + (optionalString (toolMemoryMaxIds != null) "--tool-memory-max-ids ${toString toolMemoryMaxIds}") (optionalString cors "--cors") - (optionalString (gpuDeviceIds != null) "--gpu ${escapeShellArg gpuDeviceIds}") + (optionalString (gpuDevices != null) "--gpu-devices ${escapeShellArg gpuDevices}") + (optionalString ssdStreaming "--ssd-streaming") + (optionalString ssdStreamingCold "--ssd-streaming-cold") + (optionalString (ssdStreamingCacheExperts != null) "--ssd-streaming-cache-experts ${escapeShellArg ssdStreamingCacheExperts}") + (optionalString (ssdStreamingFullLayers != null) "--ssd-streaming-full-layers ${toString ssdStreamingFullLayers}") + (optionalString (ssdStreamingPreloadExperts != null) "--ssd-streaming-preload-experts ${toString ssdStreamingPreloadExperts}") ]); in { @@ -77,6 +94,13 @@ in type = types.nullOr types.int; }; + tokens = mkOption { + description = "Default max output tokens when clients omit a limit."; + default = null; + example = 4096; + type = types.nullOr types.int; + }; + threads = mkOption { description = "Number of inference threads (default: CPU core count)."; default = null; @@ -84,13 +108,48 @@ in type = types.nullOr types.int; }; - batchSize = mkOption { - description = "Token batch size for prompt processing."; + power = mkOption { + description = "GPU duty-cycle target, 1..100 (default: 100)."; default = null; - example = 512; + example = 80; type = types.nullOr types.int; }; + prefillChunk = mkOption { + description = "Graph prefill chunk size in tokens."; + default = null; + example = 4096; + type = types.nullOr types.int; + }; + + batchedSession = mkOption { + description = "Keep N resident sessions and batch decode-ready requests."; + default = null; + example = 4; + type = types.nullOr types.int; + }; + + mixedPrefillQuantum = mkOption { + description = "Prefill chunk size while generations are active (default: 128; GLM-5.3 minimum: 1024)."; + default = null; + example = 128; + type = types.nullOr types.int; + }; + + vision = mkOption { + description = "Path to the vision encoder GGUF for the selected model."; + default = null; + example = "/var/lib/ds4/vision.gguf"; + type = types.nullOr types.str; + }; + + trace = mkOption { + description = "Write prompts, cache decisions, output, and tool calls to this file."; + default = null; + example = "/var/lib/ds4/trace.log"; + type = types.nullOr types.str; + }; + kvDiskDir = mkOption { description = "Directory for KV cache disk offloading (persists across restarts)."; default = null; @@ -105,25 +164,100 @@ in type = types.nullOr types.int; }; - noKvOffload = mkOption { - description = "Disable KV cache offloading to disk entirely."; + kvCacheMinTokens = mkOption { + description = "Do not save/load KV checkpoints shorter than this many tokens (default: 512)."; + default = null; + example = 512; + type = types.nullOr types.int; + }; + + kvCacheColdMaxTokens = mkOption { + description = "Save cold first prompts up to this many tokens; 0 disables (default: 30000)."; + default = null; + example = 30000; + type = types.nullOr types.int; + }; + + kvCacheContinuedIntervalTokens = mkOption { + description = "Save aligned continued frontiers every N tokens; 0 disables (default: 10000)."; + default = null; + example = 10000; + type = types.nullOr types.int; + }; + + kvCacheBoundaryTrimTokens = mkOption { + description = "Trim tail tokens for cold boundary saves (default: 32)."; + default = null; + example = 32; + type = types.nullOr types.int; + }; + + kvCacheBoundaryAlignTokens = mkOption { + description = "Align cold boundary saves to this multiple (default: 2048)."; + default = null; + example = 2048; + type = types.nullOr types.int; + }; + + kvCacheRejectDifferentQuant = mkOption { + description = "Reject KV checkpoints written with different routed-expert quantization."; default = false; type = types.bool; }; + toolMemoryMaxIds = mkOption { + description = "Exact tool-call IDs kept in RAM (default: 100000)."; + default = null; + example = 100000; + type = types.nullOr types.int; + }; + cors = mkOption { description = "Enable CORS headers for browser-based clients."; default = false; type = types.bool; }; - gpuDeviceIds = mkOption { + gpuDevices = mkOption { description = "GPU device IDs for multi-GPU inference (e.g. '0,1')."; default = null; example = "0,1"; type = types.nullOr types.str; }; + ssdStreaming = mkOption { + description = "Opt in to SSD-backed model streaming instead of full residency (Metal/CUDA/ROCm)."; + default = false; + type = types.bool; + }; + + ssdStreamingCacheExperts = mkOption { + description = "SSD streaming cache target: N requests dynamic expert slots; NGB also reserves two full prefill layers."; + default = null; + example = "64"; + type = types.nullOr types.str; + }; + + ssdStreamingCold = mkOption { + description = "SSD streaming: skip the default popularity-based expert-cache preload."; + default = false; + type = types.bool; + }; + + ssdStreamingFullLayers = mkOption { + description = "GLM Metal streaming: keep the first N routed layers fully resident (default: auto; 0 disables)."; + default = null; + example = 8; + type = types.nullOr types.int; + }; + + ssdStreamingPreloadExperts = mkOption { + description = "SSD streaming: upfront popularity preload count (DeepSeek auto-seeds by default; GLM demand-fills unless set)."; + default = null; + example = 128; + type = types.nullOr types.int; + }; + environment = mkOption { description = '' Extra environment variables for the ds4-server process. Useful for GPU diff --git a/pkgs/ABOUTME.md b/pkgs/ABOUTME.md index d204b19..6c92709 100644 --- a/pkgs/ABOUTME.md +++ b/pkgs/ABOUTME.md @@ -11,6 +11,7 @@ Packages here are exposed via an overlay defined in `modules/core/default.nix`. | `colibri/` | colibrì (`JustVugg/colibri`) — GLM-5.2/OLMoE local inference engine, built from source, pinned to a tagged release. Backend-parameterized (`backend = "cpu" \| "rocm"`). Exposed as `pkgs.colibri` (cpu, default), `pkgs.colibri-rocm` (Strix Halo / gfx1151). Installs the `coli` CLI (wrapped with the Python env its converter/bench subcommands need) plus the bundled `colibri`/`olmoe` engine binaries. Model weights are a runtime concern and not packaged. | | `niri-touchscreen-gestures/` | Python daemon that reads multi-touch events from a touchscreen via `evdev`, detects 2/3/4-finger swipes (up/down/left/right), and dispatches configurable `niri msg action` commands. Configured via TOML; exposed as `pkgs.niri-touchscreen-gestures`. | | `buzz-desktop/` | Block Buzz desktop (Type-2 AppImage from `block/buzz`). Wrapped with `appimageTools.wrapType2` plus extra FHS libs (`elfutils`, `zstd`, WebKit/tray, GStreamer good/bad) so it runs on NixOS. Exposed as `pkgs.buzz-desktop` (`buzz-desktop` binary + desktop entry / icons). | +| `ssh-notify/` | Nushell scripts (socket I/O delegated to `socat`) that forward `notify-send` calls from SSH sessions back to the client desktop, one socket per concurrent connection. Exposed as `pkgs.ssh-notify` (`notify-send` server-side wrapper, `ssh-notify-listener` / `ssh-notify-handle-connection` client-side). Wired up by `modules/nixos/common/ssh-notify.nix`. | ## Adding a New Package diff --git a/pkgs/ssh-notify/default.nix b/pkgs/ssh-notify/default.nix new file mode 100644 index 0000000..b114040 --- /dev/null +++ b/pkgs/ssh-notify/default.nix @@ -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"; + }; +} diff --git a/pkgs/ssh-notify/notify-send.nu b/pkgs/ssh-notify/notify-send.nu new file mode 100644 index 0000000..307362c --- /dev/null +++ b/pkgs/ssh-notify/notify-send.nu @@ -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. + } +} diff --git a/pkgs/ssh-notify/ssh-notify-handle-connection.nu b/pkgs/ssh-notify/ssh-notify-handle-connection.nu new file mode 100644 index 0000000..9cf6432 --- /dev/null +++ b/pkgs/ssh-notify/ssh-notify-handle-connection.nu @@ -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. + } +} diff --git a/pkgs/ssh-notify/ssh-notify-listener.nu b/pkgs/ssh-notify/ssh-notify-listener.nu new file mode 100644 index 0000000..0d5068f --- /dev/null +++ b/pkgs/ssh-notify/ssh-notify-listener.nu @@ -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/ 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@" +}