nixos/modules/www/www-repo.nix

69 lines
1.7 KiB
Nix
Raw Normal View History

2023-01-08 11:58:34 +00:00
{ pkgs, lib, config, ... }:
2023-04-07 21:37:23 +01:00
let
cfg = config.custom.www.www-repo;
in
2023-01-08 11:58:34 +00:00
{
2023-04-07 21:37:23 +01:00
options.custom.www.www-repo = {
enable = lib.mkEnableOption "www-repo";
location = lib.mkOption {
default = "/var/www";
type = lib.types.path;
description = "Location of the local www repository.";
2023-01-08 11:58:34 +00:00
};
2023-04-07 21:37:23 +01:00
remote = lib.mkOption {
default = "https://gitea.hillion.co.uk/JakeHillion/www.git";
type = lib.types.str;
description = "Remote to pull from for the www repository.";
};
branch = lib.mkOption {
default = "main";
type = lib.types.str;
description = "Branch to pull from the remote.";
};
};
2023-01-08 11:58:34 +00:00
2023-04-07 21:37:23 +01:00
config = lib.mkIf cfg.enable {
systemd.tmpfiles.rules = [
"d /var/www 0755 ${config.services.caddy.user} ${config.services.caddy.group} - -"
];
2023-01-08 11:58:34 +00:00
2023-04-07 21:37:23 +01:00
systemd.timers.clone-www-repo = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnBootSec = "5m";
OnUnitInactiveSec = "60m";
Unit = "clone-www-repo.service";
};
2023-01-08 11:58:34 +00:00
};
2023-04-07 21:37:23 +01:00
systemd.services.clone-www-repo = {
description = "Clone and pull the www repo";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig = {
Type = "oneshot";
User = "${config.services.caddy.user}";
Group = "${config.services.caddy.group}";
};
script = ''
if [ ! -d "${cfg.location}/.git" ] ; then
${pkgs.git}/bin/git clone ${cfg.remote} ${cfg.location}
2023-04-07 21:37:23 +01:00
else
cd ${cfg.location}
2023-04-07 21:37:23 +01:00
${pkgs.git} remote set-url origin ${cfg.remote}
${pkgs.git}/bin/git fetch
${pkgs.git}/bin/git reset --hard origin/${cfg.branch}
fi
'';
};
2023-01-08 11:58:34 +00:00
};
}