-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·262 lines (214 loc) · 7.43 KB
/
install.sh
File metadata and controls
executable file
·262 lines (214 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env bash
# If in the live environment then start the live-install.sh script
if [ -d "/iso" ] || [ "$(findmnt -o FSTYPE -n /)" = "tmpfs" ]; then
sudo ./live-install.sh
exit 0
fi
# Check if running as root. If root, script will exit.
if [[ $EUID -eq 0 ]]; then
echo "This script should not be executed as root! Exiting..."
exit 1
fi
# Check if using NixOS. If not using NixOS, script will exit.
if [[ ! "$(grep -i nixos </etc/os-release)" ]]; then
echo "This installation script only works on NixOS! Download an iso at https://nixos.org/download/"
echo "You can either use this script in the live environment or booted into a system."
exit 1
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
success() {
echo -e "${GREEN}╔═══════════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ║${NC}"
echo -e "${GREEN}║ NixOS Installation Successful! ║${NC}"
echo -e "${GREEN}║ ║${NC}"
echo -e "${GREEN}║ Please reboot your system for the changes to take effect. ║${NC}"
echo -e "${GREEN}║ ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════════════════╝${NC}"
}
info() {
echo -e "\n${GREEN}$1${NC}"
}
warn() {
echo -e "${YELLOW}$1${NC}"
}
error() {
echo -e "${RED}Error: $1${NC}" >&2
}
currentUser=$(logname)
choose_drivers() {
local host="$1"
info "Choose GPU Drivers:"
echo "1) nvidia"
echo "2) amdgpu"
echo "3) intel"
while true; do
read -p "Enter choice (1, 2 or 3): " driver_choice
case $driver_choice in
1)
sed -i -e "s/videoDriver = .*;/videoDriver = \"nvidia\";/" "./hosts/$host/variables.nix"
break
;;
2)
sed -i -e "s/videoDriver = .*;/videoDriver = \"amdgpu\";/" "./hosts/$host/variables.nix"
break
;;
3)
sed -i -e "s/videoDriver = .*;/videoDriver = \"intel\";/" "./hosts/$host/variables.nix"
break
;;
*) error "Invalid choice. Enter 1, 2, or 3." ;;
esac
done
}
open_variables() {
local selected_host="$1"
# Find available editor and open
for editor in "${EDITOR}" nano vim vi; do
if command -v "$editor" &>/dev/null; then
$editor "./hosts/$selected_host/variables.nix"
break
fi
done
}
list_hosts() {
local hosts=()
for host_dir in ./hosts/*/; do
if [ -d "$host_dir" ]; then
host_name=$(basename "$host_dir")
hosts+=("$host_name")
fi
done
printf '%s\n' "${hosts[@]}"
}
create_new_host() {
local new_name="$1"
local template="$2"
if [ -z "$new_name" ]; then
error "Host name cannot be empty"
return 1
fi
if [ -d "./hosts/$new_name" ]; then
error "Host '$new_name' already exists"
return 1
fi
if [ ! -d "./hosts/$template" ]; then
error "Template host '$template' does not exist"
return 1
fi
info "Creating new host '$new_name' from template '$template'..."
cp -r "./hosts/$template" "./hosts/$new_name" || {
error "Failed to copy template"
return 1
}
# Remove old hardware config
rm -f "./hosts/$selected_host/hardware-configuration.nix"
# Update hostname in the new host's variables.nix if it exists
if [ -f "./hosts/$new_name/variables.nix" ]; then
sed -i -e "s/hostname = .*;/hostname = \"$new_name\";/" "./hosts/$new_name/variables.nix"
fi
echo "Host '$new_name' created successfully."
return 0
}
add_host_to_flake() {
local host_name="$1"
info "Adding host '$host_name' to flake.nix..."
# Check if host already exists in flake
if grep -q "\"$host_name\"" flake.nix; then
warn "Host '$host_name' already exists in flake.nix"
return 0
fi
# Find the nixosConfigurations section and add the new host
# This uses awk to insert the new host line after the opening brace
awk -v host="$host_name" '
/nixosConfigurations = {/ {
print
getline
print
print " " host " = mkHost \"" host "\";"
next
}
{ print }
' flake.nix >flake.nix.tmp && mv flake.nix.tmp flake.nix
info "Host '$host_name' added to flake.nix"
return 0
}
info "NixOS Configuration Host Selection"
available_hosts=($(list_hosts))
if [ ${#available_hosts[@]} -eq 0 ]; then
error "No hosts found in hosts directory"
exit 1
fi
echo -e "\nAvailable hosts:"
for i in "${!available_hosts[@]}"; do
echo " $((i + 1))) ${available_hosts[$i]}"
done
echo " n) Create new host"
while true; do
read -p "Select host to use [Default: 1]: " host_choice
host_choice=${host_choice:-1}
if [[ "$host_choice" == "n" ]] || [[ "$host_choice" == "N" ]]; then
# Create new host
read -p "Enter name for new host: " new_host_name
# Validate host name (alphanumeric, hyphens, and underscores only)
if [[ ! "$new_host_name" =~ ^[a-zA-Z0-9_-]+$ ]]; then
error "Invalid host name. Use only letters, numbers, hyphens, and underscores."
continue
fi
echo "Select template host:"
for i in "${!available_hosts[@]}"; do
echo " $((i + 1))) ${available_hosts[$i]}"
done
read -p "Template choice [Default: 1]: " template_choice
template_choice=${template_choice:-1}
if [[ "$template_choice" =~ ^[0-9]+$ ]] && [ "$template_choice" -ge 1 ] && [ "$template_choice" -le ${#available_hosts[@]} ]; then
template_host="${available_hosts[$((template_choice - 1))]}"
if create_new_host "$new_host_name" "$template_host"; then
selected_host="$new_host_name"
# Add host to flake.nix
add_host_to_flake "$selected_host"
break
fi
else
error "Invalid template choice"
fi
elif [[ "$host_choice" =~ ^[0-9]+$ ]] && [ "$host_choice" -ge 1 ] && [ "$host_choice" -le ${#available_hosts[@]} ]; then
selected_host="${available_hosts[$((host_choice - 1))]}"
break
else
error "Invalid choice. Please try again."
fi
done
info "Using host: $selected_host"
# Select GPU Drivers
choose_drivers $selected_host
# Replace username variable in variables.nix with $USER
sed -i -e "s/username = .*;/username = \"$currentUser\";/" "./hosts/$selected_host/variables.nix"
# Ask if user wants to edit variables.nix
read -p "Edit variables.nix for host: $selected_host? (Y/n): " edit_vars
if [[ ! "$edit_vars" =~ ^[nN]$ ]]; then
open_variables $selected_host
fi
# Generate Hardware Configuration
info "Generating hardware configuration..."
if [ -f "/etc/nixos/hardware-configuration.nix" ]; then
sudo cp "/etc/nixos/hardware-configuration.nix" "./hosts/$selected_host/hardware-configuration.nix"
else
sudo nixos-generate-config --show-hardware-config | sudo tee "./hosts/$selected_host/hardware-configuration.nix" >/dev/null
fi
sudo git -C . add "hosts/$selected_host/*" 2>/dev/null || true
sudo git -C . add "flake.nix" 2>/dev/null || true
# Build the new configuration
info "Building NixOS configuration for host: $selected_host"
sudo nixos-rebuild boot --flake ".#$selected_host"
if [ $? -eq 0 ]; then
echo
success
# else
# echo
# failed
fi