| 1 | #!/bin/sh |
| 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 | # Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr> |
| 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 | |
| 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 | |
| 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 |
| 58 | GNU_URL="https://ftp.gnu.org/gnu/guix/" |
| 59 | OPENPGP_SIGNING_KEY_ID="3CE464558A84FDC69DB40CFB090B11993D9AEBB5" |
| 60 | |
| 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 | |
| 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. |
| 89 | declare -a warn |
| 90 | local c |
| 91 | |
| 92 | _debug "--- [ $FUNCNAME ] ---" |
| 93 | |
| 94 | for c in "$@"; do |
| 95 | command -v "$c" &>/dev/null || warn+=("$c") |
| 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" |
| 103 | } |
| 104 | |
| 105 | chk_gpg_keyring() |
| 106 | { # Check whether the Guix release signing public key is present. |
| 107 | _debug "--- [ $FUNCNAME ] ---" |
| 108 | |
| 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 || ( |
| 112 | _err "${ERR}Missing OpenPGP public key. Fetch it with this command:" |
| 113 | echo " wget https://sv.gnu.org/people/viewgpg.php?user_id=15145 -qO - | gpg --import -" |
| 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 | ;; |
| 173 | aarch64) |
| 174 | local arch=aarch64 |
| 175 | ;; |
| 176 | armv7l) |
| 177 | local arch=armhf |
| 178 | ;; |
| 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" |
| 285 | mkdir -p "${ROOT_HOME}/.config/guix" |
| 286 | ln -sf /var/guix/profiles/per-user/root/current-guix \ |
| 287 | "${ROOT_HOME}/.config/guix/current" |
| 288 | |
| 289 | GUIX_PROFILE="${ROOT_HOME}/.config/guix/current" |
| 290 | source "${GUIX_PROFILE}/etc/profile" |
| 291 | _msg "${PAS}activated root profile at ${ROOT_HOME}/.config/guix/current" |
| 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" |
| 334 | var_guix="/var/guix/profiles/per-user/root/current-guix" |
| 335 | |
| 336 | case "$INIT_SYS" in |
| 337 | upstart) |
| 338 | { initctl reload-configuration; |
| 339 | cp "${ROOT_HOME}/.config/guix/current/lib/upstart/system/guix-daemon.conf" \ |
| 340 | /etc/init/ && |
| 341 | start guix-daemon; } && |
| 342 | _msg "${PAS}enabled Guix daemon via upstart" |
| 343 | ;; |
| 344 | systemd) |
| 345 | { cp "${ROOT_HOME}/.config/guix/current/lib/systemd/system/guix-daemon.service" \ |
| 346 | /etc/systemd/system/; |
| 347 | chmod 664 /etc/systemd/system/guix-daemon.service; |
| 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 | |
| 359 | systemctl daemon-reload && |
| 360 | systemctl start guix-daemon && |
| 361 | systemctl enable guix-daemon; } && |
| 362 | _msg "${PAS}enabled Guix daemon via systemd" |
| 363 | ;; |
| 364 | NA|*) |
| 365 | _msg "${ERR}unsupported init system; run the daemon manually:" |
| 366 | echo " ${ROOT_HOME}/.config/guix/current/bin/guix-daemon --build-users-group=guixbuild" |
| 367 | ;; |
| 368 | esac |
| 369 | |
| 370 | _msg "${INF}making the guix command available to other users" |
| 371 | |
| 372 | [ -e "$local_bin" ] || mkdir -p "$local_bin" |
| 373 | ln -sf "${var_guix}/bin/guix" "$local_bin" |
| 374 | |
| 375 | [ -e "$info_path" ] || mkdir -p "$info_path" |
| 376 | for i in ${var_guix}/share/info/*; do |
| 377 | ln -sf "$i" "$info_path" |
| 378 | done |
| 379 | } |
| 380 | |
| 381 | sys_authorize_build_farms() |
| 382 | { # authorize the public key of the build farm |
| 383 | while true; do |
| 384 | read -p "Permit downloading pre-built package binaries from the project's build farm? (yes/no) " yn |
| 385 | case $yn in |
| 386 | [Yy]*) guix archive --authorize < "${ROOT_HOME}/.config/guix/current/share/guix/ci.guix.gnu.org.pub" && |
| 387 | _msg "${PAS}Authorized public key for ci.guix.gnu.org"; |
| 388 | break;; |
| 389 | [Nn]*) _msg "${INF}Skipped authorizing build farm public keys" |
| 390 | break;; |
| 391 | *) _msg "Please answer yes or no."; |
| 392 | esac |
| 393 | done |
| 394 | } |
| 395 | |
| 396 | welcome() |
| 397 | { |
| 398 | cat<<"EOF" |
| 399 | ░░░ ░░░ |
| 400 | ░░▒▒░░░░░░░░░ ░░░░░░░░░▒▒░░ |
| 401 | ░░▒▒▒▒▒░░░░░░░ ░░░░░░░▒▒▒▒▒░ |
| 402 | ░▒▒▒░░▒▒▒▒▒ ░░░░░░░▒▒░ |
| 403 | ░▒▒▒▒░ ░░░░░░ |
| 404 | ▒▒▒▒▒ ░░░░░░ |
| 405 | ▒▒▒▒▒ ░░░░░ |
| 406 | ░▒▒▒▒▒ ░░░░░ |
| 407 | ▒▒▒▒▒ ░░░░░ |
| 408 | ▒▒▒▒▒ ░░░░░ |
| 409 | ░▒▒▒▒▒░░░░░ |
| 410 | ▒▒▒▒▒▒░░░ |
| 411 | ▒▒▒▒▒▒░ |
| 412 | _____ _ _ _ _ _____ _ |
| 413 | / ____| \ | | | | | / ____| (_) |
| 414 | | | __| \| | | | | | | __ _ _ ___ __ |
| 415 | | | |_ | . ' | | | | | | |_ | | | | \ \/ / |
| 416 | | |__| | |\ | |__| | | |__| | |_| | |> < |
| 417 | \_____|_| \_|\____/ \_____|\__,_|_/_/\_\ |
| 418 | |
| 419 | This script installs GNU Guix on your system |
| 420 | |
| 421 | https://www.gnu.org/software/guix/ |
| 422 | EOF |
| 423 | echo -n "Press return to continue..." |
| 424 | read -r ANSWER |
| 425 | } |
| 426 | |
| 427 | main() |
| 428 | { |
| 429 | local tmp_path |
| 430 | welcome |
| 431 | |
| 432 | _msg "Starting installation ($(date))" |
| 433 | |
| 434 | chk_term |
| 435 | chk_require "${REQUIRE[@]}" |
| 436 | chk_gpg_keyring |
| 437 | chk_init_sys |
| 438 | chk_sys_arch |
| 439 | |
| 440 | _msg "${INF}system is ${ARCH_OS}" |
| 441 | |
| 442 | umask 0022 |
| 443 | tmp_path="$(mktemp -t -d guix.XXX)" |
| 444 | |
| 445 | guix_get_bin_list "${GNU_URL}" |
| 446 | guix_get_bin "${GNU_URL}" "${BIN_VER}" "$tmp_path" |
| 447 | |
| 448 | sys_create_store "${BIN_VER}.tar.xz" "${tmp_path}" |
| 449 | sys_create_build_user |
| 450 | sys_enable_guix_daemon |
| 451 | sys_authorize_build_farms |
| 452 | |
| 453 | _msg "${INF}cleaning up ${tmp_path}" |
| 454 | rm -r "${tmp_path}" |
| 455 | |
| 456 | _msg "${PAS}Guix has successfully been installed!" |
| 457 | _msg "${INF}Run 'info guix' to read the manual." |
| 458 | } |
| 459 | |
| 460 | main "$@" |