guix-install.sh 13 KB

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