gnu: ath9k-htc-firmware: Use INSTALL-FILE.
[jackhill/guix/guix.git] / etc / guix-install.sh
1 #!/bin/bash
2 # GNU Guix --- Functional package management for GNU
3 # Copyright © 2017 sharlatan <sharlatanus@gmail.com>
4 # Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
5 # Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
6 #
7 # This file is part of GNU Guix.
8 #
9 # GNU Guix is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or (at
12 # your option) any later version.
13 #
14 # GNU Guix is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 set -e
23
24 [ "$UID" -eq 0 ] || { echo "This script must be run as root."; exit 1; }
25
26 REQUIRE=(
27 "dirname"
28 "readlink"
29 "wget"
30 "gpg"
31 "grep"
32 "which"
33 "sed"
34 "sort"
35 "getent"
36 "mktemp"
37 "rm"
38 "chmod"
39 "uname"
40 "groupadd"
41 "tail"
42 "tr"
43 )
44
45 PAS=$'[ \033[32;1mPASS\033[0m ] '
46 ERR=$'[ \033[31;1mFAIL\033[0m ] '
47 INF="[ INFO ] "
48
49 DEBUG=0
50 GNU_URL="https://alpha.gnu.org/gnu/guix/"
51 OPENPGP_SIGNING_KEY_ID="3CE464558A84FDC69DB40CFB090B11993D9AEBB5"
52
53 # ------------------------------------------------------------------------------
54 #+UTILITIES
55
56 _err()
57 { # All errors go to stderr.
58 printf "[%s]: %s\n" "$(date +%s.%3N)" "$1"
59 }
60
61 _msg()
62 { # Default message to stdout.
63 printf "[%s]: %s\n" "$(date +%s.%3N)" "$1"
64 }
65
66 _debug()
67 {
68 if [ "${DEBUG}" = '1' ]; then
69 printf "[%s]: %s\n" "$(date +%s.%3N)" "$1"
70 fi
71 }
72
73
74 chk_require()
75 { # Check that every required command is available.
76 declare -a cmds
77 declare -a warn
78
79 cmds=(${1})
80
81 _debug "--- [ $FUNCNAME ] ---"
82
83 for c in ${cmds[@]}; do
84 command -v "$c" &>/dev/null
85 [ "$?" -eq "1" ] &&
86 warn+=("$c")
87 done
88
89 [ "${#warn}" -ne 0 ] &&
90 { _err "${ERR}Missing commands: ${warn[*]}.";
91 return 1; }
92
93 _msg "${PAS}verification of required commands completed"
94
95 gpg --list-keys ${OPENPGP_SIGNING_KEY_ID} >/dev/null 2>&1 || (
96 _err "${ERR}Missing OpenPGP public key. Fetch it with this command:"
97 echo " gpg --keyserver pgp.mit.edu --recv-keys ${OPENPGP_SIGNING_KEY_ID}"
98 exit 1
99 )
100 }
101
102 chk_term()
103 { # Check for ANSI terminal for color printing.
104 local ansi_term
105
106 if [ -t 2 ]; then
107 if [ "${TERM+set}" = 'set' ]; then
108 case "$TERM" in
109 xterm*|rxvt*|urxvt*|linux*|vt*|eterm*|screen*)
110 ansi_term=true
111 ;;
112 *)
113 ansi_term=false
114 ERR="[ FAIL ] "
115 PAS="[ PASS ] "
116 ;;
117 esac
118 fi
119 fi
120 }
121
122 chk_init_sys()
123 { # Return init system type name.
124 if [[ $(/sbin/init --version 2>/dev/null) =~ upstart ]]; then
125 _msg "${INF}init system is: upstart"
126 INIT_SYS="upstart"
127 return 0
128 elif [[ $(systemctl) =~ -\.mount ]]; then
129 _msg "${INF}init system is: systemd"
130 INIT_SYS="systemd"
131 return 0
132 elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then
133 _msg "${INF}init system is: sysv-init"
134 INIT_SYS="sysv-init"
135 return 0
136 else
137 INIT_SYS="NA"
138 _err "${ERR}Init system could not be detected."
139 fi
140 }
141
142 chk_sys_arch()
143 { # Check for operating system and architecture type.
144 local os
145 local arch
146
147 os="$(uname -s)"
148 arch="$(uname -m)"
149
150 case "$arch" in
151 i386 | i486 | i686 | i786 | x86)
152 local arch=i686
153 ;;
154 x86_64 | x86-64 | x64 | amd64)
155 local arch=x86_64
156 ;;
157 aarch64)
158 local arch=aarch64
159 ;;
160 *)
161 _err "${ERR}Unsupported CPU type: ${arch}"
162 exit 1
163 esac
164
165 case "$os" in
166 Linux | linux)
167 local os=linux
168 ;;
169 *)
170 _err "${ERR}Your operation system (${os}) is not supported."
171 exit 1
172 esac
173
174 ARCH_OS="${arch}-${os}"
175 }
176
177 # ------------------------------------------------------------------------------
178 #+MAIN
179
180 guix_get_bin_list()
181 { # Scan GNU archive and save list of binaries
182 local gnu_url="$1"
183 local -a bin_ver_ls
184 local latest_ver
185 local default_ver
186
187 _debug "--- [ $FUNCNAME ] ---"
188
189 # Filter only version and architecture
190 bin_ver_ls=("$(wget -qO- "$gnu_url" \
191 | sed -n -e 's/.*guix-binary-\([0-9.]*\)\..*.tar.xz.*/\1/p' \
192 | sort -Vu)")
193
194 latest_ver="$(echo "$bin_ver_ls" \
195 | grep -oP "([0-9]{1,2}\.){2}[0-9]{1,2}" \
196 | tail -n1)"
197
198 default_ver="guix-binary-${latest_ver}.${ARCH_OS}"
199
200 if [[ "${#bin_ver_ls}" -ne "0" ]]; then
201 _msg "${PAS}Release for your system: ${default_ver}"
202 else
203 _err "${ERR}Could not obtain list of Guix releases."
204 exit 1
205 fi
206
207 # Use default to download according to the list and local ARCH_OS.
208 BIN_VER="$default_ver"
209 }
210
211 guix_get_bin()
212 { # Download and verify binary package.
213 local url="$1"
214 local bin_ver="$2"
215 local dl_path="$3"
216
217 _debug "--- [ $FUNCNAME ] ---"
218
219 _msg "${INF}Downloading Guix release archive"
220
221 wget --help | grep -q '\--show-progress' && \
222 _PROGRESS_OPT="-q --show-progress" || _PROGRESS_OPT=""
223 wget $_PROGRESS_OPT -P "$dl_path" "${url}/${bin_ver}.tar.xz" "${url}/${bin_ver}.tar.xz.sig"
224
225 if [[ "$?" -eq 0 ]]; then
226 _msg "${PAS}download completed."
227 else
228 _err "${ERR}could not download ${url}/${bin_ver}.tar.xz."
229 exit 1
230 fi
231
232 pushd $dl_path >/dev/null
233 gpg --verify "${bin_ver}.tar.xz.sig" >/dev/null 2>&1
234 if [[ "$?" -eq 0 ]]; then
235 _msg "${PAS}Signature is valid."
236 popd >/dev/null
237 else
238 _err "${ERR}could not verify the signature."
239 exit 1
240 fi
241 }
242
243 sys_create_store()
244 { # Unpack and install /gnu/store and /var/guix
245 local pkg="$1"
246 local tmp_path="$2"
247
248 _debug "--- [ $FUNCNAME ] ---"
249
250 cd "$tmp_path"
251 tar --warning=no-timestamp \
252 --extract \
253 --file "$pkg" &&
254 _msg "${PAS}unpacked archive"
255
256 if [[ -e "/var/guix" || -e "/gnu" ]]; then
257 _err "${ERR}A previous Guix installation was found. Refusing to overwrite."
258 exit 1
259 else
260 _msg "${INF}Installing /var/guix and /gnu..."
261 mv "${tmp_path}/var/guix" /var/
262 mv "${tmp_path}/gnu" /
263 fi
264
265 _msg "${INF}Linking the root user's profile"
266 ln -sf /var/guix/profiles/per-user/root/guix-profile \
267 ~root/.guix-profile
268
269 GUIX_PROFILE="${HOME}/.guix-profile"
270 source "${GUIX_PROFILE}/etc/profile"
271 _msg "${PAS}activated root profile at /root/.guix-profile"
272 }
273
274 sys_create_build_user()
275 { # Create the group and user accounts for build users.
276
277 _debug "--- [ $FUNCNAME ] ---"
278
279 if [ $(getent group guixbuild) ]; then
280 _msg "${INF}group guixbuild exists"
281 else
282 groupadd --system guixbuild
283 _msg "${PAS}group <guixbuild> created"
284 fi
285
286 for i in $(seq -w 1 10); do
287 if id "guixbuilder${i}" &>/dev/null; then
288 _msg "${INF}user is already in the system, reset"
289 usermod -g guixbuild -G guixbuild \
290 -d /var/empty -s "$(which nologin)" \
291 -c "Guix build user $i" \
292 "guixbuilder${i}";
293 else
294 useradd -g guixbuild -G guixbuild \
295 -d /var/empty -s "$(which nologin)" \
296 -c "Guix build user $i" --system \
297 "guixbuilder${i}";
298 _msg "${PAS}user added <guixbuilder${i}>"
299 fi
300 done
301 }
302
303 sys_enable_guix_daemon()
304 { # Run the daemon, and set it to automatically start on boot.
305
306 local info_path
307 local local_bin
308 local var_guix
309
310 _debug "--- [ $FUNCNAME ] ---"
311
312 info_path="/usr/local/share/info"
313 local_bin="/usr/local/bin"
314 var_guix="/var/guix/profiles/per-user/root/guix-profile"
315
316 case "$INIT_SYS" in
317 upstart)
318 { initctl reload-configuration;
319 cp ~root/.guix-profile/lib/upstart/system/guix-daemon.conf \
320 /etc/init/ &&
321 start guix-daemon; } &&
322 _msg "${PAS}enabled Guix daemon via upstart"
323 ;;
324 systemd)
325 { cp ~root/.guix-profile/lib/systemd/system/guix-daemon.service \
326 /etc/systemd/system/;
327 chmod 664 /etc/systemd/system/guix-daemon.service;
328 systemctl daemon-reload &&
329 systemctl start guix-daemon &&
330 systemctl enable guix-daemon; } &&
331 _msg "${PAS}enabled Guix daemon via systemd"
332 ;;
333 NA|*)
334 _msg "${ERR}unsupported init system; run the daemon manually:"
335 echo " ~root/.guix-profile/bin/guix-daemon --build-users-group=guixbuild"
336 ;;
337 esac
338
339 _msg "${INF}making the guix command available to other users"
340
341 [ -e "$local_bin" ] || mkdir -p "$local_bin"
342 ln -sf "${var_guix}/bin/guix" "$local_bin"
343
344 [ -e "$info_path" ] || mkdir -p "$info_path"
345 for i in ${var_guix}/share/info/*; do
346 ln -sf "$i" "$info_path"
347 done
348 }
349
350 sys_authorize_build_farms()
351 { # authorize the public keys of the two build farms
352 while true; do
353 read -p "Permit downloading pre-built package binaries from the project's build farms? (yes/no) " yn
354 case $yn in
355 [Yy]*) guix archive --authorize < ~root/.guix-profile/share/guix/hydra.gnu.org.pub &&
356 _msg "${PAS}Authorized public key for hydra.gnu.org";
357 guix archive --authorize < ~root/.guix-profile/share/guix/berlin.guixsd.org.pub &&
358 _msg "${PAS}Authorized public key for berlin.guixsd.org";
359 break;;
360 [Nn]*) _msg "${INF}Skipped authorizing build farm public keys"
361 break;;
362 *) _msg "Please answer yes or no.";
363 esac
364 done
365 }
366
367 welcome()
368 {
369 cat<<"EOF"
370 ░░░ ░░░
371 ░░▒▒░░░░░░░░░ ░░░░░░░░░▒▒░░
372 ░░▒▒▒▒▒░░░░░░░ ░░░░░░░▒▒▒▒▒░
373 ░▒▒▒░░▒▒▒▒▒ ░░░░░░░▒▒░
374 ░▒▒▒▒░ ░░░░░░
375 ▒▒▒▒▒ ░░░░░░
376 ▒▒▒▒▒ ░░░░░
377 ░▒▒▒▒▒ ░░░░░
378 ▒▒▒▒▒ ░░░░░
379 ▒▒▒▒▒ ░░░░░
380 ░▒▒▒▒▒░░░░░
381 ▒▒▒▒▒▒░░░
382 ▒▒▒▒▒▒░
383 _____ _ _ _ _ _____ _
384 / ____| \ | | | | | / ____| (_)
385 | | __| \| | | | | | | __ _ _ ___ __
386 | | |_ | . ' | | | | | | |_ | | | | \ \/ /
387 | |__| | |\ | |__| | | |__| | |_| | |> <
388 \_____|_| \_|\____/ \_____|\__,_|_/_/\_\
389
390 This script installs GNU Guix on your system
391
392 https://www.gnu.org/software/guix/
393 EOF
394 echo -n "Press return to continue..."
395 read -r ANSWER
396 }
397
398 main()
399 {
400 local tmp_path
401 welcome
402
403 _msg "Starting installation ($(date))"
404
405 chk_term
406 chk_require "${REQUIRE[*]}"
407 chk_init_sys
408 chk_sys_arch
409
410 _msg "${INF}system is ${ARCH_OS}"
411
412 tmp_path="$(mktemp -t -d guix.XXX)"
413
414 guix_get_bin_list "${GNU_URL}"
415 guix_get_bin "${GNU_URL}" "${BIN_VER}" "$tmp_path"
416
417 sys_create_store "${BIN_VER}.tar.xz" "${tmp_path}"
418 sys_create_build_user
419 sys_enable_guix_daemon
420 sys_authorize_build_farms
421
422 _msg "${INF}cleaning up ${tmp_path}"
423 rm -r "${tmp_path}"
424
425 _msg "${PAS}Guix has successfully been installed!"
426 _msg "${INF}Run 'info guix' to read the manual."
427 }
428
429 main "$@"