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