forked from emily/nixfiles
added system-upgrade pkg
This commit is contained in:
parent
43340ba8a9
commit
c8c8d75da9
7 changed files with 101 additions and 2 deletions
|
@ -24,6 +24,7 @@
|
|||
"github:"
|
||||
"git+https://"
|
||||
"git+ssh://"
|
||||
"https://"
|
||||
];
|
||||
};
|
||||
programs.ssh = {
|
||||
|
|
21
flake.lock
21
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",
|
||||
|
|
11
flake.nix
11
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) {
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
inputs.sops-nix.nixosModules.sops
|
||||
({ ... }: {
|
||||
nixpkgs.overlays = [ self.overlays.kyouma ];
|
||||
nixpkgs.hostPlatform = system;
|
||||
nixpkgs.hostPlatform.system = system;
|
||||
})
|
||||
];
|
||||
};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
final: prev: {
|
||||
nyastodon = final.callPackage ./nyastodon/default.nix { };
|
||||
upgrade-system = final.callPackage ./upgrade-system/default.nix { };
|
||||
}
|
||||
|
|
13
pkgs/upgrade-system/default.nix
Normal file
13
pkgs/upgrade-system/default.nix
Normal file
|
@ -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
|
||||
];
|
||||
}
|
54
pkgs/upgrade-system/upgrade-system.sh
Normal file
54
pkgs/upgrade-system/upgrade-system.sh
Normal file
|
@ -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
|
Loading…
Reference in a new issue