aboutsummaryrefslogtreecommitdiff
path: root/public/install-nixos.sh
blob: 95030b55dbee114b54359149c16dfbd0043833c4 (plain) (blame)
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
#!/usr/bin/env nix-shell
#!nix-shell --pure -i bash -p bash jq envsubst wget
# shellcheck shell=bash
set -Eeuo pipefail

end="\033[0m"

green() {
  green_color="\033[0;32m"
  echo -e "${green_color}${1}${end}"
}

yellow() {
  yellow_color="\033[0;33m"
  echo -e "${yellow_color}${1}${end}"
}

blue() {
  blue_color="\033[0;34m"
  echo -e "${blue_color}${1}${end}"
}

red() {
  red_color="\033[0;31m"
  echo -e "${red_color}${1}${end}"
}

usage() {
  red "Missing input argument $1.\n"
  cat <<EOF
Usage:
    install-nixos.sh <HOST_NAME> <DEVICE>

      Arguments
        HOST_NAME         The networking.hostName of the installation. Something like 'velhinho-nixos'.
        DEVICE            The LUKS device where to install to. Use lsblk or similar tools to get it's name.

Examples:
    Download install-nixos.sh and run it with 'velhinho-nixos' as hostName and '/dev/sda3' as LUKS device:
        wget -O- https://euandre.org/dotfiles/install-nixos.sh | sh -s velhinho-nixos /dev/sda3
EOF
}

HOST_NAME="${1:-}"
DEVICE="${2:-}"

[[ -z "${HOST_NAME}" ]] && {
  usage 'HOST_NAME'
  exit 2
}

[[ -z "${DEVICE}" ]] && {
  usage 'DEVICE'
  exit 2
}

SERVER="https://euandre.org/dotfiles"

yellow "Downloading Nix files..."
TMP_DIR="$(mktemp -d)"
wget -O "${TMP_DIR}/configuration.nix" "${SERVER}/configuration.nix"
wget -O "${TMP_DIR}/template.nix"      "${SERVER}/template.nix"
green "Done."

yellow "Creating content of '/etc/nixos/local-configuration.nix'..."
export HOST_NAME
export DEVICE
envsubst < "${TMP_DIR}/template.nix" > "${TMP_DIR}/local-configuration.nix"
blue "$(cat "${TMP_DIR}/local-configuration.nix")"
read -p "Confirm content of '/etc/nixos/local-configuration.nix'? (y/N) " -n 1 -r
echo # Move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  red "Unconfirmed content of '/etc/nixos/local-configuration.nix'."
  red "Exitting."
  exit 1
fi
green "Done."

yellow "Moving presented content to '/etc/nixos/local-configuration.nix'..."
echo mv "${TMP_DIR}/local-configuration.nix" /etc/nixos/local-configuration.nix
green "Done."

yellow "Partitioning '${DEVICE}'..."
# Create a GPT partition table.
parted "${DEVICE}" -- mklabel gpt
# Add the root partition. This will fill the disk except for the end part, where the swap will live, and the space left in front (512MiB) which will be used by the boot partition.
parted "${DEVICE}" -- mkpart primary 512MiB -8GiB
# Add a swap partition of 8GiB.
parted "${DEVICE}" -- mkpart primary linux-swap -8GiB 100%
# Add the boot partition. NixOS by default uses the ESP (EFI system partition) as its /boot partition. It uses the initially reserved 512MiB at the start of the disk.
parted "${DEVICE}" -- mkpart ESP fat32 1MiB 512MiB
parted "${DEVICE}" -- set 3 boot on
green "Done."

yellow "Configuring LUKS encryption on '${DEVICE}3'..."
cryptsetup luksFormat "${DEVICE}3"
cryptsetup luksOpen "${DEVICE}3" enc-pv
pvcreate /dev/mapper/enc-pv
vgcreate vg /dev/mapper/enc-pv
lvcreate -n swap vg -L 10G
lvcreate -n root vg -l 100%FREE
green "Done."

yellow "Formatting '${DEVICE}'..."
mkfs.vfat -n BOOT "${DEVICE}2"
mkfs.ext4 -L root /dev/vg/root
mkswap -L swap /dev/vg/swap
green "Done."

yellow "Mounting and activating swap..."
mount /dev/vg/root /mnt
mkdir /mnt/boot
mount "${DEVICE}2" /mnt/boot
swapon /dev/vg/swap
green "Done."

yellow "Generating '/etc/nixos/*' Nix files'..."
nixos-generate-config --root /mnt
mv /etc/nixos/configuration.nix /etc/nixos/bkp-configuration.nix
mv "${TMP_DIR}/configuration.nix" /etc/nixos/configuration.nix
green "Done."

blue "Now inspect '/etc/nixos/configuration.nix', "
yellow "Installing NixOS!"
nixos-install
green "Done."

yellow "Rebooting..."
reboot