Create Nix module

This commit is contained in:
2024-11-14 19:34:27 +01:00
parent e367daf484
commit ff5dde09aa
3 changed files with 147 additions and 4 deletions

34
flake.lock generated
View File

@@ -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",

View File

@@ -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 {};
packages.x86_64-linux.default = self.packages.x86_64-linux.tauron-scrapper;
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
packages = rec {
tauron-scrapper = pkgs.callPackage ./package.nix {};
default = tauron-scrapper;
};
nixosModules.tauron-scrapper = pkgs.callPackage ./module.nix {inherit self;};
});
}

100
module.nix Normal file
View File

@@ -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')"
'';
};
};
}