{ config, coreutils-full, formats, system, utils, lib, self, ... }: with lib; let inherit (utils.systemdUtils.unitOptions) unitOption; cfg = config.services.tauron-scrapper; yamlConfig = (formats.yaml {}).generate "tauron-scrapper-config.yaml" cfg.config; in { options.services.tauron-scrapper = { enable = mkEnableOption "tauron-scrapper"; config = mkOption { type = types.attr; description = "The configuration of tauron-scrapper command"; example = { timezone = "Europe/Warsaw"; tauron = { username = "$_file:/run/tauron.username.key"; password = "$_file:/run/tauron.password.key"; point = "123456"; }; consumers = { influxdb = { enable = true; databaseURL = "https://influxdb.lan"; apiToken = "$__file:/run/mqtt/influxdb.token.key"; organization = "home"; bucket = "tauron"; }; mqtt = { enable = true; brokerURL = "https://mqtt.lan"; username = "$__file:/run/mqtt.username.key"; password = "$__file:/run/mqtt.password.key"; clientId = "tauron-scrapper"; prefix = "tauron"; publishOptions = { qos = 2; retain = true; }; }; }; }; }; timerConfig = mkOption { type = types.nullOr (types.attrsOf unitOption); default = { OnCalendar = "*-*-* 03:00"; Persistent = true; }; description = lib.mdDoc '' When to run the scrapping. See {manpage}`systemd.timer(5)` for details. If null no timer is created and the data will only be fetched when explicitly triggered. ''; example = { OnCalendar = "00:05"; Persistent = true; }; }; }; config = mkIf cfg.enable { systemd.timers.tauron-scrapper = { description = "Tauron Scapper"; wantedBy = ["timers.target"]; timerConfig = cfg.timerConfig // { Unit = "tauron-scrapper.service"; }; }; systemd.services.tauron-scrapper = { description = "Tauron Scrapper"; serviceConfig.Type = "oneshot"; path = [self.packages.${system}.tauron-scrapper coreutils-full]; script = '' tauron-scrapper -c ${yamlConfig} -d "$(date -d "yesterday" '+%Y-%m-%d')" ''; }; }; }