1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
36 grub-image-aspect-ratio
42 grub-theme-color-normal
43 grub-theme-color-highlight
50 grub-configuration-device
55 grub-configuration-file))
59 ;;; Configuration of GNU GRUB.
63 (define-record-type* <grub-image>
64 grub-image make-grub-image
66 (aspect-ratio grub-image-aspect-ratio ;rational number
68 (file grub-image-file)) ;file-valued gexp (SVG)
70 (define-record-type* <grub-theme>
71 grub-theme make-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)))))
80 (define %background-image
83 (file #~(string-append #$%artwork-repository "/grub/guix-4-3.svg"))))
85 (define %default-theme
86 ;; Default theme contributed by Felipe López.
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
92 (define-record-type* <grub-configuration>
93 grub-configuration make-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
100 (default-entry grub-configuration-default-entry ; integer
102 (timeout grub-configuration-timeout ; integer
104 (theme grub-configuration-theme ; <grub-theme>
105 (default %default-theme)))
107 (define-record-type* <menu-entry>
108 menu-entry make-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
118 ;;; Background image & themes.
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"
126 (system* (string-append #$inkscape "/bin/inkscape")
128 (string-append "--export-png=" #$output)
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"
138 (system* (string-append #$imagemagick "/bin/convert")
139 "-resize" #$size #$image #$output)))))
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)))))
149 (mlet %store-monad ((png (svg->png (grub-image-file image))))
150 (resize-image png width height))
151 (with-monad %store-monad
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
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)))))
164 (mlet* %store-monad ((image (grub-background-image config)))
165 (return (and image #~(format #$port "
166 function load_video {
173 # Set 'root' to the partition that contains /gnu/store.
174 search --file --set ~a/share/grub/unicode.pf2
176 if loadfont ~a/share/grub/unicode.pf2; then
180 terminal_output gfxterm
184 if background_image ~a; then
186 set color_highlight=~a
188 set menu_color_normal=cyan/blue
189 set menu_color_highlight=white/blue
193 #$(theme-colors grub-theme-color-normal)
194 #$(theme-colors grub-theme-color-highlight))))))
198 ;;; Configuration file.
201 (define* (grub-configuration-file config entries
203 (system (%current-system))
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."
209 (append entries (grub-configuration-menu-entries config)))
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~%
222 #$linux #$linux (string-join (list #$@arguments))
225 (mlet %store-monad ((sugar (eye-candy config #~port)))
227 #~(call-with-output-file #$output
233 #$(grub-configuration-default-entry config)
234 #$(grub-configuration-timeout config))
235 #$@(map entry->gexp all-entries)
237 #$@(if (pair? old-entries)
239 submenu \"GNU system, old configurations...\" {~%")
240 #$@(map entry->gexp old-entries)
244 (gexp->derivation "grub.cfg" builder)))
246 ;;; grub.scm ends here