self: { config, pkgs, system, lib, ... }: with lib; let inherit (pkgs) coreutils-full formats; cfg = config.services.tauron-scrapper; yamlConfig = (formats.yaml {}).generate "tauron-scrapper-config.yaml" cfg.config; app = pkgs.writeShellApplication { name = "tauron-scrapper"; runtimeInputs = [self.packages.${system}.tauron-scrapper]; text = '' tauron-scrapper -c "${yamlConfig}" "$@"; ''; }; in { options.services.tauron-scrapper = { enable = mkEnableOption "tauron-scrapper"; cliArgs = mkOption { type = types.listOf types.str; description = "List of CLI arguments. Do not put '-c' argument here as it will be automatically appended basing on the 'config' option."; example = [ ''--date "$(date -d '5 days ago' '+%Y-%m-%d')"'' ''--to "$(date -d 'yesterday' '+%Y-%m-%d')"'' ]; default = [ ''--date "$(date -d '5 days ago' '+%Y-%m-%d')"'' ''--to "$(date -d 'yesterday' '+%Y-%m-%d')"'' ]; }; config = mkOption { type = types.attrs; 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"; cookiesJarPath = "/tmp/tauron-scrapper.cookies.json"; }; 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.attrs; 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 = "*-*-* 15:15"; RandomizedDelaySec = "2h"; Persistent = true; }; }; }; config = mkIf cfg.enable { environment.systemPackages = [app]; 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 = [app coreutils-full]; script = '' tauron-scrapper ${lib.concatStringsSep " " cfg.cliArgs}; ''; }; }; }