install: Provide a meaningful label.
[jackhill/guix/guix.git] / gnu / system / install.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
5 ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
6 ;;; Copyright © 2017 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
23 (define-module (gnu system install)
24 #:use-module (gnu)
25 #:use-module (gnu system)
26 #:use-module (gnu bootloader u-boot)
27 #:use-module (guix gexp)
28 #:use-module (guix store)
29 #:use-module (guix monads)
30 #:use-module ((guix packages) #:select (package-version))
31 #:use-module ((guix store) #:select (%store-prefix))
32 #:use-module (gnu installer)
33 #:use-module (gnu services dbus)
34 #:use-module (gnu services networking)
35 #:use-module (gnu services shepherd)
36 #:use-module (gnu services ssh)
37 #:use-module (gnu packages admin)
38 #:use-module (gnu packages bash)
39 #:use-module (gnu packages bootloaders)
40 #:use-module (gnu packages certs)
41 #:use-module (gnu packages fonts)
42 #:use-module (gnu packages fontutils)
43 #:use-module (gnu packages guile)
44 #:use-module (gnu packages linux)
45 #:use-module (gnu packages ssh)
46 #:use-module (gnu packages cryptsetup)
47 #:use-module (gnu packages package-management)
48 #:use-module (gnu packages disk)
49 #:use-module (gnu packages texinfo)
50 #:use-module (gnu packages compression)
51 #:use-module (gnu packages nvi)
52 #:use-module (ice-9 match)
53 #:use-module (srfi srfi-26)
54 #:export (installation-os
55 a20-olinuxino-lime-installation-os
56 a20-olinuxino-lime2-emmc-installation-os
57 a20-olinuxino-micro-installation-os
58 bananapi-m2-ultra-installation-os
59 beaglebone-black-installation-os
60 mx6cuboxi-installation-os
61 nintendo-nes-classic-edition-installation-os
62 novena-installation-os
63 pine64-plus-installation-os
64 pinebook-installation-os
65 rk3399-puma-installation-os
66 wandboard-installation-os
67 os-with-u-boot))
68
69 ;;; Commentary:
70 ;;;
71 ;;; This module provides an 'operating-system' definition for use on images
72 ;;; for USB sticks etc., for the installation of the GNU system.
73 ;;;
74 ;;; Code:
75
76 \f
77 ;;;
78 ;;; Documentation service.
79 ;;;
80
81 (define %installation-node-names
82 ;; Translated name of the "System Installation" node of the manual. Ideally
83 ;; we'd extract it from the 'guix-manual' gettext domain, but that one is
84 ;; usually not available at run time, hence this hack.
85 '(("de" . "Systeminstallation")
86 ("en" . "System Installation")
87 ("fr" . "Installation du système")))
88
89 (define (log-to-info tty user)
90 "Return a script that spawns the Info reader on the right section of the
91 manual."
92 (program-file "log-to-info"
93 #~(let* ((tty (open-file #$(string-append "/dev/" tty)
94 "r0+"))
95 (locale (cadr (command-line)))
96 (language (string-take locale
97 (string-index locale #\_)))
98 (infodir "/run/current-system/profile/share/info")
99 (per-lang (string-append infodir "/guix." language
100 ".info.gz"))
101 (file (if (file-exists? per-lang)
102 per-lang
103 (string-append infodir "/guix.info")))
104 (node (or (assoc-ref '#$%installation-node-names
105 language)
106 "System Installation")))
107 (redirect-port tty (current-output-port))
108 (redirect-port tty (current-error-port))
109 (redirect-port tty (current-input-port))
110
111 (let ((pw (getpwnam #$user)))
112 (setgid (passwd:gid pw))
113 (setuid (passwd:uid pw)))
114
115 ;; 'gunzip' is needed to decompress the doc.
116 (setenv "PATH" (string-append #$gzip "/bin"))
117
118 ;; Change this process' locale so that command-line
119 ;; arguments to 'info' are properly encoded.
120 (catch #t
121 (lambda ()
122 (setlocale LC_ALL locale)
123 (setenv "LC_ALL" locale))
124 (lambda _
125 ;; Sometimes LOCALE itself is not available. In that
126 ;; case pick the one UTF-8 locale that's known to work
127 ;; instead of failing.
128 (setlocale LC_ALL "en_US.utf8")
129 (setenv "LC_ALL" "en_US.utf8")))
130
131 (execl #$(file-append info-reader "/bin/info")
132 "info" "-d" infodir "-f" file "-n" node))))
133
134 (define (documentation-shepherd-service tty)
135 (list (shepherd-service
136 (provision (list (symbol-append 'term- (string->symbol tty))))
137 (requirement '(user-processes host-name udev virtual-terminal))
138 (start #~(lambda* (#:optional (locale "en_US.utf8"))
139 (fork+exec-command
140 (list #$(log-to-info tty "documentation") locale)
141 #:environment-variables
142 `("GUIX_LOCPATH=/run/current-system/locale"
143 "TERM=linux"))))
144 (stop #~(make-kill-destructor)))))
145
146 (define %documentation-users
147 ;; User account for the Info viewer.
148 (list (user-account (name "documentation")
149 (system? #t)
150 (group "nogroup")
151 (home-directory "/var/empty"))))
152
153 (define documentation-service-type
154 ;; Documentation viewer service.
155 (service-type (name 'documentation)
156 (extensions
157 (list (service-extension shepherd-root-service-type
158 documentation-shepherd-service)
159 (service-extension account-service-type
160 (const %documentation-users))))
161 (description "Run the Info reader on a tty.")))
162
163 \f
164 (define %backing-directory
165 ;; Sub-directory used as the backing store for copy-on-write.
166 "/tmp/guix-inst")
167
168 (define (make-cow-store target)
169 "Return a gexp that makes the store copy-on-write, using TARGET as the
170 backing store. This is useful when TARGET is on a hard disk, whereas the
171 current store is on a RAM disk."
172
173 (define (set-store-permissions directory)
174 ;; Set the right perms on DIRECTORY to use it as the store.
175 #~(begin
176 (chown #$directory 0 30000) ;use the fixed 'guixbuild' GID
177 (chmod #$directory #o1775)))
178
179 #~(begin
180 ;; Bind-mount TARGET's /tmp in case we need space to build things.
181 (let ((tmpdir (string-append #$target "/tmp")))
182 (mkdir-p tmpdir)
183 (mount tmpdir "/tmp" "none" MS_BIND))
184
185 (let* ((rw-dir (string-append target #$%backing-directory))
186 (work-dir (string-append rw-dir "/../.overlayfs-workdir")))
187 (mkdir-p rw-dir)
188 (mkdir-p work-dir)
189 (mkdir-p "/.rw-store")
190 #$(set-store-permissions #~rw-dir)
191 #$(set-store-permissions "/.rw-store")
192
193 ;; Mount the overlay, then atomically make it the store.
194 (mount "none" "/.rw-store" "overlay" 0
195 (string-append "lowerdir=" #$(%store-prefix) ","
196 "upperdir=" rw-dir ","
197 "workdir=" work-dir))
198 (mount "/.rw-store" #$(%store-prefix) "" MS_MOVE)
199 (rmdir "/.rw-store"))))
200
201 (define cow-store-service-type
202 (shepherd-service-type
203 'cow-store
204 (lambda _
205 (shepherd-service
206 (requirement '(root-file-system user-processes))
207 (provision '(cow-store))
208 (documentation
209 "Make the store copy-on-write, with writes going to \
210 the given target.")
211
212 ;; This is meant to be explicitly started by the user.
213 (auto-start? #f)
214
215 (start #~(case-lambda
216 ((target)
217 #$(make-cow-store #~target)
218 target)
219 (else
220 ;; Do nothing, and mark the service as stopped.
221 #f)))
222 (stop #~(lambda (target)
223 ;; Delete the temporary directory, but leave everything
224 ;; mounted as there may still be processes using it since
225 ;; 'user-processes' doesn't depend on us. The 'user-file-systems'
226 ;; service will unmount TARGET eventually.
227 (delete-file-recursively
228 (string-append target #$%backing-directory))))))))
229
230 (define (cow-store-service)
231 "Return a service that makes the store copy-on-write, such that writes go to
232 the user's target storage device rather than on the RAM disk."
233 ;; See <http://bugs.gnu.org/18061> for the initial report.
234 (service cow-store-service-type 'mooooh!))
235
236
237 (define (/etc/configuration-files _)
238 "Return a list of tuples representing configuration templates to add to
239 /etc."
240 (define (file f)
241 (local-file (string-append "examples/" f)))
242
243 (define directory
244 (computed-file "configuration-templates"
245 (with-imported-modules '((guix build utils))
246 #~(begin
247 (mkdir #$output)
248 (for-each (lambda (file target)
249 (copy-file file
250 (string-append #$output "/"
251 target)))
252 '(#$(file "bare-bones.tmpl")
253 #$(file "beaglebone-black.tmpl")
254 #$(file "desktop.tmpl")
255 #$(file "lightweight-desktop.tmpl"))
256 '("bare-bones.scm"
257 "beaglebone-black.scm"
258 "desktop.scm"
259 "lightweight-desktop.scm"))
260 #t))))
261
262 `(("configuration" ,directory)))
263
264 (define configuration-template-service-type
265 (service-type (name 'configuration-template)
266 (extensions
267 (list (service-extension etc-service-type
268 /etc/configuration-files)))))
269
270 (define %configuration-template-service
271 (service configuration-template-service-type #t))
272
273
274 (define %nscd-minimal-caches
275 ;; Minimal in-memory caching policy for nscd.
276 (list (nscd-cache (database 'hosts)
277 (positive-time-to-live (* 3600 12))
278
279 ;; Do not cache lookup failures at all since they are
280 ;; quite likely (for instance when someone tries to ping a
281 ;; host before networking is functional.)
282 (negative-time-to-live 0)
283
284 (persistent? #f)
285 (max-database-size (* 5 (expt 2 20)))))) ;5 MiB
286
287 (define %installation-services
288 ;; List of services of the installation system.
289 (let ((motd (plain-file "motd" "
290 \x1b[1;37mWelcome to the installation of GNU Guix!\x1b[0m
291
292 \x1b[2m\
293 Using this shell, you can carry out the installation process \"manually.\"
294 Access documentation at any time by pressing Alt-F2.\x1b[0m
295 ")))
296 (define (normal-tty tty)
297 (mingetty-service (mingetty-configuration (tty tty)
298 (auto-login "root")
299 (login-pause? #t))))
300
301 (define bare-bones-os
302 (load "examples/bare-bones.tmpl"))
303
304 (list (service virtual-terminal-service-type)
305
306 (service kmscon-service-type
307 (kmscon-configuration
308 (virtual-terminal "tty1")
309 (login-program (installer-program))))
310
311 (login-service (login-configuration
312 (motd motd)))
313
314 ;; Documentation. The manual is in UTF-8, but
315 ;; 'console-font-service' sets up Unicode support and loads a font
316 ;; with all the useful glyphs like em dash and quotation marks.
317 (service documentation-service-type "tty2")
318
319 ;; Documentation add-on.
320 %configuration-template-service
321
322 ;; A bunch of 'root' ttys.
323 (normal-tty "tty3")
324 (normal-tty "tty4")
325 (normal-tty "tty5")
326 (normal-tty "tty6")
327
328 ;; The usual services.
329 (syslog-service)
330
331 ;; The build daemon. Register the hydra.gnu.org key as trusted.
332 ;; This allows the installation process to use substitutes by
333 ;; default.
334 (service guix-service-type
335 (guix-configuration (authorize-key? #t)))
336
337 ;; Start udev so that useful device nodes are available.
338 ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for
339 ;; regulations-compliant WiFi access.
340 (udev-service #:rules (list lvm2 crda))
341
342 ;; Add the 'cow-store' service, which users have to start manually
343 ;; since it takes the installation directory as an argument.
344 (cow-store-service)
345
346 ;; Install Unicode support and a suitable font.
347 (service console-font-service-type
348 (map (match-lambda
349 ("tty2"
350 ;; Use a font that contains characters such as
351 ;; curly quotes as found in the manual.
352 '("tty2" . "LatGrkCyr-8x16"))
353 (tty
354 ;; Use a font that doesn't have more than 256
355 ;; glyphs so that we can use colors with varying
356 ;; brightness levels (see note in setfont(8)).
357 `(,tty . "lat9u-16")))
358 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
359
360 ;; To facilitate copy/paste.
361 (service gpm-service-type)
362
363 ;; Add an SSH server to facilitate remote installs.
364 (service openssh-service-type
365 (openssh-configuration
366 (port-number 22)
367 (permit-root-login #t)
368 ;; The root account is passwordless, so make sure
369 ;; a password is set before allowing logins.
370 (allow-empty-passwords? #f)
371 (password-authentication? #t)
372
373 ;; Don't start it upfront.
374 (%auto-start? #f)))
375
376 ;; Since this is running on a USB stick with a overlayfs as the root
377 ;; file system, use an appropriate cache configuration.
378 (nscd-service (nscd-configuration
379 (caches %nscd-minimal-caches)))
380
381 ;; Having /bin/sh is a good idea. In particular it allows Tramp
382 ;; connections to this system to work.
383 (service special-files-service-type
384 `(("/bin/sh" ,(file-append (canonical-package bash)
385 "/bin/sh"))))
386
387 ;; Loopback device, needed by OpenSSH notably.
388 (service static-networking-service-type
389 (list (static-networking (interface "lo")
390 (ip "127.0.0.1")
391 (requirement '())
392 (provision '(loopback)))))
393
394 (service wpa-supplicant-service-type)
395 (dbus-service)
396 (service connman-service-type
397 (connman-configuration
398 (disable-vpn? #t)))
399
400 ;; Keep a reference to BARE-BONES-OS to make sure it can be
401 ;; installed without downloading/building anything. Also keep the
402 ;; things needed by 'profile-derivation' to minimize the amount of
403 ;; download.
404 (service gc-root-service-type
405 (list bare-bones-os
406 glibc-utf8-locales
407 texinfo
408 (canonical-package guile-2.2))))))
409
410 (define %issue
411 ;; Greeting.
412 "
413 \x1b[1;37mThis is an installation image of the GNU system. Welcome.\x1b[0m
414
415 \x1b[1;33mUse Alt-F2 for documentation.\x1b[0m
416 ")
417
418 (define installation-os
419 ;; The operating system used on installation images for USB sticks etc.
420 (operating-system
421 (host-name "gnu")
422 (timezone "Europe/Paris")
423 (locale "en_US.utf8")
424 (bootloader (bootloader-configuration
425 (bootloader grub-bootloader)
426 (target "/dev/sda")))
427 (label (string-append "GNU Guix installation "
428 (package-version guix)))
429
430 (file-systems
431 ;; Note: the disk image build code overrides this root file system with
432 ;; the appropriate one.
433 (cons* (file-system
434 (mount-point "/")
435 (device (file-system-label "Guix_image"))
436 (type "ext4"))
437
438 ;; Make /tmp a tmpfs instead of keeping the overlayfs. This
439 ;; originally was used for unionfs because FUSE creates
440 ;; '.fuse_hiddenXYZ' files for each open file, and this confuses
441 ;; Guix's test suite, for instance (see
442 ;; <http://bugs.gnu.org/23056>). We keep this for overlayfs to be
443 ;; on the safe side.
444 (file-system
445 (mount-point "/tmp")
446 (device "none")
447 (type "tmpfs")
448 (check? #f))
449
450 ;; XXX: This should be %BASE-FILE-SYSTEMS but we don't need
451 ;; elogind's cgroup file systems.
452 (list %pseudo-terminal-file-system
453 %shared-memory-file-system
454 %immutable-store)))
455
456 (users (list (user-account
457 (name "guest")
458 (group "users")
459 (supplementary-groups '("wheel")) ; allow use of sudo
460 (password "")
461 (comment "Guest of GNU"))))
462
463 (issue %issue)
464 (services %installation-services)
465
466 ;; We don't need setuid programs, except for 'passwd', which can be handy
467 ;; if one is to allow remote SSH login to the machine being installed.
468 (setuid-programs (list (file-append shadow "/bin/passwd")))
469
470 (pam-services
471 ;; Explicitly allow for empty passwords.
472 (base-pam-services #:allow-empty-passwords? #t))
473
474 (packages (cons* (canonical-package glibc) ;for 'tzselect' & co.
475 parted gptfdisk ddrescue
476 fontconfig
477 font-dejavu font-gnu-unifont
478 grub ;mostly so xrefs to its manual work
479 cryptsetup
480 mdadm
481 dosfstools ;mkfs.fat, for the UEFI boot partition
482 btrfs-progs
483 openssh ;we already have sshd, having ssh/scp can help
484 wireless-tools iw wpa-supplicant-minimal iproute
485 ;; XXX: We used to have GNU fdisk here, but as of version
486 ;; 2.0.0a, that pulls Guile 1.8, which takes unreasonable
487 ;; space; furthermore util-linux's fdisk is already
488 ;; available here, so we keep that.
489 bash-completion
490 nvi ;:wq!
491 nss-certs ; To access HTTPS, use git, etc.
492 %base-packages))))
493
494 (define* (os-with-u-boot os board #:key (bootloader-target "/dev/mmcblk0")
495 (triplet "arm-linux-gnueabihf"))
496 "Given OS, amend it with the u-boot bootloader for BOARD,
497 installed to BOOTLOADER-TARGET (a drive), compiled for TRIPLET.
498
499 If you want a serial console, make sure to specify one in your
500 operating-system's kernel-arguments (\"console=ttyS0\" or similar)."
501 (operating-system (inherit os)
502 (bootloader (bootloader-configuration
503 (bootloader (bootloader (inherit u-boot-bootloader)
504 (package (make-u-boot-package board triplet))))
505 (target bootloader-target)))))
506
507 (define* (embedded-installation-os bootloader bootloader-target tty
508 #:key (extra-modules '()))
509 "Return an installation os for embedded systems.
510 The initrd gets the extra modules EXTRA-MODULES.
511 A getty is provided on TTY.
512 The bootloader BOOTLOADER is installed to BOOTLOADER-TARGET."
513 (operating-system
514 (inherit installation-os)
515 (bootloader (bootloader-configuration
516 (bootloader bootloader)
517 (target bootloader-target)))
518 (kernel linux-libre)
519 (kernel-arguments
520 (cons (string-append "console=" tty)
521 (operating-system-user-kernel-arguments installation-os)))
522 (initrd-modules (append extra-modules %base-initrd-modules))))
523
524 (define beaglebone-black-installation-os
525 (embedded-installation-os u-boot-beaglebone-black-bootloader
526 "/dev/sda"
527 "ttyO0"
528 #:extra-modules
529 ;; This module is required to mount the sd card.
530 '("omap_hsmmc")))
531
532
533 (define a20-olinuxino-lime-installation-os
534 (embedded-installation-os u-boot-a20-olinuxino-lime-bootloader
535 "/dev/mmcblk0" ; SD card storage
536 "ttyS0"))
537
538 (define a20-olinuxino-lime2-emmc-installation-os
539 (embedded-installation-os u-boot-a20-olinuxino-lime2-bootloader
540 "/dev/mmcblk1" ; eMMC storage
541 "ttyS0"))
542
543 (define a20-olinuxino-micro-installation-os
544 (embedded-installation-os u-boot-a20-olinuxino-micro-bootloader
545 "/dev/mmcblk0" ; SD card storage
546 "ttyS0"))
547
548 (define bananapi-m2-ultra-installation-os
549 (embedded-installation-os u-boot-bananapi-m2-ultra-bootloader
550 "/dev/mmcblk1" ; eMMC storage
551 "ttyS0"))
552
553 (define mx6cuboxi-installation-os
554 (embedded-installation-os u-boot-mx6cuboxi-bootloader
555 "/dev/mmcblk0" ; SD card storage
556 "ttymxc0"))
557
558 (define novena-installation-os
559 (embedded-installation-os u-boot-novena-bootloader
560 "/dev/mmcblk1" ; SD card storage
561 "ttymxc1"))
562
563 (define nintendo-nes-classic-edition-installation-os
564 (embedded-installation-os u-boot-nintendo-nes-classic-edition-bootloader
565 "/dev/mmcblk0" ; SD card (solder it yourself)
566 "ttyS0"))
567
568 (define pine64-plus-installation-os
569 (embedded-installation-os u-boot-pine64-plus-bootloader
570 "/dev/mmcblk0" ; SD card storage
571 "ttyS0"))
572
573 (define pinebook-installation-os
574 (embedded-installation-os u-boot-pinebook-bootloader
575 "/dev/mmcblk0" ; SD card storage
576 "ttyS0"))
577
578 (define rk3399-puma-installation-os
579 (embedded-installation-os u-boot-puma-rk3399-bootloader
580 "/dev/mmcblk0" ; SD card storage
581 "ttyS0"))
582
583 (define wandboard-installation-os
584 (embedded-installation-os u-boot-wandboard-bootloader
585 "/dev/mmcblk0" ; SD card storage
586 "ttymxc0"))
587
588 ;; Return the default os here so 'guix system' can consume it directly.
589 installation-os
590
591 ;;; install.scm ends here