Commit | Line | Data |
---|---|---|
0ded70f3 | 1 | ;;; GNU Guix --- Functional package management for GNU |
9c09760a | 2 | ;;; Copyright © 2013, 2014, 2015 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) |
99ae9ceb LC |
28 | #:autoload (gnu packages grub) (grub) |
29 | #:autoload (gnu packages inkscape) (inkscape) | |
30 | #:autoload (gnu packages imagemagick) (imagemagick) | |
31 | #:autoload (gnu packages compression) (gzip) | |
0ded70f3 | 32 | #:use-module (ice-9 match) |
6b173ac0 | 33 | #:use-module (ice-9 regex) |
0ded70f3 | 34 | #:use-module (srfi srfi-1) |
99ae9ceb LC |
35 | #:export (grub-image |
36 | grub-image? | |
37 | grub-image-aspect-ratio | |
38 | grub-image-file | |
39 | ||
40 | grub-theme | |
41 | grub-theme? | |
42 | grub-theme-images | |
43 | grub-theme-color-normal | |
44 | grub-theme-color-highlight | |
45 | ||
46 | %background-image | |
47 | %default-theme | |
48 | ||
49 | grub-configuration | |
d5b429ab LC |
50 | grub-configuration? |
51 | grub-configuration-device | |
52 | ||
53 | menu-entry | |
0ded70f3 | 54 | menu-entry? |
d5b429ab | 55 | |
0ded70f3 LC |
56 | grub-configuration-file)) |
57 | ||
58 | ;;; Commentary: | |
59 | ;;; | |
60 | ;;; Configuration of GNU GRUB. | |
61 | ;;; | |
62 | ;;; Code: | |
63 | ||
99ae9ceb LC |
64 | (define-record-type* <grub-image> |
65 | grub-image make-grub-image | |
66 | grub-image? | |
67 | (aspect-ratio grub-image-aspect-ratio ;rational number | |
68 | (default 4/3)) | |
69 | (file grub-image-file)) ;file-valued gexp (SVG) | |
70 | ||
71 | (define-record-type* <grub-theme> | |
72 | grub-theme make-grub-theme | |
73 | grub-theme? | |
74 | (images grub-theme-images | |
75 | (default '())) ;list of <grub-image> | |
76 | (color-normal grub-theme-color-normal | |
77 | (default '((fg . cyan) (bg . blue)))) | |
78 | (color-highlight grub-theme-color-highlight | |
79 | (default '((fg . white) (bg . blue))))) | |
80 | ||
99ae9ceb LC |
81 | (define %background-image |
82 | (grub-image | |
83 | (aspect-ratio 4/3) | |
cf2abac8 LC |
84 | (file #~(string-append #$%artwork-repository |
85 | "/grub/GuixSD-fully-black-4-3.svg")))) | |
99ae9ceb LC |
86 | |
87 | (define %default-theme | |
88 | ;; Default theme contributed by Felipe López. | |
89 | (grub-theme | |
90 | (images (list %background-image)) | |
9c09760a | 91 | (color-highlight '((fg . yellow) (bg . black))) |
99ae9ceb LC |
92 | (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030 |
93 | ||
d5b429ab LC |
94 | (define-record-type* <grub-configuration> |
95 | grub-configuration make-grub-configuration | |
96 | grub-configuration? | |
97 | (grub grub-configuration-grub ; package | |
98 | (default (@ (gnu packages grub) grub))) | |
99 | (device grub-configuration-device) ; string | |
100 | (menu-entries grub-configuration-menu-entries ; list | |
101 | (default '())) | |
102 | (default-entry grub-configuration-default-entry ; integer | |
2f7f3200 | 103 | (default 0)) |
d5b429ab | 104 | (timeout grub-configuration-timeout ; integer |
99ae9ceb LC |
105 | (default 5)) |
106 | (theme grub-configuration-theme ; <grub-theme> | |
107 | (default %default-theme))) | |
d5b429ab | 108 | |
0ded70f3 LC |
109 | (define-record-type* <menu-entry> |
110 | menu-entry make-menu-entry | |
111 | menu-entry? | |
112 | (label menu-entry-label) | |
113 | (linux menu-entry-linux) | |
114 | (linux-arguments menu-entry-linux-arguments | |
f6a7b21d LC |
115 | (default '())) ; list of string-valued gexps |
116 | (initrd menu-entry-initrd)) ; file name of the initrd as a gexp | |
0ded70f3 | 117 | |
99ae9ceb LC |
118 | \f |
119 | ;;; | |
120 | ;;; Background image & themes. | |
121 | ;;; | |
122 | ||
123 | (define (svg->png svg) | |
124 | "Build a PNG from SVG." | |
125 | ;; Don't use #:local-build? so that it's substitutable. | |
126 | (gexp->derivation "grub-image.png" | |
127 | #~(zero? | |
128 | (system* (string-append #$inkscape "/bin/inkscape") | |
129 | "--without-gui" | |
130 | (string-append "--export-png=" #$output) | |
131 | #$svg)))) | |
132 | ||
133 | (define (resize-image image width height) | |
134 | "Resize IMAGE to WIDTHxHEIGHT." | |
135 | ;; Don't use #:local-build? so that it's substitutable. | |
136 | (let ((size (string-append (number->string width) | |
137 | "x" (number->string height)))) | |
138 | (gexp->derivation "grub-image.resized.png" | |
139 | #~(zero? | |
140 | (system* (string-append #$imagemagick "/bin/convert") | |
141 | "-resize" #$size #$image #$output))))) | |
142 | ||
6394fe65 | 143 | (define* (grub-background-image config #:key (width 1024) (height 768)) |
99ae9ceb LC |
144 | "Return the GRUB background image defined in CONFIG with a ratio of |
145 | WIDTH/HEIGHT, or #f if none was found." | |
146 | (let* ((ratio (/ width height)) | |
147 | (image (find (lambda (image) | |
148 | (= (grub-image-aspect-ratio image) ratio)) | |
149 | (grub-theme-images (grub-configuration-theme config))))) | |
150 | (if image | |
151 | (mlet %store-monad ((png (svg->png (grub-image-file image)))) | |
152 | (resize-image png width height)) | |
153 | (with-monad %store-monad | |
154 | (return #f))))) | |
155 | ||
6b173ac0 | 156 | (define (eye-candy config system port) |
99ae9ceb LC |
157 | "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the |
158 | 'grub.cfg' part concerned with graphics mode, background images, colors, and | |
159 | all that." | |
6b173ac0 MW |
160 | (define setup-gfxterm-body |
161 | ;; Intel systems need to be switched into graphics mode, whereas most | |
162 | ;; other modern architectures have no other mode and therefore don't need | |
163 | ;; to be switched. | |
164 | (if (string-match "^(x86_64|i[3-6]86)-" system) | |
165 | " | |
122c3a1d | 166 | # Leave 'gfxmode' to 'auto'. |
6b173ac0 MW |
167 | insmod vbe |
168 | insmod vga | |
169 | insmod video_bochs | |
170 | insmod video_cirrus | |
171 | insmod gfxterm | |
172 | terminal_output gfxterm | |
173 | " | |
174 | "")) | |
175 | ||
99ae9ceb LC |
176 | (define (theme-colors type) |
177 | (let* ((theme (grub-configuration-theme config)) | |
178 | (colors (type theme))) | |
179 | (string-append (symbol->string (assoc-ref colors 'fg)) "/" | |
180 | (symbol->string (assoc-ref colors 'bg))))) | |
181 | ||
182 | (mlet* %store-monad ((image (grub-background-image config))) | |
6b173ac0 MW |
183 | (return (and image |
184 | #~(format #$port " | |
185 | function setup_gfxterm {~a} | |
99ae9ceb | 186 | |
ccc2678b LC |
187 | # Set 'root' to the partition that contains /gnu/store. |
188 | search --file --set ~a/share/grub/unicode.pf2 | |
189 | ||
99ae9ceb | 190 | if loadfont ~a/share/grub/unicode.pf2; then |
6b173ac0 | 191 | setup_gfxterm |
99ae9ceb LC |
192 | fi |
193 | ||
194 | insmod png | |
195 | if background_image ~a; then | |
196 | set color_normal=~a | |
197 | set color_highlight=~a | |
198 | else | |
199 | set menu_color_normal=cyan/blue | |
200 | set menu_color_highlight=white/blue | |
201 | fi~%" | |
6b173ac0 MW |
202 | #$setup-gfxterm-body |
203 | #$grub #$grub | |
204 | #$image | |
205 | #$(theme-colors grub-theme-color-normal) | |
206 | #$(theme-colors grub-theme-color-highlight)))))) | |
99ae9ceb LC |
207 | |
208 | \f | |
209 | ;;; | |
210 | ;;; Configuration file. | |
211 | ;;; | |
212 | ||
d5b429ab | 213 | (define* (grub-configuration-file config entries |
fe6e3fe2 LC |
214 | #:key |
215 | (system (%current-system)) | |
216 | (old-entries '())) | |
d5b429ab | 217 | "Return the GRUB configuration file corresponding to CONFIG, a |
fe6e3fe2 LC |
218 | <grub-configuration> object. OLD-ENTRIES is taken to be a list of menu |
219 | entries corresponding to old generations of the system." | |
c448bf74 MW |
220 | (define linux-image-name |
221 | (if (string-prefix? "mips" system) | |
222 | "vmlinuz" | |
223 | "bzImage")) | |
224 | ||
d5b429ab LC |
225 | (define all-entries |
226 | (append entries (grub-configuration-menu-entries config))) | |
227 | ||
f6a7b21d | 228 | (define entry->gexp |
0ded70f3 LC |
229 | (match-lambda |
230 | (($ <menu-entry> label linux arguments initrd) | |
f6a7b21d | 231 | #~(format port "menuentry ~s { |
6c777cf8 | 232 | # Set 'root' to the partition that contains the kernel. |
c448bf74 | 233 | search --file --set ~a/~a~% |
6c777cf8 | 234 | |
c448bf74 | 235 | linux ~a/~a ~a |
d9f0a237 | 236 | initrd ~a |
0ded70f3 | 237 | }~%" |
f6a7b21d | 238 | #$label |
c448bf74 MW |
239 | #$linux #$linux-image-name |
240 | #$linux #$linux-image-name (string-join (list #$@arguments)) | |
f6a7b21d LC |
241 | #$initrd)))) |
242 | ||
6b173ac0 | 243 | (mlet %store-monad ((sugar (eye-candy config system #~port))) |
99ae9ceb LC |
244 | (define builder |
245 | #~(call-with-output-file #$output | |
246 | (lambda (port) | |
247 | #$sugar | |
248 | (format port " | |
f6a7b21d | 249 | set default=~a |
6c777cf8 | 250 | set timeout=~a~%" |
99ae9ceb | 251 | #$(grub-configuration-default-entry config) |
6c777cf8 | 252 | #$(grub-configuration-timeout config)) |
99ae9ceb LC |
253 | #$@(map entry->gexp all-entries) |
254 | ||
255 | #$@(if (pair? old-entries) | |
256 | #~((format port " | |
fe6e3fe2 | 257 | submenu \"GNU system, old configurations...\" {~%") |
99ae9ceb LC |
258 | #$@(map entry->gexp old-entries) |
259 | (format port "}~%")) | |
260 | #~())))) | |
0ded70f3 | 261 | |
99ae9ceb | 262 | (gexp->derivation "grub.cfg" builder))) |
0ded70f3 LC |
263 | |
264 | ;;; grub.scm ends here |