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) |
6b779207 | 29 | #:use-module (gnu system file-systems) |
862e38d5 | 30 | #:autoload (gnu packages bootloaders) (grub) |
99ae9ceb | 31 | #:autoload (gnu packages compression) (gzip) |
ffde82c9 | 32 | #:autoload (gnu packages gtk) (guile-cairo guile-rsvg) |
0ded70f3 | 33 | #:use-module (ice-9 match) |
6b173ac0 | 34 | #:use-module (ice-9 regex) |
0ded70f3 | 35 | #:use-module (srfi srfi-1) |
1ef8b72a | 36 | #:use-module (rnrs bytevectors) |
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 | ||
51 | grub-configuration | |
d5b429ab LC |
52 | grub-configuration? |
53 | grub-configuration-device | |
81bf2ccb | 54 | grub-configuration-grub |
d5b429ab LC |
55 | |
56 | menu-entry | |
0ded70f3 | 57 | menu-entry? |
d5b429ab | 58 | |
0ded70f3 LC |
59 | grub-configuration-file)) |
60 | ||
61 | ;;; Commentary: | |
62 | ;;; | |
63 | ;;; Configuration of GNU GRUB. | |
64 | ;;; | |
65 | ;;; Code: | |
66 | ||
1ef8b72a CM |
67 | (define (strip-mount-point mount-point file) |
68 | "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object | |
69 | denoting a file name." | |
70 | (if (string=? mount-point "/") | |
71 | file | |
72 | #~(let ((file #$file)) | |
73 | (if (string-prefix? #$mount-point file) | |
74 | (substring #$file #$(string-length mount-point)) | |
75 | file)))) | |
0f65f54e | 76 | |
99ae9ceb LC |
77 | (define-record-type* <grub-image> |
78 | grub-image make-grub-image | |
79 | grub-image? | |
80 | (aspect-ratio grub-image-aspect-ratio ;rational number | |
81 | (default 4/3)) | |
82 | (file grub-image-file)) ;file-valued gexp (SVG) | |
83 | ||
84 | (define-record-type* <grub-theme> | |
85 | grub-theme make-grub-theme | |
86 | grub-theme? | |
87 | (images grub-theme-images | |
88 | (default '())) ;list of <grub-image> | |
89 | (color-normal grub-theme-color-normal | |
90 | (default '((fg . cyan) (bg . blue)))) | |
91 | (color-highlight grub-theme-color-highlight | |
92 | (default '((fg . white) (bg . blue))))) | |
93 | ||
99ae9ceb LC |
94 | (define %background-image |
95 | (grub-image | |
96 | (aspect-ratio 4/3) | |
357db1f9 LC |
97 | (file (file-append %artwork-repository |
98 | "/grub/GuixSD-fully-black-4-3.svg")))) | |
99ae9ceb LC |
99 | |
100 | (define %default-theme | |
101 | ;; Default theme contributed by Felipe López. | |
102 | (grub-theme | |
103 | (images (list %background-image)) | |
9c09760a | 104 | (color-highlight '((fg . yellow) (bg . black))) |
99ae9ceb LC |
105 | (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030 |
106 | ||
d5b429ab LC |
107 | (define-record-type* <grub-configuration> |
108 | grub-configuration make-grub-configuration | |
109 | grub-configuration? | |
110 | (grub grub-configuration-grub ; package | |
862e38d5 | 111 | (default (@ (gnu packages bootloaders) grub))) |
d5b429ab LC |
112 | (device grub-configuration-device) ; string |
113 | (menu-entries grub-configuration-menu-entries ; list | |
114 | (default '())) | |
115 | (default-entry grub-configuration-default-entry ; integer | |
2f7f3200 | 116 | (default 0)) |
d5b429ab | 117 | (timeout grub-configuration-timeout ; integer |
99ae9ceb LC |
118 | (default 5)) |
119 | (theme grub-configuration-theme ; <grub-theme> | |
120 | (default %default-theme))) | |
d5b429ab | 121 | |
0ded70f3 LC |
122 | (define-record-type* <menu-entry> |
123 | menu-entry make-menu-entry | |
124 | menu-entry? | |
125 | (label menu-entry-label) | |
1ef8b72a CM |
126 | (device menu-entry-device ; file system uuid, label, or #f |
127 | (default #f)) | |
128 | (device-mount-point menu-entry-device-mount-point | |
129 | (default "/")) | |
0ded70f3 LC |
130 | (linux menu-entry-linux) |
131 | (linux-arguments menu-entry-linux-arguments | |
f6a7b21d LC |
132 | (default '())) ; list of string-valued gexps |
133 | (initrd menu-entry-initrd)) ; file name of the initrd as a gexp | |
0ded70f3 | 134 | |
99ae9ceb LC |
135 | \f |
136 | ;;; | |
137 | ;;; Background image & themes. | |
138 | ;;; | |
139 | ||
ffde82c9 LC |
140 | (define* (svg->png svg #:key width height) |
141 | "Build a PNG of HEIGHT x WIDTH from SVG." | |
99ae9ceb | 142 | (gexp->derivation "grub-image.png" |
ffde82c9 LC |
143 | (with-imported-modules '((gnu build svg)) |
144 | #~(begin | |
145 | ;; We need these two libraries. | |
146 | (add-to-load-path (string-append #$guile-rsvg | |
147 | "/share/guile/site/" | |
148 | (effective-version))) | |
149 | (add-to-load-path (string-append #$guile-cairo | |
150 | "/share/guile/site/" | |
151 | (effective-version))) | |
152 | ||
153 | (use-modules (gnu build svg)) | |
154 | (svg->png #$svg #$output | |
155 | #:width #$width | |
156 | #:height #$height))))) | |
99ae9ceb | 157 | |
6394fe65 | 158 | (define* (grub-background-image config #:key (width 1024) (height 768)) |
99ae9ceb LC |
159 | "Return the GRUB background image defined in CONFIG with a ratio of |
160 | WIDTH/HEIGHT, or #f if none was found." | |
161 | (let* ((ratio (/ width height)) | |
162 | (image (find (lambda (image) | |
163 | (= (grub-image-aspect-ratio image) ratio)) | |
164 | (grub-theme-images (grub-configuration-theme config))))) | |
165 | (if image | |
ffde82c9 LC |
166 | (svg->png (grub-image-file image) |
167 | #:width width #:height height) | |
99ae9ceb LC |
168 | (with-monad %store-monad |
169 | (return #f))))) | |
170 | ||
1ef8b72a CM |
171 | (define* (eye-candy config store-device store-mount-point |
172 | #:key system port) | |
99ae9ceb LC |
173 | "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the |
174 | 'grub.cfg' part concerned with graphics mode, background images, colors, and | |
1ef8b72a CM |
175 | all that. STORE-DEVICE designates the device holding the store, and |
176 | STORE-MOUNT-POINT is its mount point; these are used to determine where the | |
177 | background image and fonts must be searched for. SYSTEM must be the target | |
178 | system string---e.g., \"x86_64-linux\"." | |
6b173ac0 | 179 | (define setup-gfxterm-body |
4d0a3d8e LC |
180 | ;; Intel and EFI systems need to be switched into graphics mode, whereas |
181 | ;; most other modern architectures have no other mode and therefore don't | |
182 | ;; need to be switched. | |
6b173ac0 MW |
183 | (if (string-match "^(x86_64|i[3-6]86)-" system) |
184 | " | |
122c3a1d | 185 | # Leave 'gfxmode' to 'auto'. |
6b173ac0 MW |
186 | insmod vbe |
187 | insmod vga | |
188 | insmod video_bochs | |
189 | insmod video_cirrus | |
190 | insmod gfxterm | |
4d0a3d8e LC |
191 | |
192 | if [ \"${grub_platform}\" == efi ]; then | |
193 | # This is for (U)EFI systems (these modules are unavailable in the | |
194 | # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\", | |
195 | # which isn't convenient. | |
196 | insmod efi_gop | |
197 | insmod efi_uga | |
198 | fi | |
199 | ||
6b173ac0 MW |
200 | terminal_output gfxterm |
201 | " | |
202 | "")) | |
203 | ||
99ae9ceb LC |
204 | (define (theme-colors type) |
205 | (let* ((theme (grub-configuration-theme config)) | |
206 | (colors (type theme))) | |
207 | (string-append (symbol->string (assoc-ref colors 'fg)) "/" | |
208 | (symbol->string (assoc-ref colors 'bg))))) | |
209 | ||
6b779207 | 210 | (define font-file |
1ef8b72a | 211 | (strip-mount-point store-mount-point |
0f65f54e | 212 | (file-append grub "/share/grub/unicode.pf2"))) |
6b779207 | 213 | |
99ae9ceb | 214 | (mlet* %store-monad ((image (grub-background-image config))) |
6b173ac0 MW |
215 | (return (and image |
216 | #~(format #$port " | |
217 | function setup_gfxterm {~a} | |
99ae9ceb | 218 | |
ccc2678b | 219 | # Set 'root' to the partition that contains /gnu/store. |
6b779207 | 220 | ~a |
ccc2678b | 221 | |
6b779207 | 222 | if loadfont ~a; then |
6b173ac0 | 223 | setup_gfxterm |
99ae9ceb LC |
224 | fi |
225 | ||
226 | insmod png | |
227 | if background_image ~a; then | |
228 | set color_normal=~a | |
229 | set color_highlight=~a | |
230 | else | |
231 | set menu_color_normal=cyan/blue | |
232 | set menu_color_highlight=white/blue | |
233 | fi~%" | |
6b173ac0 | 234 | #$setup-gfxterm-body |
1ef8b72a | 235 | #$(grub-root-search store-device font-file) |
6b779207 LC |
236 | #$font-file |
237 | ||
1ef8b72a | 238 | #$(strip-mount-point store-mount-point image) |
6b173ac0 MW |
239 | #$(theme-colors grub-theme-color-normal) |
240 | #$(theme-colors grub-theme-color-highlight)))))) | |
99ae9ceb LC |
241 | |
242 | \f | |
243 | ;;; | |
244 | ;;; Configuration file. | |
245 | ;;; | |
246 | ||
1ef8b72a CM |
247 | (define (grub-root-search device file) |
248 | "Return the GRUB 'search' command to look for DEVICE, which contains FILE, | |
6b779207 LC |
249 | a gexp. The result is a gexp that can be inserted in the grub.cfg-generation |
250 | code." | |
5babe521 LC |
251 | ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but |
252 | ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of | |
253 | ;; custom menu entries. In the latter case, don't emit a 'search' command. | |
254 | (if (and (string? file) (not (string-prefix? "/" file))) | |
255 | "" | |
1ef8b72a CM |
256 | (match device |
257 | ;; Preferably refer to DEVICE by its UUID or label. This is more | |
5babe521 | 258 | ;; efficient and less ambiguous, see <>. |
1ef8b72a | 259 | ((? bytevector? uuid) |
5babe521 | 260 | (format #f "search --fs-uuid --set ~a" |
1ef8b72a CM |
261 | (uuid->string device))) |
262 | ((? string? label) | |
263 | (format #f "search --label --set ~a" label)) | |
264 | (#f | |
5babe521 | 265 | #~(format #f "search --file --set ~a" #$file))))) |
6b779207 | 266 | |
1ef8b72a | 267 | (define* (grub-configuration-file config entries |
fe6e3fe2 LC |
268 | #:key |
269 | (system (%current-system)) | |
270 | (old-entries '())) | |
d5b429ab | 271 | "Return the GRUB configuration file corresponding to CONFIG, a |
6b779207 LC |
272 | <grub-configuration> object, and where the store is available at STORE-FS, a |
273 | <file-system> object. OLD-ENTRIES is taken to be a list of menu entries | |
274 | corresponding to old generations of the system." | |
d5b429ab LC |
275 | (define all-entries |
276 | (append entries (grub-configuration-menu-entries config))) | |
277 | ||
f6a7b21d | 278 | (define entry->gexp |
0ded70f3 | 279 | (match-lambda |
1ef8b72a CM |
280 | (($ <menu-entry> label device device-mount-point |
281 | linux arguments initrd) | |
282 | ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point. | |
283 | ;; Use the right file names for LINUX and INITRD in case | |
284 | ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a | |
285 | ;; separate partition. | |
286 | (let ((linux (strip-mount-point device-mount-point linux)) | |
287 | (initrd (strip-mount-point device-mount-point initrd))) | |
0f65f54e | 288 | #~(format port "menuentry ~s { |
6b779207 | 289 | ~a |
44d5f54e | 290 | linux ~a ~a |
d9f0a237 | 291 | initrd ~a |
0ded70f3 | 292 | }~%" |
0f65f54e | 293 | #$label |
1ef8b72a | 294 | #$(grub-root-search device linux) |
0f65f54e CSLL |
295 | #$linux (string-join (list #$@arguments)) |
296 | #$initrd))))) | |
f6a7b21d | 297 | |
1ef8b72a CM |
298 | (mlet %store-monad ((sugar (eye-candy config |
299 | (menu-entry-device (first entries)) | |
300 | (menu-entry-device-mount-point | |
301 | (first entries)) | |
302 | #:system system | |
303 | #:port #~port))) | |
99ae9ceb LC |
304 | (define builder |
305 | #~(call-with-output-file #$output | |
306 | (lambda (port) | |
fdf14c64 JD |
307 | (format port |
308 | "# This file was generated from your GuixSD configuration. Any changes | |
309 | # will be lost upon reconfiguration. | |
310 | ") | |
99ae9ceb LC |
311 | #$sugar |
312 | (format port " | |
f6a7b21d | 313 | set default=~a |
6c777cf8 | 314 | set timeout=~a~%" |
99ae9ceb | 315 | #$(grub-configuration-default-entry config) |
6c777cf8 | 316 | #$(grub-configuration-timeout config)) |
99ae9ceb LC |
317 | #$@(map entry->gexp all-entries) |
318 | ||
319 | #$@(if (pair? old-entries) | |
320 | #~((format port " | |
fe6e3fe2 | 321 | submenu \"GNU system, old configurations...\" {~%") |
99ae9ceb LC |
322 | #$@(map entry->gexp old-entries) |
323 | (format port "}~%")) | |
324 | #~())))) | |
0ded70f3 | 325 | |
99ae9ceb | 326 | (gexp->derivation "grub.cfg" builder))) |
0ded70f3 LC |
327 | |
328 | ;;; grub.scm ends here |