gnu: emacs-goto-chg: Set source file-name.
[jackhill/guix/guix.git] / gnu / system / grub.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (gnu system grub)
21 #:use-module (guix store)
22 #:use-module (guix packages)
23 #:use-module (guix derivations)
24 #:use-module (guix records)
25 #:use-module (guix monads)
26 #:use-module (guix gexp)
27 #:use-module (guix download)
28 #:use-module (gnu artwork)
29 #:use-module (gnu system)
30 #:use-module (gnu system file-systems)
31 #:autoload (gnu packages bootloaders) (grub)
32 #:autoload (gnu packages compression) (gzip)
33 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:use-module (srfi srfi-1)
37 #:use-module (rnrs bytevectors)
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
52 grub-configuration
53 grub-configuration?
54 grub-configuration-device
55 grub-configuration-grub
56
57 menu-entry
58 menu-entry?
59
60 grub-configuration-file))
61
62 ;;; Commentary:
63 ;;;
64 ;;; Configuration of GNU GRUB.
65 ;;;
66 ;;; Code:
67
68 (define (strip-mount-point mount-point file)
69 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
70 denoting a file name."
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
78 (define-record-type* <grub-image>
79 grub-image make-grub-image
80 grub-image?
81 (aspect-ratio grub-image-aspect-ratio ;rational number
82 (default 4/3))
83 (file grub-image-file)) ;file-valued gexp (SVG)
84
85 (define-record-type* <grub-theme>
86 grub-theme make-grub-theme
87 grub-theme?
88 (images grub-theme-images
89 (default '())) ;list of <grub-image>
90 (color-normal grub-theme-color-normal
91 (default '((fg . cyan) (bg . blue))))
92 (color-highlight grub-theme-color-highlight
93 (default '((fg . white) (bg . blue)))))
94
95 (define %background-image
96 (grub-image
97 (aspect-ratio 4/3)
98 (file (file-append %artwork-repository
99 "/grub/GuixSD-fully-black-4-3.svg"))))
100
101 (define %default-theme
102 ;; Default theme contributed by Felipe López.
103 (grub-theme
104 (images (list %background-image))
105 (color-highlight '((fg . yellow) (bg . black)))
106 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
107
108 (define-record-type* <grub-configuration>
109 grub-configuration make-grub-configuration
110 grub-configuration?
111 (grub grub-configuration-grub ; package
112 (default (@ (gnu packages bootloaders) grub)))
113 (device grub-configuration-device) ; string
114 (menu-entries grub-configuration-menu-entries ; list
115 (default '()))
116 (default-entry grub-configuration-default-entry ; integer
117 (default 0))
118 (timeout grub-configuration-timeout ; integer
119 (default 5))
120 (theme grub-configuration-theme ; <grub-theme>
121 (default %default-theme)))
122
123 (define-record-type* <menu-entry>
124 menu-entry make-menu-entry
125 menu-entry?
126 (label menu-entry-label)
127 (device menu-entry-device ; file system uuid, label, or #f
128 (default #f))
129 (device-mount-point menu-entry-device-mount-point
130 (default "/"))
131 (linux menu-entry-linux)
132 (linux-arguments menu-entry-linux-arguments
133 (default '())) ; list of string-valued gexps
134 (initrd menu-entry-initrd)) ; file name of the initrd as a gexp
135
136 \f
137 ;;;
138 ;;; Background image & themes.
139 ;;;
140
141 (define* (svg->png svg #:key width height)
142 "Build a PNG of HEIGHT x WIDTH from SVG."
143 (gexp->derivation "grub-image.png"
144 (with-imported-modules '((gnu build svg))
145 #~(begin
146 ;; We need these two libraries.
147 (add-to-load-path (string-append #+guile-rsvg
148 "/share/guile/site/"
149 (effective-version)))
150 (add-to-load-path (string-append #+guile-cairo
151 "/share/guile/site/"
152 (effective-version)))
153
154 (use-modules (gnu build svg))
155 (svg->png #+svg #$output
156 #:width #$width
157 #:height #$height)))))
158
159 (define* (grub-background-image config #:key (width 1024) (height 768))
160 "Return the GRUB background image defined in CONFIG with a ratio of
161 WIDTH/HEIGHT, or #f if none was found."
162 (let* ((ratio (/ width height))
163 (image (find (lambda (image)
164 (= (grub-image-aspect-ratio image) ratio))
165 (grub-theme-images (grub-configuration-theme config)))))
166 (if image
167 (svg->png (grub-image-file image)
168 #:width width #:height height)
169 (with-monad %store-monad
170 (return #f)))))
171
172 (define* (eye-candy config store-device store-mount-point
173 #:key system port)
174 "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the
175 'grub.cfg' part concerned with graphics mode, background images, colors, and
176 all that. STORE-DEVICE designates the device holding the store, and
177 STORE-MOUNT-POINT is its mount point; these are used to determine where the
178 background image and fonts must be searched for. SYSTEM must be the target
179 system string---e.g., \"x86_64-linux\"."
180 (define setup-gfxterm-body
181 ;; Intel and EFI systems need to be switched into graphics mode, whereas
182 ;; most other modern architectures have no other mode and therefore don't
183 ;; need to be switched.
184 (if (string-match "^(x86_64|i[3-6]86)-" system)
185 "
186 # Leave 'gfxmode' to 'auto'.
187 insmod video_bochs
188 insmod video_cirrus
189 insmod gfxterm
190
191 if [ \"${grub_platform}\" == efi ]; then
192 # This is for (U)EFI systems (these modules are unavailable in the
193 # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
194 # which isn't convenient.
195 insmod efi_gop
196 insmod efi_uga
197 else
198 # These are specific to non-EFI Intel machines.
199 insmod vbe
200 insmod vga
201 fi
202
203 terminal_output gfxterm
204 "
205 ""))
206
207 (define (theme-colors type)
208 (let* ((theme (grub-configuration-theme config))
209 (colors (type theme)))
210 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
211 (symbol->string (assoc-ref colors 'bg)))))
212
213 (define font-file
214 (strip-mount-point store-mount-point
215 (file-append grub "/share/grub/unicode.pf2")))
216
217 (mlet* %store-monad ((image (grub-background-image config)))
218 (return (and image
219 #~(format #$port "
220 function setup_gfxterm {~a}
221
222 # Set 'root' to the partition that contains /gnu/store.
223 ~a
224
225 if loadfont ~a; then
226 setup_gfxterm
227 fi
228
229 insmod png
230 if background_image ~a; then
231 set color_normal=~a
232 set color_highlight=~a
233 else
234 set menu_color_normal=cyan/blue
235 set menu_color_highlight=white/blue
236 fi~%"
237 #$setup-gfxterm-body
238 #$(grub-root-search store-device font-file)
239 #$font-file
240
241 #$(strip-mount-point store-mount-point image)
242 #$(theme-colors grub-theme-color-normal)
243 #$(theme-colors grub-theme-color-highlight))))))
244
245 \f
246 ;;;
247 ;;; Configuration file.
248 ;;;
249
250 (define (grub-root-search device file)
251 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
252 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
253 code."
254 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
255 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
256 ;; custom menu entries. In the latter case, don't emit a 'search' command.
257 (if (and (string? file) (not (string-prefix? "/" file)))
258 ""
259 (match device
260 ;; Preferably refer to DEVICE by its UUID or label. This is more
261 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
262 ((? bytevector? uuid)
263 (format #f "search --fs-uuid --set ~a"
264 (uuid->string device)))
265 ((? string? label)
266 (format #f "search --label --set ~a" label))
267 (#f
268 #~(format #f "search --file --set ~a" #$file)))))
269
270 (define (boot-parameters->menu-entry conf)
271 "Convert a <boot-parameters> instance to a corresponding <menu-entry>."
272 (menu-entry
273 (label (boot-parameters-label conf))
274 (device (boot-parameters-store-device conf))
275 (device-mount-point (boot-parameters-store-mount-point conf))
276 (linux (boot-parameters-kernel conf))
277 (linux-arguments (boot-parameters-kernel-arguments conf))
278 (initrd (boot-parameters-initrd conf))))
279
280 (define* (grub-configuration-file config entries
281 #:key
282 (system (%current-system))
283 (old-entries '()))
284 "Return the GRUB configuration file corresponding to CONFIG, a
285 <grub-configuration> object, and where the store is available at STORE-FS, a
286 <file-system> object. OLD-ENTRIES is taken to be a list of menu entries
287 corresponding to old generations of the system."
288 (define all-entries
289 (append (map boot-parameters->menu-entry entries)
290 (grub-configuration-menu-entries config)))
291
292 (define entry->gexp
293 (match-lambda
294 (($ <menu-entry> label device device-mount-point
295 linux arguments initrd)
296 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
297 ;; Use the right file names for LINUX and INITRD in case
298 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
299 ;; separate partition.
300 (let ((linux (strip-mount-point device-mount-point linux))
301 (initrd (strip-mount-point device-mount-point initrd)))
302 #~(format port "menuentry ~s {
303 ~a
304 linux ~a ~a
305 initrd ~a
306 }~%"
307 #$label
308 #$(grub-root-search device linux)
309 #$linux (string-join (list #$@arguments))
310 #$initrd)))))
311
312 (mlet %store-monad ((sugar (eye-candy config
313 (menu-entry-device (first all-entries))
314 (menu-entry-device-mount-point
315 (first all-entries))
316 #:system system
317 #:port #~port)))
318 (define builder
319 #~(call-with-output-file #$output
320 (lambda (port)
321 (format port
322 "# This file was generated from your GuixSD configuration. Any changes
323 # will be lost upon reconfiguration.
324 ")
325 #$sugar
326 (format port "
327 set default=~a
328 set timeout=~a~%"
329 #$(grub-configuration-default-entry config)
330 #$(grub-configuration-timeout config))
331 #$@(map entry->gexp all-entries)
332
333 #$@(if (pair? old-entries)
334 #~((format port "
335 submenu \"GNU system, old configurations...\" {~%")
336 #$@(map entry->gexp (map boot-parameters->menu-entry old-entries))
337 (format port "}~%"))
338 #~()))))
339
340 (gexp->derivation "grub.cfg" builder)))
341
342 ;;; grub.scm ends here