-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-nixos.sh
executable file
·155 lines (113 loc) · 3.76 KB
/
install-nixos.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
#!/usr/bin/env bash
# Setup disk layout before installing Linux
# trigger error functions and pipes
set -Euo pipefail
shopt -s inherit_errexit failglob
#IFS=$'\n\t'
# switch to root
if [[ "$(whoami)" != "root" ]]; then
echo "Must be run as root!"
exit
fi
FUNC_NAME="unknown"
MOUNT_DIR="/mnt"
BOOT_LABEL="NIX_BOOT"
SWAP_LABEL="NIX_SWAP"
ROOT_LABEL="NIX_ROOT"
cleanup() {
echo -e "\nCleanup Step\n"
grep -qs "${MOUNT_DIR} " /proc/mounts && umount -R ${MOUNT_DIR}
if cat /proc/swaps | grep -qs "/dev/"; then
swapoff /dev/disk/by-label/${SWAP_LABEL}
fi
}
# on interupt or exit
trap cleanup INT TERM HUP
catch() {
cleanup
echo "| Issue at: ${FUNC_NAME}"
echo "| Line: ${1} | Error Code: ${2}"
echo "| Command: ${BASH_COMMAND}"
exit "${2}"
}
# proper error handling
trap 'catch ${LINENO} ${?}' ERR
read_diskname() {
FUNC_NAME="read_diskname"
read -u 2 -rp "${1}" disk
while ! [[ -b "/dev/${disk}" ]]; do
read -u 2 -rp "Please enter a valid device: " disk
done
echo "${disk}"
}
setup_disk() {
FUNC_NAME="setup_disk"
lsblk -o name,label,size
echo
dev_to_part=$(read_diskname "Select disk [ex: sda]: ")
read -rp "Should we partition? [yes/no]: " should_partition
if [[ ${should_partition} == "yes" ]]; then
echo -e "\nPartitioning Step\n"
echo "Make 1GB EFI, (RAM/2)GB swap, rest root"
read -srp "Press enter to continue..."
echo
cfdisk /dev/"${dev_to_part}"
fi
read -rp "Should we format? [yes/no]: " should_format
if [[ ${should_format} == "yes" ]]; then
echo -e "\nFormatting Step\n"
lsblk -o name,label,size
echo
boot_part=$(read_diskname "Enter BOOT partition [ex: sda1]: ")
swap_part=$(read_diskname "Enter SWAP partition [ex: sda2]: ")
root_part=$(read_diskname "Enter ROOT partition [ex: sda3]: ")
mkfs.vfat -n ${BOOT_LABEL} -F 32 /dev/"${boot_part}"
mkswap -L ${SWAP_LABEL} /dev/"${swap_part}"
mkfs.btrfs -f -L ${ROOT_LABEL} /dev/"${root_part}"
#re-read the disk
partprobe /dev/"${dev_to_part}"
fi
}
mount_disk() {
FUNC_NAME="mount_disk"
echo -e "\nMounting Step\n"
# unmount just in case
grep -qs "${MOUNT_DIR} " /proc/mounts && umount -R ${MOUNT_DIR}
# enable swap
if ! cat /proc/swaps | grep -qs "/dev/"; then
swapon /dev/disk/by-label/${SWAP_LABEL}
fi
# mount root
[[ -d ${MOUNT_DIR} ]] || mkdir -p ${MOUNT_DIR}
grep -qs "${MOUNT_DIR} " /proc/mounts || \
mount -t btrfs /dev/disk/by-label/${ROOT_LABEL} ${MOUNT_DIR}
# create btrfs subvolumes
if ! btrfs subvolume list /mnt | grep -qs "@home"; then
btrfs subvolume create ${MOUNT_DIR}/@root
btrfs subvolume create ${MOUNT_DIR}/@home
btrfs subvolume create ${MOUNT_DIR}/@nix
fi
# unmount
grep -qs "${MOUNT_DIR} " /proc/mounts && umount -R ${MOUNT_DIR}
mount_opts="compress-force=zstd,commit=60,noatime,ssd,nodiscard"
# mount root
mount -o ${mount_opts},subvol=@root /dev/disk/by-label/${ROOT_LABEL} ${MOUNT_DIR}
# create folders
[[ -d ${MOUNT_DIR}/home ]] || mkdir -p ${MOUNT_DIR}/home
[[ -d ${MOUNT_DIR}/nix ]] || mkdir -p ${MOUNT_DIR}/nix
[[ -d ${MOUNT_DIR}/boot ]] || mkdir -p ${MOUNT_DIR}/boot
# mount everything else
mount -o ${mount_opts},subvol=@home /dev/disk/by-label/${ROOT_LABEL} ${MOUNT_DIR}/home
mount -o ${mount_opts},subvol=@nix /dev/disk/by-label/${ROOT_LABEL} ${MOUNT_DIR}/nix
mount /dev/disk/by-label/${BOOT_LABEL} ${MOUNT_DIR}/boot
}
install_nix() {
FUNC_NAME="install_nix"
echo -e "\nInstall Step\n"
nixos-install --no-root-passwd --flake "github:ivangeorgiew/dots#mahcomp"
}
# Execution
setup_disk
mount_disk
install_nix
cleanup