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