guix-install.sh 16 KB

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