Commit | Line | Data |
---|---|---|
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> |
0ded70f3 LC |
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) | |
d9f0a237 | 25 | #:use-module (guix monads) |
f6a7b21d | 26 | #:use-module (guix gexp) |
99ae9ceb | 27 | #:use-module (guix download) |
84dfb458 | 28 | #:use-module (gnu artwork) |
2e58e05b | 29 | #:use-module (gnu system) |
6b779207 | 30 | #:use-module (gnu system file-systems) |
862e38d5 | 31 | #:autoload (gnu packages bootloaders) (grub) |
99ae9ceb | 32 | #:autoload (gnu packages compression) (gzip) |
ffde82c9 | 33 | #:autoload (gnu packages gtk) (guile-cairo guile-rsvg) |
0ded70f3 | 34 | #:use-module (ice-9 match) |
6b173ac0 | 35 | #:use-module (ice-9 regex) |
0ded70f3 | 36 | #:use-module (srfi srfi-1) |
1ef8b72a | 37 | #:use-module (rnrs bytevectors) |
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 | ||
52 | grub-configuration | |
d5b429ab LC |
53 | grub-configuration? |
54 | grub-configuration-device | |
81bf2ccb | 55 | grub-configuration-grub |
d5b429ab LC |
56 | |
57 | menu-entry | |
0ded70f3 | 58 | menu-entry? |
d5b429ab | 59 | |
0ded70f3 LC |
60 | grub-configuration-file)) |
61 | ||
62 | ;;; Commentary: | |
63 | ;;; | |
64 | ;;; Configuration of GNU GRUB. | |
65 | ;;; | |
66 | ;;; Code: | |
67 | ||
1ef8b72a CM |
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)))) | |
0f65f54e | 77 | |
99ae9ceb LC |
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 | ||
99ae9ceb LC |
95 | (define %background-image |
96 | (grub-image | |
97 | (aspect-ratio 4/3) | |
357db1f9 LC |
98 | (file (file-append %artwork-repository |
99 | "/grub/GuixSD-fully-black-4-3.svg")))) | |
99ae9ceb LC |
100 | |
101 | (define %default-theme | |
102 | ;; Default theme contributed by Felipe López. | |
103 | (grub-theme | |
104 | (images (list %background-image)) | |
9c09760a | 105 | (color-highlight '((fg . yellow) (bg . black))) |
99ae9ceb LC |
106 | (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030 |
107 | ||
d5b429ab LC |
108 | (define-record-type* <grub-configuration> |
109 | grub-configuration make-grub-configuration | |
110 | grub-configuration? | |
111 | (grub grub-configuration-grub ; package | |
862e38d5 | 112 | (default (@ (gnu packages bootloaders) grub))) |
d5b429ab LC |
113 | (device grub-configuration-device) ; string |
114 | (menu-entries grub-configuration-menu-entries ; list | |
115 | (default '())) | |
116 | (default-entry grub-configuration-default-entry ; integer | |
2f7f3200 | 117 | (default 0)) |
d5b429ab | 118 | (timeout grub-configuration-timeout ; integer |
99ae9ceb LC |
119 | (default 5)) |
120 | (theme grub-configuration-theme ; <grub-theme> | |
121 | (default %default-theme))) | |
d5b429ab | 122 | |
0ded70f3 LC |
123 | (define-record-type* <menu-entry> |
124 | menu-entry make-menu-entry | |
125 | menu-entry? | |
126 | (label menu-entry-label) | |
1ef8b72a CM |
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 "/")) | |
0ded70f3 LC |
131 | (linux menu-entry-linux) |
132 | (linux-arguments menu-entry-linux-arguments | |
f6a7b21d LC |
133 | (default '())) ; list of string-valued gexps |
134 | (initrd menu-entry-initrd)) ; file name of the initrd as a gexp | |
0ded70f3 | 135 | |
99ae9ceb LC |
136 | \f |
137 | ;;; | |
138 | ;;; Background image & themes. | |
139 | ;;; | |
140 | ||
ffde82c9 LC |
141 | (define* (svg->png svg #:key width height) |
142 | "Build a PNG of HEIGHT x WIDTH from SVG." | |
99ae9ceb | 143 | (gexp->derivation "grub-image.png" |
ffde82c9 LC |
144 | (with-imported-modules '((gnu build svg)) |
145 | #~(begin | |
146 | ;; We need these two libraries. | |
76f429ad | 147 | (add-to-load-path (string-append #+guile-rsvg |
ffde82c9 LC |
148 | "/share/guile/site/" |
149 | (effective-version))) | |
76f429ad | 150 | (add-to-load-path (string-append #+guile-cairo |
ffde82c9 LC |
151 | "/share/guile/site/" |
152 | (effective-version))) | |
153 | ||
154 | (use-modules (gnu build svg)) | |
76f429ad | 155 | (svg->png #+svg #$output |
ffde82c9 LC |
156 | #:width #$width |
157 | #:height #$height))))) | |
99ae9ceb | 158 | |
6394fe65 | 159 | (define* (grub-background-image config #:key (width 1024) (height 768)) |
99ae9ceb LC |
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 | |
ffde82c9 LC |
167 | (svg->png (grub-image-file image) |
168 | #:width width #:height height) | |
99ae9ceb LC |
169 | (with-monad %store-monad |
170 | (return #f))))) | |
171 | ||
1ef8b72a CM |
172 | (define* (eye-candy config store-device store-mount-point |
173 | #:key system port) | |
99ae9ceb LC |
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 | |
1ef8b72a CM |
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\"." | |
6b173ac0 | 180 | (define setup-gfxterm-body |
4d0a3d8e LC |
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. | |
6b173ac0 MW |
184 | (if (string-match "^(x86_64|i[3-6]86)-" system) |
185 | " | |
122c3a1d | 186 | # Leave 'gfxmode' to 'auto'. |
6b173ac0 MW |
187 | insmod video_bochs |
188 | insmod video_cirrus | |
189 | insmod gfxterm | |
4d0a3d8e LC |
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 | |
5f19fb6a LC |
197 | else |
198 | # These are specific to non-EFI Intel machines. | |
199 | insmod vbe | |
200 | insmod vga | |
4d0a3d8e LC |
201 | fi |
202 | ||
6b173ac0 MW |
203 | terminal_output gfxterm |
204 | " | |
205 | "")) | |
206 | ||
99ae9ceb LC |
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 | ||
6b779207 | 213 | (define font-file |
1ef8b72a | 214 | (strip-mount-point store-mount-point |
0f65f54e | 215 | (file-append grub "/share/grub/unicode.pf2"))) |
6b779207 | 216 | |
99ae9ceb | 217 | (mlet* %store-monad ((image (grub-background-image config))) |
6b173ac0 MW |
218 | (return (and image |
219 | #~(format #$port " | |
220 | function setup_gfxterm {~a} | |
99ae9ceb | 221 | |
ccc2678b | 222 | # Set 'root' to the partition that contains /gnu/store. |
6b779207 | 223 | ~a |
ccc2678b | 224 | |
6b779207 | 225 | if loadfont ~a; then |
6b173ac0 | 226 | setup_gfxterm |
99ae9ceb LC |
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~%" | |
6b173ac0 | 237 | #$setup-gfxterm-body |
1ef8b72a | 238 | #$(grub-root-search store-device font-file) |
6b779207 LC |
239 | #$font-file |
240 | ||
1ef8b72a | 241 | #$(strip-mount-point store-mount-point image) |
6b173ac0 MW |
242 | #$(theme-colors grub-theme-color-normal) |
243 | #$(theme-colors grub-theme-color-highlight)))))) | |
99ae9ceb LC |
244 | |
245 | \f | |
246 | ;;; | |
247 | ;;; Configuration file. | |
248 | ;;; | |
249 | ||
1ef8b72a CM |
250 | (define (grub-root-search device file) |
251 | "Return the GRUB 'search' command to look for DEVICE, which contains FILE, | |
6b779207 LC |
252 | a gexp. The result is a gexp that can be inserted in the grub.cfg-generation |
253 | code." | |
5babe521 LC |
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 | "" | |
1ef8b72a CM |
259 | (match device |
260 | ;; Preferably refer to DEVICE by its UUID or label. This is more | |
ecc4324f | 261 | ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>. |
1ef8b72a | 262 | ((? bytevector? uuid) |
5babe521 | 263 | (format #f "search --fs-uuid --set ~a" |
1ef8b72a CM |
264 | (uuid->string device))) |
265 | ((? string? label) | |
266 | (format #f "search --label --set ~a" label)) | |
267 | (#f | |
5babe521 | 268 | #~(format #f "search --file --set ~a" #$file))))) |
6b779207 | 269 | |
bf8b0916 DM |
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 | ||
1ef8b72a | 280 | (define* (grub-configuration-file config entries |
fe6e3fe2 LC |
281 | #:key |
282 | (system (%current-system)) | |
283 | (old-entries '())) | |
d5b429ab | 284 | "Return the GRUB configuration file corresponding to CONFIG, a |
6b779207 LC |
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." | |
d5b429ab | 288 | (define all-entries |
bf8b0916 | 289 | (append (map boot-parameters->menu-entry entries) |
2e58e05b | 290 | (grub-configuration-menu-entries config))) |
d5b429ab | 291 | |
f6a7b21d | 292 | (define entry->gexp |
0ded70f3 | 293 | (match-lambda |
1ef8b72a CM |
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))) | |
0f65f54e | 302 | #~(format port "menuentry ~s { |
6b779207 | 303 | ~a |
44d5f54e | 304 | linux ~a ~a |
d9f0a237 | 305 | initrd ~a |
0ded70f3 | 306 | }~%" |
0f65f54e | 307 | #$label |
1ef8b72a | 308 | #$(grub-root-search device linux) |
0f65f54e CSLL |
309 | #$linux (string-join (list #$@arguments)) |
310 | #$initrd))))) | |
f6a7b21d | 311 | |
1ef8b72a | 312 | (mlet %store-monad ((sugar (eye-candy config |
0ab1e8ae | 313 | (menu-entry-device (first all-entries)) |
1ef8b72a | 314 | (menu-entry-device-mount-point |
0ab1e8ae | 315 | (first all-entries)) |
1ef8b72a CM |
316 | #:system system |
317 | #:port #~port))) | |
99ae9ceb LC |
318 | (define builder |
319 | #~(call-with-output-file #$output | |
320 | (lambda (port) | |
fdf14c64 JD |
321 | (format port |
322 | "# This file was generated from your GuixSD configuration. Any changes | |
323 | # will be lost upon reconfiguration. | |
324 | ") | |
99ae9ceb LC |
325 | #$sugar |
326 | (format port " | |
f6a7b21d | 327 | set default=~a |
6c777cf8 | 328 | set timeout=~a~%" |
99ae9ceb | 329 | #$(grub-configuration-default-entry config) |
6c777cf8 | 330 | #$(grub-configuration-timeout config)) |
99ae9ceb LC |
331 | #$@(map entry->gexp all-entries) |
332 | ||
333 | #$@(if (pair? old-entries) | |
334 | #~((format port " | |
fe6e3fe2 | 335 | submenu \"GNU system, old configurations...\" {~%") |
bf8b0916 | 336 | #$@(map entry->gexp (map boot-parameters->menu-entry old-entries)) |
99ae9ceb LC |
337 | (format port "}~%")) |
338 | #~())))) | |
0ded70f3 | 339 | |
99ae9ceb | 340 | (gexp->derivation "grub.cfg" builder))) |
0ded70f3 LC |
341 | |
342 | ;;; grub.scm ends here |