Files
nix-dotfiles/modules/autopull.nix
T

107 lines
2.6 KiB
Nix
Raw Normal View History

2024-03-03 18:06:28 -05:00
{
config,
lib,
pkgs,
...
}:
2024-03-03 18:06:28 -05:00
let
cfg = config.services.autopull;
2024-05-15 00:38:59 -04:00
2024-05-16 11:39:50 -04:00
autopull-type =
with lib.types;
attrsOf (
submodule (
{ name, ... }:
{
options = {
enable = lib.mkEnableOption "autopull repo";
2024-05-15 00:38:59 -04:00
2024-05-16 11:39:50 -04:00
repo-name = lib.mkOption {
type = lib.types.str;
default = name;
2024-05-15 00:38:59 -04:00
2024-05-16 11:39:50 -04:00
description = "A name for the service which needs to be pulled";
};
2024-05-15 00:38:59 -04:00
2024-05-16 11:39:50 -04:00
path = lib.mkOption {
type = lib.types.path;
description = "Path that needs to be updated via git pull";
};
2024-05-15 00:38:59 -04:00
2024-05-16 11:39:50 -04:00
frequency = lib.mkOption {
type = lib.types.str;
description = "systemd-timer compatible time between pulls";
default = "1h";
};
2024-05-15 00:38:59 -04:00
2024-05-16 11:39:50 -04:00
ssh-key = lib.mkOption {
type = lib.types.str;
default = "";
description = "ssh-key used to pull the repository";
};
};
}
)
);
2024-03-03 18:06:28 -05:00
in
{
options = {
services.autopull = {
enable = lib.mkEnableOption "autopull";
2024-02-05 22:45:43 +01:00
2024-05-16 11:39:50 -04:00
repo = lib.mkOption { type = autopull-type; };
};
};
2024-05-15 00:38:59 -04:00
config =
let
repos = lib.filterAttrs (_: { enable, ... }: enable) cfg.repo;
2024-05-15 00:38:59 -04:00
in
lib.mkIf cfg.enable {
2025-08-01 00:16:57 -04:00
environment.systemPackages = [
pkgs.git
]
++ lib.optionals (lib.any (ssh-key: ssh-key != "") (lib.adev.mapGetAttr "ssh-key" repos)) [
pkgs.openssh
];
2024-05-15 04:37:22 -04:00
2024-05-15 00:38:59 -04:00
systemd.services = lib.mapAttrs' (
_:
2024-05-15 00:38:59 -04:00
{
2024-05-16 11:39:50 -04:00
repo-name,
2024-05-15 00:38:59 -04:00
ssh-key,
2024-05-16 11:39:50 -04:00
path,
2024-05-15 00:38:59 -04:00
...
}:
2024-05-16 11:39:50 -04:00
lib.nameValuePair "autopull@${repo-name}" {
2024-05-15 00:38:59 -04:00
requires = [ "multi-user.target" ];
after = [ "multi-user.target" ];
2024-05-16 11:39:50 -04:00
description = "Pull the latest data for ${repo-name}";
2024-05-15 00:38:59 -04:00
environment = lib.mkIf (ssh-key != "") {
GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${ssh-key} -o IdentitiesOnly=yes";
};
serviceConfig = {
Type = "oneshot";
User = "root";
2024-05-16 11:39:50 -04:00
WorkingDirectory = path;
2024-08-03 10:14:29 -04:00
ExecStart = "${pkgs.git}/bin/git pull --all --prune";
2024-05-15 00:38:59 -04:00
};
}
) repos;
2024-02-05 22:45:43 +01:00
2024-05-16 11:39:50 -04:00
systemd.timers = lib.mapAttrs' (
_:
2024-05-16 11:39:50 -04:00
{ repo-name, frequency, ... }:
lib.nameValuePair "autopull@${repo-name}" {
2024-05-15 00:38:59 -04:00
wantedBy = [ "timers.target" ];
timerConfig = {
2024-05-16 11:39:50 -04:00
OnBootSec = frequency;
OnUnitActiveSec = frequency;
Unit = "autopull@${repo-name}.service";
2024-05-15 00:38:59 -04:00
};
}
) repos;
};
}