nix-config/config/autoupdate.nix

64 lines
2.1 KiB
Nix

{ 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 = lib.mkIf config.c3d2.autoUpdate {
# the presence of this 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 = {
wantedBy = [ "multi-user.target" ];
path = with pkgs; [ nixFlakes 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";
};
script = ''
OLD=$(readlink /run/current-system)
NEW=$(curl -sLH "Accept: application/json" https://hydra.hq.c3d2.de/job/c3d2/nix-config/x86_64-linux.$(hostname)/latest | jq -r .buildoutputs.out.path)
if [ -z "$NEW" ]; then
echo "Unable to obtain updated system"
exit 1
fi
if [ "$OLD" != "$NEW" ]; then
echo "New system available: $NEW"
# this should fetch the new system from the binary cache
nix build --no-link "$NEW"
if [ -e "$NEW/etc/systemd/system/autoupdate.timer" ]; then
# switch to the new system
"$NEW/bin/switch-to-configuration" switch
else
echo "New system is not yet autoupdate-enabled. Refusing to switch into a dead end."
fi
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";
};
nix.gc = {
automatic = true;
randomizedDelaySec = "6h";
};
};
}