# The inference proxy, socket-activated: one service instance per # connection on `socket`, holding the API key as a systemd credential. # Used on a Linux host, and (with launchd in place of systemd) on macOS. { config, lib, pkgs, ... }: let cfg = config.services.hermes-llm-proxy; proxy = pkgs.writers.writePython3 "llm-proxy" { flakeIgnore = [ "E501" ]; } ( builtins.readFile ../llm-proxy.py ); in { options.services.hermes-llm-proxy = { enable = lib.mkEnableOption "the inference proxy for a confined Hermes"; socket = lib.mkOption { type = lib.types.str; description = "Where to listen."; }; socketUser = lib.mkOption { type = lib.types.str; description = "The one user who may connect."; }; upstream = lib.mkOption { type = lib.types.str; example = "https://openrouter.ai/api/v1"; description = "The OpenAI-compatible base URL to forward to."; }; keyFile = lib.mkOption { type = lib.types.str; description = "The API key, as a runtime path (not a nix path, which would copy it to the store)."; }; }; config = lib.mkIf cfg.enable { systemd.sockets.hermes-llm = { wantedBy = [ "sockets.target" ]; listenStreams = [ cfg.socket ]; socketConfig = { Accept = true; SocketUser = cfg.socketUser; SocketMode = "0600"; }; }; systemd.services."hermes-llm@" = { description = "Hermes inference proxy, one connection"; serviceConfig = { ExecStart = "${proxy} ${lib.escapeShellArg cfg.upstream} %d/llm-key"; StandardInput = "socket"; StandardOutput = "journal"; LoadCredential = [ "llm-key:${cfg.keyFile}" ]; DynamicUser = true; ProtectSystem = "strict"; ProtectHome = true; PrivateTmp = true; PrivateDevices = true; NoNewPrivileges = true; CapabilityBoundingSet = ""; RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ]; }; }; }; }