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