mirror of
https://git.sr.ht/~magic_rb/dotfiles
synced 2024-11-26 18:16:13 +01:00
aff0158ef7
Signed-off-by: magic_rb <magic_rb@redalder.org>
134 lines
2.5 KiB
Nix
134 lines
2.5 KiB
Nix
{
|
|
config,
|
|
pkgs,
|
|
lib,
|
|
...
|
|
}: let
|
|
cfg = config.services.influxdb2.provision;
|
|
|
|
inherit
|
|
(lib)
|
|
mkEnableOption
|
|
mkOption
|
|
types
|
|
mdDoc
|
|
flip
|
|
mapAttrsToList
|
|
getExe
|
|
mkIf
|
|
;
|
|
|
|
taskOptions = {...}: {
|
|
options = {
|
|
cron = mkOption {
|
|
type = with types; nullOr str;
|
|
default = null;
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
|
|
every = mkOption {
|
|
type = with types; nullOr str;
|
|
default = null;
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
|
|
fluxFile = mkOption {
|
|
type = types.path;
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
|
|
offset = mkOption {
|
|
type = types.str;
|
|
default = "0m";
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
};
|
|
};
|
|
|
|
tasksFile =
|
|
(pkgs.formats.json {}).generate "tasks.json"
|
|
(flip mapAttrsToList cfg.tasks (
|
|
name: value: {
|
|
inherit name;
|
|
flux_file = value.fluxFile;
|
|
inherit
|
|
(value)
|
|
every
|
|
cron
|
|
offset
|
|
;
|
|
}
|
|
));
|
|
in {
|
|
options = {
|
|
services.influxdb2.provision-magic = {
|
|
enable = mkEnableOption "Enable InfluxDB2 provisioning";
|
|
|
|
itpPackage = mkOption {
|
|
type = types.package;
|
|
default = pkgs.itp;
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
|
|
stateFile = mkOption {
|
|
type = types.str;
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
|
|
organization = mkOption {
|
|
type = types.str;
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
|
|
tasks = mkOption {
|
|
type = with types; attrsOf (submodule taskOptions);
|
|
default = {};
|
|
description =
|
|
mdDoc ''
|
|
'';
|
|
};
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
systemd.services.influxdb2-provision = {
|
|
after = ["influxdb2.service"];
|
|
wants = ["influxdb2.service"];
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
restartIfChanged = true;
|
|
|
|
script = ''
|
|
${getExe cfg.itpPackage} -s ${cfg.stateFile} -f ${tasksFile} -o ${cfg.organization}
|
|
'';
|
|
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
Restart = "on-failure";
|
|
RestartSec = 3;
|
|
};
|
|
};
|
|
|
|
assertions =
|
|
flip mapAttrsToList cfg.tasks
|
|
(n: v: {
|
|
assertion = (v.cron != null && v.every == null) || (v.cron == null && v.every != null);
|
|
message = "Exactly one of `services.influxdb2.provision.tasks.${n}.{cron, every}` must be non `null`";
|
|
});
|
|
};
|
|
}
|