From af14f123e1c6b04449f9c5b284043903ee99a82f Mon Sep 17 00:00:00 2001 From: ahuston-0 Date: Thu, 16 May 2024 11:39:50 -0400 Subject: [PATCH] migrate flake update service to submodules Signed-off-by: ahuston-0 --- modules/flake-update-service.nix | 92 ++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/modules/flake-update-service.nix b/modules/flake-update-service.nix index fb5c049..f84d17d 100644 --- a/modules/flake-update-service.nix +++ b/modules/flake-update-service.nix @@ -8,47 +8,56 @@ let cfg = config.services.autopull; - autopull-type = lib.types.submodule { - options = { - enable = lib.mkEnableOption "autopull repo"; + # autopull-type = lib.types.submodule { # + autopull-type = + with lib.types; + attrsOf ( + submodule ( + { name, ... }: + { + options = { + enable = lib.mkEnableOption "autopull repo"; - name = lib.mkOption { - type = lib.types.str; - default = config.module._args.name; - description = "A name for the service which needs to be pulled"; - }; + repo-name = lib.mkOption { + type = lib.types.str; + default = name; - path = lib.mkOption { - type = lib.types.path; - description = "Path that needs to be updated via git pull"; - }; + description = "A name for the service which needs to be pulled"; + }; - frequency = lib.mkOption { - type = lib.types.str; - description = "systemd-timer compatible time between pulls"; - default = "1h"; - }; + path = lib.mkOption { + type = lib.types.path; + description = "Path that needs to be updated via git pull"; + }; - ssh-key = lib.mkOption { - type = lib.types.str; - default = ""; - description = "ssh-key used to pull the repository"; - }; + frequency = lib.mkOption { + type = lib.types.str; + description = "systemd-timer compatible time between pulls"; + default = "1h"; + }; - triggers-rebuild = lib.mkOption { - type = lib.types.bool; - default = false; - description = "Whether or not the rebuild service should be triggered after pulling. Note that system.autoUpgrade must be pointed at the same directory as this service if you'd like to use this option."; - }; - }; - }; + ssh-key = lib.mkOption { + type = lib.types.str; + default = ""; + description = "ssh-key used to pull the repository"; + }; + + triggers-rebuild = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Whether or not the rebuild service should be triggered after pulling. Note that system.autoUpgrade must be pointed at the same directory as this service if you'd like to use this option."; + }; + }; + } + ) + ); in { options = { services.autopull = { enable = lib.mkEnableOption "autopull"; - repo = lib.mkOption { type = lib.types.attrsOf autopull-type; }; + repo = lib.mkOption { type = autopull-type; }; }; }; @@ -59,45 +68,46 @@ in lib.mkIf cfg.enable { environment.systemPackages = [ pkgs.git ] - ++ lib.optionals (lib.any (ssh-key: ssh-key != "") (lib.mapGetAttr "ssh-key" repos)) [ + ++ lib.optionals (lib.any (ssh-key: ssh-key != "") (lib.rad-dev.mapGetAttr "ssh-key" repos)) [ pkgs.openssh ]; systemd.services = lib.mapAttrs' ( repo: { - name, + repo-name, ssh-key, + path, triggers-rebuild, ... }: - lib.nameValuePair "autopull@${name}" { + lib.nameValuePair "autopull@${repo-name}" { requires = [ "multi-user.target" ]; wants = lib.optionals (triggers-rebuild) [ "nixos-service.service" ]; after = [ "multi-user.target" ]; before = lib.optionals (triggers-rebuild) [ "nixos-upgrade.service" ]; - description = "Pull the latest data for ${name}"; + description = "Pull the latest data for ${repo-name}"; environment = lib.mkIf (ssh-key != "") { GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${ssh-key} -o IdentitiesOnly=yes"; }; serviceConfig = { Type = "oneshot"; User = "root"; - WorkingDirectory = cfg.path; + WorkingDirectory = path; ExecStart = "${pkgs.git}/bin/git pull --all"; }; } ) repos; - systemd.timers."autopull@${cfg.name}" = lib.mapAttrs' ( + systemd.timers = lib.mapAttrs' ( repo: - { name, frequency, ... }: - lib.nameValuePair "autopull@${name}" { + { repo-name, frequency, ... }: + lib.nameValuePair "autopull@${repo-name}" { wantedBy = [ "timers.target" ]; timerConfig = { - OnBootSec = cfg.frequency; - OnUnitActiveSec = cfg.frequency; - Unit = "autopull@${cfg.name}.service"; + OnBootSec = frequency; + OnUnitActiveSec = frequency; + Unit = "autopull@${repo-name}.service"; }; } ) repos;