ui: Disable Guile deprecation warnings by default.
[jackhill/guix/guix.git] / gnu / system / 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>
0ded70f3
LC
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu system grub)
22 #:use-module (guix store)
23 #:use-module (guix packages)
24 #:use-module (guix derivations)
25 #:use-module (guix records)
d9f0a237 26 #:use-module (guix monads)
f6a7b21d 27 #:use-module (guix gexp)
99ae9ceb 28 #:use-module (guix download)
84dfb458 29 #:use-module (gnu artwork)
2e58e05b 30 #:use-module (gnu system)
6b779207 31 #:use-module (gnu system file-systems)
862e38d5 32 #:autoload (gnu packages bootloaders) (grub)
99ae9ceb 33 #:autoload (gnu packages compression) (gzip)
ffde82c9 34 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
0ded70f3 35 #:use-module (ice-9 match)
6b173ac0 36 #:use-module (ice-9 regex)
0ded70f3 37 #:use-module (srfi srfi-1)
1ef8b72a 38 #:use-module (rnrs bytevectors)
99ae9ceb
LC
39 #:export (grub-image
40 grub-image?
41 grub-image-aspect-ratio
42 grub-image-file
43
44 grub-theme
45 grub-theme?
46 grub-theme-images
47 grub-theme-color-normal
48 grub-theme-color-highlight
49
50 %background-image
51 %default-theme
52
53 grub-configuration
d5b429ab
LC
54 grub-configuration?
55 grub-configuration-device
81bf2ccb 56 grub-configuration-grub
d5b429ab
LC
57
58 menu-entry
0ded70f3 59 menu-entry?
d5b429ab 60
0ded70f3
LC
61 grub-configuration-file))
62
63;;; Commentary:
64;;;
65;;; Configuration of GNU GRUB.
66;;;
67;;; Code:
68
1ef8b72a
CM
69(define (strip-mount-point mount-point file)
70 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
71denoting a file name."
72 (if (string=? mount-point "/")
73 file
74 #~(let ((file #$file))
75 (if (string-prefix? #$mount-point file)
76 (substring #$file #$(string-length mount-point))
77 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
d5b429ab
LC
109(define-record-type* <grub-configuration>
110 grub-configuration make-grub-configuration
111 grub-configuration?
e0b2e930
LF
112 (grub grub-configuration-grub ; package
113 (default (@ (gnu packages bootloaders) grub)))
114 (device grub-configuration-device) ; string
115 (menu-entries grub-configuration-menu-entries ; list
116 (default '()))
117 (default-entry grub-configuration-default-entry ; integer
118 (default 0))
119 (timeout grub-configuration-timeout ; integer
120 (default 5))
121 (theme grub-configuration-theme ; <grub-theme>
122 (default %default-theme))
123 (terminal-outputs grub-configuration-terminal-outputs ; list of symbols
124 (default '(gfxterm)))
125 (terminal-inputs grub-configuration-terminal-inputs ; list of symbols
126 (default '()))
127 (serial-unit grub-configuration-serial-unit ; integer | #f
128 (default #f))
129 (serial-speed grub-configuration-serial-speed ; integer | #f
130 (default #f)))
d5b429ab 131
0ded70f3
LC
132(define-record-type* <menu-entry>
133 menu-entry make-menu-entry
134 menu-entry?
135 (label menu-entry-label)
1ef8b72a
CM
136 (device menu-entry-device ; file system uuid, label, or #f
137 (default #f))
138 (device-mount-point menu-entry-device-mount-point
139 (default "/"))
0ded70f3
LC
140 (linux menu-entry-linux)
141 (linux-arguments menu-entry-linux-arguments
f6a7b21d
LC
142 (default '())) ; list of string-valued gexps
143 (initrd menu-entry-initrd)) ; file name of the initrd as a gexp
0ded70f3 144
99ae9ceb
LC
145\f
146;;;
147;;; Background image & themes.
148;;;
149
ffde82c9
LC
150(define* (svg->png svg #:key width height)
151 "Build a PNG of HEIGHT x WIDTH from SVG."
99ae9ceb 152 (gexp->derivation "grub-image.png"
ffde82c9
LC
153 (with-imported-modules '((gnu build svg))
154 #~(begin
155 ;; We need these two libraries.
76f429ad 156 (add-to-load-path (string-append #+guile-rsvg
ffde82c9
LC
157 "/share/guile/site/"
158 (effective-version)))
76f429ad 159 (add-to-load-path (string-append #+guile-cairo
ffde82c9
LC
160 "/share/guile/site/"
161 (effective-version)))
162
163 (use-modules (gnu build svg))
76f429ad 164 (svg->png #+svg #$output
ffde82c9
LC
165 #:width #$width
166 #:height #$height)))))
99ae9ceb 167
6394fe65 168(define* (grub-background-image config #:key (width 1024) (height 768))
99ae9ceb
LC
169 "Return the GRUB background image defined in CONFIG with a ratio of
170WIDTH/HEIGHT, or #f if none was found."
171 (let* ((ratio (/ width height))
172 (image (find (lambda (image)
173 (= (grub-image-aspect-ratio image) ratio))
174 (grub-theme-images (grub-configuration-theme config)))))
175 (if image
ffde82c9
LC
176 (svg->png (grub-image-file image)
177 #:width width #:height height)
99ae9ceb
LC
178 (with-monad %store-monad
179 (return #f)))))
180
1ef8b72a
CM
181(define* (eye-candy config store-device store-mount-point
182 #:key system port)
99ae9ceb
LC
183 "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the
184'grub.cfg' part concerned with graphics mode, background images, colors, and
1ef8b72a
CM
185all that. STORE-DEVICE designates the device holding the store, and
186STORE-MOUNT-POINT is its mount point; these are used to determine where the
187background image and fonts must be searched for. SYSTEM must be the target
188system string---e.g., \"x86_64-linux\"."
6b173ac0 189 (define setup-gfxterm-body
4d0a3d8e
LC
190 ;; Intel and EFI systems need to be switched into graphics mode, whereas
191 ;; most other modern architectures have no other mode and therefore don't
192 ;; need to be switched.
6b173ac0
MW
193 (if (string-match "^(x86_64|i[3-6]86)-" system)
194 "
122c3a1d 195 # Leave 'gfxmode' to 'auto'.
6b173ac0
MW
196 insmod video_bochs
197 insmod video_cirrus
198 insmod gfxterm
4d0a3d8e
LC
199
200 if [ \"${grub_platform}\" == efi ]; then
201 # This is for (U)EFI systems (these modules are unavailable in the
202 # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
203 # which isn't convenient.
204 insmod efi_gop
205 insmod efi_uga
5f19fb6a
LC
206 else
207 # These are specific to non-EFI Intel machines.
208 insmod vbe
209 insmod vga
4d0a3d8e 210 fi
6b173ac0
MW
211"
212 ""))
213
e0b2e930
LF
214 (define (setup-gfxterm config font-file)
215 (if (memq 'gfxterm (grub-configuration-terminal-outputs config))
216 #~(format #f "if loadfont ~a; then
217 setup_gfxterm
218fi~%" #$font-file)
219 ""))
220
99ae9ceb
LC
221 (define (theme-colors type)
222 (let* ((theme (grub-configuration-theme config))
223 (colors (type theme)))
224 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
225 (symbol->string (assoc-ref colors 'bg)))))
226
6b779207 227 (define font-file
1ef8b72a 228 (strip-mount-point store-mount-point
0f65f54e 229 (file-append grub "/share/grub/unicode.pf2")))
6b779207 230
99ae9ceb 231 (mlet* %store-monad ((image (grub-background-image config)))
6b173ac0
MW
232 (return (and image
233 #~(format #$port "
234function setup_gfxterm {~a}
99ae9ceb 235
ccc2678b 236# Set 'root' to the partition that contains /gnu/store.
6b779207 237~a
ccc2678b 238
e0b2e930
LF
239~a
240~a
99ae9ceb
LC
241
242insmod png
243if background_image ~a; then
244 set color_normal=~a
245 set color_highlight=~a
246else
247 set menu_color_normal=cyan/blue
248 set menu_color_highlight=white/blue
249fi~%"
6b173ac0 250 #$setup-gfxterm-body
1ef8b72a 251 #$(grub-root-search store-device font-file)
e0b2e930 252 #$(setup-gfxterm config font-file)
c48a145f 253 #$(grub-setup-io config)
6b779207 254
1ef8b72a 255 #$(strip-mount-point store-mount-point image)
6b173ac0
MW
256 #$(theme-colors grub-theme-color-normal)
257 #$(theme-colors grub-theme-color-highlight))))))
99ae9ceb
LC
258
259\f
260;;;
261;;; Configuration file.
262;;;
263
e0b2e930
LF
264(define (grub-setup-io config)
265 "Return GRUB commands to configure the input / output interfaces. The result
266is a string that can be inserted in grub.cfg."
267 (let* ((symbols->string (lambda (list)
268 (string-join (map symbol->string list) " ")))
269 (outputs (grub-configuration-terminal-outputs config))
270 (inputs (grub-configuration-terminal-inputs config))
271 (unit (grub-configuration-serial-unit config))
272 (speed (grub-configuration-serial-speed config))
273
274 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
275 ;; as documented in GRUB manual section "Simple Configuration
276 ;; Handling".
277 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
278 gfxterm vga_text mda_text morse spkmodem))
279 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
280 at_keyboard usb_keyboard))
281
282 (io (string-append
283 "terminal_output "
284 (symbols->string
285 (map
286 (lambda (output)
287 (if (memq output valid-outputs) output #f)) outputs)) "\n"
288 (if (null? inputs)
289 ""
290 (string-append
291 "terminal_input "
292 (symbols->string
293 (map
294 (lambda (input)
295 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
296 ;; UNIT and SPEED are arguments to the same GRUB command
297 ;; ("serial"), so we process them together.
298 (if (or unit speed)
299 (string-append
300 "serial"
301 (if unit
302 ;; COM ports 1 through 4
303 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
304 (string-append " --unit=" (number->string unit))
305 #f)
306 "")
307 (if speed
308 (if (exact-integer? speed)
309 (string-append " --speed=" (number->string speed))
310 #f)
311 ""))
312 ""))))
313 (format #f "~a" io)))
314
1ef8b72a
CM
315(define (grub-root-search device file)
316 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
6b779207
LC
317a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
318code."
5babe521
LC
319 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
320 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
321 ;; custom menu entries. In the latter case, don't emit a 'search' command.
322 (if (and (string? file) (not (string-prefix? "/" file)))
323 ""
1ef8b72a
CM
324 (match device
325 ;; Preferably refer to DEVICE by its UUID or label. This is more
ecc4324f 326 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
1ef8b72a 327 ((? bytevector? uuid)
5babe521 328 (format #f "search --fs-uuid --set ~a"
1ef8b72a
CM
329 (uuid->string device)))
330 ((? string? label)
331 (format #f "search --label --set ~a" label))
332 (#f
5babe521 333 #~(format #f "search --file --set ~a" #$file)))))
6b779207 334
bf8b0916
DM
335(define (boot-parameters->menu-entry conf)
336 "Convert a <boot-parameters> instance to a corresponding <menu-entry>."
337 (menu-entry
338 (label (boot-parameters-label conf))
339 (device (boot-parameters-store-device conf))
340 (device-mount-point (boot-parameters-store-mount-point conf))
341 (linux (boot-parameters-kernel conf))
342 (linux-arguments (boot-parameters-kernel-arguments conf))
343 (initrd (boot-parameters-initrd conf))))
344
1ef8b72a 345(define* (grub-configuration-file config entries
fe6e3fe2
LC
346 #:key
347 (system (%current-system))
348 (old-entries '()))
d5b429ab 349 "Return the GRUB configuration file corresponding to CONFIG, a
6b779207
LC
350<grub-configuration> object, and where the store is available at STORE-FS, a
351<file-system> object. OLD-ENTRIES is taken to be a list of menu entries
352corresponding to old generations of the system."
d5b429ab 353 (define all-entries
bf8b0916 354 (append (map boot-parameters->menu-entry entries)
2e58e05b 355 (grub-configuration-menu-entries config)))
d5b429ab 356
f6a7b21d 357 (define entry->gexp
0ded70f3 358 (match-lambda
1ef8b72a
CM
359 (($ <menu-entry> label device device-mount-point
360 linux arguments initrd)
361 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
362 ;; Use the right file names for LINUX and INITRD in case
363 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
364 ;; separate partition.
365 (let ((linux (strip-mount-point device-mount-point linux))
366 (initrd (strip-mount-point device-mount-point initrd)))
0f65f54e 367 #~(format port "menuentry ~s {
6b779207 368 ~a
44d5f54e 369 linux ~a ~a
d9f0a237 370 initrd ~a
0ded70f3 371}~%"
0f65f54e 372 #$label
1ef8b72a 373 #$(grub-root-search device linux)
0f65f54e
CSLL
374 #$linux (string-join (list #$@arguments))
375 #$initrd)))))
f6a7b21d 376
1ef8b72a 377 (mlet %store-monad ((sugar (eye-candy config
0ab1e8ae 378 (menu-entry-device (first all-entries))
1ef8b72a 379 (menu-entry-device-mount-point
0ab1e8ae 380 (first all-entries))
1ef8b72a
CM
381 #:system system
382 #:port #~port)))
99ae9ceb
LC
383 (define builder
384 #~(call-with-output-file #$output
385 (lambda (port)
fdf14c64
JD
386 (format port
387 "# This file was generated from your GuixSD configuration. Any changes
388# will be lost upon reconfiguration.
389")
99ae9ceb
LC
390 #$sugar
391 (format port "
f6a7b21d 392set default=~a
6c777cf8 393set timeout=~a~%"
99ae9ceb 394 #$(grub-configuration-default-entry config)
6c777cf8 395 #$(grub-configuration-timeout config))
99ae9ceb
LC
396 #$@(map entry->gexp all-entries)
397
398 #$@(if (pair? old-entries)
399 #~((format port "
fe6e3fe2 400submenu \"GNU system, old configurations...\" {~%")
bf8b0916 401 #$@(map entry->gexp (map boot-parameters->menu-entry old-entries))
99ae9ceb
LC
402 (format port "}~%"))
403 #~()))))
0ded70f3 404
99ae9ceb 405 (gexp->derivation "grub.cfg" builder)))
0ded70f3
LC
406
407;;; grub.scm ends here