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