guix-install.sh 18 KB

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