diff --git a/config/services/hydra/nix-config.nix b/config/services/hydra/nix-config.nix index cb1f5b8..c9e1177 100644 --- a/config/services/hydra/nix-config.nix +++ b/config/services/hydra/nix-config.nix @@ -24,6 +24,7 @@ "github:" "git+https://" "git+ssh://" + "https://" ]; }; programs.ssh = { diff --git a/flake.lock b/flake.lock index d114bf0..46a0a40 100644 --- a/flake.lock +++ b/flake.lock @@ -550,6 +550,26 @@ "type": "github" } }, + "nixos-needsreboot": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1715010630, + "narHash": "sha256-zOMZDSAd3w1Dd5Jcs3nYae7aNomb3qfMJmCQl2ucZok=", + "owner": "thefossguy", + "repo": "nixos-needsreboot", + "rev": "8a3f64cc3c246cc6311485ad96ee9db0989c1377", + "type": "github" + }, + "original": { + "owner": "thefossguy", + "repo": "nixos-needsreboot", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1707092692, @@ -682,6 +702,7 @@ "home-manager": "home-manager", "kyouma-www": "kyouma-www", "nixos-hardware": "nixos-hardware", + "nixos-needsreboot": "nixos-needsreboot", "nixpkgs": "nixpkgs_2", "nixvim": "nixvim", "sops-nix": "sops-nix", diff --git a/flake.nix b/flake.nix index 9a78462..8b413f4 100644 --- a/flake.nix +++ b/flake.nix @@ -32,6 +32,10 @@ inputs.nixpkgs.follows = "nixpkgs"; inputs.flake-utils.follows = "flake-utils"; }; + nixos-needsreboot = { + url = "github:thefossguy/nixos-needsreboot"; + inputs.nixpkgs.follows = "nixpkgs"; + }; nixvim = { url = "github:nix-community/nixvim"; inputs.nixpkgs.follows = "nixpkgs"; @@ -109,7 +113,12 @@ } // flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ] (system: let pkgs = import nixpkgs { inherit system; - overlays = [ self.overlays.kyouma ]; + overlays = [ + self.overlays.kyouma + (_: _: { + nixos-needsreboot = (inputs.nixos-needsreboot.packages.${system}.default); + }) + ]; }; in { packages = shinyflakes.mapPackages (pkgs) { diff --git a/lib/shinyflakes/default.nix b/lib/shinyflakes/default.nix index a3dd49d..320bd74 100644 --- a/lib/shinyflakes/default.nix +++ b/lib/shinyflakes/default.nix @@ -32,7 +32,7 @@ inputs.sops-nix.nixosModules.sops ({ ... }: { nixpkgs.overlays = [ self.overlays.kyouma ]; - nixpkgs.hostPlatform = system; + nixpkgs.hostPlatform.system = system; }) ]; }; diff --git a/pkgs/overlay.nix b/pkgs/overlay.nix index 64b9470..ff2e0a5 100644 --- a/pkgs/overlay.nix +++ b/pkgs/overlay.nix @@ -1,3 +1,4 @@ final: prev: { nyastodon = final.callPackage ./nyastodon/default.nix { }; + upgrade-system = final.callPackage ./upgrade-system/default.nix { }; } diff --git a/pkgs/upgrade-system/default.nix b/pkgs/upgrade-system/default.nix new file mode 100644 index 0000000..78fc4f2 --- /dev/null +++ b/pkgs/upgrade-system/default.nix @@ -0,0 +1,13 @@ +{ pkgs, ... }: + +pkgs.writeShellApplication { + name = "upgrade-system"; + text = builtins.readFile ./upgrade-system.sh; + runtimeInputs = with pkgs; [ + curl + jq + nix + gnugrep + nixos-needsreboot + ]; +} diff --git a/pkgs/upgrade-system/upgrade-system.sh b/pkgs/upgrade-system/upgrade-system.sh new file mode 100644 index 0000000..cb5d43d --- /dev/null +++ b/pkgs/upgrade-system/upgrade-system.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +set -euo pipefail + +HYDRA_URL="https://hydra.kyouma.net/job/infra/nixfiles/nixosConfigurations.${HOSTNAME}/latest-finished" + +NEW_STORE_PATH="$(curl --fail -s -L -H "Accept: application/json" "${HYDRA_URL}" | jq -r ".buildoutputs.out.path")" + +OLD_STORE_PATH="$(readlink -f "/nix/var/nix/profiles/system")" + +OLD_STORE_SUFFIX="$(echo -n "$OLD_STORE_PATH" | tail -c 7)" + +get_old_path () { + for system in $(find /nix/var/nix/profiles/ -printf "%f\n" | grep "system-" | sort -nr); do + local store_path + store_path="$(readlink -f "/nix/var/nix/profiles/${system}")" + if [[ "$(echo -n "$store_path" | tail -c 7)" != "pre-git" ]]; then + OLD_STORE_PATH=$store_path + break + fi + done +} + +if [[ "$OLD_STORE_SUFFIX" == "pre-git" ]]; then + echo "Last update was pushed manually" + echo "Getting newest system profile that came from Hydra" + get_old_path +fi + +if [[ "$OLD_STORE_PATH" == "$NEW_STORE_PATH" ]]; then + echo "no update available. exiting" + exit 0 +fi + +echo "Downloading ${NEW_STORE_PATH}" +nix copy --from "https://cache.kyouma.net" "${NEW_STORE_PATH}" + +echo "Adding path to system profile" +nix-env -p "/nix/var/nix/profiles/system" --set "${NEW_STORE_PATH}" + +echo "Set new profile as boot target" +/nix/var/nix/profiles/system/bin/switch-to-configuration boot + +nixos-needsreboot + +if [[ -f "/var/run/reboot-required" ]]; then + delay=$(echo -n $RANDOM | tail -c 2) + echo "Rebooting system in ${delay} Minutes" + shutdown -r +"${delay}" +else + echo "Activating system now" + /nix/var/nix/profiles/system/bin/switch-to-configuration switch + echo "Finished upgrade" +fi