nixie/nixie
2025-10-13 13:23:56 +02:00

123 lines
3.1 KiB
Bash
Executable file

#!/usr/bin/env bash
NC='\033[0m'
Red='\033[1;31m'
Yellow='\033[1;33m'
Purple='\033[0;35m'
IPurple='\033[0;95m'
QUIT () {
popd > /dev/null
exit
}
CHOOSE_CASE () {
echo -e -n "${IPurple}What do you want to do? ${Purple}Edit/reBuild/Update/reView/Restore/Quit (e/b/u/v/r/q): ${NC}"
read CHOICE
case $CHOICE in
[eE]* ) EDIT ;;
[bB]* ) BUILD ;;
[uU]* ) UPDATE ;;
[vV]* ) REVIEW ;;
[rR]* ) RESTORE ;;
[qQ]* ) echo -e "${IPurple}Quitting nixie.${NC}" && QUIT ;;
*) echo -e -n "[ ${Yellow}incorrect input ${NC}] " && CHOOSE_CASE ;;
esac
}
EDIT () {
echo -e "${IPurple}Opening editor...${NC}"
sudo vim .
sudo git diff --stat
CHOOSE_CASE
}
BUILD () {
echo -e "${IPurple}Continuing will rebuild NixOS and commit changes to git."
echo -e -n "${IPurple}Are you sure you want to do this? ${Purple}Yes/No (Y/n): ${NC}"
read CHOICE
case $CHOICE in
[nN]* ) echo -e -n "Cancelled. " ;;
*)
sudo git add .
echo -e "${IPurple}Rebuilding NixOS...${NC}"
sudo nixos-rebuild switch
if [ $? -ne 0 ]; then
echo -e "[ ${Red}error ${NC}] ${Red}Rebuilding NixOS failed.${NC}"
CHOOSE_CASE
QUIT
fi
echo -e "${IPurple}Committing the changes...${NC}"
sudo git commit --no-status -am "$(sudo nixos-rebuild list-generations | grep True | sed 's/ .*//')"
echo -e "${IPurple}System rebuilt, configuration commited.${NC}"
echo -e -n "${IPurple}Push changes? ${Purple}Yes/Squash/No (y/s/N): ${NC}"
read CHOICE
case $CHOICE in
[yY]* )
echo -e "${IPurple}Pushing...${NC}"
sudo git push
;;
[sS]* )
echo -e "${IPurple}Squashing...${NC}"
sudo git rebase origin/HEAD
echo -e "${IPurple}Pushing...${NC}"
sudo git push
;;
*) echo -e -n "Not pushing changes. " ;;
esac
;;
esac
CHOOSE_CASE
}
UPDATE () {
echo -e -n "${IPurple}Are you sure you want to update flake.lock? ${Purple}Yes/No (Y/n): ${NC}"
read CHOICE
case $CHOICE in
[nN]* ) echo -e -n "Cancelled. " && CHOOSE_CASE ;;
*)
echo -e "${IPurple}Updating flake.lock..."
sudo nix flake update
if [ $? -ne 0 ]; then
echo -e "[ ${Red}error ${NC}] ${Red}Updating flake.lock failed.${NC}"
CHOOSE_CASE
QUIT
fi
echo -e -n "Updated. "
BUILD
;;
esac
}
REVIEW () {
echo -e "${IPurple}Showing changes...${NC}"
sudo git diff
CHOOSE_CASE
}
RESTORE () {
echo -e "[ ${Yellow}warning ${NC}] ${Purple}This will restore /etc/nixos to the last commit.${NC}"
echo -e -n "[ ${Yellow}warning ${NC}] ${Purple}Are you sure you want to disregard the changes? Yes/No (y/N): ${NC}"
read CHOICE
case $CHOICE in
[yY]* )
sudo -k git restore .
echo -e -n "Changes disregarded. "
;;
*) echo -e -n "Cancelled. " ;;
esac
CHOOSE_CASE
}
pushd /etc/nixos > /dev/null
echo -e "${IPurple}Running nixie. ${Purple}You might be asked to input the root password.${NC}"
sudo echo -n
if [ "$1" == "pull" ]; then
echo -e "${IPurple}Pulling from origin...${NC}"
sudo git pull
fi
CHOOSE_CASE
QUIT