nixos/services.prometheus.alertmanager: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-30 00:47:01 +02:00
parent 951787fba3
commit ea4bd53274

View File

@ -1,7 +1,4 @@
{ config, pkgs, lib, ... }: { config, pkgs, lib, ... }:
with lib;
let let
cfg = config.services.prometheus.alertmanager; cfg = config.services.prometheus.alertmanager;
mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration); mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
@ -25,16 +22,16 @@ let
"--log.level ${cfg.logLevel}" "--log.level ${cfg.logLevel}"
"--storage.path /var/lib/alertmanager" "--storage.path /var/lib/alertmanager"
(toString (map (peer: "--cluster.peer ${peer}:9094") cfg.clusterPeers)) (toString (map (peer: "--cluster.peer ${peer}:9094") cfg.clusterPeers))
] ++ (optional (cfg.webExternalUrl != null) ] ++ (lib.optional (cfg.webExternalUrl != null)
"--web.external-url ${cfg.webExternalUrl}" "--web.external-url ${cfg.webExternalUrl}"
) ++ (optional (cfg.logFormat != null) ) ++ (lib.optional (cfg.logFormat != null)
"--log.format ${cfg.logFormat}" "--log.format ${cfg.logFormat}"
); );
in { in {
imports = [ imports = [
(mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "user" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a user setting.") (lib.mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "user" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a user setting.")
(mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "group" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a group setting.") (lib.mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "group" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a group setting.")
(mkRemovedOptionModule [ "services" "prometheus" "alertmanagerURL" ] '' (lib.mkRemovedOptionModule [ "services" "prometheus" "alertmanagerURL" ] ''
Due to incompatibility, the alertmanagerURL option has been removed, Due to incompatibility, the alertmanagerURL option has been removed,
please use 'services.prometheus.alertmanagers' instead. please use 'services.prometheus.alertmanagers' instead.
'') '')
@ -42,12 +39,12 @@ in {
options = { options = {
services.prometheus.alertmanager = { services.prometheus.alertmanager = {
enable = mkEnableOption "Prometheus Alertmanager"; enable = lib.mkEnableOption "Prometheus Alertmanager";
package = mkPackageOption pkgs "prometheus-alertmanager" { }; package = lib.mkPackageOption pkgs "prometheus-alertmanager" { };
configuration = mkOption { configuration = lib.mkOption {
type = types.nullOr types.attrs; type = lib.types.nullOr lib.types.attrs;
default = null; default = null;
description = '' description = ''
Alertmanager configuration as nix attribute set. Alertmanager configuration as nix attribute set.
@ -57,8 +54,8 @@ in {
''; '';
}; };
configText = mkOption { configText = lib.mkOption {
type = types.nullOr types.lines; type = lib.types.nullOr lib.types.lines;
default = null; default = null;
description = '' description = ''
Alertmanager configuration as YAML text. If non-null, this option Alertmanager configuration as YAML text. If non-null, this option
@ -71,8 +68,8 @@ in {
''; '';
}; };
checkConfig = mkOption { checkConfig = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = true; default = true;
description = '' description = ''
Check configuration with `amtool check-config`. The call to `amtool` is Check configuration with `amtool check-config`. The call to `amtool` is
@ -85,24 +82,24 @@ in {
''; '';
}; };
logFormat = mkOption { logFormat = lib.mkOption {
type = types.nullOr types.str; type = lib.types.nullOr lib.types.str;
default = null; default = null;
description = '' description = ''
If set use a syslog logger or JSON logging. If set use a syslog logger or JSON logging.
''; '';
}; };
logLevel = mkOption { logLevel = lib.mkOption {
type = types.enum ["debug" "info" "warn" "error" "fatal"]; type = lib.types.enum ["debug" "info" "warn" "error" "fatal"];
default = "warn"; default = "warn";
description = '' description = ''
Only log messages with the given severity or above. Only log messages with the given severity or above.
''; '';
}; };
webExternalUrl = mkOption { webExternalUrl = lib.mkOption {
type = types.nullOr types.str; type = lib.types.nullOr lib.types.str;
default = null; default = null;
description = '' description = ''
The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy). The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy).
@ -112,8 +109,8 @@ in {
''; '';
}; };
listenAddress = mkOption { listenAddress = lib.mkOption {
type = types.str; type = lib.types.str;
default = ""; default = "";
description = '' description = ''
Address to listen on for the web interface and API. Empty string will listen on all interfaces. Address to listen on for the web interface and API. Empty string will listen on all interfaces.
@ -121,40 +118,40 @@ in {
''; '';
}; };
port = mkOption { port = lib.mkOption {
type = types.port; type = lib.types.port;
default = 9093; default = 9093;
description = '' description = ''
Port to listen on for the web interface and API. Port to listen on for the web interface and API.
''; '';
}; };
openFirewall = mkOption { openFirewall = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Open port in firewall for incoming connections. Open port in firewall for incoming connections.
''; '';
}; };
clusterPeers = mkOption { clusterPeers = lib.mkOption {
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
default = []; default = [];
description = '' description = ''
Initial peers for HA cluster. Initial peers for HA cluster.
''; '';
}; };
extraFlags = mkOption { extraFlags = lib.mkOption {
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
default = []; default = [];
description = '' description = ''
Extra commandline options when launching the Alertmanager. Extra commandline options when launching the Alertmanager.
''; '';
}; };
environmentFile = mkOption { environmentFile = lib.mkOption {
type = types.nullOr types.path; type = lib.types.nullOr lib.types.path;
default = null; default = null;
example = "/root/alertmanager.env"; example = "/root/alertmanager.env";
description = '' description = ''
@ -167,16 +164,16 @@ in {
}; };
}; };
config = mkMerge [ config = lib.mkMerge [
(mkIf cfg.enable { (lib.mkIf cfg.enable {
assertions = singleton { assertions = lib.singleton {
assertion = cfg.configuration != null || cfg.configText != null; assertion = cfg.configuration != null || cfg.configText != null;
message = "Can not enable alertmanager without a configuration. " message = "Can not enable alertmanager without a configuration. "
+ "Set either the `configuration` or `configText` attribute."; + "Set either the `configuration` or `configText` attribute.";
}; };
}) })
(mkIf cfg.enable { (lib.mkIf cfg.enable {
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port; networking.firewall.allowedTCPPorts = lib.optional cfg.openFirewall cfg.port;
systemd.services.alertmanager = { systemd.services.alertmanager = {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
@ -188,8 +185,8 @@ in {
''; '';
serviceConfig = { serviceConfig = {
ExecStart = "${cfg.package}/bin/alertmanager" + ExecStart = "${cfg.package}/bin/alertmanager" +
optionalString (length cmdlineArgs != 0) (" \\\n " + lib.optionalString (lib.length cmdlineArgs != 0) (" \\\n " +
concatStringsSep " \\\n " cmdlineArgs); lib.concatStringsSep " \\\n " cmdlineArgs);
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile; EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;