refactor(systemd-notify): take it from nix-modules

This commit is contained in:
catvayor 2025-06-14 22:15:27 +02:00
parent e1699ba735
commit c1afcb7768
2 changed files with 50 additions and 1 deletions

View file

@ -0,0 +1,49 @@
# SPDX-FileCopyrightText: 2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>
#
# SPDX-License-Identifier: EUPL-1.2
{ config, lib, ... }:
let
inherit (lib)
mkEnableOption
mkIf
mkOption
mkForce
optional
;
inherit (lib.types) attrsOf str submodule;
cfg = config.services.systemd-notify;
in
{
options.services.systemd-notify = {
enable = mkEnableOption "notifications when a systemd unit fails.";
command = mkOption {
type = str;
description = ''
Command to run on failure of a systemd unit.
Takes the name of the failed unit as an argument.
'';
};
};
options.systemd.services = mkOption {
type = attrsOf (submodule {
config.onFailure = optional cfg.enable "email@%n.service";
});
};
config = mkIf cfg.enable {
systemd.services."email@" = {
description = "Sends a status mail via sendmail on service failures.";
onFailure = mkForce [ ]; # Avoid recursive failures
serviceConfig = {
ExecStart = "${cfg.command} %i";
Type = "oneshot";
};
};
};
}