env-nixos/activate.sh

66 lines
1.7 KiB
Bash
Raw Normal View History

2023-09-05 21:23:05 -07:00
#!/usr/bin/env bash
# Activate the modified configuyrsation.nix
## Setup ##
DIR="$(dirname -- "${BASH_SOURCE[0]}")"
PROG="$(basename -- "${BASH_SOURCE[0]}")"
2023-10-17 04:33:40 -07:00
nix_ext="nix"
nixos_dir="/etc/nixos"
date_YYYYMMDD="`date "+%Y%m%d"`"
backup_dir="$nixos_dir/${date_YYYYMMDD}_Backups"
2023-09-05 21:23:05 -07:00
## Main ##
echo "Requesting sudo password if it has not already been requested recently."
2023-10-17 04:33:40 -07:00
sudo echo "Success!"
# Make a backup if one does not already exist for today.
if [[ ! -e "$backup_dir" ]]; then
echo -e "\nSaving backups for today."
sudo mkdir -pv "$backup_dir"
sudo cp -v "$nixos_dir"/*."$nix_ext" "$backup_dir"/
fi
# Ensure unmaintained files exist for import.
nix_static=$nixos_dir/static.nix
if [[ ! -e $nix_static ]]; then
echo "Creating '$nix_static'."
echo -e "{ config, pkgs, nix, ... }:\n\n{\n #\n}" | sudo tee $nix_static
fi
nix_ansible=$nixos_dir/ansible.nix
if [[ ! -e $nix_ansible ]]; then
echo "Creating '$nix_ansible' from '$nix_static'."
cp -v $nix_static $nix_ansible
fi
2023-09-05 21:23:05 -07:00
# Start the chain.
2023-10-17 04:33:40 -07:00
sleep 0 &&
2023-09-05 21:23:05 -07:00
# Essentials, jeez!
2023-10-17 04:33:40 -07:00
echo -e "\nMaking sure that /bin/bash is available." &&
sudo ln -vfs `which bash` /bin/bash &&
# Install Home Manager for usage in configuration.nix type files.
echo -e "\nAdd Home Manager." &&
sudo nix-channel \
--add https://github.com/nix-community/home-manager/archive/master.tar.gz \
home-manager
sudo nix-channel --update
2023-09-05 21:23:05 -07:00
# Main install.
2023-10-17 04:33:40 -07:00
echo -e "\nSwitching to the new configuration." &&
sudo cp "$DIR"/*."$nix_ext" "$nixos_dir"/ &&
2023-09-05 21:23:05 -07:00
sudo nixos-rebuild switch &&
2023-10-17 04:33:40 -07:00
# Completed successfully.
echo -e "\nSuccess!" &&
2023-09-05 21:23:05 -07:00
exit 0
## Errors ##
status="$?"
echo "ERROR: A command failed with status $status, please check the output."
exit $status