guix-install.sh 12 KB

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