migrate flake update service to submodules

Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
This commit is contained in:
ahuston-0 2024-05-16 11:39:50 -04:00 committed by Alice Huston
parent 5261a65ecc
commit af14f123e1

View File

@ -8,13 +8,20 @@
let let
cfg = config.services.autopull; cfg = config.services.autopull;
autopull-type = lib.types.submodule { # autopull-type = lib.types.submodule { #
autopull-type =
with lib.types;
attrsOf (
submodule (
{ name, ... }:
{
options = { options = {
enable = lib.mkEnableOption "autopull repo"; enable = lib.mkEnableOption "autopull repo";
name = lib.mkOption { repo-name = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = config.module._args.name; default = name;
description = "A name for the service which needs to be pulled"; description = "A name for the service which needs to be pulled";
}; };
@ -41,14 +48,16 @@ let
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."; 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 in
{ {
options = { options = {
services.autopull = { services.autopull = {
enable = lib.mkEnableOption "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 { lib.mkIf cfg.enable {
environment.systemPackages = environment.systemPackages =
[ pkgs.git ] [ 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 pkgs.openssh
]; ];
systemd.services = lib.mapAttrs' ( systemd.services = lib.mapAttrs' (
repo: repo:
{ {
name, repo-name,
ssh-key, ssh-key,
path,
triggers-rebuild, triggers-rebuild,
... ...
}: }:
lib.nameValuePair "autopull@${name}" { lib.nameValuePair "autopull@${repo-name}" {
requires = [ "multi-user.target" ]; requires = [ "multi-user.target" ];
wants = lib.optionals (triggers-rebuild) [ "nixos-service.service" ]; wants = lib.optionals (triggers-rebuild) [ "nixos-service.service" ];
after = [ "multi-user.target" ]; after = [ "multi-user.target" ];
before = lib.optionals (triggers-rebuild) [ "nixos-upgrade.service" ]; 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 != "") { environment = lib.mkIf (ssh-key != "") {
GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${ssh-key} -o IdentitiesOnly=yes"; GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${ssh-key} -o IdentitiesOnly=yes";
}; };
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
User = "root"; User = "root";
WorkingDirectory = cfg.path; WorkingDirectory = path;
ExecStart = "${pkgs.git}/bin/git pull --all"; ExecStart = "${pkgs.git}/bin/git pull --all";
}; };
} }
) repos; ) repos;
systemd.timers."autopull@${cfg.name}" = lib.mapAttrs' ( systemd.timers = lib.mapAttrs' (
repo: repo:
{ name, frequency, ... }: { repo-name, frequency, ... }:
lib.nameValuePair "autopull@${name}" { lib.nameValuePair "autopull@${repo-name}" {
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
timerConfig = { timerConfig = {
OnBootSec = cfg.frequency; OnBootSec = frequency;
OnUnitActiveSec = cfg.frequency; OnUnitActiveSec = frequency;
Unit = "autopull@${cfg.name}.service"; Unit = "autopull@${repo-name}.service";
}; };
} }
) repos; ) repos;