#!/usr/bin/env bash check_root() { if [[ "${EUID}" -ne 0 ]]; then echo "Please run the script as root." exit 1 fi } check_network() { 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 "Do you want to connect to an open network? [y/N]" read -r open_network if [[ "${open_network}" =~ ^([yY][eE][sS]|[yY])$ ]]; then echo "Enter the SSID of the open network:" read -r ssid wpa_supplicant -i "${interface}" -c <(wpa_passphrase "${ssid}") -B else echo "Enter the SSID:" read -r ssid 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 } select_host() { echo "Available hosts:" echo $(nix flake show --json | nix shell nixpkgs#jq --command jq -r '.nixosConfigurations | keys[]') echo "Enter host:" read -r host } select_users() { echo "Available users:" ls users/ echo "Enter the users to copy keys for (space-separated):" read -r -a users } 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 user in "${users[@]}"; do mkdir -p "/mnt/persist/home/${user}/.local/share/sops-nix" cp "./users/${user}/secrets/key.txt" "/mnt/persist/home/${user}/.local/share/sops-nix/key.txt" 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) select_users 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