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,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;