Commit | Line | Data |
---|---|---|
0ded70f3 | 1 | ;;; GNU Guix --- Functional package management for GNU |
6b779207 | 2 | ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
0ded70f3 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (gnu system grub) | |
20 | #:use-module (guix store) | |
21 | #:use-module (guix packages) | |
22 | #:use-module (guix derivations) | |
23 | #:use-module (guix records) | |
d9f0a237 | 24 | #:use-module (guix monads) |
f6a7b21d | 25 | #:use-module (guix gexp) |
99ae9ceb | 26 | #:use-module (guix download) |
84dfb458 | 27 | #:use-module (gnu artwork) |
6b779207 | 28 | #:use-module (gnu system file-systems) |
99ae9ceb LC |
29 | #:autoload (gnu packages grub) (grub) |
30 | #:autoload (gnu packages inkscape) (inkscape) | |
31 | #:autoload (gnu packages imagemagick) (imagemagick) | |
32 | #:autoload (gnu packages compression) (gzip) | |
0ded70f3 | 33 | #:use-module (ice-9 match) |
6b173ac0 | 34 | #:use-module (ice-9 regex) |
0ded70f3 | 35 | #:use-module (srfi srfi-1) |
99ae9ceb LC |
36 | #:export (grub-image |
37 | grub-image? | |
38 | grub-image-aspect-ratio | |
39 | grub-image-file | |
40 | ||
41 | grub-theme | |
42 | grub-theme? | |
43 | grub-theme-images | |
44 | grub-theme-color-normal | |
45 | grub-theme-color-highlight | |
46 | ||
47 | %background-image | |
48 | %default-theme | |
49 | ||
50 | grub-configuration | |
d5b429ab LC |
51 | grub-configuration? |
52 | grub-configuration-device | |
53 | ||
54 | menu-entry | |
0ded70f3 | 55 | menu-entry? |
d5b429ab | 56 | |
0ded70f3 LC |
57 | grub-configuration-file)) |
58 | ||
59 | ;;; Commentary: | |
60 | ;;; | |
61 | ;;; Configuration of GNU GRUB. | |
62 | ;;; | |
63 | ;;; Code: | |
64 | ||
0f65f54e CSLL |
65 | (define (strip-mount-point fs file) |
66 | "Strip the mount point of FS from FILE, which is a gexp or other lowerable | |
67 | object denoting a file name." | |
68 | (let ((mount-point (file-system-mount-point fs))) | |
69 | (if (string=? mount-point "/") | |
70 | file | |
71 | #~(let ((file #$file)) | |
72 | (if (string-prefix? #$mount-point file) | |
73 | (substring #$file #$(string-length mount-point)) | |
74 | file))))) | |
75 | ||
99ae9ceb LC |
76 | (define-record-type* <grub-image> |
77 | grub-image make-grub-image | |
78 | grub-image? | |
79 | (aspect-ratio grub-image-aspect-ratio ;rational number | |
80 | (default 4/3)) | |
81 | (file grub-image-file)) ;file-valued gexp (SVG) | |
82 | ||
83 | (define-record-type* <grub-theme> | |
84 | grub-theme make-grub-theme | |
85 | grub-theme? | |
86 | (images grub-theme-images | |
87 | (default '())) ;list of <grub-image> | |
88 | (color-normal grub-theme-color-normal | |
89 | (default '((fg . cyan) (bg . blue)))) | |
90 | (color-highlight grub-theme-color-highlight | |
91 | (default '((fg . white) (bg . blue))))) | |
92 | ||
99ae9ceb LC |
93 | (define %background-image |
94 | (grub-image | |
95 | (aspect-ratio 4/3) | |
cf2abac8 LC |
96 | (file #~(string-append #$%artwork-repository |
97 | "/grub/GuixSD-fully-black-4-3.svg")))) | |
99ae9ceb LC |
98 | |
99 | (define %default-theme | |
100 | ;; Default theme contributed by Felipe López. | |
101 | (grub-theme | |
102 | (images (list %background-image)) | |
9c09760a | 103 | (color-highlight '((fg . yellow) (bg . black))) |
99ae9ceb LC |
104 | (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030 |
105 | ||
d5b429ab LC |
106 | (define-record-type* <grub-configuration> |
107 | grub-configuration make-grub-configuration | |
108 | grub-configuration? | |
109 | (grub grub-configuration-grub ; package | |
110 | (default (@ (gnu packages grub) grub))) | |
111 | (device grub-configuration-device) ; string | |
112 | (menu-entries grub-configuration-menu-entries ; list | |
113 | (default '())) | |
114 | (default-entry grub-configuration-default-entry ; integer | |
2f7f3200 | 115 | (default 0)) |
d5b429ab | 116 | (timeout grub-configuration-timeout ; integer |
99ae9ceb LC |
117 | (default 5)) |
118 | (theme grub-configuration-theme ; <grub-theme> | |
119 | (default %default-theme))) | |
d5b429ab | 120 | |
0ded70f3 LC |
121 | (define-record-type* <menu-entry> |
122 | menu-entry make-menu-entry | |
123 | menu-entry? | |
124 | (label menu-entry-label) | |
125 | (linux menu-entry-linux) | |
126 | (linux-arguments menu-entry-linux-arguments | |
f6a7b21d LC |
127 | (default '())) ; list of string-valued gexps |
128 | (initrd menu-entry-initrd)) ; file name of the initrd as a gexp | |
0ded70f3 | 129 | |
99ae9ceb LC |
130 | \f |
131 | ;;; | |
132 | ;;; Background image & themes. | |
133 | ;;; | |
134 | ||
135 | (define (svg->png svg) | |
136 | "Build a PNG from SVG." | |
137 | ;; Don't use #:local-build? so that it's substitutable. | |
138 | (gexp->derivation "grub-image.png" | |
139 | #~(zero? | |
140 | (system* (string-append #$inkscape "/bin/inkscape") | |
141 | "--without-gui" | |
142 | (string-append "--export-png=" #$output) | |
143 | #$svg)))) | |
144 | ||
145 | (define (resize-image image width height) | |
146 | "Resize IMAGE to WIDTHxHEIGHT." | |
147 | ;; Don't use #:local-build? so that it's substitutable. | |
148 | (let ((size (string-append (number->string width) | |
149 | "x" (number->string height)))) | |
150 | (gexp->derivation "grub-image.resized.png" | |
151 | #~(zero? | |
152 | (system* (string-append #$imagemagick "/bin/convert") | |
153 | "-resize" #$size #$image #$output))))) | |
154 | ||
6394fe65 | 155 | (define* (grub-background-image config #:key (width 1024) (height 768)) |
99ae9ceb LC |
156 | "Return the GRUB background image defined in CONFIG with a ratio of |
157 | WIDTH/HEIGHT, or #f if none was found." | |
158 | (let* ((ratio (/ width height)) | |
159 | (image (find (lambda (image) | |
160 | (= (grub-image-aspect-ratio image) ratio)) | |
161 | (grub-theme-images (grub-configuration-theme config))))) | |
162 | (if image | |
163 | (mlet %store-monad ((png (svg->png (grub-image-file image)))) | |
164 | (resize-image png width height)) | |
165 | (with-monad %store-monad | |
166 | (return #f))))) | |
167 | ||
6b779207 | 168 | (define (eye-candy config root-fs system port) |
99ae9ceb LC |
169 | "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the |
170 | 'grub.cfg' part concerned with graphics mode, background images, colors, and | |
6b779207 LC |
171 | all that. ROOT-FS is a file-system object denoting the root file system where |
172 | the store is. SYSTEM must be the target system string---e.g., | |
173 | \"x86_64-linux\"." | |
6b173ac0 MW |
174 | (define setup-gfxterm-body |
175 | ;; Intel systems need to be switched into graphics mode, whereas most | |
176 | ;; other modern architectures have no other mode and therefore don't need | |
177 | ;; to be switched. | |
178 | (if (string-match "^(x86_64|i[3-6]86)-" system) | |
179 | " | |
122c3a1d | 180 | # Leave 'gfxmode' to 'auto'. |
6b173ac0 MW |
181 | insmod vbe |
182 | insmod vga | |
183 | insmod video_bochs | |
184 | insmod video_cirrus | |
185 | insmod gfxterm | |
186 | terminal_output gfxterm | |
187 | " | |
188 | "")) | |
189 | ||
99ae9ceb LC |
190 | (define (theme-colors type) |
191 | (let* ((theme (grub-configuration-theme config)) | |
192 | (colors (type theme))) | |
193 | (string-append (symbol->string (assoc-ref colors 'fg)) "/" | |
194 | (symbol->string (assoc-ref colors 'bg))))) | |
195 | ||
6b779207 | 196 | (define font-file |
0f65f54e CSLL |
197 | (strip-mount-point root-fs |
198 | (file-append grub "/share/grub/unicode.pf2"))) | |
6b779207 | 199 | |
99ae9ceb | 200 | (mlet* %store-monad ((image (grub-background-image config))) |
6b173ac0 MW |
201 | (return (and image |
202 | #~(format #$port " | |
203 | function setup_gfxterm {~a} | |
99ae9ceb | 204 | |
ccc2678b | 205 | # Set 'root' to the partition that contains /gnu/store. |
6b779207 | 206 | ~a |
ccc2678b | 207 | |
6b779207 | 208 | if loadfont ~a; then |
6b173ac0 | 209 | setup_gfxterm |
99ae9ceb LC |
210 | fi |
211 | ||
212 | insmod png | |
213 | if background_image ~a; then | |
214 | set color_normal=~a | |
215 | set color_highlight=~a | |
216 | else | |
217 | set menu_color_normal=cyan/blue | |
218 | set menu_color_highlight=white/blue | |
219 | fi~%" | |
6b173ac0 | 220 | #$setup-gfxterm-body |
6b779207 LC |
221 | #$(grub-root-search root-fs font-file) |
222 | #$font-file | |
223 | ||
0f65f54e | 224 | #$(strip-mount-point root-fs image) |
6b173ac0 MW |
225 | #$(theme-colors grub-theme-color-normal) |
226 | #$(theme-colors grub-theme-color-highlight)))))) | |
99ae9ceb LC |
227 | |
228 | \f | |
229 | ;;; | |
230 | ;;; Configuration file. | |
231 | ;;; | |
232 | ||
6b779207 LC |
233 | (define (grub-root-search root-fs file) |
234 | "Return the GRUB 'search' command to look for ROOT-FS, which contains FILE, | |
235 | a gexp. The result is a gexp that can be inserted in the grub.cfg-generation | |
236 | code." | |
5babe521 LC |
237 | ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but |
238 | ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of | |
239 | ;; custom menu entries. In the latter case, don't emit a 'search' command. | |
240 | (if (and (string? file) (not (string-prefix? "/" file))) | |
241 | "" | |
242 | (case (file-system-title root-fs) | |
243 | ;; Preferably refer to ROOT-FS by its UUID or label. This is more | |
244 | ;; efficient and less ambiguous, see <>. | |
245 | ((uuid) | |
246 | (format #f "search --fs-uuid --set ~a" | |
247 | (uuid->string (file-system-device root-fs)))) | |
248 | ((label) | |
249 | (format #f "search --label --set ~a" | |
250 | (file-system-device root-fs))) | |
251 | (else | |
252 | ;; As a last resort, look for any device containing FILE. | |
253 | #~(format #f "search --file --set ~a" #$file))))) | |
6b779207 LC |
254 | |
255 | (define* (grub-configuration-file config store-fs entries | |
fe6e3fe2 LC |
256 | #:key |
257 | (system (%current-system)) | |
258 | (old-entries '())) | |
d5b429ab | 259 | "Return the GRUB configuration file corresponding to CONFIG, a |
6b779207 LC |
260 | <grub-configuration> object, and where the store is available at STORE-FS, a |
261 | <file-system> object. OLD-ENTRIES is taken to be a list of menu entries | |
262 | corresponding to old generations of the system." | |
d5b429ab LC |
263 | (define all-entries |
264 | (append entries (grub-configuration-menu-entries config))) | |
265 | ||
f6a7b21d | 266 | (define entry->gexp |
0ded70f3 LC |
267 | (match-lambda |
268 | (($ <menu-entry> label linux arguments initrd) | |
0f65f54e CSLL |
269 | ;; Use the right file names for LINUX and STORE-FS in case STORE-FS is |
270 | ;; not the "/" file system. | |
271 | (let ((linux (strip-mount-point store-fs linux)) | |
272 | (initrd (strip-mount-point store-fs initrd))) | |
273 | #~(format port "menuentry ~s { | |
6b779207 | 274 | ~a |
44d5f54e | 275 | linux ~a ~a |
d9f0a237 | 276 | initrd ~a |
0ded70f3 | 277 | }~%" |
0f65f54e CSLL |
278 | #$label |
279 | #$(grub-root-search store-fs linux) | |
280 | #$linux (string-join (list #$@arguments)) | |
281 | #$initrd))))) | |
f6a7b21d | 282 | |
6b779207 | 283 | (mlet %store-monad ((sugar (eye-candy config store-fs system #~port))) |
99ae9ceb LC |
284 | (define builder |
285 | #~(call-with-output-file #$output | |
286 | (lambda (port) | |
fdf14c64 JD |
287 | (format port |
288 | "# This file was generated from your GuixSD configuration. Any changes | |
289 | # will be lost upon reconfiguration. | |
290 | ") | |
99ae9ceb LC |
291 | #$sugar |
292 | (format port " | |
f6a7b21d | 293 | set default=~a |
6c777cf8 | 294 | set timeout=~a~%" |
99ae9ceb | 295 | #$(grub-configuration-default-entry config) |
6c777cf8 | 296 | #$(grub-configuration-timeout config)) |
99ae9ceb LC |
297 | #$@(map entry->gexp all-entries) |
298 | ||
299 | #$@(if (pair? old-entries) | |
300 | #~((format port " | |
fe6e3fe2 | 301 | submenu \"GNU system, old configurations...\" {~%") |
99ae9ceb LC |
302 | #$@(map entry->gexp old-entries) |
303 | (format port "}~%")) | |
304 | #~())))) | |
0ded70f3 | 305 | |
99ae9ceb | 306 | (gexp->derivation "grub.cfg" builder))) |
0ded70f3 LC |
307 | |
308 | ;;; grub.scm ends here |