outage-detector/nix/module.nix
2023-10-04 19:11:55 +02:00

62 lines
1.4 KiB
Nix

inputs: {
config,
lib,
pkgs,
system,
...
}: let
cfg = config.outage-detector;
package = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.default;
inherit (lib) mkIf mkEnableOption mkOption types;
in {
options.outage-detector = {
enable = mkEnableOption "outage-detector";
timer = lib.mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The time intervall in seconds the script should be repeated.
'';
};
settings = mkOption {
type = with types; attrsOf (either str int);
description = lib.mdDoc ''
The settings for the outage-detector script.
Example:
{
logFile = "/home/user/outage-detector.log";
host = "1.1.1.1";
timeout = 2;
};
'';
};
};
config = mkIf cfg.enable {
environment.systemPackages = [package];
systemd.services.outage-detector = mkIf (cfg.timer
!= null) {
script = "${package}/bin/outage_detector log ${cfg.settings.logFile} --host ${cfg.settings.host} --timeout ${toString cfg.settings.timeout}";
serviceConfig = {
Type = "oneshot";
};
};
systemd.timers.outage-detector = mkIf (cfg.timer
!= null) {
wantedBy = ["timers.target"];
timerConfig = {
OnBootSec = "0s";
OnUnitActiveSec = "${toString cfg.timer}s";
Unit = "outage-detector.service";
};
};
};
}