This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
install.sh
executable file
·122 lines (104 loc) · 3.76 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
#!/usr/bin/env bash
set -e
prompt1="Enter your option: "
ESP="/boot/efi"
MOUNTPOINT="/mnt"
contains_element() {
#check if an element exist in a string
for e in "${@:2}"; do [[ $e == "$1" ]] && break; done
}
#SELECT DEVICE
select_device() {
devices_list=($(lsblk -d | awk '{print "/dev/" $1}' | grep 'sd\|hd\|vd\|nvme\|mmcblk'))
PS3="$prompt1"
echo -e "Attached Devices:\n"
lsblk -lnp -I 2,3,8,9,22,34,56,57,58,65,66,67,68,69,70,71,72,91,128,129,130,131,132,133,134,135,259 | awk '{print $1,$4,$6,$7}' | column -t
echo -e "\n"
echo -e "Select device to partition:\n"
select device in "${devices_list[@]}"; do
if contains_element "${device}" "${devices_list[@]}"; then
break
else
exit 1
fi
done
if [ "$1" = "-n" ]; then
ROOT_PARTITION="${device}p2"
ESP_PARTITION="${device}p1"
else
ROOT_PARTITION="${device}2"
ESP_PARTITION="${device}1"
fi
echo "Root partition: ${ROOT_PARTITION}"
echo "ESP partition: ${ESP_PARTITION}"
}
#CREATE_PARTITION
create_partition() {
wipefs -a "${device}"
# Set GPT scheme
parted "${device}" mklabel gpt &>/dev/null
# Create ESP for /efi
parted "${device}" mkpart primary fat32 1MiB 512MiB &>/dev/null
parted "${device}" set 1 esp on &>/dev/null
# Create /
parted "${device}" mkpart primary 512MiB 100% &>/dev/null
}
#FORMAT_PARTITION
format_partition() {
mkfs.fat -F32 "${ESP_PARTITION}" >/dev/null
echo "LUKS Setup for '/' partition"
cryptsetup luksFormat --type luks1 -s 512 -h sha512 -i 3000 "${ROOT_PARTITION}"
echo "Open '/' partition"
cryptsetup open "${ROOT_PARTITION}" cryptroot
mkfs.ext4 /dev/mapper/cryptroot >/dev/null
}
#MOUNT_PARTITION
mount_partition() {
mount /dev/mapper/cryptroot "${MOUNTPOINT}"
mkdir -p "${MOUNTPOINT}"${ESP}
mount "${ESP_PARTITION}" "${MOUNTPOINT}"${ESP}
}
#CREATE_KEYFILE
create_keyfile() {
dd bs=512 count=4 if=/dev/random of=${MOUNTPOINT}/etc/nixos/secrets/keyfile.bin iflag=fullblock
echo "Add key to root partition"
cryptsetup luksAddKey "${ROOT_PARTITION}" ${MOUNTPOINT}/etc/nixos/secrets/keyfile.bin
chmod 600 ${MOUNTPOINT}/etc/nixos/secrets/keyfile.bin
}
# NIXOS_INSTALL
nixos_install() {
git clone https://github.com/LEXUGE/nixos ${MOUNTPOINT}/etc/nixos/
rm ${MOUNTPOINT}/etc/nixos/secrets/keyfile.bin
rm ${MOUNTPOINT}/etc/nixos/hardware-configuration.nix
create_keyfile
reset
# Create new options.nix and open it to let user customize.
echo "Generate and open build options for configuration..."
read -n 1 -s -r -p "[CONFIG] Adapt whatever on your needs. Press any key to continue"
nano ${MOUNTPOINT}/etc/nixos/configuration.nix
reset
read -n 1 -s -r -p "[USERS] In the next step, you MUST change the user passwords, else you are gonna to be locked out. Press any key to continue"
nano ${MOUNTPOINT}/etc/nixos/src/users.nix
reset
read -n 1 -s -r -p "[CLASH] In the next step, you'd better set up the appropriate proxy if you are not in a free Internet. Press any key to continue"
nano ${MOUNTPOINT}/etc/nixos/secrets/clash.yaml
reset
nixos-generate-config --root ${MOUNTPOINT}
# FIXME: Don't know why we need no-check-sigs
nix copy --to ${MOUNTPOINT} "nixpkgs#nixFlakes" --no-check-sigs
# We need to have both source in /nix/store and /mnt/nixos/store due to current buggy implementation of upstream tools.
nix flake archive "${MOUNTPOINT}/etc/nixos"
# Impure flag is needed because nix thinks `/mnt/nix/store` as a non-store path
nix build "${MOUNTPOINT}/etc/nixos#x1c7-toplevel" --option store ${MOUNTPOINT} --impure
# Install NixOS. We don't need root password.
# Use `-f` to follow the coding style in `nixos-install`.
# The `nixos-install`'s implementation has already specified `--store /mnt` for us.
nixos-install --system "$(readlink -f ./result)" --no-root-passwd
reboot
}
# INSTALLATION
select_device "$@"
create_partition
format_partition
mount_partition
nixos_install