gnu: xf86-input-libinput: Reduce closure size.
[jackhill/guix/guix.git] / gnu / system / image.scm
CommitLineData
f19cf27c
MO
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu system image)
20 #:use-module (guix gexp)
21 #:use-module (guix modules)
22 #:use-module (guix monads)
23 #:use-module (guix records)
24 #:use-module (guix store)
25 #:use-module (guix ui)
26 #:use-module (guix utils)
27 #:use-module ((guix self) #:select (make-config.scm))
28 #:use-module (gnu bootloader)
29 #:use-module (gnu bootloader grub)
30 #:use-module (gnu image)
31 #:use-module (gnu services)
32 #:use-module (gnu services base)
33 #:use-module (gnu system)
34 #:use-module (gnu system file-systems)
35 #:use-module (gnu system uuid)
36 #:use-module (gnu system vm)
37 #:use-module (guix packages)
38 #:use-module (gnu packages base)
39 #:use-module (gnu packages bootloaders)
40 #:use-module (gnu packages cdrom)
41 #:use-module (gnu packages disk)
42 #:use-module (gnu packages gawk)
43 #:use-module (gnu packages genimage)
44 #:use-module (gnu packages guile)
45 #:autoload (gnu packages gnupg) (guile-gcrypt)
c77b9285 46 #:use-module (gnu packages hurd)
f19cf27c
MO
47 #:use-module (gnu packages linux)
48 #:use-module (gnu packages mtools)
49 #:use-module ((srfi srfi-1) #:prefix srfi-1:)
50 #:use-module (srfi srfi-11)
51 #:use-module (srfi srfi-26)
52 #:use-module (srfi srfi-35)
53 #:use-module (rnrs bytevectors)
54 #:use-module (ice-9 match)
55 #:export (esp-partition
56 root-partition
57
c77b9285 58 hurd-disk-image
f19cf27c
MO
59 efi-disk-image
60 iso9660-image
61
62 find-image
63 system-image))
64
65\f
66;;;
67;;; Images definitions.
68;;;
69
b7b45372
MO
70;; This is the offset before the first partition. GRUB will install itself in
71;; this post-MBR gap.
72(define root-offset (* 512 2048))
73
74;; Generic root partition label.
75(define root-label "Guix_image")
76
f19cf27c
MO
77(define esp-partition
78 (partition
79 (size (* 40 (expt 2 20)))
b7b45372 80 (offset root-offset)
f19cf27c
MO
81 (label "GNU-ESP") ;cosmetic only
82 ;; Use "vfat" here since this property is used when mounting. The actual
83 ;; FAT-ness is based on file system size (16 in this case).
84 (file-system "vfat")
85 (flags '(esp))
86 (initializer (gexp initialize-efi-partition))))
87
88(define root-partition
89 (partition
90 (size 'guess)
b7b45372 91 (label root-label)
f19cf27c
MO
92 (file-system "ext4")
93 (flags '(boot))
94 (initializer (gexp initialize-root-partition))))
95
c77b9285
MO
96(define hurd-initialize-root-partition
97 #~(lambda* (#:rest args)
98 (apply initialize-root-partition
99 (append args
100 (list #:make-device-nodes
101 make-hurd-device-nodes)))))
102
103(define hurd-disk-image
104 (image
105 (format 'disk-image)
106 (partitions
107 (list (partition
108 (size 'guess)
109 (offset root-offset)
110 (label root-label)
111 (file-system "ext2")
112 (file-system-options '("-o" "hurd" "-O" "ext_attr"))
113 (flags '(boot))
114 (initializer hurd-initialize-root-partition))))))
115
f19cf27c
MO
116(define efi-disk-image
117 (image
118 (format 'disk-image)
119 (partitions (list esp-partition root-partition))))
120
121(define iso9660-image
122 (image
123 (format 'iso9660)
124 (partitions
125 (list (partition
126 (size 'guess)
127 (label "GUIX_IMAGE")
128 (flags '(boot)))))
129 ;; XXX: Temporarily disable compression to speed-up the tests.
130 (compression? #f)))
131
132\f
133;;
134;; Helpers.
135;;
136
137(define not-config?
138 ;; Select (guix …) and (gnu …) modules, except (guix config).
139 (match-lambda
140 (('guix 'config) #f)
141 (('guix rest ...) #t)
142 (('gnu rest ...) #t)
143 (rest #f)))
144
145(define (partition->gexp partition)
146 "Turn PARTITION, a <partition> object, into a list-valued gexp suitable for
147'make-partition-image'."
148 #~'(#$@(list (partition-size partition))
149 #$(partition-file-system partition)
bd3716f6 150 #$(partition-file-system-options partition)
f19cf27c
MO
151 #$(partition-label partition)
152 #$(and=> (partition-uuid partition)
153 uuid-bytevector)))
154
155(define gcrypt-sqlite3&co
156 ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
157 (srfi-1:append-map
158 (lambda (package)
159 (cons package
160 (match (package-transitive-propagated-inputs package)
161 (((labels packages) ...)
162 packages))))
163 (list guile-gcrypt guile-sqlite3)))
164
165(define-syntax-rule (with-imported-modules* gexp* ...)
166 (with-extensions gcrypt-sqlite3&co
167 (with-imported-modules `(,@(source-module-closure
168 '((gnu build vm)
169 (gnu build image)
b37c5441 170 (gnu build hurd-boot)
c77b9285 171 (gnu build linux-boot)
f19cf27c
MO
172 (guix store database))
173 #:select? not-config?)
174 ((guix config) => ,(make-config.scm)))
175 #~(begin
176 (use-modules (gnu build vm)
177 (gnu build image)
b37c5441 178 (gnu build hurd-boot)
c77b9285 179 (gnu build linux-boot)
f19cf27c
MO
180 (guix store database)
181 (guix build utils))
182 gexp* ...))))
183
7feefb3b
MO
184(define (root-partition? partition)
185 "Return true if PARTITION is the root partition, false otherwise."
186 (member 'boot (partition-flags partition)))
187
188(define (find-root-partition image)
189 "Return the root partition of the given IMAGE."
190 (srfi-1:find root-partition? (image-partitions image)))
191
192(define (root-partition-index image)
193 "Return the index of the root partition of the given IMAGE."
194 (1+ (srfi-1:list-index root-partition? (image-partitions image))))
195
f19cf27c
MO
196\f
197;;
198;; Disk image.
199;;
200
201(define* (system-disk-image image
202 #:key
203 (name "disk-image")
204 bootcfg
205 bootloader
206 register-closures?
207 (inputs '()))
208 "Return as a file-like object, the disk-image described by IMAGE. Said
209image can be copied on a USB stick as is. BOOTLOADER is the bootloader that
210will be installed and configured according to BOOTCFG parameter.
211
212Raw images of the IMAGE partitions are first created. Then, genimage is used
213to assemble the partition images into a disk-image without resorting to a
214virtual machine.
215
216INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
217true, register INPUTS in the store database of the image so that Guix can be
218used in the image."
219
220 (define genimage-name "image")
221
222 (define (image->genimage-cfg image)
223 ;; Return as a file-like object, the genimage configuration file
224 ;; describing the given IMAGE.
225 (define (format->image-type format)
226 ;; Return the genimage format corresponding to FORMAT. For now, only
227 ;; the hdimage format (raw disk-image) is supported.
228 (case format
229 ((disk-image) "hdimage")
230 (else
231 (raise (condition
232 (&message
233 (message
234 (format #f (G_ "Unsupported image type ~a~%.") format))))))))
235
236 (define (partition->dos-type partition)
237 ;; Return the MBR partition type corresponding to the given PARTITION.
238 ;; See: https://en.wikipedia.org/wiki/Partition_type.
239 (let ((flags (partition-flags partition)))
240 (cond
241 ((member 'esp flags) "0xEF")
242 (else "0x83"))))
243
244 (define (partition-image partition)
245 ;; Return as a file-like object, an image of the given PARTITION. A
246 ;; directory, filled by calling the PARTITION initializer procedure, is
247 ;; first created within the store. Then, an image of this directory is
248 ;; created using tools such as 'mke2fs' or 'mkdosfs', depending on the
249 ;; partition file-system type.
250 (let* ((os (image-operating-system image))
251 (schema (local-file (search-path %load-path
252 "guix/store/schema.sql")))
253 (graph (match inputs
254 (((names . _) ...)
255 names)))
256 (root-builder
257 (with-imported-modules*
258 (let* ((initializer #$(partition-initializer partition)))
259 (sql-schema #$schema)
260
261 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be
262 ;; decoded.
263 (setenv "GUIX_LOCPATH"
264 #+(file-append glibc-utf8-locales "/lib/locale"))
265 (setlocale LC_ALL "en_US.utf8")
266
267 (initializer #$output
268 #:references-graphs '#$graph
269 #:deduplicate? #f
270 #:system-directory #$os
05f37c16 271 #:grub-efi #+grub-efi
f19cf27c 272 #:bootloader-package
9c1adb24
MO
273 #+(bootloader-package bootloader)
274 #:bootloader-installer
275 #+(bootloader-installer bootloader)
f19cf27c
MO
276 #:bootcfg #$bootcfg
277 #:bootcfg-location
278 #$(bootloader-configuration-file bootloader)))))
279 (image-root
280 (computed-file "partition-image-root" root-builder
281 #:options `(#:references-graphs ,inputs)))
282 (type (partition-file-system partition))
283 (image-builder
284 (with-imported-modules*
1dbd0005 285 (let ((inputs '#+(list e2fsprogs dosfstools mtools)))
f19cf27c
MO
286 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
287 (make-partition-image #$(partition->gexp partition)
288 #$output
289 #$image-root)))))
290 (computed-file "partition.img" image-builder)))
291
292 (define (partition->config partition)
293 ;; Return the genimage partition configuration for PARTITION.
294 (let ((label (partition-label partition))
295 (dos-type (partition->dos-type partition))
1b4fa785
MO
296 (image (partition-image partition))
297 (offset (partition-offset partition)))
f19cf27c 298 #~(format #f "~/partition ~a {
7d4ecda6
MO
299~/~/partition-type = ~a
300~/~/image = \"~a\"
301~/~/offset = \"~a\"
302~/}"
1b4fa785
MO
303 #$label
304 #$dos-type
305 #$image
1dd7b87f 306 #$offset)))
f19cf27c
MO
307
308 (let* ((format (image-format image))
309 (image-type (format->image-type format))
310 (partitions (image-partitions image))
311 (partitions-config (map partition->config partitions))
312 (builder
313 #~(begin
314 (let ((format (@ (ice-9 format) format)))
315 (call-with-output-file #$output
316 (lambda (port)
317 (format port
318 "\
319image ~a {
320~/~a {}
321~{~a~^~%~}
322}~%" #$genimage-name #$image-type (list #$@partitions-config))))))))
323 (computed-file "genimage.cfg" builder)))
324
325 (let* ((substitutable? (image-substitutable? image))
326 (builder
327 (with-imported-modules*
7feefb3b
MO
328 (let ((inputs '#+(list genimage coreutils findutils))
329 (bootloader-installer
330 #+(bootloader-disk-image-installer bootloader)))
f19cf27c 331 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
7feefb3b
MO
332 (genimage #$(image->genimage-cfg image) #$output)
333 ;; Install the bootloader directly on the disk-image.
334 (when bootloader-installer
335 (bootloader-installer
336 #+(bootloader-package bootloader)
337 #$(root-partition-index image)
338 (string-append #$output "/" #$genimage-name))))))
f19cf27c
MO
339 (image-dir (computed-file "image-dir" builder)))
340 (computed-file name
341 #~(symlink
342 (string-append #$image-dir "/" #$genimage-name)
343 #$output)
344 #:options `(#:substitutable? ,substitutable?))))
345
346\f
347;;
348;; ISO9660 image.
349;;
350
351(define (has-guix-service-type? os)
352 "Return true if OS contains a service of the type GUIX-SERVICE-TYPE."
353 (not (not (srfi-1:find (lambda (service)
354 (eq? (service-kind service) guix-service-type))
355 (operating-system-services os)))))
356
357(define* (system-iso9660-image image
358 #:key
359 (name "iso9660-image")
360 bootcfg
361 bootloader
362 register-closures?
363 (inputs '())
364 (grub-mkrescue-environment '()))
365 "Return as a file-like object a bootable, stand-alone iso9660 image.
366
367INPUTS is a list of inputs (as for packages). When REGISTER-CLOSURES? is
368true, register INPUTS in the store database of the image so that Guix can be
369used in the image. "
370 (define root-label
371 (match (image-partitions image)
372 ((partition)
373 (partition-label partition))))
374
375 (define root-uuid
376 (match (image-partitions image)
377 ((partition)
378 (uuid-bytevector (partition-uuid partition)))))
379
380 (let* ((os (image-operating-system image))
381 (bootloader (bootloader-package bootloader))
382 (compression? (image-compression? image))
383 (substitutable? (image-substitutable? image))
384 (schema (local-file (search-path %load-path
385 "guix/store/schema.sql")))
386 (graph (match inputs
387 (((names . _) ...)
388 names)))
389 (root-builder
390 (with-imported-modules*
391 (sql-schema #$schema)
392
393 ;; Allow non-ASCII file names--e.g., 'nss-certs'--to be decoded.
394 (setenv "GUIX_LOCPATH"
395 #+(file-append glibc-utf8-locales "/lib/locale"))
396 (setlocale LC_ALL "en_US.utf8")
397
398 (initialize-root-partition #$output
399 #:references-graphs '#$graph
400 #:deduplicate? #f
401 #:system-directory #$os)))
402 (image-root
403 (computed-file "image-root" root-builder
404 #:options `(#:references-graphs ,inputs)))
405 (builder
406 (with-imported-modules*
407 (let* ((inputs '#$(list parted e2fsprogs dosfstools xorriso
408 sed grep coreutils findutils gawk)))
409 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
410 (make-iso9660-image #$xorriso
411 '#$grub-mkrescue-environment
412 #$bootloader
413 #$bootcfg
414 #$os
415 #$image-root
416 #$output
417 #:references-graphs '#$graph
418 #:register-closures? #$register-closures?
419 #:compression? #$compression?
420 #:volume-id #$root-label
421 #:volume-uuid #$root-uuid)))))
422 (computed-file name builder
423 #:options `(#:references-graphs ,inputs
424 #:substitutable? ,substitutable?))))
425
426\f
427;;
428;; Image creation.
429;;
430
f19cf27c
MO
431(define (image->root-file-system image)
432 "Return the IMAGE root partition file-system type."
433 (let ((format (image-format image)))
434 (if (eq? format 'iso9660)
435 "iso9660"
436 (partition-file-system (find-root-partition image)))))
437
438(define (root-size image)
439 "Return the root partition size of IMAGE."
440 (let* ((image-size (image-size image))
441 (root-partition (find-root-partition image))
442 (root-size (partition-size root-partition)))
443 (cond
444 ((and (eq? root-size 'guess) image-size)
445 image-size)
446 (else root-size))))
447
448(define* (image-with-os base-image os)
449 "Return an image based on BASE-IMAGE but with the operating-system field set
450to OS. Also set the UUID and the size of the root partition."
451 (define root-file-system
452 (srfi-1:find
453 (lambda (fs)
454 (string=? (file-system-mount-point fs) "/"))
455 (operating-system-file-systems os)))
456
74938105
MO
457 (image
458 (inherit base-image)
459 (operating-system os)
460 (partitions
461 (map (lambda (p)
462 (if (root-partition? p)
463 (partition
464 (inherit p)
465 (uuid (file-system-device root-file-system))
466 (size (root-size base-image)))
467 p))
468 (image-partitions base-image)))))
f19cf27c
MO
469
470(define (operating-system-for-image image)
471 "Return an operating-system based on the one specified in IMAGE, but
472suitable for image creation. Assign an UUID to the root file-system, so that
473it can be used for bootloading."
474 (define volatile-root? (image-volatile-root? image))
475
476 (define (root-uuid os)
477 ;; UUID of the root file system, computed in a deterministic fashion.
478 ;; This is what we use to locate the root file system so it has to be
479 ;; different from the user's own file system UUIDs.
480 (let ((type (if (eq? (image-format image) 'iso9660)
481 'iso9660
482 'dce)))
483 (operating-system-uuid os type)))
484
485 (let* ((root-file-system-type (image->root-file-system image))
486 (base-os (image-operating-system image))
487 (file-systems-to-keep
488 (srfi-1:remove
489 (lambda (fs)
490 (string=? (file-system-mount-point fs) "/"))
491 (operating-system-file-systems base-os)))
492 (format (image-format image))
493 (os
494 (operating-system
495 (inherit base-os)
496 (initrd (lambda (file-systems . rest)
497 (apply (operating-system-initrd base-os)
498 file-systems
499 #:volatile-root? volatile-root?
500 rest)))
501 (bootloader (if (eq? format 'iso9660)
502 (bootloader-configuration
503 (inherit
504 (operating-system-bootloader base-os))
505 (bootloader grub-mkrescue-bootloader))
506 (operating-system-bootloader base-os)))
507 (file-systems (cons (file-system
508 (mount-point "/")
509 (device "/dev/placeholder")
510 (type root-file-system-type))
511 file-systems-to-keep))))
512 (uuid (root-uuid os)))
513 (operating-system
514 (inherit os)
515 (file-systems (cons (file-system
516 (mount-point "/")
517 (device uuid)
518 (type root-file-system-type))
519 file-systems-to-keep)))))
520
e3f0155c 521(define* (system-image image)
f19cf27c
MO
522 "Return the derivation of IMAGE. It can be a raw disk-image or an ISO9660
523image, depending on IMAGE format."
524 (define substitutable? (image-substitutable? image))
525
526 (let* ((os (operating-system-for-image image))
527 (image* (image-with-os image os))
528 (register-closures? (has-guix-service-type? os))
529 (bootcfg (operating-system-bootcfg os))
530 (bootloader (bootloader-configuration-bootloader
531 (operating-system-bootloader os))))
532 (case (image-format image)
533 ((disk-image)
534 (system-disk-image image*
535 #:bootcfg bootcfg
536 #:bootloader bootloader
537 #:register-closures? register-closures?
538 #:inputs `(("system" ,os)
539 ("bootcfg" ,bootcfg))))
540 ((iso9660)
541 (system-iso9660-image image*
542 #:bootcfg bootcfg
543 #:bootloader bootloader
544 #:register-closures? register-closures?
545 #:inputs `(("system" ,os)
546 ("bootcfg" ,bootcfg))
547 #:grub-mkrescue-environment
548 '(("MKRESCUE_SED_MODE" . "mbr_hfs")))))))
549
550(define (find-image file-system-type)
551 "Find and return an image that could match the given FILE-SYSTEM-TYPE. This
552is useful to adapt to interfaces written before the addition of the <image>
553record."
c77b9285
MO
554 (mlet %store-monad ((target (current-target-system)))
555 (mbegin %store-monad
556 (return
557 (match file-system-type
558 ("iso9660" iso9660-image)
559 (_ (cond
560 ((and target
561 (hurd-triplet? target))
562 hurd-disk-image)
563 (else
564 efi-disk-image))))))))
f19cf27c
MO
565
566;;; image.scm ends here