Commit | Line | Data |
---|---|---|
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> |
ebbf9154 | 6 | # Copyright © 2019, 2020 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. | |
25 | if [ "x$BASH_VERSION" = "x" ] | |
26 | then | |
27 | exec bash "$0" "$@" | |
28 | fi | |
29 | ||
6f4e8693 RW |
30 | set -e |
31 | ||
32 | [ "$UID" -eq 0 ] || { echo "This script must be run as root."; exit 1; } | |
33 | ||
34 | REQUIRE=( | |
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 | ||
53 | PAS=$'[ \033[32;1mPASS\033[0m ] ' | |
54 | ERR=$'[ \033[31;1mFAIL\033[0m ] ' | |
55 | INF="[ INFO ] " | |
56 | ||
57 | DEBUG=0 | |
3a3e9f2b | 58 | GNU_URL="https://ftp.gnu.org/gnu/guix/" |
6f4e8693 RW |
59 | OPENPGP_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. | |
64 | ROOT_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 | ||
87 | chk_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 | ||
105 | chk_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 | ||
118 | chk_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 | ||
138 | chk_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 | ||
158 | chk_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 | ||
199 | guix_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 | ||
230 | guix_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 | ||
262 | sys_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 | ||
294 | sys_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 | ||
323 | sys_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) | |
1a1faa78 TGR |
345 | { # systemd .mount units must be named after the target directory. |
346 | # Here we assume a hard-coded name of /gnu/store. | |
ebbf9154 TGR |
347 | # XXX Work around <https://issues.guix.gnu.org/41356> until next release. |
348 | if [ -f "${ROOT_HOME}/.config/guix/current/lib/systemd/system/gnu-store.mount" ]; then | |
349 | cp "${ROOT_HOME}/.config/guix/current/lib/systemd/system/gnu-store.mount" \ | |
350 | /etc/systemd/system/; | |
351 | chmod 664 /etc/systemd/system/gnu-store.mount; | |
352 | fi | |
1a1faa78 TGR |
353 | |
354 | cp "${ROOT_HOME}/.config/guix/current/lib/systemd/system/guix-daemon.service" \ | |
6f4e8693 RW |
355 | /etc/systemd/system/; |
356 | chmod 664 /etc/systemd/system/guix-daemon.service; | |
e1e3fe08 LC |
357 | |
358 | # Work around <https://bugs.gnu.org/36074>, present in 1.0.1. | |
359 | sed -i /etc/systemd/system/guix-daemon.service \ | |
360 | -e "s/GUIX_LOCPATH='/'GUIX_LOCPATH=/"; | |
361 | ||
362 | # Work around <https://bugs.gnu.org/35671>, present in 1.0.1. | |
363 | if ! grep en_US /etc/systemd/system/guix-daemon.service >/dev/null; | |
364 | then sed -i /etc/systemd/system/guix-daemon.service \ | |
365 | -e 's/^Environment=\(.*\)$/Environment=\1 LC_ALL=en_US.UTF-8'; | |
366 | fi; | |
367 | ||
6f4e8693 | 368 | systemctl daemon-reload && |
1a1faa78 TGR |
369 | systemctl start gnu-store.mount guix-daemon && |
370 | systemctl enable gnu-store.mount guix-daemon; } && | |
6f4e8693 RW |
371 | _msg "${PAS}enabled Guix daemon via systemd" |
372 | ;; | |
fe60ef99 DM |
373 | sysv-init) |
374 | { mkdir -p /etc/init.d; | |
375 | cp "${ROOT_HOME}/.config/guix/current/etc/init.d/guix-daemon" \ | |
376 | /etc/init.d/guix-daemon; | |
377 | chmod 775 /etc/init.d/guix-daemon; | |
378 | ||
379 | update-rc.d guix-daemon defaults && | |
380 | update-rc.d guix-daemon enable && | |
381 | service guix-daemon start; } && | |
382 | _msg "${PAS}enabled Guix daemon via sysv" | |
383 | ;; | |
6f4e8693 RW |
384 | NA|*) |
385 | _msg "${ERR}unsupported init system; run the daemon manually:" | |
e9926f80 | 386 | echo " ${ROOT_HOME}/.config/guix/current/bin/guix-daemon --build-users-group=guixbuild" |
6f4e8693 RW |
387 | ;; |
388 | esac | |
389 | ||
390 | _msg "${INF}making the guix command available to other users" | |
391 | ||
392 | [ -e "$local_bin" ] || mkdir -p "$local_bin" | |
393 | ln -sf "${var_guix}/bin/guix" "$local_bin" | |
394 | ||
395 | [ -e "$info_path" ] || mkdir -p "$info_path" | |
396 | for i in ${var_guix}/share/info/*; do | |
397 | ln -sf "$i" "$info_path" | |
398 | done | |
399 | } | |
400 | ||
401 | sys_authorize_build_farms() | |
414c4de1 | 402 | { # authorize the public key of the build farm |
6f4e8693 | 403 | while true; do |
414c4de1 | 404 | read -p "Permit downloading pre-built package binaries from the project's build farm? (yes/no) " yn |
6f4e8693 | 405 | case $yn in |
414c4de1 | 406 | [Yy]*) guix archive --authorize < "${ROOT_HOME}/.config/guix/current/share/guix/ci.guix.gnu.org.pub" && |
825c39bf | 407 | _msg "${PAS}Authorized public key for ci.guix.gnu.org"; |
6f4e8693 RW |
408 | break;; |
409 | [Nn]*) _msg "${INF}Skipped authorizing build farm public keys" | |
410 | break;; | |
411 | *) _msg "Please answer yes or no."; | |
412 | esac | |
413 | done | |
414 | } | |
415 | ||
30810aff PG |
416 | sys_create_init_profile() |
417 | { # Create /etc/profile.d/guix.sh for better desktop integration | |
29ba58c0 | 418 | [ -d "/etc/profile.d" ] || mkdir /etc/profile.d # Just in case |
30810aff PG |
419 | cat <<"EOF" > /etc/profile.d/guix.sh |
420 | # _GUIX_PROFILE: `guix pull` profile | |
421 | _GUIX_PROFILE="$HOME/.config/guix/current" | |
5c03516a PG |
422 | if [ -L $_GUIX_PROFILE ]; then |
423 | export PATH="$_GUIX_PROFILE/bin${PATH:+:}$PATH" | |
424 | # Export INFOPATH so that the updated info pages can be found | |
425 | # and read by both /usr/bin/info and/or $GUIX_PROFILE/bin/info | |
3c69701f LC |
426 | # When INFOPATH is unset, add a trailing colon so that Emacs |
427 | # searches 'Info-default-directory-list'. | |
428 | export INFOPATH="$_GUIX_PROFILE/share/info:$INFOPATH" | |
5c03516a | 429 | fi |
30810aff PG |
430 | |
431 | # GUIX_PROFILE: User's default profile | |
432 | GUIX_PROFILE="$HOME/.guix-profile" | |
433 | [ -L $GUIX_PROFILE ] || return | |
434 | GUIX_LOCPATH="$GUIX_PROFILE/lib/locale" | |
435 | export GUIX_PROFILE GUIX_LOCPATH | |
436 | ||
e69b8bba | 437 | [ -f "$GUIX_PROFILE/etc/profile" ] && . "$GUIX_PROFILE/etc/profile" |
30810aff PG |
438 | |
439 | # set XDG_DATA_DIRS to include Guix installations | |
7ff169d0 | 440 | export XDG_DATA_DIRS="$GUIX_PROFILE/share:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}" |
30810aff PG |
441 | EOF |
442 | } | |
443 | ||
6f4e8693 RW |
444 | welcome() |
445 | { | |
446 | cat<<"EOF" | |
447 | ░░░ ░░░ | |
448 | ░░▒▒░░░░░░░░░ ░░░░░░░░░▒▒░░ | |
449 | ░░▒▒▒▒▒░░░░░░░ ░░░░░░░▒▒▒▒▒░ | |
450 | ░▒▒▒░░▒▒▒▒▒ ░░░░░░░▒▒░ | |
451 | ░▒▒▒▒░ ░░░░░░ | |
452 | ▒▒▒▒▒ ░░░░░░ | |
453 | ▒▒▒▒▒ ░░░░░ | |
454 | ░▒▒▒▒▒ ░░░░░ | |
455 | ▒▒▒▒▒ ░░░░░ | |
456 | ▒▒▒▒▒ ░░░░░ | |
457 | ░▒▒▒▒▒░░░░░ | |
458 | ▒▒▒▒▒▒░░░ | |
459 | ▒▒▒▒▒▒░ | |
460 | _____ _ _ _ _ _____ _ | |
461 | / ____| \ | | | | | / ____| (_) | |
462 | | | __| \| | | | | | | __ _ _ ___ __ | |
463 | | | |_ | . ' | | | | | | |_ | | | | \ \/ / | |
464 | | |__| | |\ | |__| | | |__| | |_| | |> < | |
465 | \_____|_| \_|\____/ \_____|\__,_|_/_/\_\ | |
466 | ||
467 | This script installs GNU Guix on your system | |
468 | ||
469 | https://www.gnu.org/software/guix/ | |
470 | EOF | |
471 | echo -n "Press return to continue..." | |
472 | read -r ANSWER | |
473 | } | |
474 | ||
475 | main() | |
476 | { | |
477 | local tmp_path | |
478 | welcome | |
479 | ||
480 | _msg "Starting installation ($(date))" | |
481 | ||
482 | chk_term | |
6c77d79a | 483 | chk_require "${REQUIRE[@]}" |
5d8e505c | 484 | chk_gpg_keyring |
6f4e8693 RW |
485 | chk_init_sys |
486 | chk_sys_arch | |
487 | ||
488 | _msg "${INF}system is ${ARCH_OS}" | |
489 | ||
32c06aff | 490 | umask 0022 |
6f4e8693 RW |
491 | tmp_path="$(mktemp -t -d guix.XXX)" |
492 | ||
493 | guix_get_bin_list "${GNU_URL}" | |
494 | guix_get_bin "${GNU_URL}" "${BIN_VER}" "$tmp_path" | |
495 | ||
496 | sys_create_store "${BIN_VER}.tar.xz" "${tmp_path}" | |
497 | sys_create_build_user | |
498 | sys_enable_guix_daemon | |
499 | sys_authorize_build_farms | |
30810aff | 500 | sys_create_init_profile |
6f4e8693 RW |
501 | |
502 | _msg "${INF}cleaning up ${tmp_path}" | |
503 | rm -r "${tmp_path}" | |
504 | ||
505 | _msg "${PAS}Guix has successfully been installed!" | |
506 | _msg "${INF}Run 'info guix' to read the manual." | |
507 | } | |
508 | ||
509 | main "$@" |