From fcd9326cf953aaa1e2211f05a9f5abb958be7f7f Mon Sep 17 00:00:00 2001 From: Kristian Krsnik Date: Fri, 4 Aug 2023 23:10:47 +0200 Subject: [PATCH] updated nix modules --- dyn_gandi/main.py | 7 +---- nix/hm-module.nix | 43 ++++++++++++++++-------------- nix/module.nix | 66 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 30 deletions(-) diff --git a/dyn_gandi/main.py b/dyn_gandi/main.py index 82bd792..664dcf7 100644 --- a/dyn_gandi/main.py +++ b/dyn_gandi/main.py @@ -6,7 +6,7 @@ from datetime import datetime 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: with open(path, 'r') as file: # 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) -def anewfunction(): - for i, key in range(12): - print(i) - - def log(logline: str, path: str = './log.txt') -> None: # Appends logline at the end of the file file with a timestamp with open(path, 'a') as file: diff --git a/nix/hm-module.nix b/nix/hm-module.nix index 9672c42..ad13477 100644 --- a/nix/hm-module.nix +++ b/nix/hm-module.nix @@ -5,23 +5,24 @@ self: { ... }: let cfg = config.services.dyn-gandi; + package = self.packages.${pkgs.stdenv.hostPlatform.system}.default; inherit (lib) mkIf mkEnableOption mkOption types; in { options.services.dyn-gandi = { enable = mkEnableOption "dyn-gandi"; timer = lib.mkOption { - type = lib.types.int; - default = 0; + type = types.nullOr types.int; + default = null; description = lib.mdDoc '' The time intervall in seconds the script should be repeated. - 0 disables automatic starts. ''; }; settings = mkOption { type = with types; let valueType = nullOr (oneOf [ + # TODO: restrict type to actual config file structure bool int float @@ -32,33 +33,37 @@ in { ]); in valueType; - - default = {}; - description = ''''; + default = throw "Please specify services.dyn-gandi.settings"; }; }; 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" = { text = builtins.toJSON cfg.settings; }; - systemd.user.services.dyn-gandi = - mkIf cfg.timer - != 0 { - Unit = { - Description = "DNS Updater tool for Gandi.net"; - After = ["graphical-session-pre.target"]; - PartOf = ["graphical-session.target"]; - }; + systemd.user.services.dyn-gandi = mkIf (cfg.timer + != null) { + Unit = { + After = ["network.target"]; }; - systemd.user.timers.dyn-gandi = { - wantedBy = ["timers.target"]; - timerConfig = { + + Install = {WantedBy = ["default.target"];}; + + 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"; - OnUnitActiveSec = "${cfg.timer}s"; + OnUnitActiveSec = "${toString cfg.timer}s"; Unit = "dyn-gandi.service"; }; }; diff --git a/nix/module.nix b/nix/module.nix index 3d1933f..7c54312 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -1,18 +1,74 @@ -inputs: { +self: { config, lib, pkgs, + system, ... }: let cfg = config.services.dyn-gandi; - inherit (lib) mkIf mkEnableOption; - #package = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.default; + package = self.packages.${pkgs.stdenv.hostPlatform.system}.default; + inherit (lib) mkIf mkEnableOption mkOption types; in { 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 { - 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"; + }; + }; }; }