@@ -0,0 +1,116 @@
1+{ config, pkgs, lib, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.prometheus.alertmanager;
7+ mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
8+in {
9+ options = {
10+ services.prometheus.alertmanager = {
11+ enable = mkEnableOption "Prometheus Alertmanager";
12+
13+ user = mkOption {
14+ type = types.str;
15+ default = "nobody";
16+ description = ''
17+ User name under which Alertmanager shall be run.
18+ '';
19+ };
20+
21+ group = mkOption {
22+ type = types.str;
23+ default = "nogroup";
24+ description = ''
25+ Group under which Alertmanager shall be run.
26+ '';
27+ };
28+
29+ configuration = mkOption {
30+ type = types.attrs;
31+ default = {};
32+ description = ''
33+ Alertmanager configuration as nix attribute set.
34+ '';
35+ };
36+
37+ logFormat = mkOption {
38+ type = types.nullOr types.str;
39+ default = null;
40+ description = ''
41+ If set use a syslog logger or JSON logging.
42+ '';
43+ };
44+
45+ logLevel = mkOption {
46+ type = types.enum ["debug" "info" "warn" "error" "fatal"];
47+ default = "warn";
48+ description = ''
49+ Only log messages with the given severity or above.
50+ '';
51+ };
52+
53+ webExternalUrl = mkOption {
54+ type = types.nullOr types.str;
55+ default = null;
56+ description = ''
57+ The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy).
58+ Used for generating relative and absolute links back to Alertmanager itself.
59+ If the URL has a path portion, it will be used to prefix all HTTP endoints served by Alertmanager.
60+ If omitted, relevant URL components will be derived automatically.
61+ '';
62+ };
63+
64+ listenAddress = mkOption {
65+ type = types.nullOr types.str;
66+ default = null;
67+ description = ''
68+ Address to listen on for the web interface and API.
69+ '';
70+ };
71+
72+ port = mkOption {
73+ type = types.int;
74+ default = 9093;
75+ description = ''
76+ Port to listen on for the web interface and API.
77+ '';
78+ };
79+
80+ openFirewall = mkOption {
81+ type = types.bool;
82+ default = false;
83+ description = ''
84+ Open port in firewall for incoming connections.
85+ '';
86+ };
87+ };
88+ };
89+
90+
91+ config = mkIf cfg.enable {
92+ networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
93+
94+ systemd.services.alertmanager = {
95+ wantedBy = [ "multi-user.target" ];
96+ after = [ "network.target" ];
97+ script = ''
98+ ${pkgs.prometheus-alertmanager.bin}/bin/alertmanager \
99+ -config.file ${mkConfigFile} \
100+ -web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
101+ -log.level ${cfg.logLevel} \
102+ ${optionalString (cfg.webExternalUrl != null) ''-web.external-url ${cfg.webExternalUrl} \''}
103+ ${optionalString (cfg.logFormat != null) "-log.format ${cfg.logFormat}"}
104+ '';
105+
106+ serviceConfig = {
107+ User = cfg.user;
108+ Group = cfg.group;
109+ Restart = "always";
110+ PrivateTmp = true;
111+ WorkingDirectory = "/tmp";
112+ ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
113+ };
114+ };
115+ };
116+}