Files
nix-config/common/ntfy/ssh-login.nix
Zuckerberg 1dd1b420d5
All checks were successful
Check Flake / check-flake (push) Successful in 3m34s
Add ntfy ssh login alerts. Include systemd service logs with service errors
2026-02-26 21:40:51 -08:00

37 lines
981 B
Nix

{ config, lib, pkgs, ... }:
let
cfg = config.ntfy-alerts;
notifyScript = pkgs.writeShellScript "ssh-login-notify" ''
# Only notify on session open, not close
[ "$PAM_TYPE" = "open_session" ] || exit 0
. /run/agenix/ntfy-token
# Send notification in background so login isn't delayed
${lib.getExe pkgs.curl} \
--fail --silent --show-error \
--max-time 10 --retry 1 \
${cfg.curlExtraArgs} \
-H "Authorization: Bearer $NTFY_TOKEN" \
-H "Title: SSH login on ${config.networking.hostName}" \
-H "Tags: key" \
-d "$PAM_USER from $PAM_RHOST at $(date +%c)" \
"${cfg.serverUrl}/ssh-logins" &
'';
in
{
config = lib.mkIf config.thisMachine.hasRole."ntfy" {
security.pam.services.sshd.rules.session.ntfy-login = {
order = 99999;
control = "optional";
modulePath = "${pkgs.pam}/lib/security/pam_exec.so";
args = [
"quiet"
(toString notifyScript)
];
};
};
}