Update git-auto-update.nix

This commit is contained in:
ska
2025-12-26 00:19:32 +00:00
parent 0c4305f02e
commit 7c630df381

View File

@@ -1,8 +1,18 @@
{ config, pkgs, ... }: { config, pkgs, ... }:
let {
gitUpdateScript = pkgs.writeShellScript "nix-git-auto-update" '' ########################################
set -euo pipefail # NixOS Git Auto Update Script
########################################
environment.etc."nixos-git-update.sh" = {
mode = "0700";
text = ''
#!/run/current-system/sw/bin/bash
set -e
export PATH="/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin"
export NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
LOCAL_PATH="/var/lib/nixos-config" LOCAL_PATH="/var/lib/nixos-config"
REPO_URL="https://git.skarockoi.de/ska/nixos-production.git" REPO_URL="https://git.skarockoi.de/ska/nixos-production.git"
@@ -10,65 +20,43 @@ let
if [ ! -d "$LOCAL_PATH/.git" ]; then if [ ! -d "$LOCAL_PATH/.git" ]; then
mkdir -p "$LOCAL_PATH" mkdir -p "$LOCAL_PATH"
chmod 700 "$LOCAL_PATH" chmod 700 "$LOCAL_PATH"
${pkgs.git}/bin/git clone "$REPO_URL" "$LOCAL_PATH" git clone "$REPO_URL" "$LOCAL_PATH"
exit 0 exit 0
fi fi
cd "$LOCAL_PATH" cd "$LOCAL_PATH"
${pkgs.git}/bin/git fetch origin git fetch origin
LOCAL_HEAD=$(git rev-parse HEAD)
LOCAL_HEAD=$(${pkgs.git}/bin/git rev-parse HEAD) REMOTE_HEAD=$(git rev-parse origin/main)
REMOTE_HEAD=$(${pkgs.git}/bin/git rev-parse origin/main)
if [ "$LOCAL_HEAD" != "$REMOTE_HEAD" ]; then if [ "$LOCAL_HEAD" != "$REMOTE_HEAD" ]; then
${pkgs.git}/bin/git reset --hard origin/main git reset --hard origin/main
${pkgs.nixos-rebuild}/bin/nixos-rebuild boot \ nixos-rebuild boot -I nixos-config="$LOCAL_PATH/configuration.nix"
-I nixos-config="$LOCAL_PATH/configuration.nix"
fi fi
''; '';
in
{
##############################
# Git Auto Update Script
##############################
environment.etc."nix-git-auto-update.sh" = {
mode = "0700";
source = gitUpdateScript;
}; };
############################## systemd.services.nixos-git-update = {
# systemd service description = "Update NixOS from Git configuration";
############################## script = "/etc/nixos-git-update.sh";
systemd.services.nix-git-auto-update = {
description = "Automatically update NixOS from Git";
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
ExecStart = "/etc/nix-git-auto-update.sh"; User = "root";
Group = "root";
TimeoutStartSec = "10min"; TimeoutStartSec = "10min";
Restart = "on-failure"; Restart = "on-failure";
StandardOutput = "journal";
StandardError = "journal";
}; };
}; };
############################## systemd.timers.nixos-git-update = {
# systemd timer description = "Periodic NixOS Git configuration check";
##############################
systemd.timers.nix-git-auto-update = {
description = "Periodic NixOS Git update check";
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
timerConfig = { timerConfig = {
OnBootSec = "2min"; OnBootSec = "60s";
OnUnitActiveSec = "5min"; OnUnitActiveSec = "5min";
Persistent = true;
}; };
}; };
} }