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