{ config, pkgs, lib, ... }: # pass-broker: Proton Pass items for darman's processes (Claude included), # each gated by a tap on Telegram. The daemon runs as its own user and holds # the host's only pass-cli session, an agent PAT scoped to the homelab vaults, # so nothing running as darman can read the session itself. Client: # `pass-broker get --item --reason <why>`; scripts/deploy uses it. # # Telegram, not a desktop dialog: any process of darman's can fake Wayland # input and click its own approval. A bot has one getUpdates consumer, so # either this broker owns a dedicated bot (`telegram`) or it asks the shared # gateway on mars (`gateway`, pass-broker-gateway.nix), which owns the one # bot for every host. let cfg = config.homelab.passBroker; pass-broker = pkgs.callPackage ../../pkgs/pass-broker.nix { }; stateDir = "/var/lib/pass-broker"; configFile = pkgs.writeText "pass-broker.json" (builtins.toJSON { host = config.networking.hostName; default_vault = cfg.vault; allowed_users = cfg.users; pass_cli = "${cfg.passCli}/bin/pass-cli"; session_dir = "${stateDir}/session"; pat_file = cfg.patFile; approval_timeout_secs = cfg.approvalTimeout; approval = if cfg.gateway != null then { gateway = { inherit (cfg.gateway) url; token_file = cfg.gateway.tokenFile; }; } else { telegram = { api_base = cfg.telegram.apiBase; token_file = cfg.telegram.tokenFile; chat_id_file = cfg.telegram.chatIdFile; }; }; rules = map (r: { inherit (r) vault item approval; grant_minutes = r.grantMinutes; }) cfg.rules; }); in { options.homelab.passBroker = { enable = lib.mkEnableOption "Telegram-approved Proton Pass broker"; passCli = lib.mkOption { type = lib.types.package; default = pkgs.proton-pass-cli; defaultText = lib.literalExpression "pkgs.proton-pass-cli"; description = "Package providing bin/pass-cli."; }; patFile = lib.mkOption { type = lib.types.path; description = "Agent personal access token (pst_…::…), readable by pass-broker."; }; vault = lib.mkOption { type = lib.types.str; default = "HomeLab"; description = "Vault used when a request names none."; }; users = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ "darman" ]; description = "Users allowed on the socket, besides root."; }; approvalTimeout = lib.mkOption { type = lib.types.int; default = 120; description = "Seconds to wait for a tap before refusing."; }; telegram = lib.mkOption { default = null; description = "Prompt via this broker's own bot. Exactly one of telegram/gateway."; type = lib.types.nullOr (lib.types.submodule { options = { apiBase = lib.mkOption { type = lib.types.str; default = "https://api.telegram.org"; }; tokenFile = lib.mkOption { type = lib.types.path; description = "Bot token of a bot nothing else polls."; }; chatIdFile = lib.mkOption { type = lib.types.path; description = "Your Telegram user id; only its taps count."; }; }; }); }; gateway = lib.mkOption { default = null; description = "Prompt via the shared gateway. Exactly one of telegram/gateway."; type = lib.types.nullOr (lib.types.submodule { options = { url = lib.mkOption { type = lib.types.str; default = "http://mars.orbit.sol:8097"; }; tokenFile = lib.mkOption { type = lib.types.path; description = '' This host's gateway token; its sha256 goes in the gateway's homelab.passBrokerGateway.hosts.<host>.tokenSha256. ''; }; }; }); }; rules = lib.mkOption { description = "First match wins; an item no rule matches is refused."; default = [ { item = "darman@*"; } { item = "root@*"; } ]; type = lib.types.listOf (lib.types.submodule { options = { vault = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; description = "null = the default vault."; }; item = lib.mkOption { type = lib.types.str; description = "Item title; `*` matches anything."; }; approval = lib.mkOption { type = lib.types.enum [ "auto" "ask" "deny" ]; default = "ask"; }; grantMinutes = lib.mkOption { type = lib.types.int; default = 10; description = "How long one approval covers the item; 0 = this request only."; }; }; }); }; }; config = lib.mkIf cfg.enable { assertions = [{ assertion = (cfg.telegram == null) != (cfg.gateway == null); message = "homelab.passBroker: set exactly one of telegram or gateway."; }]; environment.systemPackages = [ pass-broker ]; users.users.pass-broker = { isSystemUser = true; group = "pass-broker"; home = stateDir; }; users.groups.pass-broker = { }; # World-writable on purpose: the daemon checks the peer uid, so adding a # client needs no group membership and no re-login. systemd.sockets.pass-broker = { wantedBy = [ "sockets.target" ]; socketConfig = { ListenStream = "/run/pass-broker.sock"; SocketMode = "0666"; }; }; systemd.services.pass-broker = { description = "Proton Pass broker (Telegram-approved)"; requires = [ "pass-broker.socket" ]; wants = [ "network-online.target" ]; after = [ "network-online.target" ]; environment = { PASS_BROKER_CONFIG = configFile; HOME = stateDir; # No keyring for a system user; the session key sits in the 0700 state dir. PROTON_PASS_KEY_PROVIDER = "fs"; PROTON_PASS_DISABLE_TELEMETRY = "1"; PROTON_PASS_NO_UPDATE_CHECK = "1"; }; serviceConfig = { ExecStart = "${pass-broker}/bin/pass-broker daemon"; User = "pass-broker"; Group = "pass-broker"; StateDirectory = "pass-broker"; StateDirectoryMode = "0700"; UMask = "0077"; Restart = "on-failure"; RestartSec = 5; NoNewPrivileges = true; ProtectSystem = "strict"; ProtectHome = true; PrivateTmp = true; PrivateDevices = true; ProtectKernelTunables = true; ProtectKernelModules = true; ProtectControlGroups = true; RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ]; RestrictNamespaces = true; LockPersonality = true; SystemCallArchitectures = "native"; }; }; }; }