-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnixos-installer.sh
executable file
·201 lines (162 loc) · 5.87 KB
/
nixos-installer.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
#!/bin/sh
HDD="/dev/sda"
USER=""
PASSWORD=""
HOST_NAME=""
ENCRYPT=""
KEY=""
TYPE=""
# The timezone of the host.
TIME_ZONE="Australia/Adelaide"
AUTO_GC="false"
AUTO_DEDUP="false"
AUTO_UPDATE="false"
AUTO_REBOOT="false"
# The WiFi configuration.
ETH_DHCP=""
WIFI_ENABLE="false"
WIFI_SSID=""
WIFI_PASSWD=""
# Guest Additions.
VBOX_GUEST="false"
VMWARE_GUEST="false"
X11_GUEST="false"
# Include the common helper functions.
. $(dirname "$0")/hdd_setup/helpers.sh
# Path to the generators.
GEN_PATH=$(dirname "$0")/generators
FILE_NAME="${0##*/}"
################################################################################
# Check whether the combination of arguments look sensible.
################################################################################
validate_args() {
if [[ ${ENCRYPT} == "full" || ${ENCRYPT} == "root" ]]; then
if [ ${KEY} == "" ]; then
ehco "An ecryption key must be specified using -k or --key."
fi
fi
}
################################################################################
# Prepare for installation. This has to perform three actions:
# 1. Disable all swap.
# 2. Umount everything on /mnt.
# 3. Remove all LVM volume groups.
################################################################################
prepare() {
# Disable all swap.
swapoff --all
# Unmount anything on /mnt.
umount -A --recursive /mnt >/dev/null 2>&1
#check_error "$?" "${FILE_NAME}" ${LINENO} "Failed to unmount /mnt."
# Get a list of all the volume groups.
VGS=(`vgdisplay | grep "VG Name" | awk '{print $3}'`)
# Delete all the logical volumes associated with each volume group.
for vg in ${VGS[@]}; do
lvremove -q -f ${vg} >/dev/null 2>&1
vgremove -q -f ${vg} >/dev/null 2>&1
done
# Get a list of all the physical volumes.
PVS=(`pvdisplay | grep "PV Name" | awk '{print $3}'`)
# Delete all the physical volumes.
for pv in ${PVS[@]}; do
pvremove -q -f ${pv} >/dev/null 2>&1
done
# Get a list of all the open encrypted volumes.
EVS=(`dmsetup ls --target crypt | awk '{print $1}'`)
for ev in ${EVS[@]}; do
cryptsetup luksClose ${ev}
done
}
################################################################################
# EXEC START
################################################################################
# Parse the arguments to the script.
while [ "$#" -gt 0 ]; do
case "$1" in
--hdd=*) HDD="${1#*=}"; shift 1;;
--user=*) USER="${1#*=}"; shift 1;;
--password=*) PASSWORD="${1#*=}"; shift 1;;
--host-name=*) HOST_NAME="${1#*=}"; shift 1;;
--encrypt=*) ENCRYPT="${1#*=}"; shift 1;;
--key=*) KEY="${1#*=}"; shift 1;;
--type=*) TYPE="${1#*=}"; shift 1;;
--time-zone=*) TIME_ZONE="${1#*=}"; shift 1;;
--eth-dhcp=*) ETH_DHCP="${1#*=}"; shift 1;;
--wifi-enable) WIFI_ENABLE="true"; shift 1;;
--wifi-ssid=*) WIFI_SSID="${1#*=}"; shift 1;;
--wifi-passwd=*) WIFI_PASSWD="${1#*=}"; shift 1;;
--auto-gc) AUTO_GC="true"; shift 1;;
--auto-dedup) AUTO_DEDUP="true"; shift 1;;
--auto-update) AUTO_UPDATE="true"; shift 1;;
--auto-reboot) AUTO_REBOOT="true"; shift 1;;
--vbox) VBOX_GUEST="true"; shift 1;;
--vmware) VMWARE_GUEST="true"; shift 1;;
*) echo "invalid argument: $1" >&2; exit 1;;
esac
done
# Makre sure the script is run as root.
must_be_root ${LINENO}
# Check that the script args are valid.
validate_args
# Prepare the host for installation / reinstallation.
prepare
# Configure the hard-drive.
case ${ENCRYPT} in
"full")
$(dirname "$0")/hdd_setup/encrypt_full.sh ${HDD} ${KEY}
check_error "$?" "${FILE_NAME}" "${LINENO}" \
"Installation failed."
;;
"root")
$(dirname "$0")/hdd_setup/encrypt_root.sh ${HDD} ${KEY}
check_error "$?" "${FILE_NAME}" "${LINENO}" \
"Installation failed."
;;
*)
$(dirname "$0")/hdd_setup/encrypt_none.sh ${HDD}
check_error "$?" "${FILE_NAME}" "${LINENO}" \
"Installation failed."
;;
esac
# Generate the hardware configuration without the filesystems section since this
# is done automatically by the hdd_setup scripts.
ERR=$(nixos-generate-config --root /mnt --no-filesystems 2>&1)
check_error "$?" "${FILE_NAME}" ${LINENO} \
"Failed to generate NixOS config because:\n\n${ERR}"
# Copy the configuration of the system.
cp -r -f $(dirname "$0")/configuration/* "/mnt/etc/nixos/"
check_error "$?" "${FILE_NAME}" ${LINENO} "Failed to copy default configuration file."
mkdir -p /mnt/etc/nixos/host
mkdir -p /mnt/etc/nixos/security
mkdir -p /mnt/etc/nixos/applications
# Generate the desktop configuration.
${GEN_PATH}/gen_desktop.sh --type=${TYPE}
# Generate the network configuration.
${GEN_PATH}/gen_network.sh --host-name=${HOST_NAME} \
--eth-dhcp=${ETH_DHCP} --wifi-enable=${WIFI_ENABLE} \
--wifi-ssid=${WIFI_SSID} --wifi-passwd=${WIFI_PASSWD}
# Generate the user configuration.
${GEN_PATH}/gen_users.sh --user=${USER} --password=${PASSWORD}
# Generate the maintenance configuration.
${GEN_PATH}/gen_maintenance.sh --auto-gc=${AUTO_GC} --auto-dedup=${AUTO_DEDUP} \
--auto-update=${AUTO_UPDATE} --auto-reboot=${AUTO_REBOOT}
# Configure any guest additions for VBox or Vmware.
if [ ${TYPE} == "desktop" ]; then
X11_GUEST="true"
fi
${GEN_PATH}/gen_vm_guest.sh --vbox=${VBOX_GUEST} --vmware=${VMWARE_GUEST} \
--x11=${X11_GUEST}
# Rebuild the distribution.
print_info "${FILE_NAME}" "${LINENO}" "Installing NixOS....."
ERR=$(nixos-install --no-root-passwd 2>&1)
check_error "$?" "${FILE_NAME}" "${LINENO}" \
"Failed to install NixOS because:\n\n${ERR}"
# If deploying a desktop, manually copy the /etc/skel files for the created
# user account.
if [ "${TYPE}" == "desktop" ]; then
cp -r ./generators/skel/.config "/mnt/home/${USER}"
chown -R nixos:users /mnt/home/${USER}/.config
chmod -R 700 /mnt/home/${USER}/.config
fi
print_info "${FILE_NAME}" "${LINENO}" "Done! Enjoy NixOS!"
exit 0