system: Factor out agetty-default-service.
[jackhill/guix/guix.git] / gnu / system / install.scm
CommitLineData
fc91c17a 1;;; GNU Guix --- Functional package management for GNU
239c2266 2;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
b2a5fa59 3;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
f0fbf2c1 4;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
c80cd4df 5;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
fdfdecdb 6;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
fc91c17a
LC
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)
ceb39527 25 #:use-module (gnu bootloader u-boot)
fc91c17a 26 #:use-module (guix gexp)
e87f0591 27 #:use-module (guix store)
fc91c17a 28 #:use-module (guix monads)
83a17b62 29 #:use-module ((guix store) #:select (%store-prefix))
0190c1c0 30 #:use-module (gnu services shepherd)
c80cd4df 31 #:use-module (gnu services ssh)
db84467a 32 #:use-module (gnu packages admin)
f4bdfe73 33 #:use-module (gnu packages bash)
862e38d5 34 #:use-module (gnu packages bootloaders)
af4a761e 35 #:use-module (gnu packages guile)
fc91c17a 36 #:use-module (gnu packages linux)
1e8d398a 37 #:use-module (gnu packages ssh)
b419c7f5 38 #:use-module (gnu packages cryptsetup)
fc91c17a 39 #:use-module (gnu packages package-management)
cc4a2aeb 40 #:use-module (gnu packages disk)
fc91c17a 41 #:use-module (gnu packages texinfo)
dd6b28d1 42 #:use-module (gnu packages compression)
a8cb87ab 43 #:use-module (gnu packages nvi)
e1fbc32a
LC
44 #:use-module (ice-9 match)
45 #:use-module (srfi srfi-26)
ceb39527 46 #:export (installation-os
4b9e9abb 47 a20-olinuxino-lime2-emmc-installation-os
a7bb327e 48 a20-olinuxino-micro-installation-os
30aeb846 49 banana-pi-m2-ultra-installation-os
ceb39527 50 beaglebone-black-installation-os))
fc91c17a
LC
51
52;;; Commentary:
53;;;
54;;; This module provides an 'operating-system' definition for use on images
55;;; for USB sticks etc., for the installation of the GNU system.
56;;;
57;;; Code:
58
9d3fb6c7 59\f
fc91c17a
LC
60(define (log-to-info)
61 "Return a script that spawns the Info reader on the right section of the
62manual."
ce8a6dfc 63 (program-file "log-to-info"
dd6b28d1
LC
64 #~(begin
65 ;; 'gunzip' is needed to decompress the doc.
66 (setenv "PATH" (string-append #$gzip "/bin"))
67
68abb9b0 68 (execl (string-append #$info-reader "/bin/info") "info"
dd6b28d1
LC
69 "-d" "/run/current-system/profile/share/info"
70 "-f" (string-append #$guix "/share/info/guix.info")
71 "-n" "System Installation"))))
fc91c17a 72
83a17b62
LC
73(define %backing-directory
74 ;; Sub-directory used as the backing store for copy-on-write.
75 "/tmp/guix-inst")
76
77(define (make-cow-store target)
78 "Return a gexp that makes the store copy-on-write, using TARGET as the
79backing store. This is useful when TARGET is on a hard disk, whereas the
80current store is on a RAM disk."
83a17b62
LC
81
82 (define (set-store-permissions directory)
83 ;; Set the right perms on DIRECTORY to use it as the store.
84 #~(begin
85 (chown #$directory 0 30000) ;use the fixed 'guixbuild' GID
86 (chmod #$directory #o1775)))
87
88 #~(begin
0adabad7
LC
89 ;; Bind-mount TARGET's /tmp in case we need space to build things.
90 (let ((tmpdir (string-append #$target "/tmp")))
91 (mkdir-p tmpdir)
92 (mount tmpdir "/tmp" "none" MS_BIND))
93
d9565f7d
HG
94 (let* ((rw-dir (string-append target #$%backing-directory))
95 (work-dir (string-append rw-dir "/../.overlayfs-workdir")))
83a17b62 96 (mkdir-p rw-dir)
d9565f7d 97 (mkdir-p work-dir)
83a17b62
LC
98 (mkdir-p "/.rw-store")
99 #$(set-store-permissions #~rw-dir)
100 #$(set-store-permissions "/.rw-store")
101
d9565f7d
HG
102 ;; Mount the overlay, then atomically make it the store.
103 (mount "none" "/.rw-store" "overlay" 0
104 (string-append "lowerdir=" #$(%store-prefix) ","
105 "upperdir=" rw-dir ","
106 "workdir=" work-dir))
107 (mount "/.rw-store" #$(%store-prefix) "" MS_MOVE)
108 (rmdir "/.rw-store"))))
83a17b62 109
0adfe95a 110(define cow-store-service-type
d4053c71 111 (shepherd-service-type
00184239 112 'cow-store
0adfe95a 113 (lambda _
d4053c71 114 (shepherd-service
0adfe95a
LC
115 (requirement '(root-file-system user-processes))
116 (provision '(cow-store))
117 (documentation
118 "Make the store copy-on-write, with writes going to \
119the given target.")
120
121 ;; This is meant to be explicitly started by the user.
122 (auto-start? #f)
123
124 (start #~(case-lambda
125 ((target)
126 #$(make-cow-store #~target)
127 target)
128 (else
129 ;; Do nothing, and mark the service as stopped.
130 #f)))
131 (stop #~(lambda (target)
132 ;; Delete the temporary directory, but leave everything
133 ;; mounted as there may still be processes using it since
134 ;; 'user-processes' doesn't depend on us. The 'user-unmount'
135 ;; service will unmount TARGET eventually.
136 (delete-file-recursively
137 (string-append target #$%backing-directory))))))))
138
83a17b62
LC
139(define (cow-store-service)
140 "Return a service that makes the store copy-on-write, such that writes go to
141the user's target storage device rather than on the RAM disk."
142 ;; See <http://bugs.gnu.org/18061> for the initial report.
0adfe95a
LC
143 (service cow-store-service-type 'mooooh!))
144
145
146(define (/etc/configuration-files _)
147 "Return a list of tuples representing configuration templates to add to
148/etc."
149 (define (file f)
bae90dc7 150 (local-file (string-append "examples/" f)))
0adfe95a
LC
151
152 (define directory
153 (computed-file "configuration-templates"
4ee96a79
LC
154 (with-imported-modules '((guix build utils))
155 #~(begin
156 (mkdir #$output)
157 (for-each (lambda (file target)
158 (copy-file file
159 (string-append #$output "/"
160 target)))
161 '(#$(file "bare-bones.tmpl")
9f1e39d1 162 #$(file "beaglebone-black.tmpl")
4ee96a79
LC
163 #$(file "desktop.tmpl")
164 #$(file "lightweight-desktop.tmpl"))
165 '("bare-bones.scm"
9f1e39d1 166 "beaglebone-black.scm"
4ee96a79
LC
167 "desktop.scm"
168 "lightweight-desktop.scm"))
169 #t))))
0adfe95a
LC
170
171 `(("configuration" ,directory)))
172
173(define configuration-template-service-type
174 (service-type (name 'configuration-template)
175 (extensions
176 (list (service-extension etc-service-type
177 /etc/configuration-files)))))
178
179(define %configuration-template-service
180 (service configuration-template-service-type #t))
be1c2c54 181
1dac8566 182
61ff0a3a
LC
183(define %nscd-minimal-caches
184 ;; Minimal in-memory caching policy for nscd.
185 (list (nscd-cache (database 'hosts)
186 (positive-time-to-live (* 3600 12))
c96ba2cf
LC
187
188 ;; Do not cache lookup failures at all since they are
189 ;; quite likely (for instance when someone tries to ping a
190 ;; host before networking is functional.)
191 (negative-time-to-live 0)
192
61ff0a3a
LC
193 (persistent? #f)
194 (max-database-size (* 5 (expt 2 20)))))) ;5 MiB
195
58b21e1e
LC
196(define %installation-services
197 ;; List of services of the installation system.
ce8a6dfc 198 (let ((motd (plain-file "motd" "
8638362f 199\x1b[1;37mWelcome to the installation of the Guix System Distribution!\x1b[0m
fc91c17a 200
8638362f 201\x1b[2mThere is NO WARRANTY, to the extent permitted by law. In particular, you may
fc91c17a 202LOSE ALL YOUR DATA as a side effect of the installation process. Furthermore,
c82c060d 203it is 'beta' software, so it may contain bugs.
fc91c17a 204
8638362f 205You have been warned. Thanks for being so brave.\x1b[0m
fc91c17a
LC
206")))
207 (define (normal-tty tty)
66e4f01c 208 (mingetty-service (mingetty-configuration (tty tty)
66e4f01c
LC
209 (auto-login "root")
210 (login-pause? #t))))
fc91c17a 211
4e854b18
LC
212 (define bare-bones-os
213 (load "examples/bare-bones.tmpl"))
214
66e4f01c
LC
215 (list (mingetty-service (mingetty-configuration
216 (tty "tty1")
66e4f01c 217 (auto-login "root")))
fc91c17a 218
2932ab9c
DC
219 (login-service (login-configuration
220 (motd motd)))
221
62ca0fdf
LC
222 ;; Documentation. The manual is in UTF-8, but
223 ;; 'console-font-service' sets up Unicode support and loads a font
224 ;; with all the useful glyphs like em dash and quotation marks.
66e4f01c
LC
225 (mingetty-service (mingetty-configuration
226 (tty "tty2")
66e4f01c
LC
227 (auto-login "guest")
228 (login-program (log-to-info))))
fc91c17a 229
1dac8566 230 ;; Documentation add-on.
0adfe95a 231 %configuration-template-service
1dac8566 232
fc91c17a
LC
233 ;; A bunch of 'root' ttys.
234 (normal-tty "tty3")
235 (normal-tty "tty4")
236 (normal-tty "tty5")
237 (normal-tty "tty6")
238
239 ;; The usual services.
240 (syslog-service)
2c5c696c
LC
241
242 ;; The build daemon. Register the hydra.gnu.org key as trusted.
243 ;; This allows the installation process to use substitutes by
244 ;; default.
0adfe95a 245 (guix-service (guix-configuration (authorize-key? #t)))
2c5c696c 246
e11390df 247 ;; Start udev so that useful device nodes are available.
68ac258b
LC
248 ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for
249 ;; regulations-compliant WiFi access.
250 (udev-service #:rules (list lvm2 crda))
e11390df 251
83a17b62
LC
252 ;; Add the 'cow-store' service, which users have to start manually
253 ;; since it takes the installation directory as an argument.
254 (cow-store-service)
255
8638362f
LC
256 ;; Install Unicode support and a suitable font. Use a font that
257 ;; doesn't have more than 256 glyphs so that we can use colors with
258 ;; varying brightness levels (see note in setfont(8)).
4a84a487
LC
259 (service console-font-service-type
260 (map (lambda (tty)
8638362f 261 (cons tty "lat9u-16"))
4a84a487 262 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
62ca0fdf 263
ae7ffa9e
LC
264 ;; To facilitate copy/paste.
265 (gpm-service)
266
c80cd4df
MB
267 ;; Add an SSH server to facilitate remote installs.
268 (service openssh-service-type
269 (openssh-configuration
270 (port-number 22)
271 (permit-root-login #t)
272 ;; The root account is passwordless, so make sure
273 ;; a password is set before allowing logins.
274 (allow-empty-passwords? #f)
aab322d9
LC
275 (password-authentication? #t)
276
277 ;; Don't start it upfront.
278 (%auto-start? #f)))
c80cd4df 279
d9565f7d 280 ;; Since this is running on a USB stick with a overlayfs as the root
61ff0a3a
LC
281 ;; file system, use an appropriate cache configuration.
282 (nscd-service (nscd-configuration
50cb948f
LC
283 (caches %nscd-minimal-caches)))
284
285 ;; Having /bin/sh is a good idea. In particular it allows Tramp
286 ;; connections to this system to work.
287 (service special-files-service-type
288 `(("/bin/sh" ,(file-append (canonical-package bash)
4e854b18
LC
289 "/bin/sh"))))
290
291 ;; Keep a reference to BARE-BONES-OS to make sure it can be
af4a761e
LC
292 ;; installed without downloading/building anything. Also keep the
293 ;; things needed by 'profile-derivation' to minimize the amount of
294 ;; download.
295 (service gc-root-service-type
296 (list bare-bones-os
297 glibc-utf8-locales
298 texinfo
299 (canonical-package guile-2.2))))))
fc91c17a
LC
300
301(define %issue
302 ;; Greeting.
303 "
8638362f 304\x1b[1;37mThis is an installation image of the GNU system. Welcome.\x1b[0m
fc91c17a 305
8638362f 306\x1b[1;33mUse Alt-F2 for documentation.\x1b[0m
fc91c17a
LC
307")
308
309(define installation-os
310 ;; The operating system used on installation images for USB sticks etc.
311 (operating-system
312 (host-name "gnu")
313 (timezone "Europe/Paris")
9cd0dfaa 314 (locale "en_US.utf8")
fdfdecdb
TGR
315 (bootloader (bootloader-configuration
316 (bootloader grub-bootloader)
317 (target "/dev/sda")))
fc91c17a
LC
318 (file-systems
319 ;; Note: the disk image build code overrides this root file system with
320 ;; the appropriate one.
ee03b75d
LC
321 (cons* (file-system
322 (mount-point "/")
0862b954 323 (device "GuixSD_image")
ee03b75d
LC
324 (title 'label)
325 (type "ext4"))
326
d9565f7d
HG
327 ;; Make /tmp a tmpfs instead of keeping the overlayfs. This
328 ;; originally was used for unionfs because FUSE creates
329 ;; '.fuse_hiddenXYZ' files for each open file, and this confuses
330 ;; Guix's test suite, for instance (see
331 ;; <http://bugs.gnu.org/23056>). We keep this for overlayfs to be
795ec760 332 ;; on the safe side.
ee03b75d
LC
333 (file-system
334 (mount-point "/tmp")
335 (device "none")
336 (title 'device)
337 (type "tmpfs")
338 (check? #f))
339
0feefb53
LC
340 ;; XXX: This should be %BASE-FILE-SYSTEMS but we don't need
341 ;; elogind's cgroup file systems.
342 (list %pseudo-terminal-file-system
343 %shared-memory-file-system
344 %immutable-store)))
fc91c17a
LC
345
346 (users (list (user-account
347 (name "guest")
72507e23 348 (group "users")
ee03b75d 349 (supplementary-groups '("wheel")) ; allow use of sudo
fc91c17a
LC
350 (password "")
351 (comment "Guest of GNU")
352 (home-directory "/home/guest"))))
fc91c17a
LC
353
354 (issue %issue)
58b21e1e 355 (services %installation-services)
fc91c17a 356
903ae630
LC
357 ;; We don't need setuid programs, except for 'passwd', which can be handy
358 ;; if one is to allow remote SSH login to the machine being installed.
359 (setuid-programs (list (file-append shadow "/bin/passwd")))
fc91c17a
LC
360
361 (pam-services
362 ;; Explicitly allow for empty passwords.
363 (base-pam-services #:allow-empty-passwords? #t))
364
a6312f1d 365 (packages (cons* (canonical-package glibc) ;for 'tzselect' & co.
72524ae8 366 parted gptfdisk ddrescue
7eda0c56 367 grub ;mostly so xrefs to its manual work
b419c7f5 368 cryptsetup
f0fbf2c1 369 mdadm
f731529e 370 dosfstools ;mkfs.fat, for the UEFI boot partition
3a9cfba8 371 btrfs-progs
1e8d398a 372 openssh ;we already have sshd, having ssh/scp can help
1ce6f43a 373 wireless-tools iw wpa-supplicant-minimal iproute
8f297d42
LC
374 ;; XXX: We used to have GNU fdisk here, but as of version
375 ;; 2.0.0a, that pulls Guile 1.8, which takes unreasonable
376 ;; space; furthermore util-linux's fdisk is already
377 ;; available here, so we keep that.
f4bdfe73 378 bash-completion
a8cb87ab 379 nvi ;:wq!
6f436c54 380 %base-packages))))
fc91c17a 381
8bac66de
DM
382(define* (agetty-default-service #:optional (tty "ttyS0"))
383 "Return an agetty-service on the given TTY"
384 (agetty-service (agetty-configuration
385 (extra-options '("-L"))
386 (baud-rate "115200")
387 (term "vt100")
388 (tty tty))))
389
ceb39527
MO
390(define beaglebone-black-installation-os
391 (operating-system
392 (inherit installation-os)
393 (bootloader (bootloader-configuration
394 (bootloader u-boot-beaglebone-black-bootloader)
395 (target "/dev/sda")))
396 (kernel linux-libre)
397 (initrd (lambda (fs . rest)
398 (apply base-initrd fs
399 ;; This module is required to mount the sd card.
400 #:extra-modules (list "omap_hsmmc")
401 rest)))
8bac66de
DM
402 (services (cons* (agetty-default-service "ttyO0")
403 (operating-system-user-services installation-os)))))
ceb39527 404
4b9e9abb
DM
405(define a20-olinuxino-lime2-emmc-installation-os
406 (operating-system
407 (inherit installation-os)
408 (bootloader (bootloader-configuration
409 (bootloader u-boot-a20-olinuxino-lime2-bootloader)
410 (target "/dev/mmcblk1"))) ; eMMC storage
411 (kernel linux-libre)
8bac66de
DM
412 (services (cons* (agetty-default-service "ttyS0")
413 (operating-system-user-services installation-os)))))
a7bb327e
DM
414
415(define a20-olinuxino-micro-installation-os
416 (operating-system
417 (inherit installation-os)
418 (bootloader (bootloader-configuration
419 (bootloader u-boot-a20-olinuxino-lime2-bootloader)
420 (target "/dev/mmcblk0"))) ; SD card storage
421 (kernel linux-libre)
8bac66de
DM
422 (services (cons* (agetty-default-service "ttyS0")
423 (operating-system-user-services installation-os)))))
4b9e9abb 424
30aeb846
DM
425(define banana-pi-m2-ultra-installation-os
426 (operating-system
427 (inherit installation-os)
428 (bootloader (bootloader-configuration
429 (bootloader u-boot-banana-pi-m2-ultra-bootloader)
430 (target "/dev/mmcblk1"))) ; eMMC storage
431 (kernel linux-libre)
8bac66de
DM
432 (services (cons* (agetty-default-service "ttyS0")
433 (operating-system-user-services installation-os)))))
30aeb846 434
ceb39527 435;; Return the default os here so 'guix system' can consume it directly.
fc91c17a
LC
436installation-os
437
438;;; install.scm ends here