gexp: Add 'mixed-text-file'.
[jackhill/guix/guix.git] / gnu / system / install.scm
CommitLineData
fc91c17a 1;;; GNU Guix --- Functional package management for GNU
e87f0591 2;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
b2a5fa59 3;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
fc91c17a
LC
4;;;
5;;; This file is part of GNU Guix.
6;;;
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.
11;;;
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.
16;;;
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/>.
19
20(define-module (gnu system install)
21 #:use-module (gnu)
22 #:use-module (guix gexp)
e87f0591 23 #:use-module (guix store)
fc91c17a 24 #:use-module (guix monads)
83a17b62 25 #:use-module ((guix store) #:select (%store-prefix))
9d3fb6c7 26 #:use-module (guix profiles)
db84467a 27 #:use-module (gnu packages admin)
f4bdfe73 28 #:use-module (gnu packages bash)
fc91c17a 29 #:use-module (gnu packages linux)
b419c7f5 30 #:use-module (gnu packages cryptsetup)
fc91c17a 31 #:use-module (gnu packages package-management)
cc4a2aeb 32 #:use-module (gnu packages disk)
7eda0c56 33 #:use-module (gnu packages grub)
fc91c17a 34 #:use-module (gnu packages texinfo)
dd6b28d1 35 #:use-module (gnu packages compression)
e1fbc32a
LC
36 #:use-module (ice-9 match)
37 #:use-module (srfi srfi-26)
9d3fb6c7
LC
38 #:export (self-contained-tarball
39 installation-os))
fc91c17a
LC
40
41;;; Commentary:
42;;;
43;;; This module provides an 'operating-system' definition for use on images
44;;; for USB sticks etc., for the installation of the GNU system.
45;;;
46;;; Code:
47
9d3fb6c7
LC
48\f
49(define* (self-contained-tarball #:key (guix guix))
50 "Return a self-contained tarball containing a store initialized with the
51closure of GUIX. The tarball contains /gnu/store, /var/guix, and a profile
52under /root/.guix-profile where GUIX is installed."
53 (mlet %store-monad ((profile (profile-derivation
54 (manifest
55 (list (package->manifest-entry guix))))))
56 (define build
57 #~(begin
58 (use-modules (guix build utils)
59 (gnu build install))
60
61 (define %root "root")
62
63 (setenv "PATH"
64 (string-append #$guix "/sbin:" #$tar "/bin:" #$xz "/bin"))
65
08fa7613
LC
66 ;; Note: there is not much to gain here with deduplication and there
67 ;; is the overhead of the '.links' directory, so turn it off.
9d3fb6c7
LC
68 (populate-single-profile-directory %root
69 #:profile #$profile
08fa7613
LC
70 #:closure "profile"
71 #:deduplicate? #f)
9d3fb6c7
LC
72
73 ;; Create the tarball. Use GNU format so there's no file name
01dbc7e0 74 ;; length limitation.
9d3fb6c7
LC
75 (with-directory-excursion %root
76 (zero? (system* "tar" "--xz" "--format=gnu"
92226a47
MW
77
78 ;; avoid non-determinism in the archive
79 "--sort=name"
6c92551a 80 "--mtime=@0" ;for files in /var/guix
92226a47
MW
81 "--owner=root:0"
82 "--group=root:0"
83
08fa7613 84 "--check-links"
b2a5fa59 85 "-cvf" #$output
7acd3439 86 ;; Avoid adding / and /var to the tarball,
b2a5fa59
MW
87 ;; so that the ownership and permissions of those
88 ;; directories will not be overwritten when
7acd3439
LC
89 ;; extracting the archive. Do not include /root
90 ;; because the root account might have a different
91 ;; home directory.
b2a5fa59 92 "./var/guix"
781d0a2c 93 (string-append "." (%store-directory)))))))
9d3fb6c7
LC
94
95 (gexp->derivation "guix-tarball.tar.xz" build
96 #:references-graphs `(("profile" ,profile))
97 #:modules '((guix build utils)
98 (guix build store-copy)
99 (gnu build install)))))
100
101\f
fc91c17a
LC
102(define (log-to-info)
103 "Return a script that spawns the Info reader on the right section of the
104manual."
105 (gexp->script "log-to-info"
dd6b28d1
LC
106 #~(begin
107 ;; 'gunzip' is needed to decompress the doc.
108 (setenv "PATH" (string-append #$gzip "/bin"))
109
110 (execl (string-append #$texinfo-4 "/bin/info") "info"
111 "-d" "/run/current-system/profile/share/info"
112 "-f" (string-append #$guix "/share/info/guix.info")
113 "-n" "System Installation"))))
fc91c17a 114
83a17b62
LC
115(define %backing-directory
116 ;; Sub-directory used as the backing store for copy-on-write.
117 "/tmp/guix-inst")
118
119(define (make-cow-store target)
120 "Return a gexp that makes the store copy-on-write, using TARGET as the
121backing store. This is useful when TARGET is on a hard disk, whereas the
122current store is on a RAM disk."
123 (define (unionfs read-only read-write mount-point)
124 ;; Make MOUNT-POINT the union of READ-ONLY and READ-WRITE.
125
126 ;; Note: in the command below, READ-WRITE appears before READ-ONLY so that
127 ;; it is considered a "higher-level branch", as per unionfs-fuse(8),
128 ;; thereby allowing files existing on READ-ONLY to be copied over to
129 ;; READ-WRITE.
130 #~(fork+exec-command
131 (list (string-append #$unionfs-fuse "/bin/unionfs")
132 "-o"
133 "cow,allow_other,use_ino,max_files=65536,nonempty"
134 (string-append #$read-write "=RW:" #$read-only "=RO")
135 #$mount-point)))
136
137 (define (set-store-permissions directory)
138 ;; Set the right perms on DIRECTORY to use it as the store.
139 #~(begin
140 (chown #$directory 0 30000) ;use the fixed 'guixbuild' GID
141 (chmod #$directory #o1775)))
142
143 #~(begin
144 (unless (file-exists? "/.ro-store")
145 (mkdir "/.ro-store")
146 (mount #$(%store-prefix) "/.ro-store" "none"
147 (logior MS_BIND MS_RDONLY)))
148
149 (let ((rw-dir (string-append target #$%backing-directory)))
150 (mkdir-p rw-dir)
151 (mkdir-p "/.rw-store")
152 #$(set-store-permissions #~rw-dir)
153 #$(set-store-permissions "/.rw-store")
154
155 ;; Mount the union, then atomically make it the store.
156 (and #$(unionfs "/.ro-store" #~rw-dir "/.rw-store")
157 (begin
158 (sleep 1) ;XXX: wait for unionfs to be ready
159 (mount "/.rw-store" #$(%store-prefix) "" MS_MOVE)
160 (rmdir "/.rw-store"))))))
161
162(define (cow-store-service)
163 "Return a service that makes the store copy-on-write, such that writes go to
164the user's target storage device rather than on the RAM disk."
165 ;; See <http://bugs.gnu.org/18061> for the initial report.
166 (with-monad %store-monad
167 (return (service
168 (requirement '(root-file-system user-processes))
169 (provision '(cow-store))
170 (documentation
171 "Make the store copy-on-write, with writes going to \
172the given target.")
fdaacbad
LC
173
174 ;; This is meant to be explicitly started by the user.
175 (auto-start? #f)
176
83a17b62
LC
177 (start #~(case-lambda
178 ((target)
179 #$(make-cow-store #~target)
180 target)
181 (else
182 ;; Do nothing, and mark the service as stopped.
183 #f)))
184 (stop #~(lambda (target)
185 ;; Delete the temporary directory, but leave everything
186 ;; mounted as there may still be processes using it
d6e2a622
LC
187 ;; since 'user-processes' doesn't depend on us. The
188 ;; 'user-unmount' service will unmount TARGET
189 ;; eventually.
83a17b62
LC
190 (delete-file-recursively
191 (string-append target #$%backing-directory))))))))
192
1dac8566
LC
193(define (configuration-template-service)
194 "Return a dummy service whose purpose is to install an operating system
195configuration template file in the installation system."
196
e1fbc32a
LC
197 (define search
198 (cut search-path %load-path <>))
199 (define templates
200 (map (match-lambda
201 ((file '-> target)
202 (list (local-file (search file))
203 (string-append "/etc/configuration/" target))))
204 '(("gnu/system/examples/bare-bones.tmpl" -> "bare-bones.scm")
205 ("gnu/system/examples/desktop.tmpl" -> "desktop.scm"))))
1dac8566 206
e1fbc32a 207 (with-monad %store-monad
1dac8566
LC
208 (return (service
209 (requirement '(root-file-system))
210 (provision '(os-config-template))
211 (documentation
212 "This dummy service installs an OS configuration template.")
213 (start #~(const #t))
214 (stop #~(const #f))
215 (activate
e1fbc32a
LC
216 #~(begin
217 (use-modules (ice-9 match)
218 (guix build utils))
219
220 (mkdir-p "/etc/configuration")
221 (for-each (match-lambda
222 ((file target)
223 (unless (file-exists? target)
224 (copy-file file target))))
225 '#$templates)))))))
1dac8566 226
61ff0a3a
LC
227(define %nscd-minimal-caches
228 ;; Minimal in-memory caching policy for nscd.
229 (list (nscd-cache (database 'hosts)
230 (positive-time-to-live (* 3600 12))
231 (negative-time-to-live 20)
232 (persistent? #f)
233 (max-database-size (* 5 (expt 2 20)))))) ;5 MiB
234
fc91c17a
LC
235(define (installation-services)
236 "Return the list services for the installation image."
237 (let ((motd (text-file "motd" "
c73adb09 238Welcome to the installation of the Guix System Distribution!
fc91c17a
LC
239
240There is NO WARRANTY, to the extent permitted by law. In particular, you may
241LOSE ALL YOUR DATA as a side effect of the installation process. Furthermore,
242it is alpha software, so it may BREAK IN UNEXPECTED WAYS.
243
244You have been warned. Thanks for being so brave.
245")))
246 (define (normal-tty tty)
247 (mingetty-service tty
248 #:motd motd
249 #:auto-login "root"
250 #:login-pause? #t))
251
252 (list (mingetty-service "tty1"
253 #:motd motd
254 #:auto-login "root")
255
62ca0fdf
LC
256 ;; Documentation. The manual is in UTF-8, but
257 ;; 'console-font-service' sets up Unicode support and loads a font
258 ;; with all the useful glyphs like em dash and quotation marks.
fc91c17a
LC
259 (mingetty-service "tty2"
260 #:motd motd
261 #:auto-login "guest"
262 #:login-program (log-to-info))
263
1dac8566
LC
264 ;; Documentation add-on.
265 (configuration-template-service)
266
fc91c17a
LC
267 ;; A bunch of 'root' ttys.
268 (normal-tty "tty3")
269 (normal-tty "tty4")
270 (normal-tty "tty5")
271 (normal-tty "tty6")
272
273 ;; The usual services.
274 (syslog-service)
2c5c696c
LC
275
276 ;; The build daemon. Register the hydra.gnu.org key as trusted.
277 ;; This allows the installation process to use substitutes by
278 ;; default.
279 (guix-service #:authorize-hydra-key? #t)
280
e11390df 281 ;; Start udev so that useful device nodes are available.
68ac258b
LC
282 ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for
283 ;; regulations-compliant WiFi access.
284 (udev-service #:rules (list lvm2 crda))
e11390df 285
83a17b62
LC
286 ;; Add the 'cow-store' service, which users have to start manually
287 ;; since it takes the installation directory as an argument.
288 (cow-store-service)
289
62ca0fdf
LC
290 ;; Install Unicode support and a suitable font.
291 (console-font-service "tty1")
292 (console-font-service "tty2")
293 (console-font-service "tty3")
294 (console-font-service "tty4")
295 (console-font-service "tty5")
296 (console-font-service "tty6")
297
61ff0a3a
LC
298 ;; Since this is running on a USB stick with a unionfs as the root
299 ;; file system, use an appropriate cache configuration.
300 (nscd-service (nscd-configuration
301 (caches %nscd-minimal-caches))))))
fc91c17a
LC
302
303(define %issue
304 ;; Greeting.
305 "
306This is an installation image of the GNU system. Welcome.
307
308Use Alt-F2 for documentation.
309")
310
311(define installation-os
312 ;; The operating system used on installation images for USB sticks etc.
313 (operating-system
314 (host-name "gnu")
315 (timezone "Europe/Paris")
9cd0dfaa 316 (locale "en_US.utf8")
fc91c17a
LC
317 (bootloader (grub-configuration
318 (device "/dev/sda")))
319 (file-systems
320 ;; Note: the disk image build code overrides this root file system with
321 ;; the appropriate one.
a69576ea 322 (cons (file-system
fc91c17a
LC
323 (mount-point "/")
324 (device "gnu-disk-image")
a69576ea
LC
325 (type "ext4"))
326 %base-file-systems))
fc91c17a
LC
327
328 (users (list (user-account
329 (name "guest")
72507e23
LC
330 (group "users")
331 (supplementary-groups '("wheel")) ; allow use of sudo
fc91c17a
LC
332 (password "")
333 (comment "Guest of GNU")
334 (home-directory "/home/guest"))))
fc91c17a
LC
335
336 (issue %issue)
337
338 (services (installation-services))
339
340 ;; We don't need setuid programs so pass the empty list so we don't pull
341 ;; additional programs here.
342 (setuid-programs '())
343
344 (pam-services
345 ;; Explicitly allow for empty passwords.
346 (base-pam-services #:allow-empty-passwords? #t))
347
7eda0c56 348 (packages (cons* texinfo-4 ;for the standalone Info reader
8f297d42 349 parted ddrescue
7eda0c56 350 grub ;mostly so xrefs to its manual work
b419c7f5 351 cryptsetup
1ce6f43a 352 wireless-tools iw wpa-supplicant-minimal iproute
8f297d42
LC
353 ;; XXX: We used to have GNU fdisk here, but as of version
354 ;; 2.0.0a, that pulls Guile 1.8, which takes unreasonable
355 ;; space; furthermore util-linux's fdisk is already
356 ;; available here, so we keep that.
f4bdfe73 357 bash-completion
6f436c54 358 %base-packages))))
fc91c17a
LC
359
360;; Return it here so 'guix system' can consume it directly.
361installation-os
362
363;;; install.scm ends here