{ config, pkgs, lib, ... }: # Voice loop for luna: SUPER+V opens the mic, talk, hear her answer. # # mic -> whisper-cpp (local, vulkan) -> Hermes' OpenAI-compatible API on mars # -> ElevenLabs streaming TTS -> speaker. State, amplitude, captions and a # viseme timeline are published on a unix socket; the rail's LunaPanel reads # them (dotfiles/quickshell/HyprChrome/Widgets/Bar/Luna), and the viseme # stream is there for the avatar that replaces its meter later. # # A USER service, not a system one: it needs darman's pipewire session and his # XDG_RUNTIME_DIR for the socket. That is also why the two secrets it reads # are owner-darman in hosts/terra/secrets.nix — a user manager cannot read a # root-owned 0400 file, and LoadCredential does not bridge that gap. # # STT is LOCAL on purpose: mars already runs faster-whisper for Telegram voice # notes, but routing the mic through it would put every spoken word on the # network for no latency win. This is the client-direct path Hermes itself # documents. let cfg = config.homelab.lunaVoice; luna-voice = pkgs.callPackage ../../pkgs/luna-voice.nix { }; # Pinned like the KittenTTS models on mars: a model that re-downloads at # first use is a service that fails on a fresh install with no network. whisperModel = pkgs.fetchurl { url = "https://huggingface.co/ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-base.en.bin"; sha256 = "sha256-oDd5yG3zMjB19eeWyyzlAp8A7Ihp7uP9+4l6/jbG0AI="; }; in { options.homelab.lunaVoice = { enable = lib.mkEnableOption "luna voice daemon"; user = lib.mkOption { type = lib.types.str; default = "darman"; description = "User whose session runs the daemon."; }; hermesUrl = lib.mkOption { type = lib.types.str; default = "http://mars.orbit.sol:8642"; description = "Base URL of luna's Hermes API server (tailnet-only)."; }; model = lib.mkOption { type = lib.types.str; default = "hermes-agent"; description = "Model name to request; Hermes routes it to her configured provider."; }; voiceId = lib.mkOption { type = lib.types.str; description = "ElevenLabs voice id — luna's voice."; }; ttsModel = lib.mkOption { type = lib.types.str; default = "eleven_v3_conversational"; description = '' eleven_v3* streams over the Text to Dialogue socket and performs audio tags ([laughs], [sighs]); it also gets her German right, which Flash reads with an English accent. Anything else uses the TTS socket, e.g. "eleven_flash_v2_5" — half the price per character, faster, no tags. ''; }; hermesKeyFile = lib.mkOption { type = lib.types.path; description = "File holding the API_SERVER_KEY bearer token."; }; elevenLabsKeyFile = lib.mkOption { type = lib.types.path; description = "File holding the ElevenLabs API key."; }; # Mic tuning. Defaults are set for terra's AIR 192|4, which is QUIET: # ~0.002 ambient against ~0.0045 for normal speech. Raising the gain on # the interface is worth more than any value here — everything downstream, # whisper included, works better with headroom. inputDevice = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; description = "Substring of the input device name; null uses the system default."; }; vadMult = lib.mkOption { type = lib.types.float; default = 1.8; description = "Speech triggers above noise_floor * this."; }; vadFloor = lib.mkOption { type = lib.types.float; default = 0.0025; description = "Absolute lower bound on the trigger, whatever the floor says."; }; silenceMs = lib.mkOption { type = lib.types.int; default = 2000; description = "Silence that ends an utterance."; }; voiceMarker = lib.mkOption { type = lib.types.str; default = "[voice]"; description = '' Prefixed to every spoken message so luna can tell this channel from Telegram or the dashboard, and adjust — no kaomoji, no markdown, short answers. Put the rule in HER profile on mars rather than only in systemPrompt below: the marker travels with the message, so it still identifies the channel in whatever history Hermes keeps, and one place governs her voice manners across every client that sends it. Empty string disables the marker. ''; }; systemPrompt = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; description = '' Prepended to the conversation. Worth setting to something like "You are being spoken to out loud; keep replies short" — her Telegram register is too long-form to listen to. ''; }; }; config = lib.mkIf cfg.enable { environment.systemPackages = [ luna-voice ]; systemd.user.services.luna-voiced = { description = "luna voice loop"; wantedBy = [ "graphical-session.target" ]; after = [ "graphical-session.target" "pipewire.service" ]; partOf = [ "graphical-session.target" ]; environment = { LUNA_HERMES_URL = cfg.hermesUrl; LUNA_MODEL = cfg.model; LUNA_VOICE_ID = cfg.voiceId; LUNA_TTS_MODEL = cfg.ttsModel; LUNA_HERMES_KEY_FILE = toString cfg.hermesKeyFile; LUNA_ELEVENLABS_KEY_FILE = toString cfg.elevenLabsKeyFile; LUNA_WHISPER_BIN = "${pkgs.whisper-cpp-vulkan}/bin/whisper-cli"; LUNA_WHISPER_MODEL = whisperModel; LUNA_VAD_MULT = toString cfg.vadMult; LUNA_VAD_FLOOR = toString cfg.vadFloor; LUNA_SILENCE_MS = toString cfg.silenceMs; LUNA_VOICE_MARKER = cfg.voiceMarker; } // lib.optionalAttrs (cfg.systemPrompt != null) { LUNA_SYSTEM_PROMPT = cfg.systemPrompt; } // lib.optionalAttrs (cfg.inputDevice != null) { LUNA_INPUT_DEVICE = cfg.inputDevice; }; serviceConfig = { ExecStart = "${luna-voice}/bin/luna-voice daemon"; Restart = "on-failure"; RestartSec = 3; }; }; }; }