gnu: imapfilter: Update to 2.7.6.
[jackhill/guix/guix.git] / gnu / installer.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2019, 2020 Tobias Geerinckx-Rice <me@tobias.gr>
5 ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
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 (define-module (gnu installer)
23 #:use-module (guix discovery)
24 #:use-module (guix packages)
25 #:use-module (guix gexp)
26 #:use-module (guix modules)
27 #:use-module (guix utils)
28 #:use-module (guix ui)
29 #:use-module ((guix self) #:select (make-config.scm))
30 #:use-module (guix packages)
31 #:use-module (guix git-download)
32 #:use-module (gnu installer utils)
33 #:use-module (gnu packages admin)
34 #:use-module (gnu packages base)
35 #:use-module (gnu packages bash)
36 #:use-module (gnu packages compression)
37 #:use-module (gnu packages connman)
38 #:use-module (gnu packages cryptsetup)
39 #:use-module (gnu packages disk)
40 #:use-module (gnu packages file-systems)
41 #:use-module (gnu packages guile)
42 #:use-module (gnu packages guile-xyz)
43 #:autoload (gnu packages gnupg) (guile-gcrypt)
44 #:use-module (gnu packages iso-codes)
45 #:use-module (gnu packages linux)
46 #:use-module (gnu packages nano)
47 #:use-module (gnu packages ncurses)
48 #:use-module (gnu packages package-management)
49 #:use-module (gnu packages tls)
50 #:use-module (gnu packages xorg)
51 #:use-module (gnu system locale)
52 #:use-module (ice-9 match)
53 #:use-module (srfi srfi-1)
54 #:export (installer-program))
55
56 (define module-to-import?
57 ;; Return true for modules that should be imported. For (gnu system …) and
58 ;; (gnu packages …) modules, we simply add the whole 'guix' package via
59 ;; 'with-extensions' (to avoid having to rebuild it all), which is why these
60 ;; modules are excluded here.
61 (match-lambda
62 (('guix 'config) #f)
63 (('gnu 'installer _ ...) #t)
64 (('gnu 'build _ ...) #t)
65 (('guix 'build _ ...) #t)
66 (('guix 'read-print) #t)
67 (_ #f)))
68
69 (define not-config?
70 ;; Select (guix …) and (gnu …) modules, except (guix config).
71 (match-lambda
72 (('guix 'config) #f)
73 (('guix _ ...) #t)
74 (('gnu _ ...) #t)
75 (_ #f)))
76
77 (define* (build-compiled-file name locale-builder)
78 "Return a file-like object that evaluates the gexp LOCALE-BUILDER and store
79 its result in the scheme file NAME. The derivation will also build a compiled
80 version of this file."
81 (define set-utf8-locale
82 #~(begin
83 (setenv "LOCPATH"
84 #$(file-append glibc-utf8-locales "/lib/locale/"
85 (version-major+minor
86 (package-version glibc-utf8-locales))))
87 (setlocale LC_ALL "en_US.utf8")))
88
89 (define builder
90 (with-extensions (list guile-json-3)
91 (with-imported-modules `(,@(source-module-closure
92 '((gnu installer locale))
93 #:select? not-config?)
94 ((guix config) => ,(make-config.scm)))
95 #~(begin
96 (use-modules (gnu installer locale))
97
98 ;; The locale files contain non-ASCII characters.
99 #$set-utf8-locale
100
101 (mkdir #$output)
102 (let ((locale-file
103 (string-append #$output "/" #$name ".scm"))
104 (locale-compiled-file
105 (string-append #$output "/" #$name ".go")))
106 (call-with-output-file locale-file
107 (lambda (port)
108 (write #$locale-builder port)))
109 (compile-file locale-file
110 #:output-file locale-compiled-file))))))
111 (computed-file name builder))
112
113 (define apply-locale
114 ;; Install the specified locale.
115 (with-imported-modules (source-module-closure '((gnu services herd)))
116 #~(lambda (locale)
117 (false-if-exception
118 (setlocale LC_ALL locale))
119
120 ;; Restart the documentation viewer so it displays the manual in
121 ;; language that corresponds to LOCALE. Make sure that nothing is
122 ;; printed on the console.
123 (parameterize ((shepherd-message-port
124 (%make-void-port "w")))
125 (stop-service 'term-tty2)
126 (start-service 'term-tty2 (list locale))))))
127
128 (define* (compute-locale-step #:key
129 locales-name
130 iso639-languages-name
131 iso3166-territories-name)
132 "Return a gexp that run the locale-page of INSTALLER, and install the
133 selected locale. The list of locales, languages and territories passed to
134 locale-page are computed in derivations named respectively LOCALES-NAME,
135 ISO639-LANGUAGES-NAME and ISO3166-TERRITORIES-NAME. Those lists are compiled,
136 so that when the installer is run, all the lengthy operations have already
137 been performed at build time."
138 (define (compiled-file-loader file name)
139 #~(load-compiled
140 (string-append #$file "/" #$name ".go")))
141
142 (let* ((supported-locales #~(supported-locales->locales
143 #+(glibc-supported-locales)))
144 (iso-codes #~(string-append #$iso-codes "/share/iso-codes/json/"))
145 (iso639-3 #~(string-append #$iso-codes "iso_639-3.json"))
146 (iso639-5 #~(string-append #$iso-codes "iso_639-5.json"))
147 (iso3166 #~(string-append #$iso-codes "iso_3166-1.json"))
148 (locales-file (build-compiled-file
149 locales-name
150 #~`(quote ,#$supported-locales)))
151 (iso639-file (build-compiled-file
152 iso639-languages-name
153 #~`(quote ,(iso639->iso639-languages
154 #$supported-locales
155 #$iso639-3 #$iso639-5))))
156 (iso3166-file (build-compiled-file
157 iso3166-territories-name
158 #~`(quote ,(iso3166->iso3166-territories #$iso3166))))
159 (locales-loader (compiled-file-loader locales-file
160 locales-name))
161 (iso639-loader (compiled-file-loader iso639-file
162 iso639-languages-name))
163 (iso3166-loader (compiled-file-loader iso3166-file
164 iso3166-territories-name)))
165 #~(lambda (current-installer)
166 (let ((result
167 ((installer-locale-page current-installer)
168 #:supported-locales #$locales-loader
169 #:iso639-languages #$iso639-loader
170 #:iso3166-territories #$iso3166-loader)))
171 (#$apply-locale result)
172 result))))
173
174 (define apply-keymap
175 ;; Apply the specified keymap. Use the default keyboard model.
176 #~(match-lambda
177 ((layout variant options)
178 (kmscon-update-keymap (default-keyboard-model)
179 layout variant options))))
180
181 (define* (compute-keymap-step context)
182 "Return a gexp that runs the keymap-page of INSTALLER and install the
183 selected keymap."
184 #~(lambda (current-installer)
185 (let ((result
186 (call-with-values
187 (lambda ()
188 (xkb-rules->models+layouts
189 (string-append #$xkeyboard-config
190 "/share/X11/xkb/rules/base.xml")))
191 (lambda (models layouts)
192 ((installer-keymap-page current-installer)
193 layouts '#$context)))))
194 (and result (#$apply-keymap result))
195 result)))
196
197 (define (installer-steps)
198 (let ((locale-step (compute-locale-step
199 #:locales-name "locales"
200 #:iso639-languages-name "iso639-languages"
201 #:iso3166-territories-name "iso3166-territories"))
202 (timezone-data #~(string-append #$tzdata
203 "/share/zoneinfo/zone.tab")))
204 #~(lambda (current-installer)
205 ((installer-parameters-menu current-installer)
206 (lambda ()
207 ((installer-parameters-page current-installer)
208 (lambda _
209 (#$(compute-keymap-step 'param)
210 current-installer)))))
211 (list
212 ;; Ask the user to choose a locale among those supported by
213 ;; the glibc. Install the selected locale right away, so that
214 ;; the user may benefit from any available translation for the
215 ;; installer messages.
216 (installer-step
217 (id 'locale)
218 (description (G_ "Locale"))
219 (compute (lambda _
220 (#$locale-step current-installer)))
221 (configuration-formatter locale->configuration))
222
223 ;; Welcome the user and ask them to choose between manual
224 ;; installation and graphical install.
225 (installer-step
226 (id 'welcome)
227 (compute (lambda _
228 ((installer-welcome-page current-installer)
229 #$(local-file "installer/aux-files/logo.txt")))))
230
231 ;; Ask the user to select a timezone under glibc format.
232 (installer-step
233 (id 'timezone)
234 (description (G_ "Timezone"))
235 (compute (lambda _
236 ((installer-timezone-page current-installer)
237 #$timezone-data)))
238 (configuration-formatter posix-tz->configuration))
239
240 ;; The installer runs in a kmscon virtual terminal where loadkeys
241 ;; won't work. kmscon uses libxkbcommon as a backend for keyboard
242 ;; input. It is possible to update kmscon current keymap by sending
243 ;; it a keyboard model, layout, variant and options, in a somehow
244 ;; similar way as what is done with setxkbmap utility.
245 ;;
246 ;; So ask for a keyboard model, layout and variant to update the
247 ;; current kmscon keymap. For non-Latin layouts, we add an
248 ;; appropriate second layout and toggle via Alt+Shift.
249 (installer-step
250 (id 'keymap)
251 (description (G_ "Keyboard mapping selection"))
252 (compute (lambda _
253 (#$(compute-keymap-step 'default)
254 current-installer)))
255 (configuration-formatter keyboard-layout->configuration))
256
257 ;; Ask the user to input a hostname for the system.
258 (installer-step
259 (id 'hostname)
260 (description (G_ "Hostname"))
261 (compute (lambda _
262 ((installer-hostname-page current-installer))))
263 (configuration-formatter hostname->configuration))
264
265 ;; Provide an interface above connmanctl, so that the user can select
266 ;; a network susceptible to acces Internet.
267 (installer-step
268 (id 'network)
269 (description (G_ "Network selection"))
270 (compute (lambda _
271 ((installer-network-page current-installer)))))
272
273 ;; Ask whether to enable substitute server discovery.
274 (installer-step
275 (id 'substitutes)
276 (description (G_ "Substitute server discovery"))
277 (compute (lambda _
278 ((installer-substitutes-page current-installer)))))
279
280 ;; Prompt for users (name, group and home directory).
281 (installer-step
282 (id 'user)
283 (description (G_ "User creation"))
284 (compute (lambda _
285 ((installer-user-page current-installer))))
286 (configuration-formatter users->configuration))
287
288 ;; Ask the user to choose one or many desktop environment(s).
289 (installer-step
290 (id 'services)
291 (description (G_ "Services"))
292 (compute (lambda _
293 ((installer-services-page current-installer))))
294 (configuration-formatter system-services->configuration))
295
296 ;; Run a partitioning tool allowing the user to modify
297 ;; partition tables, partitions and their mount points.
298 ;; Do this last so the user has something to boot if any
299 ;; of the previous steps didn't go as expected.
300 (installer-step
301 (id 'partition)
302 (description (G_ "Partitioning"))
303 (compute (lambda _
304 ((installer-partition-page current-installer))))
305 (configuration-formatter user-partitions->configuration))
306
307 (installer-step
308 (id 'final)
309 (description (G_ "Configuration file"))
310 (compute
311 (lambda (result prev-steps)
312 ((installer-final-page current-installer)
313 result prev-steps))))))))
314
315 (define (installer-program)
316 "Return a file-like object that runs the given INSTALLER."
317 (define init-gettext
318 ;; Initialize gettext support, so that installer messages can be
319 ;; translated.
320 #~(begin
321 (bindtextdomain "guix" (string-append #$guix "/share/locale"))
322 (textdomain "guix")
323 (setlocale LC_ALL "")))
324
325 (define set-installer-path
326 ;; Add the specified binary to PATH for later use by the installer.
327 #~(let* ((inputs
328 '#$(list bash ;start subshells
329 connman ;call connmanctl
330 cryptsetup
331 dosfstools ;mkfs.fat
332 e2fsprogs ;mkfs.ext4
333 lvm2-static ;dmsetup
334 btrfs-progs
335 jfsutils ;jfs_mkfs
336 ntfs-3g ;mkfs.ntfs
337 xfsprogs ;mkfs.xfs
338 kbd ;chvt
339 util-linux ;mkwap
340 nano
341 shadow
342 tar ;dump
343 gzip ;dump
344 coreutils)))
345 (with-output-to-port (%make-void-port "w")
346 (lambda ()
347 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)))))
348
349 (define steps (installer-steps))
350 (define modules
351 (scheme-modules*
352 (string-append (current-source-directory) "/..")
353 "gnu/installer"))
354
355 (define installer-builder
356 ;; Note: Include GUIX as an extension to get all the (gnu system …), (gnu
357 ;; packages …), etc. modules.
358 (with-extensions (list guile-gcrypt guile-newt
359 guile-parted guile-bytestructures
360 guile-json-3 guile-git guile-webutils
361 guix gnutls)
362 (with-imported-modules `(,@(source-module-closure
363 `(,@modules
364 (gnu services herd)
365 (guix build utils))
366 #:select? module-to-import?)
367 ((guix config) => ,(make-config.scm)))
368 #~(begin
369 (use-modules (gnu installer record)
370 (gnu installer keymap)
371 (gnu installer steps)
372 (gnu installer dump)
373 (gnu installer final)
374 (gnu installer hostname)
375 (gnu installer locale)
376 (gnu installer parted)
377 (gnu installer services)
378 (gnu installer timezone)
379 (gnu installer user)
380 (gnu installer utils)
381 (gnu installer newt)
382 ((gnu installer newt keymap)
383 #:select (keyboard-layout->configuration))
384 (gnu services herd)
385 (guix i18n)
386 (guix build utils)
387 ((system repl debug)
388 #:select (terminal-width))
389 (ice-9 match)
390 (ice-9 textual-ports))
391
392 ;; Initialize gettext support so that installers can use
393 ;; (guix i18n) module.
394 #$init-gettext
395
396 ;; Add some binaries used by the installers to PATH.
397 #$set-installer-path
398
399 ;; Arrange for language and territory name translations to be
400 ;; available. We need them at run time, not just compile time,
401 ;; because some territories have several corresponding languages
402 ;; (e.g., "French" is always displayed as "français", but
403 ;; "Belgium" could be translated to Dutch, French, or German.)
404 (bindtextdomain "iso_639-3" ;languages
405 #+(file-append iso-codes "/share/locale"))
406 (bindtextdomain "iso_3166-1" ;territories
407 #+(file-append iso-codes "/share/locale"))
408
409 ;; Likewise for XKB keyboard layout names.
410 (bindtextdomain "xkeyboard-config"
411 #+(file-append xkeyboard-config "/share/locale"))
412
413 ;; Initialize 'terminal-width' in (system repl debug)
414 ;; to a large-enough value to make backtrace more
415 ;; verbose.
416 (terminal-width 200)
417
418 (define current-installer newt-installer)
419 (define steps (#$steps current-installer))
420
421 (dynamic-wind
422 (installer-init current-installer)
423 (lambda ()
424 (parameterize
425 ((run-command-in-installer
426 (installer-run-command current-installer)))
427 (catch #t
428 (lambda ()
429 (define results
430 (run-installer-steps
431 #:rewind-strategy 'menu
432 #:menu-proc (installer-menu-page current-installer)
433 #:steps steps))
434
435 (match (result-step results 'final)
436 ('success
437 ;; We did it! Let's reboot!
438 (sync)
439 (stop-service 'root))
440 (_
441 ;; The installation failed, exit so that it is
442 ;; restarted by login.
443 #f)))
444 (const #f)
445 (lambda (key . args)
446 (installer-log-line "crashing due to uncaught exception: ~s ~s"
447 key args)
448 (define dump-dir
449 (prepare-dump key args #:result %current-result))
450 (define action
451 ((installer-exit-error current-installer)
452 (get-string-all
453 (open-input-file
454 (string-append dump-dir "/installer-backtrace")))))
455 (match action
456 ('dump
457 (let* ((dump-files
458 ((installer-dump-page current-installer)
459 dump-dir))
460 (dump-archive
461 (make-dump dump-dir dump-files)))
462 ((installer-report-page current-installer)
463 dump-archive)))
464 (_ #f))
465 (exit 1)))))
466
467 (installer-exit current-installer))))))
468
469 (program-file
470 "installer"
471 #~(begin
472 ;; Set the default locale to install unicode support. For
473 ;; some reason, unicode support is not correctly installed
474 ;; when calling this in 'installer-builder'.
475 (setenv "LANG" "en_US.UTF-8")
476 (execl #$(program-file "installer-real" installer-builder
477 #:guile guile-3.0-latest)
478 "installer-real"))))