dyn-gandi/nix/hm-module.nix

71 lines
1.6 KiB
Nix
Raw Normal View History

2023-08-01 19:59:54 +00:00
self: {
config,
lib,
pkgs,
...
}: let
2023-08-13 12:00:02 +00:00
cfg = config.dyn-gandi;
2023-08-04 21:10:47 +00:00
package = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
2023-08-01 19:59:54 +00:00
inherit (lib) mkIf mkEnableOption mkOption types;
format = pkgs.formats.json {};
configFile = format.generate "config.json" cfg.settings;
in {
2023-08-13 12:00:02 +00:00
options.dyn-gandi = {
2023-08-01 19:59:54 +00:00
enable = mkEnableOption "dyn-gandi";
2023-08-01 19:59:54 +00:00
timer = lib.mkOption {
2023-08-04 21:10:47 +00:00
type = types.nullOr types.int;
default = null;
2023-08-01 19:59:54 +00:00
description = lib.mdDoc ''
The time intervall in seconds the script should be repeated.
'';
};
settings = mkOption {
type = with types; let
valueType = nullOr (oneOf [
2023-08-04 21:10:47 +00:00
# TODO: restrict type to actual config file structure
2023-08-01 19:59:54 +00:00
bool
int
float
str
path
(attrsOf valueType)
(listOf valueType)
]);
in
valueType;
2023-08-13 12:00:02 +00:00
default = throw "Please specify dyn-gandi.settings";
};
};
2023-08-01 19:59:54 +00:00
config = mkIf cfg.enable {
2023-08-04 21:10:47 +00:00
home.packages = [package]; # TODO: make this architecture independent
2023-08-04 21:10:47 +00:00
systemd.user.services.dyn-gandi = mkIf (cfg.timer
!= null) {
Unit = {
After = ["network.target"];
};
Install = {WantedBy = ["default.target"];};
Service = {
Type = "oneshot";
ExecStart = "${package}/bin/dyn_gandi --config ${configFile}";
2023-08-01 19:59:54 +00:00
};
2023-08-04 21:10:47 +00:00
};
systemd.user.timers.dyn-gandi = mkIf (cfg.timer
!= null) {
Install = {WantedBy = ["timers.target"];};
Timer = {
2023-08-01 19:59:54 +00:00
OnBootSec = "0s";
2023-08-04 21:10:47 +00:00
OnUnitActiveSec = "${toString cfg.timer}s";
2023-08-01 19:59:54 +00:00
Unit = "dyn-gandi.service";
};
};
};
}