use hydra’s latestbuild api to find latest build for a job
All checks were successful
Hydra nixosConfigurations.container-default-x86_64-linux Hydra build #22915 of nixos-config:pr618:nixosConfigurations.container-default-x86_64-linux
Hydra checks.x86_64-linux.containers-default Hydra build #22912 of nixos-config:pr618:checks.x86_64-linux.containers-default
Hydra nixosConfigurations.container-default-aarch64-linux Hydra build #22913 of nixos-config:pr618:nixosConfigurations.container-default-aarch64-linux
Hydra nixosConfigurations.container-default-riscv64-linux Hydra build #22914 of nixos-config:pr618:nixosConfigurations.container-default-riscv64-linux
Hydra nixosConfigurations.not522 Hydra build #22916 of nixos-config:pr618:nixosConfigurations.not522
Hydra nixosConfigurations.not522-installer Hydra build #22917 of nixos-config:pr618:nixosConfigurations.not522-installer
All checks were successful
Hydra nixosConfigurations.container-default-x86_64-linux Hydra build #22915 of nixos-config:pr618:nixosConfigurations.container-default-x86_64-linux
Hydra checks.x86_64-linux.containers-default Hydra build #22912 of nixos-config:pr618:checks.x86_64-linux.containers-default
Hydra nixosConfigurations.container-default-aarch64-linux Hydra build #22913 of nixos-config:pr618:nixosConfigurations.container-default-aarch64-linux
Hydra nixosConfigurations.container-default-riscv64-linux Hydra build #22914 of nixos-config:pr618:nixosConfigurations.container-default-riscv64-linux
Hydra nixosConfigurations.not522 Hydra build #22916 of nixos-config:pr618:nixosConfigurations.not522
Hydra nixosConfigurations.not522-installer Hydra build #22917 of nixos-config:pr618:nixosConfigurations.not522-installer
This commit is contained in:
parent
31d5bce7b8
commit
fcc5491f8d
1 changed files with 80 additions and 78 deletions
|
@ -3,89 +3,91 @@
|
|||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
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";
|
||||
}: let
|
||||
cfg = config.nix.auto-update;
|
||||
in
|
||||
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";
|
||||
};
|
||||
job = mkOption {
|
||||
type = types.str;
|
||||
description = "Job name to use";
|
||||
default = "nixosConfigurations.${config.networking.hostName}";
|
||||
};
|
||||
};
|
||||
source = mkOption {
|
||||
type = types.str;
|
||||
description = "Source to update from";
|
||||
default = "${config.nix.auto-update.hydraServer}/jobset/nixos-config/pr618/evals";
|
||||
};
|
||||
attr = mkOption {
|
||||
type = types.str;
|
||||
description = "Attribute to update from";
|
||||
default = "nixosConfigurations.${config.networking.hostName}";
|
||||
};
|
||||
};
|
||||
|
||||
config.nix.auto-update.enable = mkDefault config.nix.enable;
|
||||
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;
|
||||
config.nix.auto-update.enable = mkDefault config.nix.enable;
|
||||
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;
|
||||
|
||||
wants = ["network-online.target"];
|
||||
after = ["network-online.target"];
|
||||
wants = ["network-online.target"];
|
||||
after = ["network-online.target"];
|
||||
|
||||
serviceConfig.Type = "oneshot";
|
||||
serviceConfig.Type = "oneshot";
|
||||
|
||||
script = ''
|
||||
#!${pkgs.bash}/bin/bash
|
||||
set -euxo pipefail
|
||||
builds=$(${pkgs.curl}/bin/curl -H "accept: application/json" ${config.nix.auto-update.source} | ${pkgs.jq}/bin/jq -r '.evals[0].builds[]')
|
||||
for build in $builds; do
|
||||
script = ''
|
||||
#!${pkgs.bash}/bin/bash
|
||||
set -euxo pipefail
|
||||
build=$(${pkgs.curl}/bin/curl -H "accept: application/json" ${cfg.hydraServer}/api/latestbuilds -d "nr=10" -d "project=${cfg.project}" | ${pkgs.jq}/bin/jq -r '[.[]|select(.buildstatus==0)][0]][0].id')
|
||||
doc=$(${pkgs.curl}/bin/curl -H "accept: application/json" ${config.nix.auto-update.hydraServer}/build/$build)
|
||||
jobname=$(echo $doc | ${pkgs.jq}/bin/jq -r '.job')
|
||||
if [ "$jobname" = "${config.nix.auto-update.attr}" ]; then
|
||||
drvname=$(echo $doc | ${pkgs.jq}/bin/jq -r '.drvpath')
|
||||
output=$(${pkgs.nix}/bin/nix-store -r $drvname)
|
||||
${pkgs.nix}/bin/nix-env -p /nix/var/nix/profiles/system --set $output
|
||||
${
|
||||
if config.nix.auto-update.reboot
|
||||
then ''
|
||||
$output/bin/switch-to-configuration 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
|
||||
drvname=$(echo $doc | ${pkgs.jq}/bin/jq -r '.drvpath')
|
||||
output=$(${pkgs.nix}/bin/nix-store -r $drvname)
|
||||
${pkgs.nix}/bin/nix-env -p /nix/var/nix/profiles/system --set $output
|
||||
${
|
||||
if config.nix.auto-update.reboot
|
||||
then ''
|
||||
$output/bin/switch-to-configuration 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
|
||||
$output/bin/switch-to-configuration switch
|
||||
else
|
||||
${pkgs.systemd}/bin/shutdown -r +1
|
||||
fi
|
||||
exit
|
||||
''
|
||||
else ''
|
||||
$output/bin/switch-to-configuration switch
|
||||
else
|
||||
${pkgs.systemd}/bin/shutdown -r +1
|
||||
fi
|
||||
exit
|
||||
''
|
||||
else ''
|
||||
$output/bin/switch-to-configuration switch
|
||||
''
|
||||
}
|
||||
fi
|
||||
done
|
||||
'';
|
||||
};
|
||||
|
||||
config.systemd.timers.nixos-upgrade = {
|
||||
enable = config.nix.auto-update.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";
|
||||
''
|
||||
}
|
||||
'';
|
||||
};
|
||||
};
|
||||
config.assertions = [
|
||||
{
|
||||
assertion = config.nix.auto-update.enable -> config.nix.enable;
|
||||
message = "Auto updating will only work when nix itself is enabled.";
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
config.systemd.timers.nixos-upgrade = {
|
||||
enable = config.nix.auto-update.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";
|
||||
};
|
||||
};
|
||||
config.assertions = [
|
||||
{
|
||||
assertion = config.nix.auto-update.enable -> config.nix.enable;
|
||||
message = "Auto updating will only work when nix itself is enabled.";
|
||||
}
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue