-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
108 lines (90 loc) · 2.83 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 <disk> <hostname> [-c <config-url>] [-y]"
echo " -h <hostname> : Required hostname for the new system"
echo " -c <config-url> : Optional URL to a NixOS configuration (default: https://github.com/RyanGibb/nixos.git)"
echo " -y : Run non-interactively (skip all prompts)"
exit 1
}
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
URL="https://github.com/RyanGibb/nixos.git"
AUTO_YES=false
if [ $# -lt 2 ]; then
usage
fi
DISK=$1
HOST=$2
shift 2
while getopts ":c:y" opt; do
case $opt in
c) URL="$OPTARG" ;;
y) AUTO_YES=true ;;
*) usage ;;
esac
done
PART_SUFFIX=""
if [[ "$DISK" == *nvme* ]]; then
PART_SUFFIX="p"
fi
echo "Partitioning $DISK..."
parted "$DISK" -- mklabel gpt
parted "$DISK" -- mkpart ESP fat32 1MB 512MB
parted "$DISK" -- mkpart primary 512MiB 100%
parted "$DISK" -- set 1 esp on
echo "Making file systems..."
mkfs.fat -F 32 -n boot "${DISK}${PART_SUFFIX}1"
mkfs.ext4 -L nixos "${DISK}${PART_SUFFIX}2"
echo "Mounting partitions..."
mount "${DISK}${PART_SUFFIX}2" /mnt
mkdir -p /mnt/boot
mount "${DISK}${PART_SUFFIX}1" /mnt/boot
if [ "$AUTO_YES" = false ]; then
echo
echo "Disk partitioning and mounting complete."
echo "The following partitions have been created and mounted on $DISK:"
echo " - ${DISK}${PART_SUFFIX}1 (ESP, FAT32, /boot) mounted at /mnt/boot"
echo " - ${DISK}${PART_SUFFIX}2 (Primary, ext4, /) mounted at /mnt"
echo
echo "Do you want to use the NixOS configuration included in this ISO or from a remote git repository?"
echo "1) Use local"
echo "2) Use remote: $CONFIG_URL"
read -r -p "Select an option [1/2] (default is 2): " config_choice
config_choice=${config_choice:-2}
else
config_choice=2
fi
mkdir -p /mnt/etc/nixos/
if [[ "$config_choice" == "1" ]]; then
echo "Copying local configuration to /mnt/etc/nixos ..."
cp -R /home/nixos/nixos /mnt/etc/nixos/
else
echo "Cloning $URL to /mnt/etc/nixos ..."
git clone "$URL" /mnt/etc/nixos
fi
echo "Generating configuration at /mnt/etc/nixos/hosts/$HOST ..."
mkdir -p /mnt/etc/nixos/hosts/"$HOST"
nixos-generate-config --show-hardware-config >> /mnt/etc/nixos/hosts/"$HOST"/hardware-configuration.nix
(cd /mnt/etc/nixos/hosts/"$HOST"; nix flake init -t /mnt/etc/nixos#host)
if [ "$AUTO_YES" = false ]; then
echo
read -r -p "Do you want to continue with the NixOS installation? [y/N] " response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Installation aborted by user."
exit 0
fi
fi
echo "Installing NixOS..."
nixos-install --root /mnt/ --flake /mnt/etc/nixos#"$HOST"
if [ "$AUTO_YES" = false ]; then
echo
read -r -p "Installation is complete. Do you want to reboot the system now? [y/N] " response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Reboot canceled by user. You may continue using the system."
exit 0
fi
fi
reboot