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