gnu: Add toxic.
[jackhill/guix/guix.git] / gnu / bootloader / grub.scm
CommitLineData
0ded70f3 1;;; GNU Guix --- Functional package management for GNU
4d0a3d8e 2;;; Copyright © 2013, 2014, 2015, 2016, 2017 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
LC
23 #:use-module (guix store)
24 #:use-module (guix packages)
25 #:use-module (guix derivations)
26 #:use-module (guix records)
d9f0a237 27 #:use-module (guix monads)
f6a7b21d 28 #:use-module (guix gexp)
99ae9ceb 29 #:use-module (guix download)
84dfb458 30 #:use-module (gnu artwork)
2e58e05b 31 #:use-module (gnu system)
b09a8da4 32 #:use-module (gnu bootloader)
6b779207 33 #:use-module (gnu system file-systems)
862e38d5 34 #:autoload (gnu packages bootloaders) (grub)
99ae9ceb 35 #:autoload (gnu packages compression) (gzip)
ffde82c9 36 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
0ded70f3 37 #:use-module (ice-9 match)
6b173ac0 38 #:use-module (ice-9 regex)
0ded70f3 39 #:use-module (srfi srfi-1)
1ef8b72a 40 #:use-module (rnrs bytevectors)
99ae9ceb
LC
41 #:export (grub-image
42 grub-image?
43 grub-image-aspect-ratio
44 grub-image-file
45
46 grub-theme
47 grub-theme?
48 grub-theme-images
49 grub-theme-color-normal
50 grub-theme-color-highlight
51
52 %background-image
53 %default-theme
54
b09a8da4
MO
55 grub-bootloader
56 grub-efi-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
94 (default '((fg . white) (bg . blue)))))
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."
99ae9ceb 121 (gexp->derivation "grub-image.png"
ffde82c9
LC
122 (with-imported-modules '((gnu build svg))
123 #~(begin
124 ;; We need these two libraries.
76f429ad 125 (add-to-load-path (string-append #+guile-rsvg
ffde82c9
LC
126 "/share/guile/site/"
127 (effective-version)))
76f429ad 128 (add-to-load-path (string-append #+guile-cairo
ffde82c9
LC
129 "/share/guile/site/"
130 (effective-version)))
131
132 (use-modules (gnu build svg))
76f429ad 133 (svg->png #+svg #$output
ffde82c9
LC
134 #:width #$width
135 #:height #$height)))))
99ae9ceb 136
6394fe65 137(define* (grub-background-image config #:key (width 1024) (height 768))
99ae9ceb
LC
138 "Return the GRUB background image defined in CONFIG with a ratio of
139WIDTH/HEIGHT, or #f if none was found."
140 (let* ((ratio (/ width height))
141 (image (find (lambda (image)
142 (= (grub-image-aspect-ratio image) ratio))
b09a8da4
MO
143 (grub-theme-images
144 (bootloader-theme config)))))
99ae9ceb 145 (if image
ffde82c9
LC
146 (svg->png (grub-image-file image)
147 #:width width #:height height)
99ae9ceb
LC
148 (with-monad %store-monad
149 (return #f)))))
150
1ef8b72a
CM
151(define* (eye-candy config store-device store-mount-point
152 #:key system port)
99ae9ceb
LC
153 "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the
154'grub.cfg' part concerned with graphics mode, background images, colors, and
1ef8b72a
CM
155all that. STORE-DEVICE designates the device holding the store, and
156STORE-MOUNT-POINT is its mount point; these are used to determine where the
157background image and fonts must be searched for. SYSTEM must be the target
158system string---e.g., \"x86_64-linux\"."
6b173ac0 159 (define setup-gfxterm-body
4d0a3d8e
LC
160 ;; Intel and EFI systems need to be switched into graphics mode, whereas
161 ;; most other modern architectures have no other mode and therefore don't
162 ;; need to be switched.
6b173ac0
MW
163 (if (string-match "^(x86_64|i[3-6]86)-" system)
164 "
122c3a1d 165 # Leave 'gfxmode' to 'auto'.
6b173ac0
MW
166 insmod video_bochs
167 insmod video_cirrus
168 insmod gfxterm
4d0a3d8e
LC
169
170 if [ \"${grub_platform}\" == efi ]; then
171 # This is for (U)EFI systems (these modules are unavailable in the
172 # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
173 # which isn't convenient.
174 insmod efi_gop
175 insmod efi_uga
5f19fb6a
LC
176 else
177 # These are specific to non-EFI Intel machines.
178 insmod vbe
179 insmod vga
4d0a3d8e 180 fi
6b173ac0
MW
181"
182 ""))
183
e0b2e930 184 (define (setup-gfxterm config font-file)
b09a8da4
MO
185 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
186 #~(format #f "if loadfont ~a; then
e0b2e930
LF
187 setup_gfxterm
188fi~%" #$font-file)
b09a8da4 189 ""))
e0b2e930 190
99ae9ceb 191 (define (theme-colors type)
b09a8da4 192 (let* ((theme (bootloader-theme config))
99ae9ceb
LC
193 (colors (type theme)))
194 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
195 (symbol->string (assoc-ref colors 'bg)))))
196
6b779207 197 (define font-file
1ef8b72a 198 (strip-mount-point store-mount-point
0f65f54e 199 (file-append grub "/share/grub/unicode.pf2")))
6b779207 200
99ae9ceb 201 (mlet* %store-monad ((image (grub-background-image config)))
6b173ac0
MW
202 (return (and image
203 #~(format #$port "
204function setup_gfxterm {~a}
99ae9ceb 205
ccc2678b 206# Set 'root' to the partition that contains /gnu/store.
6b779207 207~a
ccc2678b 208
e0b2e930
LF
209~a
210~a
99ae9ceb
LC
211
212insmod png
213if background_image ~a; then
214 set color_normal=~a
215 set color_highlight=~a
216else
217 set menu_color_normal=cyan/blue
218 set menu_color_highlight=white/blue
219fi~%"
6b173ac0 220 #$setup-gfxterm-body
1ef8b72a 221 #$(grub-root-search store-device font-file)
e0b2e930 222 #$(setup-gfxterm config font-file)
c48a145f 223 #$(grub-setup-io config)
6b779207 224
1ef8b72a 225 #$(strip-mount-point store-mount-point image)
6b173ac0
MW
226 #$(theme-colors grub-theme-color-normal)
227 #$(theme-colors grub-theme-color-highlight))))))
99ae9ceb
LC
228
229\f
230;;;
231;;; Configuration file.
232;;;
233
e0b2e930
LF
234(define (grub-setup-io config)
235 "Return GRUB commands to configure the input / output interfaces. The result
236is a string that can be inserted in grub.cfg."
237 (let* ((symbols->string (lambda (list)
238 (string-join (map symbol->string list) " ")))
b09a8da4
MO
239 (outputs (bootloader-configuration-terminal-outputs config))
240 (inputs (bootloader-configuration-terminal-inputs config))
241 (unit (bootloader-configuration-serial-unit config))
242 (speed (bootloader-configuration-serial-speed config))
e0b2e930
LF
243
244 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
245 ;; as documented in GRUB manual section "Simple Configuration
246 ;; Handling".
247 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
248 gfxterm vga_text mda_text morse spkmodem))
249 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
250 at_keyboard usb_keyboard))
251
252 (io (string-append
253 "terminal_output "
254 (symbols->string
255 (map
256 (lambda (output)
257 (if (memq output valid-outputs) output #f)) outputs)) "\n"
258 (if (null? inputs)
259 ""
260 (string-append
261 "terminal_input "
262 (symbols->string
263 (map
264 (lambda (input)
265 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
266 ;; UNIT and SPEED are arguments to the same GRUB command
267 ;; ("serial"), so we process them together.
268 (if (or unit speed)
269 (string-append
270 "serial"
271 (if unit
272 ;; COM ports 1 through 4
273 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
274 (string-append " --unit=" (number->string unit))
275 #f)
276 "")
277 (if speed
278 (if (exact-integer? speed)
279 (string-append " --speed=" (number->string speed))
280 #f)
281 ""))
282 ""))))
283 (format #f "~a" io)))
284
1ef8b72a
CM
285(define (grub-root-search device file)
286 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
6b779207
LC
287a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
288code."
5babe521
LC
289 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
290 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
291 ;; custom menu entries. In the latter case, don't emit a 'search' command.
292 (if (and (string? file) (not (string-prefix? "/" file)))
293 ""
1ef8b72a
CM
294 (match device
295 ;; Preferably refer to DEVICE by its UUID or label. This is more
ecc4324f 296 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
1ef8b72a 297 ((? bytevector? uuid)
5babe521 298 (format #f "search --fs-uuid --set ~a"
1ef8b72a
CM
299 (uuid->string device)))
300 ((? string? label)
301 (format #f "search --label --set ~a" label))
302 (#f
5babe521 303 #~(format #f "search --file --set ~a" #$file)))))
6b779207 304
1ef8b72a 305(define* (grub-configuration-file config entries
fe6e3fe2
LC
306 #:key
307 (system (%current-system))
308 (old-entries '()))
d5b429ab 309 "Return the GRUB configuration file corresponding to CONFIG, a
b09a8da4
MO
310<bootloader-configuration> object, and where the store is available at
311STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
312entries corresponding to old generations of the system."
d5b429ab 313 (define all-entries
8b22107e
MO
314 (append entries (map menu-entry->boot-parameters
315 (bootloader-configuration-menu-entries config))))
316
317 (define (boot-parameters->gexp params)
318 (let ((device (boot-parameters-store-device params))
319 (device-mount-point (boot-parameters-store-mount-point params))
320 (label (boot-parameters-label params))
321 (kernel (boot-parameters-kernel params))
322 (arguments (boot-parameters-kernel-arguments params))
323 (initrd (boot-parameters-initrd params)))
1ef8b72a 324 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
8b22107e 325 ;; Use the right file names for KERNEL and INITRD in case
1ef8b72a
CM
326 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
327 ;; separate partition.
8b22107e
MO
328 (let ((kernel (strip-mount-point device-mount-point kernel))
329 (initrd (strip-mount-point device-mount-point initrd)))
0f65f54e 330 #~(format port "menuentry ~s {
6b779207 331 ~a
44d5f54e 332 linux ~a ~a
d9f0a237 333 initrd ~a
0ded70f3 334}~%"
0f65f54e 335 #$label
8b22107e
MO
336 #$(grub-root-search device kernel)
337 #$kernel (string-join (list #$@arguments))
338 #$initrd))))
f6a7b21d 339
1ef8b72a 340 (mlet %store-monad ((sugar (eye-candy config
8b22107e
MO
341 (boot-parameters-store-device
342 (first all-entries))
343 (boot-parameters-store-mount-point
0ab1e8ae 344 (first all-entries))
1ef8b72a
CM
345 #:system system
346 #:port #~port)))
99ae9ceb
LC
347 (define builder
348 #~(call-with-output-file #$output
349 (lambda (port)
fdf14c64
JD
350 (format port
351 "# This file was generated from your GuixSD configuration. Any changes
352# will be lost upon reconfiguration.
353")
99ae9ceb
LC
354 #$sugar
355 (format port "
f6a7b21d 356set default=~a
6c777cf8 357set timeout=~a~%"
b09a8da4
MO
358 #$(bootloader-configuration-default-entry config)
359 #$(bootloader-configuration-timeout config))
8b22107e 360 #$@(map boot-parameters->gexp all-entries)
99ae9ceb
LC
361
362 #$@(if (pair? old-entries)
363 #~((format port "
fe6e3fe2 364submenu \"GNU system, old configurations...\" {~%")
8b22107e 365 #$@(map boot-parameters->gexp old-entries)
99ae9ceb
LC
366 (format port "}~%"))
367 #~()))))
0ded70f3 368
99ae9ceb 369 (gexp->derivation "grub.cfg" builder)))
0ded70f3 370
b09a8da4
MO
371\f
372
373;;;
374;;; Install procedures.
375;;;
376
377(define install-grub
378 #~(lambda (bootloader device mount-point)
379 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
380 (let ((grub (string-append bootloader "/sbin/grub-install"))
381 (install-dir (string-append mount-point "/boot")))
382 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
383 ;; root partition.
384 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
385
386 (unless (zero? (system* grub "--no-floppy"
387 "--boot-directory" install-dir
388 device))
389 (error "failed to install GRUB")))))
390
391\f
392
393;;;
394;;; Bootloader definitions.
395;;;
396
397(define grub-bootloader
398 (bootloader
399 (name 'grub)
400 (package grub)
401 (installer install-grub)
402 (configuration-file "/boot/grub/grub.cfg")
403 (configuration-file-generator grub-configuration-file)))
404
405(define* grub-efi-bootloader
406 (bootloader
407 (inherit grub-bootloader)
408 (name 'grub-efi)
409 (package grub-efi)))
410
411\f
412;;;
413;;; Compatibility macros.
414;;;
415
416(define-syntax grub-configuration
417 (syntax-rules (grub)
418 ((_ (grub package) fields ...)
419 (if (eq? package grub)
420 (bootloader-configuration
421 (bootloader grub-bootloader)
422 fields ...)
423 (bootloader-configuration
424 (bootloader grub-efi-bootloader)
425 fields ...)))
426 ((_ fields ...)
427 (bootloader-configuration
428 (bootloader grub-bootloader)
429 fields ...))))
430
0ded70f3 431;;; grub.scm ends here