{ config, lib, pkgs, ... }: with lib; let cfg = config.skyg.nixos.server.services.signal-cli; in { options = { skyg.nixos.server.services.signal-cli = { enable = mkEnableOption "signal-cli daemon in HTTP mode (backs the Hermes Signal adapter)."; package = mkPackageOption pkgs "signal-cli" { }; host = mkOption { description = "Address the signal-cli HTTP daemon binds to."; default = "127.0.0.1"; example = "0.0.0.0"; type = types.str; }; port = mkOption { description = "TCP port the signal-cli HTTP daemon listens on."; default = 8080; type = types.port; }; configDir = mkOption { description = '' signal-cli config/state directory. The Signal account must be linked into this directory once (interactively) before the daemon can start: sudo signal-cli --config link -n "HermesAgent" ''; default = "/var/lib/signal-cli"; type = types.str; }; account = mkOption { description = '' Signal account number in E.164 format (e.g. "+15551234567"). When null, the daemon reads SIGNAL_ACCOUNT from environmentFile instead. ''; default = null; type = types.nullOr types.str; }; environmentFile = mkOption { description = '' Path to an EnvironmentFile (e.g. an agenix-decrypted secret) that provides SIGNAL_ACCOUNT. Used when account is null. Avoids putting the phone number in the world-readable Nix store. ''; default = null; type = types.nullOr types.path; }; openFirewall = mkOption { description = "Open the HTTP port in the firewall."; default = false; type = types.bool; }; }; }; config = mkIf cfg.enable { assertions = [{ assertion = cfg.account != null || cfg.environmentFile != null; message = "skyg.nixos.server.services.signal-cli: set either `account` or `environmentFile` (providing SIGNAL_ACCOUNT)."; }]; # Ensure the config/state directory exists (the account is linked into it # manually). 0700 since it holds Signal account credentials. systemd.tmpfiles.rules = [ "d ${cfg.configDir} 0700 root root - -" ]; systemd.services.signal-cli-daemon = { description = "signal-cli daemon (HTTP) for the Hermes Signal adapter"; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "simple"; EnvironmentFile = mkIf (cfg.environmentFile != null) cfg.environmentFile; ExecStart = let account = if cfg.account != null then cfg.account else "\${SIGNAL_ACCOUNT}"; in "${cfg.package}/bin/signal-cli --config ${cfg.configDir} --account ${account} daemon --http ${cfg.host}:${toString cfg.port}"; Restart = "on-failure"; RestartSec = 5; }; }; networking.firewall = mkIf cfg.openFirewall { allowedTCPPorts = [ cfg.port ]; }; }; }