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