From afcad6222a1e8bd6427e0ed1100bbd268de16475 Mon Sep 17 00:00:00 2001 From: "quentin@aristote.fr" Date: Wed, 9 Aug 2023 20:43:52 +0200 Subject: systemd: monitor: refactor options --- modules/nixos/personal/monitoring.nix | 67 ++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 33 deletions(-) (limited to 'modules/nixos/personal/monitoring.nix') diff --git a/modules/nixos/personal/monitoring.nix b/modules/nixos/personal/monitoring.nix index 8d72c3c..44eceb6 100644 --- a/modules/nixos/personal/monitoring.nix +++ b/modules/nixos/personal/monitoring.nix @@ -1,19 +1,17 @@ { config, lib, pkgs, ... }: -let cfg = config.personal.monitoring; -in { - options.personal.monitoring = { - enable = lib.mkEnableOption "e-mail monitoring"; - services = lib.mkOption { - type = with lib.types; listOf str; - default = [ ]; - description = "The list of services whose failure should be notified."; - }; +{ + options.systemd.services = lib.mkOption { + type = with lib.types; + attrsOf (submodule ({ name, ... }: { + personal.monitor = + lib.mkEnableOption "e-mail monitoring for the ${name} service"; + })); }; config = { programs.msmtp = { - enable = cfg.enable; + enable = true; accounts.default = { auth = true; tls = true; @@ -26,30 +24,33 @@ in { }; }; - systemd.services = lib.mkIf cfg.enable (lib.mkMerge ([{ - "notify@" = { - enable = true; - description = "Send the status of the %i service as an e-mail."; - serviceConfig = { - Type = "oneshot"; - ExecStart = let - netCfg = config.networking; - me = "${netCfg.hostName}.${netCfg.domain}"; - script = pkgs.writeScript "notify" '' - #!${pkgs.runtimeShell} - service="$1" - echo \ - "Subject: ${me}: service $service failed - Service $service failed on ${me}, with the following status: + systemd.services = lib.mkMerge [ + config.systemd.services + { + "notify@" = lib.mkDefault { + description = "Send the status of the %i service as an e-mail."; + serviceConfig = { + Type = "oneshot"; + ExecStart = let + netCfg = config.networking; + me = "${netCfg.hostName}.${netCfg.domain}"; + script = pkgs.writeScript "notify" '' + #!${pkgs.runtimeShell} + service="$1" + echo \ + "Subject: ${me}: service $service failed + Service $service failed on ${me}, with the following status: - $(systemctl status $service) - " | ${pkgs.msmtp}/bin/msmtp quentin@aristote.fr - ''; - in "${script} %i"; + $(systemctl status $service) + " | ${pkgs.msmtp}/bin/msmtp quentin@aristote.fr + ''; + in "${script} %i"; + }; }; - }; - }] ++ builtins.map - (service: { "${service}".onFailure = [ "notify@%i.service" ]; }) - cfg.services)); + } + (builtins.mapAttrs (_: value: { + onFailure = lib.optional value.personal.monitor "notify@%i.service"; + }) config.systemd.services) + ]; }; } -- cgit v1.2.3