gnu: Add rust-escargot-0.5.
[jackhill/guix/guix.git] / gnu / bootloader / grub.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
4 ;;; Copyright © 2017 Leo Famulari <leo@famulari.name>
5 ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;; Copyright © 2019, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
7 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
8 ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
9 ;;;
10 ;;; This file is part of GNU Guix.
11 ;;;
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
16 ;;;
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25 (define-module (gnu bootloader grub)
26 #:use-module (guix records)
27 #:use-module ((guix utils) #:select (%current-system))
28 #:use-module (guix gexp)
29 #:use-module (gnu artwork)
30 #:use-module (gnu bootloader)
31 #:use-module (gnu system uuid)
32 #:use-module (gnu system file-systems)
33 #:use-module (gnu system keyboard)
34 #:use-module (gnu packages bootloaders)
35 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
36 #:autoload (gnu packages xorg) (xkeyboard-config)
37 #:use-module (ice-9 match)
38 #:use-module (ice-9 regex)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-2)
41 #:export (grub-theme
42 grub-theme?
43 grub-theme-image
44 grub-theme-resolution
45 grub-theme-color-normal
46 grub-theme-color-highlight
47 grub-theme-gfxmode
48
49 grub-bootloader
50 grub-efi-bootloader
51 grub-mkrescue-bootloader
52 grub-minimal-bootloader
53
54 grub-configuration))
55
56 ;;; Commentary:
57 ;;;
58 ;;; Configuration of GNU GRUB.
59 ;;;
60 ;;; Code:
61
62 (define* (normalize-file file mount-point store-directory-prefix)
63 "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a
64 G-expression or other lowerable object denoting a file name."
65
66 (define (strip-mount-point mount-point file)
67 (if mount-point
68 (if (string=? mount-point "/")
69 file
70 #~(let ((file #$file))
71 (if (string-prefix? #$mount-point file)
72 (substring #$file #$(string-length mount-point))
73 file)))
74 file))
75
76 (define (prepend-store-directory-prefix store-directory-prefix file)
77 (if store-directory-prefix
78 #~(string-append #$store-directory-prefix #$file)
79 file))
80
81 (prepend-store-directory-prefix store-directory-prefix
82 (strip-mount-point mount-point file)))
83
84
85
86 (define-record-type* <grub-theme>
87 ;; Default theme contributed by Felipe López.
88 grub-theme make-grub-theme
89 grub-theme?
90 (image grub-theme-image
91 (default (file-append %artwork-repository
92 "/grub/GuixSD-fully-black-4-3.svg")))
93 (resolution grub-theme-resolution
94 (default '(1024 . 768)))
95 (color-normal grub-theme-color-normal
96 (default '((fg . light-gray) (bg . black))))
97 (color-highlight grub-theme-color-highlight
98 (default '((fg . yellow) (bg . black))))
99 (gfxmode grub-theme-gfxmode
100 (default '("auto")))) ;list of string
101
102 \f
103 ;;;
104 ;;; Background image & themes.
105 ;;;
106
107 (define (bootloader-theme config)
108 "Return user defined theme in CONFIG if defined or a default theme
109 otherwise."
110 (or (bootloader-configuration-theme config) (grub-theme)))
111
112 (define* (image->png image #:key width height)
113 "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\".
114 Otherwise the picture in IMAGE is just copied."
115 (computed-file "grub-image.png"
116 (with-imported-modules '((gnu build svg))
117 (with-extensions (list guile-rsvg guile-cairo)
118 #~(if (string-suffix? ".svg" #+image)
119 (begin
120 (use-modules (gnu build svg))
121 (svg->png #+image #$output
122 #:width #$width
123 #:height #$height))
124 (copy-file #+image #$output))))))
125
126 (define* (grub-background-image config)
127 "Return the GRUB background image defined in CONFIG or #f if none was found.
128 If the suffix of the image file is \".svg\", then it is converted into a PNG
129 file with the resolution provided in CONFIG."
130 (let* ((theme (bootloader-theme config))
131 (image (grub-theme-image theme)))
132 (and image
133 (match (grub-theme-resolution theme)
134 (((? number? width) . (? number? height))
135 (image->png image #:width width #:height height))
136 (_ #f)))))
137
138 (define* (eye-candy config store-device store-mount-point
139 #:key store-directory-prefix port)
140 "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part
141 concerned with graphics mode, background images, colors, and all that.
142 STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is
143 its mount point; these are used to determine where the background image and
144 fonts must be searched for. STORE-DIRECTORY-PREFIX is a directory prefix to
145 prepend to any store file name."
146 (define (setup-gfxterm config font-file)
147 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
148 #~(format #f "
149 if loadfont ~a; then
150 set gfxmode=~a
151 insmod all_video
152 insmod gfxterm
153 fi~%"
154 #+font-file
155 #$(string-join
156 (grub-theme-gfxmode (bootloader-theme config))
157 ";"))
158 ""))
159
160 (define (theme-colors type)
161 (let* ((theme (bootloader-theme config))
162 (colors (type theme)))
163 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
164 (symbol->string (assoc-ref colors 'bg)))))
165
166 (define font-file
167 (normalize-file (file-append grub "/share/grub/unicode.pf2")
168 store-mount-point
169 store-directory-prefix))
170
171 (define image
172 (normalize-file (grub-background-image config)
173 store-mount-point
174 store-directory-prefix))
175
176 (and image
177 #~(format #$port "
178 # Set 'root' to the partition that contains /gnu/store.
179 ~a
180
181 ~a
182 ~a
183
184 insmod png
185 if background_image ~a; then
186 set color_normal=~a
187 set color_highlight=~a
188 else
189 set menu_color_normal=cyan/blue
190 set menu_color_highlight=white/blue
191 fi~%"
192 #$(grub-root-search store-device font-file)
193 #$(setup-gfxterm config font-file)
194 #$(grub-setup-io config)
195
196 #$image
197 #$(theme-colors grub-theme-color-normal)
198 #$(theme-colors grub-theme-color-highlight))))
199
200 \f
201 ;;;
202 ;;; Configuration file.
203 ;;;
204
205 (define* (keyboard-layout-file layout
206 #:key
207 (grub grub))
208 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
209 and return a file in the format for GRUB keymaps. LAYOUT must be present in
210 the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
211 (define builder
212 (with-imported-modules '((guix build utils))
213 #~(begin
214 (use-modules (guix build utils))
215
216 ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
217 ;; (from the 'console-setup' package).
218 (invoke #+(file-append grub "/bin/grub-mklayout")
219 "-i" #+(keyboard-layout->console-keymap layout)
220 "-o" #$output))))
221
222 (computed-file (string-append "grub-keymap."
223 (string-map (match-lambda
224 (#\, #\-)
225 (chr chr))
226 (keyboard-layout-name layout)))
227 builder))
228
229 (define (grub-setup-io config)
230 "Return GRUB commands to configure the input / output interfaces. The result
231 is a string that can be inserted in grub.cfg."
232 (let* ((symbols->string (lambda (list)
233 (string-join (map symbol->string list) " ")))
234 (outputs (bootloader-configuration-terminal-outputs config))
235 (inputs (bootloader-configuration-terminal-inputs config))
236 (unit (bootloader-configuration-serial-unit config))
237 (speed (bootloader-configuration-serial-speed config))
238
239 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
240 ;; as documented in GRUB manual section "Simple Configuration
241 ;; Handling".
242 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
243 gfxterm vga_text mda_text morse spkmodem))
244 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
245 at_keyboard usb_keyboard))
246
247 (io (string-append
248 "terminal_output "
249 (symbols->string
250 (map
251 (lambda (output)
252 (if (memq output valid-outputs) output #f)) outputs)) "\n"
253 (if (null? inputs)
254 ""
255 (string-append
256 "terminal_input "
257 (symbols->string
258 (map
259 (lambda (input)
260 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
261 ;; UNIT and SPEED are arguments to the same GRUB command
262 ;; ("serial"), so we process them together.
263 (if (or unit speed)
264 (string-append
265 "serial"
266 (if unit
267 ;; COM ports 1 through 4
268 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
269 (string-append " --unit=" (number->string unit))
270 #f)
271 "")
272 (if speed
273 (if (exact-integer? speed)
274 (string-append " --speed=" (number->string speed))
275 #f)
276 ""))
277 ""))))
278 (format #f "~a" io)))
279
280 (define (grub-root-search device file)
281 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
282 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
283 code."
284 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
285 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
286 ;; custom menu entries. In the latter case, don't emit a 'search' command.
287 (if (and (string? file) (not (string-prefix? "/" file)))
288 ""
289 (match device
290 ;; Preferably refer to DEVICE by its UUID or label. This is more
291 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
292 ((? uuid? uuid)
293 (format #f "search --fs-uuid --set ~a"
294 (uuid->string device)))
295 ((? file-system-label? label)
296 (format #f "search --label --set ~a"
297 (file-system-label->string label)))
298 ((or #f (? string?))
299 #~(format #f "search --file --set ~a" #$file)))))
300
301 (define* (grub-configuration-file config entries
302 #:key
303 (system (%current-system))
304 (old-entries '())
305 store-directory-prefix)
306 "Return the GRUB configuration file corresponding to CONFIG, a
307 <bootloader-configuration> object, and where the store is available at
308 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
309 entries corresponding to old generations of the system.
310 STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required
311 when booting a root file system on a Btrfs subvolume."
312 (define all-entries
313 (append entries (bootloader-configuration-menu-entries config)))
314 (define (menu-entry->gexp entry)
315 (let ((label (menu-entry-label entry))
316 (linux (menu-entry-linux entry))
317 (device (menu-entry-device entry))
318 (device-mount-point (menu-entry-device-mount-point entry)))
319 (if linux
320 (let ((arguments (menu-entry-linux-arguments entry))
321 (linux (normalize-file linux
322 device-mount-point
323 store-directory-prefix))
324 (initrd (normalize-file (menu-entry-initrd entry)
325 device-mount-point
326 store-directory-prefix)))
327 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
328 ;; Use the right file names for LINUX and INITRD in case
329 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
330 ;; separate partition.
331
332 ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and
333 ;; initrd paths, to allow booting from a Btrfs subvolume.
334 #~(format port "menuentry ~s {
335 ~a
336 linux ~a ~a
337 initrd ~a
338 }~%"
339 #$label
340 #$(grub-root-search device linux)
341 #$linux (string-join (list #$@arguments))
342 #$initrd))
343 (let ((kernel (menu-entry-multiboot-kernel entry))
344 (arguments (menu-entry-multiboot-arguments entry))
345 (modules (menu-entry-multiboot-modules entry))
346 (root-index 1)) ; XXX EFI will need root-index 2
347 #~(format port "
348 menuentry ~s {
349 multiboot ~a root=device:hd0s~a~a~a
350 }~%"
351 #$label
352 #$kernel
353 #$root-index (string-join (list #$@arguments) " " 'prefix)
354 (string-join (map string-join '#$modules)
355 "\n module " 'prefix))))))
356
357 (define (sugar)
358 (let* ((entry (first all-entries))
359 (device (menu-entry-device entry))
360 (mount-point (menu-entry-device-mount-point entry)))
361 (eye-candy config
362 device
363 mount-point
364 #:store-directory-prefix store-directory-prefix
365 #:port #~port)))
366
367 (define keyboard-layout-config
368 (let* ((layout (bootloader-configuration-keyboard-layout config))
369 (grub (bootloader-package
370 (bootloader-configuration-bootloader config)))
371 (keymap* (and layout
372 (keyboard-layout-file layout #:grub grub)))
373 (keymap (and keymap*
374 (if store-directory-prefix
375 #~(string-append #$store-directory-prefix
376 #$keymap*)
377 keymap*))))
378 #~(when #$keymap
379 (format port "\
380 insmod keylayouts
381 keymap ~a~%" #$keymap))))
382
383 (define builder
384 #~(call-with-output-file #$output
385 (lambda (port)
386 (format port
387 "# This file was generated from your Guix configuration. Any changes
388 # will be lost upon reconfiguration.
389 ")
390 #$(sugar)
391 #$keyboard-layout-config
392 (format port "
393 set default=~a
394 set timeout=~a~%"
395 #$(bootloader-configuration-default-entry config)
396 #$(bootloader-configuration-timeout config))
397 #$@(map menu-entry->gexp all-entries)
398
399 #$@(if (pair? old-entries)
400 #~((format port "
401 submenu \"GNU system, old configurations...\" {~%")
402 #$@(map menu-entry->gexp old-entries)
403 (format port "}~%"))
404 #~())
405 (format port "
406 if [ \"${grub_platform}\" == efi ]; then
407 menuentry \"Firmware setup\" {
408 fwsetup
409 }
410 fi~%"))))
411
412 ;; Since this file is rather unique, there's no point in trying to
413 ;; substitute it.
414 (computed-file "grub.cfg" builder
415 #:options '(#:local-build? #t
416 #:substitutable? #f)))
417
418 \f
419
420 ;;;
421 ;;; Install procedures.
422 ;;;
423
424 (define install-grub
425 #~(lambda (bootloader device mount-point)
426 (let ((grub (string-append bootloader "/sbin/grub-install"))
427 (install-dir (string-append mount-point "/boot")))
428 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE
429 ;; is #f, then we populate the disk-image rooted at MOUNT-POINT.
430 (if device
431 (begin
432 ;; Tell 'grub-install' that there might be a LUKS-encrypted
433 ;; /boot or root partition.
434 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
435
436 ;; Hide potentially confusing messages from the user, such as
437 ;; "Installing for i386-pc platform."
438 (invoke/quiet grub "--no-floppy" "--target=i386-pc"
439 "--boot-directory" install-dir
440 device))
441 ;; When creating a disk-image, only install GRUB modules.
442 (copy-recursively (string-append bootloader "/lib/")
443 install-dir)))))
444
445 (define install-grub-disk-image
446 #~(lambda (bootloader root-index image)
447 ;; Install GRUB on the given IMAGE. The root partition index is
448 ;; ROOT-INDEX.
449 (let ((grub-mkimage
450 (string-append bootloader "/bin/grub-mkimage"))
451 (modules '("biosdisk" "part_msdos" "fat" "ext2"))
452 (grub-bios-setup
453 (string-append bootloader "/sbin/grub-bios-setup"))
454 (root-device (format #f "hd0,msdos~a" root-index))
455 (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img"))
456 (device-map "device.map"))
457
458 ;; Create a minimal, standalone GRUB image that will be written
459 ;; directly in the MBR-GAP (space between the end of the MBR and the
460 ;; first partition).
461 (apply invoke grub-mkimage
462 "-O" "i386-pc"
463 "-o" "core.img"
464 "-p" (format #f "(~a)/boot/grub" root-device)
465 modules)
466
467 ;; Create a device mapping file.
468 (call-with-output-file device-map
469 (lambda (port)
470 (format port "(hd0) ~a~%" image)))
471
472 ;; Copy the default boot.img, that will be written on the MBR sector
473 ;; by GRUB-BIOS-SETUP.
474 (copy-file boot-img "boot.img")
475
476 ;; Install both the "boot.img" and the "core.img" files on the given
477 ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB
478 ;; written in the MBR-GAP. GRUB configuration and missing modules will
479 ;; be read from ROOT-DEVICE.
480 (invoke grub-bios-setup
481 "-m" device-map
482 "-r" root-device
483 "-d" "."
484 image))))
485
486 (define install-grub-efi
487 #~(lambda (bootloader efi-dir mount-point)
488 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
489 ;; system whose root is mounted at MOUNT-POINT.
490 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
491 (install-dir (string-append mount-point "/boot"))
492 ;; When installing Guix, it's common to mount EFI-DIR below
493 ;; MOUNT-POINT rather than /boot/efi on the live image.
494 (target-esp (if (file-exists? (string-append mount-point efi-dir))
495 (string-append mount-point efi-dir)
496 efi-dir)))
497 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
498 ;; root partition.
499 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
500 (invoke/quiet grub-install "--boot-directory" install-dir
501 "--bootloader-id=Guix"
502 "--efi-directory" target-esp))))
503
504 \f
505
506 ;;;
507 ;;; Bootloader definitions.
508 ;;;
509
510 (define grub-bootloader
511 (bootloader
512 (name 'grub)
513 (package grub)
514 (installer install-grub)
515 (disk-image-installer install-grub-disk-image)
516 (configuration-file "/boot/grub/grub.cfg")
517 (configuration-file-generator grub-configuration-file)))
518
519 (define* grub-minimal-bootloader
520 (bootloader
521 (inherit grub-bootloader)
522 (package grub-minimal)))
523
524 (define* grub-efi-bootloader
525 (bootloader
526 (inherit grub-bootloader)
527 (installer install-grub-efi)
528 (disk-image-installer #f)
529 (name 'grub-efi)
530 (package grub-efi)))
531
532 (define* grub-mkrescue-bootloader
533 (bootloader
534 (inherit grub-efi-bootloader)
535 (package grub-hybrid)))
536
537 \f
538 ;;;
539 ;;; Compatibility macros.
540 ;;;
541
542 (define-syntax grub-configuration
543 (syntax-rules (grub)
544 ((_ (grub package) fields ...)
545 (if (eq? package grub)
546 (bootloader-configuration
547 (bootloader grub-bootloader)
548 fields ...)
549 (bootloader-configuration
550 (bootloader grub-efi-bootloader)
551 fields ...)))
552 ((_ fields ...)
553 (bootloader-configuration
554 (bootloader grub-bootloader)
555 fields ...))))
556
557 ;;; grub.scm ends here