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 ++++++++++++++++++----------------- modules/nixos/personal/nix.nix | 9 +++-- 2 files changed, 40 insertions(+), 36 deletions(-) (limited to 'modules/nixos') 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) + ]; }; } diff --git a/modules/nixos/personal/nix.nix b/modules/nixos/personal/nix.nix index 8744e80..d1bd3cb 100644 --- a/modules/nixos/personal/nix.nix +++ b/modules/nixos/personal/nix.nix @@ -34,8 +34,11 @@ in { [ "--commit-lock-file" ] ++ pkgs.personal.lib.updateInputFlag "nixpkgs"; }; systemd.services = { - nix-gc.after = - lib.optional (cfg.autoUpgrade && cfg.gc.enable) "nixos-upgrade.service"; + nix-gc = { + after = lib.optional (cfg.autoUpgrade && cfg.gc.enable) + "nixos-upgrade.service"; + personal.monitor = true; + }; nix-gc-remove-dead-roots = { enable = cfg.gc.enable; description = "Remove dead symlinks in /nix/var/nix/gcroots"; @@ -43,9 +46,9 @@ in { script = "find /nix/var/nix/gcroots -xtype l -delete"; before = lib.mkIf config.nix.gc.automatic [ "nix-gc.service" ]; wantedBy = lib.mkIf config.nix.gc.automatic [ "nix-gc.service" ]; + personal.monitor = true; }; }; - personal.monitoring.services = [ "nixos-upgrade" "nix-gc" ]; programs.git = lib.mkIf (cfg.flake != null && lib.hasPrefix "git+file" cfg.flake) { enable = true; -- cgit v1.2.3