nix-config/modules/autoupdate.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
3.7 KiB
Nix
Raw Normal View History

2022-01-10 03:34:34 +01:00
{ config, lib, pkgs, ... }:
{
options.c3d2.autoUpdate = with lib; mkOption {
description = ''
Enables a timer that periodically checks hydra.hq.c3d2.de for the last build of the local system, and switches to it if it is different.
Also enables periodical /nix/store GC.
'';
type = types.bool;
default = false;
};
config = {
# the presence of this .service file signifies that the system is
# autoupdate-enabled. it is checked to prevent autoupdating back
# to a system without autoupdate when deploying with autoupdate
# for the first time.
systemd.services.autoupdate = lib.mkIf config.c3d2.autoUpdate {
2022-01-10 03:34:34 +01:00
wantedBy = [ "multi-user.target" ];
2022-09-27 02:01:38 +02:00
path = with pkgs; [ nix nettools curl jq ];
serviceConfig = {
Type = "oneshot";
# switch-to-configuration may not return. HACK: cap running
# time so that the timer can be scheduled again.
TimeoutStartSec = "30min";
};
2022-01-10 03:34:34 +01:00
script = ''
OLD=$(readlink /run/current-system)
echo Current system: $(basename $OLD)
NEW=$(curl -sLH "Accept: application/json" https://hydra.hq.c3d2.de/job/c3d2/nix-config/${config.networking.hostName}/latest | jq -er .buildoutputs.out.path)
2022-02-08 19:17:44 +01:00
if [ -z "$NEW" ] || [ "$NEW" = "null" ]; then
2022-01-10 03:34:34 +01:00
echo "Unable to obtain updated system"
exit 1
fi
echo New system: $(basename $NEW)
2022-01-10 03:34:34 +01:00
if [ "$OLD" != "$NEW" ]; then
echo "Fetching new system built by https://hydra.hq.c3d2.de/jobset/c3d2/nix-config"
2022-01-10 03:34:34 +01:00
# this should fetch the new system from the binary cache
2023-04-03 20:34:04 +02:00
nix copy --from https://nix-cache.hq.c3d2.de "$NEW"
if [ -e "$NEW/etc/systemd/system/autoupdate.timer" ]; then
echo "Switch to the new system..."
nix-env -p /nix/var/nix/profiles/system --set $NEW
"$NEW/bin/switch-to-configuration" switch
else
echo "New system is not yet autoupdate-enabled. Refusing to switch into a dead end."
fi
2022-01-10 03:34:34 +01:00
else
echo "No update required"
fi
'';
# don't let the switch kill this service, aborting the switch
restartIfChanged = false;
unitConfig.X-StopOnRemoval = false;
# create timer
startAt = "hourly";
2022-01-10 03:34:34 +01:00
};
2023-10-16 16:57:35 +02:00
nix = {
# Show a diff when activating a new system except for microvms which handle this seperately
2023-10-26 20:43:01 +02:00
diffSystem = config.c3d2.deployment.server or "" == "";
2023-10-16 16:57:35 +02:00
gc = lib.mkIf config.c3d2.autoUpdate {
automatic = true;
randomizedDelaySec = "6h";
options = "--delete-older-than 21d";
};
};
environment.systemPackages = [ (
# Provide a manual updating script that fetches the latest
# updated+built system from Hydra
pkgs.writeScriptBin "update-from-hydra" ''
#! ${pkgs.runtimeShell} -e
OLD=$(readlink /run/current-system)
echo Current system: $(basename $OLD)
NEW=$(curl -sLH "Accept: application/json" https://hydra.hq.c3d2.de/job/c3d2/nix-config/${config.networking.hostName}/latest | ${pkgs.jq}/bin/jq -er .buildoutputs.out.path)
2022-02-08 19:17:44 +01:00
if [ -z "$NEW" ] || [ "$NEW" = "null" ]; then
echo "Unable to obtain updated system"
exit 1
fi
echo New system: $(basename $NEW)
if [ "$OLD" != "$NEW" ]; then
echo "Fetching new system built by https://hydra.hq.c3d2.de/jobset/c3d2/nix-config"
# this should fetch the new system from the binary cache
2023-04-03 20:34:04 +02:00
nix copy --from https://nix-cache.hq.c3d2.de "$NEW"
echo "Switch to the new system..."
nix-env -p /nix/var/nix/profiles/system --set $NEW
"$NEW/bin/switch-to-configuration" switch
else
echo "No update required"
fi
''
) ];
2022-01-10 03:34:34 +01:00
};
}