Kieran's opinionated (and probably slightly dumb) nix config
1#!/usr/bin/env bash
2
3# NixOS Installation Script
4# This script automates the post-network setup installation process
5
6set -e # Exit on any error
7
8echo "==== NixOS Installation Automation ===="
9echo "This script will automate the installation process after network setup."
10echo "Make sure you have already set up network connectivity before running this script."
11
12# Verify network connectivity
13echo -n "Checking network connectivity... "
14if ping -c 1 1.1.1.1 &> /dev/null; then
15 echo "Success!"
16else
17 echo "Failed!"
18 echo "Error: No internet connection detected. Please set up your network first."
19 echo "You can use the following commands to connect to WiFi:"
20 echo " sudo systemctl start wpa_supplicant"
21 echo " wpa_cli"
22 echo " > add_network 0"
23 echo " > set_network 0 ssid \"your SSID\""
24 echo " > set_network 0 psk \"your password\""
25 echo " > enable network 0"
26 echo " > exit"
27 exit 1
28fi
29
30# Get sudo privileges and maintain them
31echo "Acquiring root permissions..."
32sudo -v
33# Keep sudo privileges active
34while true; do sudo -v; sleep 60; done &
35KEEP_SUDO_PID=$!
36
37# Function to clean up the background sudo process on exit
38cleanup() {
39 kill $KEEP_SUDO_PID 2>/dev/null
40}
41trap cleanup EXIT
42
43# Check if git is already enabled in the configuration
44echo "Checking git configuration..."
45if grep -q "programs.git.enable = true" /etc/nixos/configuration.nix; then
46 echo "Git is already enabled in configuration.nix"
47else
48 echo "Enabling git..."
49 sudo sed -i 's/^{$/{\n programs.git.enable = true;/' /etc/nixos/configuration.nix
50 sudo nixos-rebuild switch
51fi
52
53# Download and run the disk configuration
54echo "Downloading and running disk configuration..."
55curl -L https://github.com/taciturnaxolotl/dots/raw/main/moonlark/disk-config.nix -o /tmp/disk-config.nix
56sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode destroy,format,mount /tmp/disk-config.nix
57
58# Generate NixOS configuration
59echo "Generating NixOS configuration..."
60sudo nixos-generate-config --root /mnt
61cd /mnt/etc/nixos
62
63# Check if repository already exists
64echo "Setting up configuration..."
65if [ -d ".git" ]; then
66 echo "Configuration already exists, pulling latest changes..."
67 sudo git pull
68else
69 # Clone the repository to the NixOS configuration directory
70 echo "Cloning nixos repository..."
71 sudo rm -f *
72 sudo git clone https://github.com/taciturnaxolotl/dots.git .
73fi
74
75# Prompt user for SSH key setup
76echo ""
77echo "SSH Key Setup"
78read -p "Do you want to add an SSH private key? (y/n): " add_ssh_key
79if [[ "$add_ssh_key" =~ ^[Yy]$ ]]; then
80 echo "How would you like to add your SSH key?"
81 echo "1) From a local file"
82 echo "2) From a URL"
83 read -p "Enter your choice (1/2): " ssh_key_method
84
85 sudo mkdir -p /mnt/etc/ssh/
86
87 if [[ "$ssh_key_method" == "1" ]]; then
88 echo "Please enter the path to your SSH private key:"
89 read ssh_key_path
90
91 if [ -f "$ssh_key_path" ]; then
92 sudo cp "$ssh_key_path" /mnt/etc/ssh/id_rsa
93 sudo chmod 600 /mnt/etc/ssh/id_rsa
94 echo "SSH key added from local file!"
95 else
96 echo "Warning: SSH key file not found. Proceeding without SSH key."
97 fi
98 elif [[ "$ssh_key_method" == "2" ]]; then
99 echo "Please enter the URL to download your SSH private key:"
100 read ssh_key_url
101
102 echo "Downloading SSH key from URL..."
103 if curl -s "$ssh_key_url" -o /tmp/downloaded_ssh_key; then
104 sudo mv /tmp/downloaded_ssh_key /mnt/etc/ssh/id_rsa
105 sudo chmod 600 /mnt/etc/ssh/id_rsa
106 echo "SSH key successfully downloaded and added!"
107 else
108 echo "Warning: Failed to download SSH key from URL. Proceeding without SSH key."
109 fi
110 else
111 echo "Invalid choice. Proceeding without SSH key."
112 fi
113else
114 echo "Proceeding without SSH key."
115fi
116
117# Prompt for hostname configuration
118echo ""
119echo "Hostname Configuration"
120echo "Available configurations in this repo:"
121echo "1) moonlark (default)"
122read -p "Which configuration would you like to use? (Press Enter for moonlark): " hostname_choice
123hostname=${hostname_choice:-moonlark}
124
125# Install the flake
126echo "Installing the flake for configuration: $hostname"
127sudo nixos-install --flake .#${hostname} --no-root-passwd
128
129echo "Installation complete! The system will now reboot."
130echo ""
131echo "After reboot, you'll need to complete these post-installation tasks:"
132echo "1. Change your password"
133echo "2. Move config to local directory: sudo mv /etc/nixos ~/dots"
134echo "3. Link to /etc/nixos: sudo ln -s ~/dots /etc"
135echo "4. Change permissions: sudo chown -R \$(id -un):users ~/dots"
136echo "5. Setup fingerprint reader (optional): sudo fprintd-enroll -f right-index-finger \$(whoami)"
137
138read -p "Press Enter to unmount and reboot..."
139sudo umount -R /mnt
140sudo reboot