#!/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 "Connect to a network before proceeding." exit 1 fi setup_wifi } setup_wifi() { echo "Available Wi-Fi interfaces:" nmcli device status | awk '$2 == "wifi" {print $1}' echo "Enter the Wi-Fi interface you want to use:" read -r interface echo "Scanning for Wi-Fi networks..." nmcli device wifi rescan echo "Available Wi-Fi networks:" nmcli device wifi list echo "Enter the SSID of the network:" read -r ssid echo "Is this network open? [y/N]" read -r open_network if [[ "${open_network}" =~ ^([yY][eE][sS]|[yY])$ ]]; then nmcli device wifi connect "${ssid}" ifname "${interface}" else echo "Enter the passphrase:" read -rs passphrase nmcli device wifi connect "${ssid}" password "${passphrase}" ifname "${interface}" fi 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 } copy_config() { echo "Would you like to copy the current configuration (including keys) to the target system? [y/N]" read -r copy_config if [[ "${copy_config}" =~ ^([yY][eE][sS]|[yY])$ ]]; then rm -rf /mnt/etc/nixos cp -r . /mnt/etc/nixos echo "Configuration copied successfully." fi } 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 copy_config echo "Installation complete. Reboot your system." ;; 2) prepare_disk "mount" install echo "Repair complete. Reboot your system." ;; *) echo "Invalid choice." ;; esac } main