updated nix modules

This commit is contained in:
Kristian Krsnik 2023-08-04 23:10:47 +02:00
parent e4f94e04a6
commit fcd9326cf9
3 changed files with 86 additions and 30 deletions

View File

@ -6,7 +6,7 @@ from datetime import datetime
def loadSettings(path: str = f"{os.path.expanduser('~')}/.config/dyn-gandi/config.json") -> dict: def loadSettings(path: str = f"{os.path.expanduser('~')}/.config/dyn-gandi/config.json") -> dict:
# TODO: check integrity # TODO: check integrity of the config file
try: try:
with open(path, 'r') as file: with open(path, 'r') as file:
# Read if the API keys are path to files # Read if the API keys are path to files
@ -26,11 +26,6 @@ def loadSettings(path: str = f"{os.path.expanduser('~')}/.config/dyn-gandi/confi
# TODO log error and remove code below (do not create an unwanted config) # TODO log error and remove code below (do not create an unwanted config)
def anewfunction():
for i, key in range(12):
print(i)
def log(logline: str, path: str = './log.txt') -> None: def log(logline: str, path: str = './log.txt') -> None:
# Appends logline at the end of the file file with a timestamp # Appends logline at the end of the file file with a timestamp
with open(path, 'a') as file: with open(path, 'a') as file:

View File

@ -5,23 +5,24 @@ self: {
... ...
}: let }: let
cfg = config.services.dyn-gandi; cfg = config.services.dyn-gandi;
package = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
inherit (lib) mkIf mkEnableOption mkOption types; inherit (lib) mkIf mkEnableOption mkOption types;
in { in {
options.services.dyn-gandi = { options.services.dyn-gandi = {
enable = mkEnableOption "dyn-gandi"; enable = mkEnableOption "dyn-gandi";
timer = lib.mkOption { timer = lib.mkOption {
type = lib.types.int; type = types.nullOr types.int;
default = 0; default = null;
description = lib.mdDoc '' description = lib.mdDoc ''
The time intervall in seconds the script should be repeated. The time intervall in seconds the script should be repeated.
0 disables automatic starts.
''; '';
}; };
settings = mkOption { settings = mkOption {
type = with types; let type = with types; let
valueType = nullOr (oneOf [ valueType = nullOr (oneOf [
# TODO: restrict type to actual config file structure
bool bool
int int
float float
@ -32,33 +33,37 @@ in {
]); ]);
in in
valueType; valueType;
default = throw "Please specify services.dyn-gandi.settings";
default = {};
description = '''';
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
home.packages = [self.packages.x86_64-linux.default]; home.packages = [package]; # TODO: make this architecture independent
xdg.configFile."dyn-gandi/config.json" = { xdg.configFile."dyn-gandi/config.json" = {
text = builtins.toJSON cfg.settings; text = builtins.toJSON cfg.settings;
}; };
systemd.user.services.dyn-gandi = systemd.user.services.dyn-gandi = mkIf (cfg.timer
mkIf cfg.timer != null) {
!= 0 { Unit = {
Unit = { After = ["network.target"];
Description = "DNS Updater tool for Gandi.net";
After = ["graphical-session-pre.target"];
PartOf = ["graphical-session.target"];
};
}; };
systemd.user.timers.dyn-gandi = {
wantedBy = ["timers.target"]; Install = {WantedBy = ["default.target"];};
timerConfig = {
Service = {
Type = "oneshot";
ExecStart = "${package}/bin/dyn_gandi"; # TODO: add config file via command line options
};
};
systemd.user.timers.dyn-gandi = mkIf (cfg.timer
!= null) {
Install = {WantedBy = ["timers.target"];};
Timer = {
OnBootSec = "0s"; OnBootSec = "0s";
OnUnitActiveSec = "${cfg.timer}s"; OnUnitActiveSec = "${toString cfg.timer}s";
Unit = "dyn-gandi.service"; Unit = "dyn-gandi.service";
}; };
}; };

View File

@ -1,18 +1,74 @@
inputs: { self: {
config, config,
lib, lib,
pkgs, pkgs,
system,
... ...
}: let }: let
cfg = config.services.dyn-gandi; cfg = config.services.dyn-gandi;
inherit (lib) mkIf mkEnableOption; package = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
#package = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.default; inherit (lib) mkIf mkEnableOption mkOption types;
in { in {
options.services.dyn-gandi = { options.services.dyn-gandi = {
enable = mkEnableOption null; enable = mkEnableOption "dyn-gandi";
timer = lib.mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The time intervall in seconds the script should be repeated.
'';
};
settings = mkOption {
type = with types; let
valueType = nullOr (oneOf [
# TODO: restrict type to actual config file structure
bool
int
float
str
path
(attrsOf valueType)
(listOf valueType)
]);
in
valueType;
default = throw "Please specify services.dyn-gandi.settings";
};
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.systemPackages = [cfg.package]; environment = {
systemPackages = [package];
}; # TODO: make this architecture independent
xdg.configFile."dyn-gandi/config.json" = {
text = builtins.toJSON cfg.settings;
};
systemd.services.dyn-gandi = mkIf (cfg.timer
!= null) {
Unit = {
After = ["network.target"];
};
Install = {WantedBy = ["default.target"];};
Service = {
Type = "oneshot";
ExecStart = "${package}/bin/dyn_gandi"; # TODO: add config file via command line options
};
};
systemd.timers.dyn-gandi = mkIf (cfg.timer
!= null) {
Install = {WantedBy = ["timers.target"];};
Timer = {
OnBootSec = "0s";
OnUnitActiveSec = "${toString cfg.timer}s";
Unit = "dyn-gandi.service";
};
};
}; };
} }