Move modifications to autoUpgrade to seperate file

This commit is contained in:
batteredbunny 2024-03-28 12:06:22 +02:00 committed by batteredbunny
parent bd4b20225a
commit 759fd0c386
2 changed files with 71 additions and 31 deletions

View file

@ -13,6 +13,7 @@ in {
./containers.nix
./caddy.nix
./services/qbittorrent-nox.nix
./services/nixos-upgrade.nix
inputs.nix-minecraft.nixosModules.minecraft-servers
];
@ -25,6 +26,11 @@ in {
enable = true;
allowReboot = true;
flake = "/etc/nixos";
updateFlake = true;
failureNotification = {
enable = true;
ntfyUrlFile = "/etc/secrets/failureNotification.env";
};
};
nix = {
@ -61,46 +67,17 @@ in {
TS_NO_LOGS_NO_SUPPORT = "true";
};
nixos-upgrade-failure = let
NTFY_DOMAIN = "ntfy.catnip.ee";
in {
path = with pkgs; ["/run/wrappers" "/run/current-system/sw" curl];
script = ''
journalctl _SYSTEMD_INVOCATION_ID=`systemctl show --value -p InvocationID nixos-upgrade.service` > /tmp/upgrade-failure.txt
curl -T /tmp/upgrade-failure.txt -H "Filename: failure-logs.txt" -H "Title: Nixos auto upgrade failed for $(hostname)" https://${NTFY_DOMAIN}/$NTFY_TOPIC
rm /tmp/upgrade-failure.txt
'';
serviceConfig = {
User = "root";
# Contains NTFY_TOPIC=
EnvironmentFile = "/etc/secrets/NTFY.env";
};
};
nixos-upgrade = {
onFailure = ["nixos-upgrade-failure.service"];
serviceConfig.ExecStartPre = pkgs.writeShellScript "update.sh" ''
cd ${config.system.autoUpgrade.flake}
${lib.getExe pkgs.git} pull
${lib.getExe pkgs.nix} flake update --commit-lock-file
${lib.getExe pkgs.git} push
'';
};
modded-mc-server = {
enable = true;
path = with pkgs; [
openjdk17-bootstrap
mcrcon
];
serviceConfig = {
serviceConfig = rec {
User = "owo";
WorkingDirectory = "/home/owo/Documents/aof7";
ExecStart = pkgs.writeShellScript "start.sh" ''
cd /home/owo/Documents/aof7
cd ${WorkingDirectory}
java -jar serverstarter-2.4.0.jar
'';
ExecStop = pkgs.writeShellScript "stop.sh" ''

View file

@ -0,0 +1,63 @@
{
config,
lib,
pkgs,
...
}: let
cfg = config.system.autoUpgrade;
updateScript = pkgs.writeShellApplication {
name = "update";
runtimeInputs = with pkgs; [git nix];
text = ''
cd ${cfg.flake}
git pull
nix flake update --commit-lock-file
git push
'';
};
in {
options.system.autoUpgrade = {
# TODO: make sure flake is a local folder
updateFlake = lib.mkOption {
default = false;
description = "Update lockfile of the flake.";
};
failureNotification = {
enable = lib.mkOption {
default = false;
description = "Enable ntfy notification on upgrade failure.";
};
ntfyUrlFile = lib.mkOption {
type = lib.types.str;
description = "Environment file containing NTFY_URL";
};
};
};
config.systemd.services = lib.mkIf cfg.enable {
nixos-upgrade.serviceConfig.ExecStartPre =
lib.mkIf cfg.updateFlake
(lib.getExe updateScript);
nios-upgrade-failure = lib.mkIf cfg.failureNotification.enable {
path = with pkgs; ["/run/wrappers" "/run/current-system/sw" curl];
script = ''
journalctl _SYSTEMD_INVOCATION_ID=`systemctl show --value -p InvocationID nixos-upgrade.service` > /tmp/upgrade-failure.txt
curl -T /tmp/upgrade-failure.txt -H "Filename: failure-logs.txt" -H "Title: Nixos auto upgrade failed for $(hostname)" $NTFY_URL
rm /tmp/upgrade-failure.txt
'';
serviceConfig = {
User = "root";
EnvironmentFile = cfg.failureNotification.ntfyUrlFile;
};
};
nixos-upgrade.onFailure = lib.mkIf cfg.failureNotification.enable ["nixos-upgrade-failure.service"];
};
}