-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·218 lines (179 loc) · 6.2 KB
/
install.sh
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
function yesno() {
local prompt="$1"
while true; do
read -rp "$prompt [y/n] " yn
case $yn in
[Yy]* ) echo "y"; return;;
[Nn]* ) echo "n"; return;;
* ) echo "Please answer yes or no.";;
esac
done
}
cat << Introduction
The *entire* disk will be formatted with a 1GB boot partition
(labelled NIXBOOT), 16GB of swap, and the rest allocated to ZFS.
The following ZFS datasets will be created:
- zroot/root (mounted at / with blank snapshot)
- zroot/nix (mounted at /nix)
- zroot/tmp (mounted at /tmp)
- zroot/persist (mounted at /persist)
- zroot/cache (mounted at /cache)
** IMPORTANT **
This script assumes that the relevant "fileSystems" are declared within the
NixOS config to be installed. It does not create any hardware configuration
or modify the NixOS config to be installed in any way. If you have not done
so, you will need to add the necessary zfs options and filesystems before
proceeding or your install WILL NOT BOOT.
Introduction
# in a vm, special case
if [[ -b "/dev/vda" ]]; then
DISK="/dev/vda"
else
# listing with the standard lsblk to help with viewing partitions
lsblk
# Get the list of disks
mapfile -t disks < <(lsblk -ndo NAME,SIZE,MODEL)
echo -e "\nAvailable disks:\n"
for i in "${!disks[@]}"; do
printf "%d) %s\n" $((i+1)) "${disks[i]}"
done
# Get user selection
while true; do
echo ""
read -rp "Enter the number of the disk to install to: " selection
if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le ${#disks[@]} ]; then
break
else
echo "Invalid selection. Please try again."
fi
done
# Get the selected disk
DISK="/dev/$(echo "${disks[$selection-1]}" | awk '{print $1}')"
fi
# if disk contains "nvme", append "p" to partitions
if [[ "$DISK" =~ "nvme" ]]; then
BOOTDISK="${DISK}p3"
SWAPDISK="${DISK}p2"
ZFSDISK="${DISK}p1"
else
BOOTDISK="${DISK}3"
SWAPDISK="${DISK}2"
ZFSDISK="${DISK}1"
fi
echo "Boot Partiton: $BOOTDISK"
echo "SWAP Partiton: $SWAPDISK"
echo "ZFS Partiton: $ZFSDISK"
echo ""
do_format=$(yesno "This irreversibly formats the entire disk. Are you sure?")
if [[ $do_format == "n" ]]; then
exit
fi
echo "Creating partitions"
sudo blkdiscard -f "$DISK"
sudo sgdisk --clear"$DISK"
sudo sgdisk -n3:1M:+1G -t3:EF00 "$DISK"
sudo sgdisk -n2:0:+16G -t2:8200 "$DISK"
sudo sgdisk -n1:0:0 -t1:BF01 "$DISK"
# notify kernel of partition changes
sudo sgdisk -p "$DISK" > /dev/null
sleep 5
echo "Creating Swap"
sudo mkswap "$SWAPDISK" --label "SWAP"
sudo swapon "$SWAPDISK"
echo "Creating Boot Disk"
sudo mkfs.fat -F 32 "$BOOTDISK" -n NIXBOOT
# setup encryption
use_encryption=$(yesno "Use encryption? (Encryption must also be enabled within host config.)")
if [[ $use_encryption == "y" ]]; then
encryption_options=(-O encryption=aes-256-gcm -O keyformat=passphrase -O keylocation=prompt)
else
encryption_options=()
fi
echo "Creating base zpool"
sudo zpool create -f \
-o ashift=12 \
-o autotrim=on \
-O compression=zstd \
-O acltype=posixacl \
-O atime=off \
-O xattr=sa \
-O normalization=formD \
-O mountpoint=none \
"${encryption_options[@]}" \
zroot "$ZFSDISK"
echo "Creating /"
sudo zfs create -o mountpoint=legacy zroot/root
sudo zfs snapshot zroot/root@blank
sudo mount -t zfs zroot/root /mnt
# create the boot parition after creating root
echo "Mounting /boot (efi)"
sudo mount --mkdir "$BOOTDISK" /mnt/boot
echo "Creating /nix"
sudo zfs create -o mountpoint=legacy zroot/nix
sudo mount --mkdir -t zfs zroot/nix /mnt/nix
echo "Creating /tmp"
sudo zfs create -o mountpoint=legacy zroot/tmp
sudo mount --mkdir -t zfs zroot/tmp /mnt/tmp
echo "Creating /cache"
sudo zfs create -o mountpoint=legacy zroot/cache
sudo mount --mkdir -t zfs zroot/cache /mnt/cache
# handle persist, possibly from snapshot
restore_snapshot=$(yesno "Do you want to restore from a persist snapshot?")
if [[ $restore_snapshot == "y" ]]; then
echo "Enter full path to snapshot: "
read -r snapshot_file_path
echo
echo "Creating /persist"
# disable shellcheck (sudo doesn't affect redirects)
# shellcheck disable=SC2024
sudo zfs receive -o mountpoint=legacy zroot/persist < "$snapshot_file_path"
else
echo "Creating /persist"
sudo zfs create -o mountpoint=legacy zroot/persist
fi
sudo mount --mkdir -t zfs zroot/persist /mnt/persist
# Get repo to install from
read -rp "Enter flake URL (default: github:iynaix/dotfiles): " repo
repo="${repo:-github:iynaix/dotfiles}"
# qol for iynaix os
if [[ $repo == "github:iynaix/dotfiles" ]]; then
hosts=("desktop" "framework" "xps" "vm" "vm-hyprland")
echo "Available hosts:"
for i in "${!hosts[@]}"; do
printf "%d) %s\n" $((i+1)) "${hosts[i]}"
done
while true; do
echo ""
read -rp "Enter the number of the host to install: " selection
if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le ${#hosts[@]} ]; then
host="${hosts[$selection-1]}"
break
else
echo "Invalid selection. Please enter a number between 1 and ${#hosts[@]}."
fi
done
else
read -rp "Which host to install?" host
fi
read -rp "Enter git rev for flake (default: main): " git_rev
echo "Installing NixOS"
# nixos minimal iso does not have git for whatever fucking stupid reason???
if [[ $repo == "github:iynaix/dotfiles" ]]; then
# root password is irrelevant if initialPassword is set in the config
nix-shell -p git nixFlakes --command \
"sudo nixos-install --no-root-password --flake \"$repo/${git_rev:-main}#$host\" --option tarball-ttl 0"
else
nix-shell -p git nixFlakes --command \
"sudo nixos-install --flake \"$repo/${git_rev:-main}#$host\" --option tarball-ttl 0"
fi
# only relevant for iynaix os
if [[ $repo == "github:iynaix/dotfiles" ]]; then
echo "To setup secrets, run \"install-remote-secrets\" on the other host."
IP_ADDR=$(ifconfig | awk '/inet / && !/127.0.0.1/ {print $2; exit}')
echo "The IP address of this host is $IP_ADDR"
fi
echo "Installation complete. It is now safe to reboot."