dotfiles/nixos/modules/influx-provisioning.nix

120 lines
2.5 KiB
Nix
Raw Normal View History

{ 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 = {
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`";
});
};
}