gnu: Add python-requests_ntlm.
[jackhill/guix/guix.git] / etc / guix-install.sh
CommitLineData
f5fdc54d 1#!/bin/sh
6f4e8693
RW
2# GNU Guix --- Functional package management for GNU
3# Copyright © 2017 sharlatan <sharlatanus@gmail.com>
4# Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
ea6b1bae 5# Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
6c77d79a 6# Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
6f4e8693
RW
7#
8# This file is part of GNU Guix.
9#
10# GNU Guix is free software; you can redistribute it and/or modify it
11# under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 3 of the License, or (at
13# your option) any later version.
14#
15# GNU Guix is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
f5fdc54d
LC
23# We require Bash but for portability we'd rather not use /bin/bash or
24# /usr/bin/env in the shebang, hence this hack.
25if [ "x$BASH_VERSION" = "x" ]
26then
27 exec bash "$0" "$@"
28fi
29
6f4e8693
RW
30set -e
31
32[ "$UID" -eq 0 ] || { echo "This script must be run as root."; exit 1; }
33
34REQUIRE=(
35 "dirname"
36 "readlink"
37 "wget"
38 "gpg"
39 "grep"
40 "which"
41 "sed"
42 "sort"
43 "getent"
44 "mktemp"
45 "rm"
46 "chmod"
47 "uname"
48 "groupadd"
49 "tail"
50 "tr"
51)
52
53PAS=$'[ \033[32;1mPASS\033[0m ] '
54ERR=$'[ \033[31;1mFAIL\033[0m ] '
55INF="[ INFO ] "
56
57DEBUG=0
3a3e9f2b 58GNU_URL="https://ftp.gnu.org/gnu/guix/"
6f4e8693
RW
59OPENPGP_SIGNING_KEY_ID="3CE464558A84FDC69DB40CFB090B11993D9AEBB5"
60
3cd4447f
CM
61# This script needs to know where root's home directory is. However, we
62# cannot simply use the HOME environment variable, since there is no guarantee
63# that it points to root's home directory.
64ROOT_HOME="$(echo ~root)"
65
6f4e8693
RW
66# ------------------------------------------------------------------------------
67#+UTILITIES
68
69_err()
70{ # All errors go to stderr.
71 printf "[%s]: %s\n" "$(date +%s.%3N)" "$1"
72}
73
74_msg()
75{ # Default message to stdout.
76 printf "[%s]: %s\n" "$(date +%s.%3N)" "$1"
77}
78
79_debug()
80{
81 if [ "${DEBUG}" = '1' ]; then
82 printf "[%s]: %s\n" "$(date +%s.%3N)" "$1"
83 fi
84}
85
86
87chk_require()
88{ # Check that every required command is available.
6f4e8693 89 declare -a warn
7a2e0c52 90 local c
6f4e8693 91
6f4e8693
RW
92 _debug "--- [ $FUNCNAME ] ---"
93
6c77d79a 94 for c in "$@"; do
593fe736 95 command -v "$c" &>/dev/null || warn+=("$c")
6f4e8693
RW
96 done
97
98 [ "${#warn}" -ne 0 ] &&
99 { _err "${ERR}Missing commands: ${warn[*]}.";
100 return 1; }
101
102 _msg "${PAS}verification of required commands completed"
5d8e505c
TGR
103}
104
105chk_gpg_keyring()
106{ # Check whether the Guix release signing public key is present.
107 _debug "--- [ $FUNCNAME ] ---"
6f4e8693 108
9b2644c2
TGR
109 # Without --dry-run this command will create a ~/.gnupg owned by root on
110 # systems where gpg has never been used, causing errors and confusion.
111 gpg --dry-run --list-keys ${OPENPGP_SIGNING_KEY_ID} >/dev/null 2>&1 || (
6f4e8693 112 _err "${ERR}Missing OpenPGP public key. Fetch it with this command:"
cea5db1b 113 echo " wget https://sv.gnu.org/people/viewgpg.php?user_id=15145 -qO - | gpg --import -"
6f4e8693
RW
114 exit 1
115 )
116}
117
118chk_term()
119{ # Check for ANSI terminal for color printing.
120 local ansi_term
121
122 if [ -t 2 ]; then
123 if [ "${TERM+set}" = 'set' ]; then
124 case "$TERM" in
125 xterm*|rxvt*|urxvt*|linux*|vt*|eterm*|screen*)
126 ansi_term=true
127 ;;
128 *)
129 ansi_term=false
130 ERR="[ FAIL ] "
131 PAS="[ PASS ] "
132 ;;
133 esac
134 fi
135 fi
136}
137
138chk_init_sys()
139{ # Return init system type name.
140 if [[ $(/sbin/init --version 2>/dev/null) =~ upstart ]]; then
141 _msg "${INF}init system is: upstart"
142 INIT_SYS="upstart"
143 return 0
144 elif [[ $(systemctl) =~ -\.mount ]]; then
145 _msg "${INF}init system is: systemd"
146 INIT_SYS="systemd"
147 return 0
148 elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then
149 _msg "${INF}init system is: sysv-init"
150 INIT_SYS="sysv-init"
151 return 0
152 else
153 INIT_SYS="NA"
154 _err "${ERR}Init system could not be detected."
155 fi
156}
157
158chk_sys_arch()
159{ # Check for operating system and architecture type.
160 local os
161 local arch
162
163 os="$(uname -s)"
164 arch="$(uname -m)"
165
166 case "$arch" in
167 i386 | i486 | i686 | i786 | x86)
168 local arch=i686
169 ;;
170 x86_64 | x86-64 | x64 | amd64)
171 local arch=x86_64
172 ;;
ea6b1bae
EF
173 aarch64)
174 local arch=aarch64
175 ;;
2510bd87
LC
176 armv7l)
177 local arch=armhf
178 ;;
6f4e8693
RW
179 *)
180 _err "${ERR}Unsupported CPU type: ${arch}"
181 exit 1
182 esac
183
184 case "$os" in
185 Linux | linux)
186 local os=linux
187 ;;
188 *)
189 _err "${ERR}Your operation system (${os}) is not supported."
190 exit 1
191 esac
192
193 ARCH_OS="${arch}-${os}"
194}
195
196# ------------------------------------------------------------------------------
197#+MAIN
198
199guix_get_bin_list()
200{ # Scan GNU archive and save list of binaries
201 local gnu_url="$1"
202 local -a bin_ver_ls
203 local latest_ver
204 local default_ver
205
206 _debug "--- [ $FUNCNAME ] ---"
207
208 # Filter only version and architecture
209 bin_ver_ls=("$(wget -qO- "$gnu_url" \
210 | sed -n -e 's/.*guix-binary-\([0-9.]*\)\..*.tar.xz.*/\1/p' \
211 | sort -Vu)")
212
213 latest_ver="$(echo "$bin_ver_ls" \
214 | grep -oP "([0-9]{1,2}\.){2}[0-9]{1,2}" \
215 | tail -n1)"
216
217 default_ver="guix-binary-${latest_ver}.${ARCH_OS}"
218
219 if [[ "${#bin_ver_ls}" -ne "0" ]]; then
220 _msg "${PAS}Release for your system: ${default_ver}"
221 else
222 _err "${ERR}Could not obtain list of Guix releases."
223 exit 1
224 fi
225
226 # Use default to download according to the list and local ARCH_OS.
227 BIN_VER="$default_ver"
228}
229
230guix_get_bin()
231{ # Download and verify binary package.
232 local url="$1"
233 local bin_ver="$2"
234 local dl_path="$3"
235
236 _debug "--- [ $FUNCNAME ] ---"
237
238 _msg "${INF}Downloading Guix release archive"
239
240 wget --help | grep -q '\--show-progress' && \
241 _PROGRESS_OPT="-q --show-progress" || _PROGRESS_OPT=""
242 wget $_PROGRESS_OPT -P "$dl_path" "${url}/${bin_ver}.tar.xz" "${url}/${bin_ver}.tar.xz.sig"
243
244 if [[ "$?" -eq 0 ]]; then
245 _msg "${PAS}download completed."
246 else
247 _err "${ERR}could not download ${url}/${bin_ver}.tar.xz."
248 exit 1
249 fi
250
251 pushd $dl_path >/dev/null
252 gpg --verify "${bin_ver}.tar.xz.sig" >/dev/null 2>&1
253 if [[ "$?" -eq 0 ]]; then
254 _msg "${PAS}Signature is valid."
255 popd >/dev/null
256 else
257 _err "${ERR}could not verify the signature."
258 exit 1
259 fi
260}
261
262sys_create_store()
263{ # Unpack and install /gnu/store and /var/guix
264 local pkg="$1"
265 local tmp_path="$2"
266
267 _debug "--- [ $FUNCNAME ] ---"
268
269 cd "$tmp_path"
270 tar --warning=no-timestamp \
271 --extract \
272 --file "$pkg" &&
273 _msg "${PAS}unpacked archive"
274
275 if [[ -e "/var/guix" || -e "/gnu" ]]; then
276 _err "${ERR}A previous Guix installation was found. Refusing to overwrite."
277 exit 1
278 else
279 _msg "${INF}Installing /var/guix and /gnu..."
280 mv "${tmp_path}/var/guix" /var/
281 mv "${tmp_path}/gnu" /
282 fi
283
284 _msg "${INF}Linking the root user's profile"
e9926f80
LC
285 mkdir -p "${ROOT_HOME}/.config/guix"
286 ln -sf /var/guix/profiles/per-user/root/current-guix \
287 "${ROOT_HOME}/.config/guix/current"
6f4e8693 288
e9926f80 289 GUIX_PROFILE="${ROOT_HOME}/.config/guix/current"
6f4e8693 290 source "${GUIX_PROFILE}/etc/profile"
e9926f80 291 _msg "${PAS}activated root profile at ${ROOT_HOME}/.config/guix/current"
6f4e8693
RW
292}
293
294sys_create_build_user()
295{ # Create the group and user accounts for build users.
296
297 _debug "--- [ $FUNCNAME ] ---"
298
299 if [ $(getent group guixbuild) ]; then
300 _msg "${INF}group guixbuild exists"
301 else
302 groupadd --system guixbuild
303 _msg "${PAS}group <guixbuild> created"
304 fi
305
306 for i in $(seq -w 1 10); do
307 if id "guixbuilder${i}" &>/dev/null; then
308 _msg "${INF}user is already in the system, reset"
309 usermod -g guixbuild -G guixbuild \
310 -d /var/empty -s "$(which nologin)" \
311 -c "Guix build user $i" \
312 "guixbuilder${i}";
313 else
314 useradd -g guixbuild -G guixbuild \
315 -d /var/empty -s "$(which nologin)" \
316 -c "Guix build user $i" --system \
317 "guixbuilder${i}";
318 _msg "${PAS}user added <guixbuilder${i}>"
319 fi
320 done
321}
322
323sys_enable_guix_daemon()
324{ # Run the daemon, and set it to automatically start on boot.
325
326 local info_path
327 local local_bin
328 local var_guix
329
330 _debug "--- [ $FUNCNAME ] ---"
331
332 info_path="/usr/local/share/info"
333 local_bin="/usr/local/bin"
e9926f80 334 var_guix="/var/guix/profiles/per-user/root/current-guix"
6f4e8693
RW
335
336 case "$INIT_SYS" in
337 upstart)
338 { initctl reload-configuration;
e9926f80 339 cp "${ROOT_HOME}/.config/guix/current/lib/upstart/system/guix-daemon.conf" \
6f4e8693
RW
340 /etc/init/ &&
341 start guix-daemon; } &&
342 _msg "${PAS}enabled Guix daemon via upstart"
343 ;;
344 systemd)
e9926f80 345 { cp "${ROOT_HOME}/.config/guix/current/lib/systemd/system/guix-daemon.service" \
6f4e8693
RW
346 /etc/systemd/system/;
347 chmod 664 /etc/systemd/system/guix-daemon.service;
e1e3fe08
LC
348
349 # Work around <https://bugs.gnu.org/36074>, present in 1.0.1.
350 sed -i /etc/systemd/system/guix-daemon.service \
351 -e "s/GUIX_LOCPATH='/'GUIX_LOCPATH=/";
352
353 # Work around <https://bugs.gnu.org/35671>, present in 1.0.1.
354 if ! grep en_US /etc/systemd/system/guix-daemon.service >/dev/null;
355 then sed -i /etc/systemd/system/guix-daemon.service \
356 -e 's/^Environment=\(.*\)$/Environment=\1 LC_ALL=en_US.UTF-8';
357 fi;
358
6f4e8693
RW
359 systemctl daemon-reload &&
360 systemctl start guix-daemon &&
361 systemctl enable guix-daemon; } &&
362 _msg "${PAS}enabled Guix daemon via systemd"
363 ;;
fe60ef99
DM
364 sysv-init)
365 { mkdir -p /etc/init.d;
366 cp "${ROOT_HOME}/.config/guix/current/etc/init.d/guix-daemon" \
367 /etc/init.d/guix-daemon;
368 chmod 775 /etc/init.d/guix-daemon;
369
370 update-rc.d guix-daemon defaults &&
371 update-rc.d guix-daemon enable &&
372 service guix-daemon start; } &&
373 _msg "${PAS}enabled Guix daemon via sysv"
374 ;;
6f4e8693
RW
375 NA|*)
376 _msg "${ERR}unsupported init system; run the daemon manually:"
e9926f80 377 echo " ${ROOT_HOME}/.config/guix/current/bin/guix-daemon --build-users-group=guixbuild"
6f4e8693
RW
378 ;;
379 esac
380
381 _msg "${INF}making the guix command available to other users"
382
383 [ -e "$local_bin" ] || mkdir -p "$local_bin"
384 ln -sf "${var_guix}/bin/guix" "$local_bin"
385
386 [ -e "$info_path" ] || mkdir -p "$info_path"
387 for i in ${var_guix}/share/info/*; do
388 ln -sf "$i" "$info_path"
389 done
390}
391
392sys_authorize_build_farms()
414c4de1 393{ # authorize the public key of the build farm
6f4e8693 394 while true; do
414c4de1 395 read -p "Permit downloading pre-built package binaries from the project's build farm? (yes/no) " yn
6f4e8693 396 case $yn in
414c4de1 397 [Yy]*) guix archive --authorize < "${ROOT_HOME}/.config/guix/current/share/guix/ci.guix.gnu.org.pub" &&
825c39bf 398 _msg "${PAS}Authorized public key for ci.guix.gnu.org";
6f4e8693
RW
399 break;;
400 [Nn]*) _msg "${INF}Skipped authorizing build farm public keys"
401 break;;
402 *) _msg "Please answer yes or no.";
403 esac
404 done
405}
406
30810aff
PG
407sys_create_init_profile()
408{ # Create /etc/profile.d/guix.sh for better desktop integration
29ba58c0 409 [ -d "/etc/profile.d" ] || mkdir /etc/profile.d # Just in case
30810aff
PG
410 cat <<"EOF" > /etc/profile.d/guix.sh
411# _GUIX_PROFILE: `guix pull` profile
412_GUIX_PROFILE="$HOME/.config/guix/current"
5c03516a
PG
413if [ -L $_GUIX_PROFILE ]; then
414 export PATH="$_GUIX_PROFILE/bin${PATH:+:}$PATH"
415 # Export INFOPATH so that the updated info pages can be found
416 # and read by both /usr/bin/info and/or $GUIX_PROFILE/bin/info
3c69701f
LC
417 # When INFOPATH is unset, add a trailing colon so that Emacs
418 # searches 'Info-default-directory-list'.
419 export INFOPATH="$_GUIX_PROFILE/share/info:$INFOPATH"
5c03516a 420fi
30810aff
PG
421
422# GUIX_PROFILE: User's default profile
423GUIX_PROFILE="$HOME/.guix-profile"
424[ -L $GUIX_PROFILE ] || return
425GUIX_LOCPATH="$GUIX_PROFILE/lib/locale"
426export GUIX_PROFILE GUIX_LOCPATH
427
e69b8bba 428[ -f "$GUIX_PROFILE/etc/profile" ] && . "$GUIX_PROFILE/etc/profile"
30810aff
PG
429
430# set XDG_DATA_DIRS to include Guix installations
7ff169d0 431export XDG_DATA_DIRS="$GUIX_PROFILE/share:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}"
30810aff
PG
432EOF
433}
434
6f4e8693
RW
435welcome()
436{
437 cat<<"EOF"
438 ░░░ ░░░
439 ░░▒▒░░░░░░░░░ ░░░░░░░░░▒▒░░
440 ░░▒▒▒▒▒░░░░░░░ ░░░░░░░▒▒▒▒▒░
441 ░▒▒▒░░▒▒▒▒▒ ░░░░░░░▒▒░
442 ░▒▒▒▒░ ░░░░░░
443 ▒▒▒▒▒ ░░░░░░
444 ▒▒▒▒▒ ░░░░░
445 ░▒▒▒▒▒ ░░░░░
446 ▒▒▒▒▒ ░░░░░
447 ▒▒▒▒▒ ░░░░░
448 ░▒▒▒▒▒░░░░░
449 ▒▒▒▒▒▒░░░
450 ▒▒▒▒▒▒░
451 _____ _ _ _ _ _____ _
452 / ____| \ | | | | | / ____| (_)
453 | | __| \| | | | | | | __ _ _ ___ __
454 | | |_ | . ' | | | | | | |_ | | | | \ \/ /
455 | |__| | |\ | |__| | | |__| | |_| | |> <
456 \_____|_| \_|\____/ \_____|\__,_|_/_/\_\
457
458This script installs GNU Guix on your system
459
460https://www.gnu.org/software/guix/
461EOF
462 echo -n "Press return to continue..."
463 read -r ANSWER
464}
465
466main()
467{
468 local tmp_path
469 welcome
470
471 _msg "Starting installation ($(date))"
472
473 chk_term
6c77d79a 474 chk_require "${REQUIRE[@]}"
5d8e505c 475 chk_gpg_keyring
6f4e8693
RW
476 chk_init_sys
477 chk_sys_arch
478
479 _msg "${INF}system is ${ARCH_OS}"
480
32c06aff 481 umask 0022
6f4e8693
RW
482 tmp_path="$(mktemp -t -d guix.XXX)"
483
484 guix_get_bin_list "${GNU_URL}"
485 guix_get_bin "${GNU_URL}" "${BIN_VER}" "$tmp_path"
486
487 sys_create_store "${BIN_VER}.tar.xz" "${tmp_path}"
488 sys_create_build_user
489 sys_enable_guix_daemon
490 sys_authorize_build_farms
30810aff 491 sys_create_init_profile
6f4e8693
RW
492
493 _msg "${INF}cleaning up ${tmp_path}"
494 rm -r "${tmp_path}"
495
496 _msg "${PAS}Guix has successfully been installed!"
497 _msg "${INF}Run 'info guix' to read the manual."
498 }
499
500main "$@"