diff --git a/flake.lock b/flake.lock index 9102d04..d7d724e 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,23 @@ { "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1731319897, @@ -18,8 +36,24 @@ }, "root": { "inputs": { + "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index f5b8945..a766c10 100644 --- a/flake.nix +++ b/flake.nix @@ -3,14 +3,23 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + flake-utils.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, nixpkgs, - }: { - packages.x86_64-linux.tauron-scrapper = nixpkgs.legacyPackages.x86_64-linux.callPackage ./package.nix {}; + flake-utils, + }: + flake-utils.lib.eachDefaultSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + in { + packages = rec { + tauron-scrapper = pkgs.callPackage ./package.nix {}; + default = tauron-scrapper; + }; - packages.x86_64-linux.default = self.packages.x86_64-linux.tauron-scrapper; - }; + nixosModules.tauron-scrapper = pkgs.callPackage ./module.nix {inherit self;}; + }); } diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..f87d0a4 --- /dev/null +++ b/module.nix @@ -0,0 +1,100 @@ +{ + 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 = { + usernamePath = "/run/tauron.username.key"; + passwordPath = "/run/tauron.password.key"; + point = "123456"; + }; + + consumers = { + influxdb = { + enable = true; + databaseURL = "https://influxdb.lan"; + apiToken = "/run/mqtt/influxdb.token.key"; + organization = "home"; + bucket = "tauron"; + }; + + mqtt = { + enable = true; + brokerURL = "https://mqtt.lan"; + usernamePath = "/run/mqtt.username.key"; + passwordPath = "/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')" + ''; + }; + }; +}