1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 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 #: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)
38 grub-image-aspect-ratio
44 grub-theme-color-normal
45 grub-theme-color-highlight
52 grub-configuration-device
57 grub-configuration-file))
61 ;;; Configuration of GNU GRUB.
65 (define-record-type* <grub-image>
66 grub-image make-grub-image
68 (aspect-ratio grub-image-aspect-ratio ;rational number
70 (file grub-image-file)) ;file-valued gexp (SVG)
72 (define-record-type* <grub-theme>
73 grub-theme make-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)))))
82 (define %background-image
85 (file #~(string-append #$%artwork-repository
86 "/grub/GuixSD-fully-black-4-3.svg"))))
88 (define %default-theme
89 ;; Default theme contributed by Felipe López.
91 (images (list %background-image))
92 (color-highlight '((fg . yellow) (bg . black)))
93 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
95 (define-record-type* <grub-configuration>
96 grub-configuration make-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
103 (default-entry grub-configuration-default-entry ; integer
105 (timeout grub-configuration-timeout ; integer
107 (theme grub-configuration-theme ; <grub-theme>
108 (default %default-theme)))
110 (define-record-type* <menu-entry>
111 menu-entry make-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
121 ;;; Background image & themes.
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"
129 (system* (string-append #$inkscape "/bin/inkscape")
131 (string-append "--export-png=" #$output)
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"
141 (system* (string-append #$imagemagick "/bin/convert")
142 "-resize" #$size #$image #$output)))))
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)))))
152 (mlet %store-monad ((png (svg->png (grub-image-file image))))
153 (resize-image png width height))
154 (with-monad %store-monad
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.,
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
167 (if (string-match "^(x86_64|i[3-6]86)-" system)
169 # Leave 'gfxmode' to 'auto'.
175 terminal_output gfxterm
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)))))
186 #~(string-append #$grub "/share/grub/unicode.pf2"))
188 (mlet* %store-monad ((image (grub-background-image config)))
191 function setup_gfxterm {~a}
193 # Set 'root' to the partition that contains /gnu/store.
201 if background_image ~a; then
203 set color_highlight=~a
205 set menu_color_normal=cyan/blue
206 set menu_color_highlight=white/blue
209 #$(grub-root-search root-fs font-file)
213 #$(theme-colors grub-theme-color-normal)
214 #$(theme-colors grub-theme-color-highlight))))))
218 ;;; Configuration file.
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
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 <>.
229 (format #f "search --fs-uuid --set ~a"
230 (uuid->string (file-system-device root-fs))))
232 (format #f "search --label --set ~a"
233 (file-system-device root-fs)))
235 ;; As a last resort, look for any device containing FILE.
236 #~(format #f "search --file --set ~a" #$file))))
238 (define* (grub-configuration-file config store-fs entries
240 (system (%current-system))
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)
252 (append entries (grub-configuration-menu-entries config)))
256 (($ <menu-entry> label linux arguments initrd)
257 #~(format port "menuentry ~s {
263 #$(grub-root-search store-fs
264 #~(string-append #$linux "/"
266 #$linux #$linux-image-name (string-join (list #$@arguments))
269 (mlet %store-monad ((sugar (eye-candy config store-fs system #~port)))
271 #~(call-with-output-file #$output
277 #$(grub-configuration-default-entry config)
278 #$(grub-configuration-timeout config))
279 #$@(map entry->gexp all-entries)
281 #$@(if (pair? old-entries)
283 submenu \"GNU system, old configurations...\" {~%")
284 #$@(map entry->gexp old-entries)
288 (gexp->derivation "grub.cfg" builder)))
290 ;;; grub.scm ends here