2024-03-03 18:06:28 -05:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2024-01-01 12:41:32 -05:00
|
|
|
|
2024-03-03 18:06:28 -05:00
|
|
|
let
|
|
|
|
cfg = config.services.autopull;
|
|
|
|
in
|
|
|
|
{
|
2024-01-01 12:41:32 -05:00
|
|
|
options = {
|
|
|
|
services.autopull = {
|
|
|
|
enable = lib.mkEnableOption "autopull";
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "dotfiles";
|
|
|
|
description = "A name for the service which needs to be pulled";
|
|
|
|
};
|
2024-02-05 22:45:43 +01:00
|
|
|
|
2024-01-01 12:41:32 -05:00
|
|
|
path = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
|
|
|
default = null;
|
|
|
|
description = "Path that needs to be updated via git pull";
|
|
|
|
};
|
2024-02-05 22:45:43 +01:00
|
|
|
|
2024-01-01 12:41:32 -05:00
|
|
|
frequency = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "systemd-timer compatible time between pulls";
|
|
|
|
default = "1h";
|
|
|
|
};
|
2024-02-05 22:45:43 +01:00
|
|
|
|
2024-01-01 12:41:32 -05:00
|
|
|
ssh-key = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2024-02-06 23:58:33 +01:00
|
|
|
default = "";
|
2024-01-01 12:41:32 -05:00
|
|
|
description = "ssh-key used to pull the repository";
|
|
|
|
};
|
2024-02-05 22:45:43 +01:00
|
|
|
|
2024-01-01 12:41:32 -05:00
|
|
|
triggersRebuild = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
2024-02-05 22:45:43 +01:00
|
|
|
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.";
|
2024-01-01 12:41:32 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf (cfg.enable && !(builtins.isNull cfg.path)) {
|
2024-03-03 18:06:28 -05:00
|
|
|
environment.systemPackages = [
|
|
|
|
pkgs.openssh
|
|
|
|
pkgs.git
|
|
|
|
];
|
2024-01-01 12:41:32 -05:00
|
|
|
systemd.services."autopull@${cfg.name}" = {
|
2024-03-24 13:08:42 -04:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2024-01-01 12:41:32 -05:00
|
|
|
description = "Pull the latest data for ${cfg.name}";
|
2024-03-03 18:06:28 -05:00
|
|
|
environment = lib.mkIf (cfg.ssh-key != "") {
|
|
|
|
GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${cfg.ssh-key} -o IdentitiesOnly=yes";
|
|
|
|
};
|
2024-01-01 12:41:32 -05:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "root";
|
|
|
|
WorkingDirectory = cfg.path;
|
|
|
|
ExecStart = "${pkgs.git}/bin/git pull --all";
|
|
|
|
};
|
|
|
|
};
|
2024-02-05 22:45:43 +01:00
|
|
|
|
2024-01-01 12:41:32 -05:00
|
|
|
systemd.timers."autopull@${cfg.name}" = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnBootSec = cfg.frequency;
|
|
|
|
OnUnitActiveSec = cfg.frequency;
|
|
|
|
Unit = "autopull@${cfg.name}.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|