1 { config, pkgs, lib, ... }:
6 cfg = config.services.prometheus.alertmanager;
7 mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
9 checkedConfig = file: pkgs.runCommand "checked-config" { buildInputs = [ cfg.package ]; } ''
11 amtool check-config $out
15 yml = if cfg.configText != null then
16 pkgs.writeText "alertmanager.yml" cfg.configText
20 cmdlineArgs = cfg.extraFlags ++ [
21 "--config.file /tmp/alert-manager-substituted.yaml"
22 "--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
23 "--log.level ${cfg.logLevel}"
24 "--storage.path /var/lib/alertmanager"
25 (toString (map (peer: "--cluster.peer ${peer}:9094") cfg.clusterPeers))
26 ] ++ (optional (cfg.webExternalUrl != null)
27 "--web.external-url ${cfg.webExternalUrl}"
28 ) ++ (optional (cfg.logFormat != null)
29 "--log.format ${cfg.logFormat}"
33 (mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "user" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a user setting.")
34 (mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "group" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a group setting.")
35 (mkRemovedOptionModule [ "services" "prometheus" "alertmanagerURL" ] ''
36 Due to incompatibility, the alertmanagerURL option has been removed,
37 please use 'services.prometheus2.alertmanagers' instead.
42 services.prometheus.alertmanager = {
43 enable = mkEnableOption (lib.mdDoc "Prometheus Alertmanager");
47 default = pkgs.prometheus-alertmanager;
48 defaultText = literalExpression "pkgs.alertmanager";
49 description = lib.mdDoc ''
50 Package that should be used for alertmanager.
54 configuration = mkOption {
55 type = types.nullOr types.attrs;
57 description = lib.mdDoc ''
58 Alertmanager configuration as nix attribute set.
62 configText = mkOption {
63 type = types.nullOr types.lines;
65 description = lib.mdDoc ''
66 Alertmanager configuration as YAML text. If non-null, this option
67 defines the text that is written to alertmanager.yml. If null, the
68 contents of alertmanager.yml is generated from the structured config
73 logFormat = mkOption {
74 type = types.nullOr types.str;
76 description = lib.mdDoc ''
77 If set use a syslog logger or JSON logging.
82 type = types.enum ["debug" "info" "warn" "error" "fatal"];
84 description = lib.mdDoc ''
85 Only log messages with the given severity or above.
89 webExternalUrl = mkOption {
90 type = types.nullOr types.str;
92 description = lib.mdDoc ''
93 The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy).
94 Used for generating relative and absolute links back to Alertmanager itself.
95 If the URL has a path portion, it will be used to prefix all HTTP endoints served by Alertmanager.
96 If omitted, relevant URL components will be derived automatically.
100 listenAddress = mkOption {
103 description = lib.mdDoc ''
104 Address to listen on for the web interface and API. Empty string will listen on all interfaces.
105 "localhost" will listen on 127.0.0.1 (but not ::1).
112 description = lib.mdDoc ''
113 Port to listen on for the web interface and API.
117 openFirewall = mkOption {
120 description = lib.mdDoc ''
121 Open port in firewall for incoming connections.
125 clusterPeers = mkOption {
126 type = types.listOf types.str;
128 description = lib.mdDoc ''
129 Initial peers for HA cluster.
133 extraFlags = mkOption {
134 type = types.listOf types.str;
136 description = lib.mdDoc ''
137 Extra commandline options when launching the Alertmanager.
141 environmentFile = mkOption {
142 type = types.nullOr types.path;
144 example = "/root/alertmanager.env";
145 description = lib.mdDoc ''
146 File to load as environment file. Environment variables
147 from this file will be interpolated into the config file
148 using envsubst with this syntax:
149 `$ENVIRONMENT ''${VARIABLE}`
157 assertions = singleton {
158 assertion = cfg.configuration != null || cfg.configText != null;
159 message = "Can not enable alertmanager without a configuration. "
160 + "Set either the `configuration` or `configText` attribute.";
164 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
166 systemd.services.alertmanager = {
167 wantedBy = [ "multi-user.target" ];
168 after = [ "network-online.target" ];
170 ${lib.getBin pkgs.envsubst}/bin/envsubst -o "/tmp/alert-manager-substituted.yaml" \
171 -i "${alertmanagerYml}"
175 StateDirectory = "alertmanager";
176 DynamicUser = true; # implies PrivateTmp
177 EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
178 WorkingDirectory = "/tmp";
179 ExecStart = "${cfg.package}/bin/alertmanager" +
180 optionalString (length cmdlineArgs != 0) (" \\\n " +
181 concatStringsSep " \\\n " cmdlineArgs);
182 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";