feat(packages/rebuild): improve rebuild script

This commit is contained in:
Mohammad Rafiq 2025-05-25 22:34:35 +08:00
parent 70814354aa
commit fbec06178a
No known key found for this signature in database

View file

@ -1,42 +1,49 @@
{ pkgs, ... }: { pkgs, ... }:
pkgs.writeShellScriptBin "rebuild" # sh pkgs.writeShellScriptBin "rebuild" # sh
'' ''
CURRENT_GENERATION=$(readlink /nix/var/nix/profiles/system | cut -d- -f2)
if [ ! -f "flake.nix" ]; then if [ ! -f "flake.nix" ]; then
echo "Error: flake.nix not found in the current directory. Exiting." echo "Error: flake.nix not found in the current directory. Exiting."
exit 1 # Indicate an error exit 1 # Indicate an error
fi fi
echo "--- building the new config... ---"
git add . git add .
nh os test . || { nh os test . || {
echo "Error: nixos-rebuild switch failed. Check the output for details." echo "Error: nixos-rebuild switch failed. Check the output for details."
exit 1 exit 1
} }
echo "--- diffing... ---"
git diff HEAD --color=always --stat --patch git diff HEAD --color=always --stat --patch
echo "--- opening test shell... ---" (export PS1="Test shell> "
${pkgs.cowsay}/bin/cowsay -f elephant "Entering a test shell. Type 'exit 1' to abort changes and 'exit' to commit." exec ${pkgs.bash}/bin/bash) || {
PS1="Test shell> " $SHELL || {
${pkgs.cowsay}/bin/cowsay "You aborted." ${pkgs.cowsay}/bin/cowsay "You aborted."
exit 1 exit 1
} }
echo "--- adding the built configuration to the bootloader... ---" export NIXOS_LABEL="$(date +%d%m%y\ %H:%M:%S)"
nh os boot . || { nh os boot . || {
echo "Error: nixos-rebuild switch failed. Check the output for details." echo "Error: nixos-rebuild switch failed. Check the output for details."
exit 1 exit 1
} }
git commit
read -p "Reboot the system now? (y/n) [n]: " -n 1 -r NEW_GENERATION=$(readlink /nix/var/nix/profiles/system | cut -d- -f2)
echo # (optional) move to a new line echo "New generation is $NEW_GENERATION. Current is $CURRENT_GENERATION."
if [[ $REPLY =~ ^[Yy]$ ]]; then if [ ! $NEW_GENERATION -gt $CURRENT_GENERATION ]; then
echo "Rebooting the system..." echo "ERROR: New config was not added to bootloader. Exiting..."
sudo systemctl reboot exit 1
else else
echo "Not rebooting." git commit
exit 0
read -p "Reboot the system now? (y/n) [n]: " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Rebooting the system..."
sudo systemctl reboot
else
echo "Not rebooting."
exit 0
fi
fi fi
'' ''