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