system: Adjust 'grub.cfg' to work on systems with a separate /boot.
[jackhill/guix/guix.git] / gnu / system / grub.scm
CommitLineData
0ded70f3 1;;; GNU Guix --- Functional package management for GNU
735c6dd7 2;;; Copyright © 2013, 2014 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
LC
32 #:use-module (ice-9 match)
33 #:use-module (srfi srfi-1)
99ae9ceb
LC
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
d5b429ab
LC
49 grub-configuration?
50 grub-configuration-device
51
52 menu-entry
0ded70f3 53 menu-entry?
d5b429ab 54
0ded70f3
LC
55 grub-configuration-file))
56
57;;; Commentary:
58;;;
59;;; Configuration of GNU GRUB.
60;;;
61;;; Code:
62
99ae9ceb
LC
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
99ae9ceb
LC
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))
28567712 89 (color-highlight '((fg . cyan) (bg . black))) ;XXX: fg should be #x3bb7f5
99ae9ceb
LC
90 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
91
d5b429ab
LC
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
2f7f3200 101 (default 0))
d5b429ab 102 (timeout grub-configuration-timeout ; integer
99ae9ceb
LC
103 (default 5))
104 (theme grub-configuration-theme ; <grub-theme>
105 (default %default-theme)))
d5b429ab 106
0ded70f3
LC
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
f6a7b21d
LC
113 (default '())) ; list of string-valued gexps
114 (initrd menu-entry-initrd)) ; file name of the initrd as a gexp
0ded70f3 115
99ae9ceb
LC
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
143WIDTH/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
157all 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 "
166function load_video {
167 insmod vbe
168 insmod vga
169 insmod video_bochs
170 insmod video_cirrus
171}
172
173if loadfont ~a/share/grub/unicode.pf2; then
174 set gfxmode=640x480
175 load_video
176 insmod gfxterm
177 terminal_output gfxterm
178fi
179
180insmod png
181if background_image ~a; then
182 set color_normal=~a
183 set color_highlight=~a
184else
185 set menu_color_normal=cyan/blue
186 set menu_color_highlight=white/blue
187fi~%"
188 #$grub
189 #$image
190 #$(theme-colors grub-theme-color-normal)
191 #$(theme-colors grub-theme-color-highlight))))))
192
193\f
194;;;
195;;; Configuration file.
196;;;
197
d5b429ab 198(define* (grub-configuration-file config entries
fe6e3fe2
LC
199 #:key
200 (system (%current-system))
201 (old-entries '()))
d5b429ab 202 "Return the GRUB configuration file corresponding to CONFIG, a
fe6e3fe2
LC
203<grub-configuration> object. OLD-ENTRIES is taken to be a list of menu
204entries corresponding to old generations of the system."
d5b429ab
LC
205 (define all-entries
206 (append entries (grub-configuration-menu-entries config)))
207
f6a7b21d 208 (define entry->gexp
0ded70f3
LC
209 (match-lambda
210 (($ <menu-entry> label linux arguments initrd)
f6a7b21d 211 #~(format port "menuentry ~s {
6c777cf8
LC
212 # Set 'root' to the partition that contains the kernel.
213 search --file --set ~a/bzImage~%
214
f6a7b21d 215 linux ~a/bzImage ~a
d9f0a237 216 initrd ~a
0ded70f3 217}~%"
f6a7b21d 218 #$label
6c777cf8 219 #$linux #$linux (string-join (list #$@arguments))
f6a7b21d
LC
220 #$initrd))))
221
99ae9ceb
LC
222 (mlet %store-monad ((sugar (eye-candy config #~port)))
223 (define builder
224 #~(call-with-output-file #$output
225 (lambda (port)
226 #$sugar
227 (format port "
f6a7b21d 228set default=~a
6c777cf8 229set timeout=~a~%"
99ae9ceb 230 #$(grub-configuration-default-entry config)
6c777cf8 231 #$(grub-configuration-timeout config))
99ae9ceb
LC
232 #$@(map entry->gexp all-entries)
233
234 #$@(if (pair? old-entries)
235 #~((format port "
fe6e3fe2 236submenu \"GNU system, old configurations...\" {~%")
99ae9ceb
LC
237 #$@(map entry->gexp old-entries)
238 (format port "}~%"))
239 #~()))))
0ded70f3 240
99ae9ceb 241 (gexp->derivation "grub.cfg" builder)))
0ded70f3
LC
242
243;;; grub.scm ends here