2024-10-31 18:08:50 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
2024-11-09 14:02:26 +00:00
|
|
|
}:
|
|
|
|
let
|
2024-11-02 07:13:30 +00:00
|
|
|
cfg = config.nix.auto-update;
|
|
|
|
in
|
2024-11-09 14:02:26 +00:00
|
|
|
with lib;
|
|
|
|
{
|
|
|
|
options.nix.auto-update = {
|
|
|
|
enable = mkEnableOption "enable automatic updates";
|
|
|
|
reboot = mkEnableOption "Reboot if kernel change";
|
|
|
|
hydraServer = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Location of hydra server";
|
|
|
|
default = "https://hydra.chir.rs";
|
|
|
|
};
|
|
|
|
project = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Project name to use";
|
|
|
|
default = "nixos-config";
|
|
|
|
};
|
|
|
|
jobset = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Jobset name to use";
|
|
|
|
default = "pr618";
|
2024-10-31 18:08:50 +00:00
|
|
|
};
|
2024-11-09 14:02:26 +00:00
|
|
|
job = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Job name to use";
|
|
|
|
default = "nixosConfigurations.${config.networking.hostName}";
|
|
|
|
defaultText = literalExpression ''"nixosConfigurations.''${config.networking.hostName}"'';
|
|
|
|
};
|
|
|
|
specialisation = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "specialisation to switch into";
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
2024-10-31 18:08:50 +00:00
|
|
|
|
2024-11-15 13:47:52 +00:00
|
|
|
config.nix.auto-update.enable = mkDefault config.nix.enable;
|
2024-11-09 14:02:26 +00:00
|
|
|
config.nix.auto-update.reboot = mkDefault true;
|
|
|
|
config.systemd.services.nixos-upgrade = mkIf config.nix.enable {
|
|
|
|
description = "NixOS Upgrade";
|
|
|
|
restartIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
2024-10-31 18:08:50 +00:00
|
|
|
|
2024-11-09 14:02:26 +00:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
2024-10-31 18:08:50 +00:00
|
|
|
|
2024-11-09 14:02:26 +00:00
|
|
|
serviceConfig.Type = "oneshot";
|
2024-10-31 18:08:50 +00:00
|
|
|
|
2024-11-09 14:02:26 +00:00
|
|
|
script =
|
|
|
|
let
|
|
|
|
output =
|
|
|
|
if cfg.specialisation == null then "$output" else "$output/specialisation/${cfg.specialisation}";
|
2024-11-08 10:28:46 +00:00
|
|
|
switchToConfiguration = "${output}/bin/switch-to-configuration";
|
2024-11-09 14:02:26 +00:00
|
|
|
in
|
|
|
|
''
|
2024-11-02 07:13:30 +00:00
|
|
|
#!${pkgs.bash}/bin/bash
|
|
|
|
set -euxo pipefail
|
2024-11-02 12:45:42 +00:00
|
|
|
build=$(${pkgs.curl}/bin/curl -H "accept: application/json" -G ${cfg.hydraServer}/api/latestbuilds -d "nr=10" -d "project=${cfg.project}" -d "jobset=${cfg.jobset}" -d "job=${cfg.job}" | ${pkgs.jq}/bin/jq -r '[.[]|select(.buildstatus==0)][0].id')
|
2024-11-05 08:11:22 +00:00
|
|
|
doc=$(${pkgs.curl}/bin/curl -H "accept: application/json" ${cfg.hydraServer}/build/$build)
|
2024-11-02 07:13:30 +00:00
|
|
|
drvname=$(echo $doc | ${pkgs.jq}/bin/jq -r '.drvpath')
|
|
|
|
output=$(${pkgs.nix}/bin/nix-store -r $drvname)
|
2024-11-17 10:51:44 +00:00
|
|
|
${pkgs.nix}/bin/nix-env -p /nix/var/nix/profiles/system --set $output
|
2024-11-02 07:13:30 +00:00
|
|
|
${
|
2024-11-09 14:02:26 +00:00
|
|
|
if cfg.reboot then
|
|
|
|
''
|
|
|
|
${switchToConfiguration} boot
|
|
|
|
booted="$(${pkgs.coreutils}/bin/readlink /run/booted-system/{initrd,kernel,kernel-modules})"
|
|
|
|
built="$(${pkgs.coreutils}/bin/readlink ${output}/{initrd,kernel,kernel-modules})"
|
|
|
|
if [ "$booted" = "$built" ]; then
|
|
|
|
${switchToConfiguration} switch
|
|
|
|
else
|
|
|
|
${pkgs.systemd}/bin/shutdown -r +1
|
|
|
|
fi
|
|
|
|
exit
|
|
|
|
''
|
|
|
|
else
|
|
|
|
''
|
2024-11-05 13:51:00 +00:00
|
|
|
${switchToConfiguration} switch
|
2024-11-09 14:02:26 +00:00
|
|
|
''
|
2024-11-02 07:13:30 +00:00
|
|
|
}
|
|
|
|
'';
|
2024-11-09 14:02:26 +00:00
|
|
|
};
|
2024-10-31 18:08:50 +00:00
|
|
|
|
2024-11-09 14:02:26 +00:00
|
|
|
config.systemd.timers.nixos-upgrade = {
|
|
|
|
enable = cfg.enable;
|
|
|
|
description = "Automatically update nixos";
|
|
|
|
requires = [ "nixos-upgrade.service" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnUnitActiveSec = "30min";
|
|
|
|
RandomizedDelaySec = "1h";
|
2024-10-31 18:08:50 +00:00
|
|
|
};
|
2024-11-09 14:02:26 +00:00
|
|
|
};
|
|
|
|
config.assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.enable -> config.nix.enable;
|
|
|
|
message = "Auto updating will only work when nix itself is enabled.";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = (cfg.specialisation != null) -> config.isSpecialisation;
|
|
|
|
message = "Automatic update switching to specialisation is only allowed in specialisations";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = config.isSpecialisation -> (cfg.specialisation != null);
|
|
|
|
message = "Specifying the specialization name is required for autoupdate to work!";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|