Files
nix/scripts/install.sh
Nikolaos Karaolidis e23e71560f Add elara
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
2024-12-18 20:57:26 +00:00

152 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
check_root() {
if [[ "${EUID}" -ne 0 ]]; then
echo "Please run the script as root."
exit 1
fi
}
check_network() {
rfkill unblock all
if ping -c 1 google.com &>/dev/null; then
echo "Network connection detected, skipping Wi-Fi setup."
return
fi
echo "No network connection detected."
echo "Would you like to connect to a Wi-Fi network? [y/N]"
read -r connect_wifi
if ! [[ "${connect_wifi}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Please connect to a network before proceeding."
exit 1
fi
setup_wifi
}
setup_wifi() {
echo "Available network interfaces:"
ip link show | grep -E '^[0-9]+:' | awk '{print $2}' | tr -d ':'
echo "Enter the network interface you want to use:"
read -r interface
echo "Enter the SSID of the open network:"
read -r ssid
echo "Do you want to connect to an open network? [y/N]"
read -r open_network
if [[ "${open_network}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
wpa_supplicant -i "${interface}" -c <(wpa_passphrase "${ssid}") -B
else
echo "Enter the passphrase:"
read -rs passphrase
wpa_passphrase "${ssid}" "${passphrase}" > wifi.conf
wpa_supplicant -i "${interface}" -c wifi.conf -B
rm wifi.conf
fi
dhcpcd
echo "Waiting for a network connection..."
for i in {1..10}; do
if ping -c 1 google.com &>/dev/null; then
echo "Connected to the network successfully."
return
fi
sleep 1
done
echo "Failed to establish a connection within the timeout period."
exit 1
}
select_host() {
echo "Available hosts:"
echo $(nix --experimental-features "nix-command flakes" flake show --json |
nix --experimental-features "nix-command flakes" shell nixpkgs#jq --command jq -r '.nixosConfigurations | keys[]')
echo "Enter host:"
read -r host
}
prepare_disk() {
local mode="$1"
device=$(grep -oP '(?<=device = ")[^"]+' "./hosts/${host}/default.nix")
nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode "${mode}" "./hosts/${host}/format.nix" --arg device "\"${device}\""
}
copy_keys() {
mkdir -p /mnt/persist/etc/ssh
cp "./hosts/${host}/secrets/ssh_host_ed25519_key" /mnt/persist/etc/ssh/ssh_host_ed25519_key
for path in "./hosts/${host}/users"/*; do
user=$(basename "${path}")
echo "User detected: ${user}"
echo "Available keys for ${user}:"
ls ./secrets/*/key.txt
echo "Enter the key file to copy (or press Enter to skip this user):"
read -r key
if [[ -z "${key}" ]]; then
echo "Skipping ${user}"
continue
fi
mkdir -p "/mnt/persist/home/${user}/.config/sops-nix"
cp "${key}" "/mnt/persist/home/${user}/.config/sops-nix/key.txt"
uid=$(cat "./hosts/${host}/users/${user}/uid")
gid=100
chown -R "${uid}:${gid}" "/mnt/persist/home/${user}"
done
}
install() {
nixos-install --root /mnt --flake ".#${host}"
}
main() {
check_root
check_network
select_host
echo "What would you like to do with ${host}?"
echo "1) Install"
echo "2) Repair"
read -r choice
case ${choice} in
1)
prepare_disk "disko"
copy_keys
install
echo "Installation complete. Please reboot your system."
;;
2)
prepare_disk "mount"
install
echo "Repair complete. Please reboot your system."
;;
*)
echo "Invalid choice."
;;
esac
}
main