1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu installer)
21 #:use-module (guix discovery)
22 #:use-module (guix packages)
23 #:use-module (guix gexp)
24 #:use-module (guix modules)
25 #:use-module (guix utils)
26 #:use-module (guix ui)
27 #:use-module ((guix self) #:select (make-config.scm))
28 #:use-module (gnu packages admin)
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages bash)
31 #:use-module (gnu packages connman)
32 #:use-module (gnu packages cryptsetup)
33 #:use-module (gnu packages disk)
34 #:use-module (gnu packages guile)
35 #:use-module (gnu packages guile-xyz)
36 #:autoload (gnu packages gnupg) (guile-gcrypt)
37 #:use-module (gnu packages iso-codes)
38 #:use-module (gnu packages linux)
39 #:use-module (gnu packages ncurses)
40 #:use-module (gnu packages package-management)
41 #:use-module (gnu packages xorg)
42 #:use-module (gnu system locale)
43 #:use-module (ice-9 match)
44 #:use-module (srfi srfi-1)
45 #:export (installer-program))
47 (define module-to-import?
48 ;; Return true for modules that should be imported. For (gnu system …) and
49 ;; (gnu packages …) modules, we simply add the whole 'guix' package via
50 ;; 'with-extensions' (to avoid having to rebuild it all), which is why these
51 ;; modules are excluded here.
54 (('gnu 'installer _ ...) #t)
55 (('gnu 'build _ ...) #t)
56 (('guix 'build _ ...) #t)
59 (define* (build-compiled-file name locale-builder)
60 "Return a file-like object that evalutes the gexp LOCALE-BUILDER and store
61 its result in the scheme file NAME. The derivation will also build a compiled
62 version of this file."
63 (define set-utf8-locale
66 #$(file-append glibc-utf8-locales "/lib/locale/"
68 (package-version glibc-utf8-locales))))
69 (setlocale LC_ALL "en_US.utf8")))
72 (with-extensions (list guile-json-3)
73 (with-imported-modules (source-module-closure
74 '((gnu installer locale)))
76 (use-modules (gnu installer locale))
78 ;; The locale files contain non-ASCII characters.
83 (string-append #$output "/" #$name ".scm"))
85 (string-append #$output "/" #$name ".go")))
86 (call-with-output-file locale-file
88 (write #$locale-builder port)))
89 (compile-file locale-file
90 #:output-file locale-compiled-file))))))
91 (computed-file name builder))
94 ;; Install the specified locale.
95 (with-imported-modules (source-module-closure '((gnu services herd)))
98 (setlocale LC_ALL locale))
100 ;; Restart the documentation viewer so it displays the manual in
101 ;; language that corresponds to LOCALE.
102 (with-error-to-port (%make-void-port "w")
104 (stop-service 'term-tty2)
105 (start-service 'term-tty2 (list locale)))))))
107 (define* (compute-locale-step #:key
109 iso639-languages-name
110 iso3166-territories-name)
111 "Return a gexp that run the locale-page of INSTALLER, and install the
112 selected locale. The list of locales, languages and territories passed to
113 locale-page are computed in derivations named respectively LOCALES-NAME,
114 ISO639-LANGUAGES-NAME and ISO3166-TERRITORIES-NAME. Those lists are compiled,
115 so that when the installer is run, all the lengthy operations have already
116 been performed at build time."
117 (define (compiled-file-loader file name)
119 (string-append #$file "/" #$name ".go")))
121 (let* ((supported-locales #~(supported-locales->locales
122 #+(glibc-supported-locales)))
123 (iso-codes #~(string-append #$iso-codes "/share/iso-codes/json/"))
124 (iso639-3 #~(string-append #$iso-codes "iso_639-3.json"))
125 (iso639-5 #~(string-append #$iso-codes "iso_639-5.json"))
126 (iso3166 #~(string-append #$iso-codes "iso_3166-1.json"))
127 (locales-file (build-compiled-file
129 #~`(quote ,#$supported-locales)))
130 (iso639-file (build-compiled-file
131 iso639-languages-name
132 #~`(quote ,(iso639->iso639-languages
134 #$iso639-3 #$iso639-5))))
135 (iso3166-file (build-compiled-file
136 iso3166-territories-name
137 #~`(quote ,(iso3166->iso3166-territories #$iso3166))))
138 (locales-loader (compiled-file-loader locales-file
140 (iso639-loader (compiled-file-loader iso639-file
141 iso639-languages-name))
142 (iso3166-loader (compiled-file-loader iso3166-file
143 iso3166-territories-name)))
144 #~(lambda (current-installer)
146 ((installer-locale-page current-installer)
147 #:supported-locales #$locales-loader
148 #:iso639-languages #$iso639-loader
149 #:iso3166-territories #$iso3166-loader)))
150 (#$apply-locale result)
154 ;; Apply the specified keymap. Use the default keyboard model.
157 (kmscon-update-keymap (default-keyboard-model)
160 (define* (compute-keymap-step)
161 "Return a gexp that runs the keymap-page of INSTALLER and install the
163 #~(lambda (current-installer)
167 (xkb-rules->models+layouts
168 (string-append #$xkeyboard-config
169 "/share/X11/xkb/rules/base.xml")))
170 (lambda (models layouts)
171 ((installer-keymap-page current-installer)
173 (#$apply-keymap result)
176 (define (installer-steps)
177 (let ((locale-step (compute-locale-step
178 #:locales-name "locales"
179 #:iso639-languages-name "iso639-languages"
180 #:iso3166-territories-name "iso3166-territories"))
181 (keymap-step (compute-keymap-step))
182 (timezone-data #~(string-append #$tzdata
183 "/share/zoneinfo/zone.tab")))
184 #~(lambda (current-installer)
186 ;; Ask the user to choose a locale among those supported by
187 ;; the glibc. Install the selected locale right away, so that
188 ;; the user may benefit from any available translation for the
189 ;; installer messages.
192 (description (G_ "Locale"))
194 (#$locale-step current-installer)))
195 (configuration-formatter locale->configuration))
197 ;; Welcome the user and ask them to choose between manual
198 ;; installation and graphical install.
202 ((installer-welcome-page current-installer)
203 #$(local-file "installer/aux-files/logo.txt")))))
205 ;; Ask the user to select a timezone under glibc format.
208 (description (G_ "Timezone"))
210 ((installer-timezone-page current-installer)
212 (configuration-formatter posix-tz->configuration))
214 ;; The installer runs in a kmscon virtual terminal where loadkeys
215 ;; won't work. kmscon uses libxkbcommon as a backend for keyboard
216 ;; input. It is possible to update kmscon current keymap by sending it
217 ;; a keyboard model, layout and variant, in a somehow similar way as
218 ;; what is done with setxkbmap utility.
220 ;; So ask for a keyboard model, layout and variant to update the
221 ;; current kmscon keymap.
224 (description (G_ "Keyboard mapping selection"))
226 (#$keymap-step current-installer)))
227 (configuration-formatter keyboard-layout->configuration))
229 ;; Run a partitioning tool allowing the user to modify
230 ;; partition tables, partitions and their mount points.
233 (description (G_ "Partitioning"))
235 ((installer-partition-page current-installer))))
236 (configuration-formatter user-partitions->configuration))
238 ;; Ask the user to input a hostname for the system.
241 (description (G_ "Hostname"))
243 ((installer-hostname-page current-installer))))
244 (configuration-formatter hostname->configuration))
246 ;; Provide an interface above connmanctl, so that the user can select
247 ;; a network susceptible to acces Internet.
250 (description (G_ "Network selection"))
252 ((installer-network-page current-installer)))))
254 ;; Prompt for users (name, group and home directory).
257 (description (G_ "User creation"))
259 ((installer-user-page current-installer))))
260 (configuration-formatter users->configuration))
262 ;; Ask the user to choose one or many desktop environment(s).
265 (description (G_ "Services"))
267 ((installer-services-page current-installer))))
268 (configuration-formatter system-services->configuration))
272 (description (G_ "Configuration file"))
274 (lambda (result prev-steps)
275 ((installer-final-page current-installer)
276 result prev-steps))))))))
278 (define (installer-program)
279 "Return a file-like object that runs the given INSTALLER."
281 ;; Initialize gettext support, so that installer messages can be
284 (bindtextdomain "guix" (string-append #$guix "/share/locale"))
285 (textdomain "guix")))
287 (define set-installer-path
288 ;; Add the specified binary to PATH for later use by the installer.
290 '#$(append (list bash ;start subshells
291 connman ;call connmanctl
297 guix ;guix system init call
300 (map canonical-package (list coreutils)))))
301 (with-output-to-port (%make-void-port "w")
303 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)))))
305 (define steps (installer-steps))
308 (string-append (current-source-directory) "/..")
311 (define installer-builder
312 ;; Note: Include GUIX as an extension to get all the (gnu system …), (gnu
313 ;; packages …), etc. modules.
314 (with-extensions (list guile-gcrypt guile-newt
315 guile-parted guile-bytestructures
316 guile-json-3 guile-git guix)
317 (with-imported-modules `(,@(source-module-closure
321 #:select? module-to-import?)
322 ((guix config) => ,(make-config.scm)))
324 (use-modules (gnu installer record)
325 (gnu installer keymap)
326 (gnu installer steps)
327 (gnu installer final)
328 (gnu installer hostname)
329 (gnu installer locale)
330 (gnu installer parted)
331 (gnu installer services)
332 (gnu installer timezone)
335 ((gnu installer newt keymap)
336 #:select (keyboard-layout->configuration))
341 #:select (terminal-width))
344 ;; Initialize gettext support so that installers can use
345 ;; (guix i18n) module.
348 ;; Add some binaries used by the installers to PATH.
351 ;; Arrange for language and territory name translations to be
352 ;; available. We need them at run time, not just compile time,
353 ;; because some territories have several corresponding languages
354 ;; (e.g., "French" is always displayed as "français", but
355 ;; "Belgium" could be translated to Dutch, French, or German.)
356 (bindtextdomain "iso_639-3" ;languages
357 #+(file-append iso-codes "/share/locale"))
358 (bindtextdomain "iso_3166-1" ;territories
359 #+(file-append iso-codes "/share/locale"))
361 ;; Likewise for XKB keyboard layout names.
362 (bindtextdomain "xkeyboard-config"
363 #+(file-append xkeyboard-config "/share/locale"))
365 ;; Initialize 'terminal-width' in (system repl debug)
366 ;; to a large-enough value to make backtrace more
370 (let* ((current-installer newt-installer)
371 (steps (#$steps current-installer)))
372 ((installer-init current-installer))
378 #:rewind-strategy 'menu
379 #:menu-proc (installer-menu-page current-installer)
382 (match (result-step results 'final)
384 ;; We did it! Let's reboot!
386 (stop-service 'root))
387 (_ ;installation failed
388 ;; TODO: Honor the result of 'run-install-failed-page'.
392 (let ((error-file "/tmp/last-installer-error"))
393 (call-with-output-file error-file
395 (display-backtrace (make-stack #t) port)
396 (print-exception port
397 (stack-ref (make-stack #t) 1)
399 ((installer-exit-error current-installer)
400 error-file key args))
403 ((installer-exit current-installer)))))))
408 ;; Set the default locale to install unicode support. For
409 ;; some reason, unicode support is not correctly installed
410 ;; when calling this in 'installer-builder'.
411 (setenv "LANG" "en_US.UTF-8")
412 (execl #$(program-file "installer-real" installer-builder)