-
Notifications
You must be signed in to change notification settings - Fork 57
/
install
executable file
·179 lines (159 loc) · 4.49 KB
/
install
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
#!/bin/bash
set -eu
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
## Bring in some helper functions
source "$here/util"
## Sanity-check
log_start "Checking environment... "
check_existence "mount"
check_existence "modprobe"
check_existence "chroot"
check_existence "wget"
check_existence "sed"
check_existence "grep"
check_existence "unsquashfs"
check_existence "mktemp"
check_existence "id"
check_existence "sha256sum"
log_end "seems sane"
if [ -d /sys/firmware/efi ];
then
log "UEFI systems are not supported yet. See issue #30 on github." >&2
exit 1
fi
## Setup essential variables; allow overriding with input
root_mount=$(mount | grep "/ " | sed 's/ /\n/' | head -n1)
root_type=$(mount | grep -Eo "/ type \w+" | sed 's/ /\n/g' | tail -n1)
if grep '/dev/nvme' <<< $root_mount >/dev/null; then
grub_device=$(echo $root_mount | sed "s|p[0-9]\+$||");
elif grep '/dev/sd' <<< $root_mount >/dev/null; then
grub_device=$(echo $root_mount | sed "s|[0-9]\+||");
elif grep '/dev/vd' <<< $root_mount >/dev/null; then
grub_device=$(echo $root_mount | sed "s|[0-9]\+||");
else
log "Unable to determine your grub boot device! Please specify with the -g option."
fi
working_directory=$(mktemp -d)
## Try to work out which architecture to use
set +e
arch=$(uname -a | grep -o "x86_64" | head -n1)
set -e
[ ! "$arch" = "x86_64" ] && arch=i686
primary_version=16.09
secondary_version=680.4e14fd5
full_version=$primary_version.$secondary_version
minimal_iso=nixos-minimal-$full_version-${arch}-linux.iso
graphical_iso=nixos-graphical-$full_version-${arch}-linux.iso
iso=$minimal_iso
minimal_checksum=8700fd1861e83dba0c3c2cb348099d2499b2c52844d1771f1aacbf7458990bc8
graphical_checksum=f1144de2645b1d099035416cb335e33cb56fdf71c8f8900c75a6a91ac2f49464
checksum=$minimal_checksum
minimal_space=3
graphical_space=5
required_space=$minimal_space
extra_config=$(readlink -f default-extra-config.nix)
digitalocean=false
while getopts ":g:r:t:Gdw:c:h" opt; do
case $opt in
g)
grub_device=$OPTARG
;;
r)
root_mount=$OPTARG
;;
t)
root_type=$OPTARG
;;
G)
iso=$graphical_iso
required_space=$graphical_space
checksum=$graphical_checksum
;;
d)
digitalocean=true
;;
w)
working_directory=$OPTARG
;;
c)
extra_config=$(readlink -f $OPTARG)
if [ ! -f $extra_config ];
then
log "ERROR extra config file missing: $extra_config" >&2
exit 1
fi
;;
h)
show_help "$0"
;;
\?)
log "ERROR invalid option: -$OPTARG" >&2
exit 1
;;
:)
log "ERROR option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ "$(id -u)" != "0" ]; then
log "You need to be root to do this"
exit 1
fi
space=$(df -B 1G "$working_directory" | tail -n1 | sed 's/ \+/ /g' | cut -d' ' -f4)
if [ "$space" -lt "$required_space" ];
then
log "WARNING: It *looks* like you don't have enough space for the install"
log "You need ~${minimal_space}GB for the minimal ISO and ~${graphical_space}GB for the graphical ISO"
log_start "Continue anyway? [yn] "
read -n 1 -r
log_end
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
log "Exiting; nothing was harmed"
exit 1
fi
fi
## Give one last chance to back out
log "NixOS installer (nixos-in-place)"
log " GRUB => $grub_device"
log " Root => $root_mount ($root_type)"
log " ISO => $iso"
log " Digital Ocean => $digitalocean"
log " Working directory => $working_directory"
log " Extra config => $extra_config"
log_start "Continue? [yn] "
read -n 1 -r
log_end
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
log "Exiting; nothing was harmed"
exit 1
fi
pushd "$working_directory"
log "Downloading NixOS $iso"
mkdir -p mnt host/nix
url=https://d3g5gsiof5omrk.cloudfront.net/nixos/$primary_version/nixos-$full_version
wget -c $url/$iso
# From $url/$iso.sha256
log "Validating checksum"
sha256sum -c <(echo "$checksum $iso")
log "Extracting ISO"
modprobe loop
mount -o loop $iso mnt
unsquashfs -d host/nix/store mnt/nix-store.squashfs '*'
umount mnt
rm -rf ./mnt
## Setup the chroot environment before install
log "Embarking stage1!"
"$here/stage1" "$here" "$root_mount" "$root_type" "$grub_device" "$digitalocean" "$extra_config"
## Minimize residual space usage
# /var/empty is immutable https://github.com/NixOS/nixpkgs/pull/18365
chattr -i ./host/var/empty
rm -rf ./host
popd
## Installation is complete
log_start "Reboot into NixOS now? [yn] "
read -n 1 -r || REPLY=n
log_end
[[ $REPLY =~ ^[Yy]$ ]] && reboot