nixos/modules/www/www-repo.nix
Jake Hillion f047111de7
Some checks reported errors
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build was killed
www/global: migrate vm.strangervm->jorah
2023-11-26 12:24:54 +00:00

69 lines
1.7 KiB
Nix

{ pkgs, lib, config, ... }:
let
cfg = config.custom.www.www-repo;
in
{
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.";
};
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.";
};
};
config = lib.mkIf cfg.enable {
systemd.tmpfiles.rules = [
"d /var/www 0755 ${config.services.caddy.user} ${config.services.caddy.group} - -"
];
systemd.timers.clone-www-repo = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnBootSec = "5m";
OnUnitInactiveSec = "60m";
Unit = "clone-www-repo.service";
};
};
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}
else
cd ${cfg.location}
${pkgs.git} remote set-url origin ${cfg.remote}
${pkgs.git}/bin/git fetch
${pkgs.git}/bin/git reset --hard origin/${cfg.branch}
fi
'';
};
};
}