gnu: Add ghc-aeson-pretty.
[jackhill/guix/guix.git] / gnu / system / grub.scm
CommitLineData
0ded70f3 1;;; GNU Guix --- Functional package management for GNU
6b779207 2;;; Copyright © 2013, 2014, 2015, 2016 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)
6b779207 28 #:use-module (gnu system file-systems)
99ae9ceb
LC
29 #:autoload (gnu packages grub) (grub)
30 #:autoload (gnu packages inkscape) (inkscape)
31 #:autoload (gnu packages imagemagick) (imagemagick)
32 #:autoload (gnu packages compression) (gzip)
0ded70f3 33 #:use-module (ice-9 match)
6b173ac0 34 #:use-module (ice-9 regex)
0ded70f3 35 #:use-module (srfi srfi-1)
99ae9ceb
LC
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
d5b429ab
LC
51 grub-configuration?
52 grub-configuration-device
53
54 menu-entry
0ded70f3 55 menu-entry?
d5b429ab 56
0ded70f3
LC
57 grub-configuration-file))
58
59;;; Commentary:
60;;;
61;;; Configuration of GNU GRUB.
62;;;
63;;; Code:
64
99ae9ceb
LC
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
99ae9ceb
LC
82(define %background-image
83 (grub-image
84 (aspect-ratio 4/3)
cf2abac8
LC
85 (file #~(string-append #$%artwork-repository
86 "/grub/GuixSD-fully-black-4-3.svg"))))
99ae9ceb
LC
87
88(define %default-theme
89 ;; Default theme contributed by Felipe López.
90 (grub-theme
91 (images (list %background-image))
9c09760a 92 (color-highlight '((fg . yellow) (bg . black)))
99ae9ceb
LC
93 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
94
d5b429ab
LC
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
2f7f3200 104 (default 0))
d5b429ab 105 (timeout grub-configuration-timeout ; integer
99ae9ceb
LC
106 (default 5))
107 (theme grub-configuration-theme ; <grub-theme>
108 (default %default-theme)))
d5b429ab 109
0ded70f3
LC
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
f6a7b21d
LC
116 (default '())) ; list of string-valued gexps
117 (initrd menu-entry-initrd)) ; file name of the initrd as a gexp
0ded70f3 118
99ae9ceb
LC
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
6394fe65 144(define* (grub-background-image config #:key (width 1024) (height 768))
99ae9ceb
LC
145 "Return the GRUB background image defined in CONFIG with a ratio of
146WIDTH/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
6b779207 157(define (eye-candy config root-fs system port)
99ae9ceb
LC
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
6b779207
LC
160all that. ROOT-FS is a file-system object denoting the root file system where
161the store is. SYSTEM must be the target system string---e.g.,
162\"x86_64-linux\"."
6b173ac0
MW
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 "
122c3a1d 169 # Leave 'gfxmode' to 'auto'.
6b173ac0
MW
170 insmod vbe
171 insmod vga
172 insmod video_bochs
173 insmod video_cirrus
174 insmod gfxterm
175 terminal_output gfxterm
176"
177 ""))
178
99ae9ceb
LC
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
6b779207
LC
185 (define font-file
186 #~(string-append #$grub "/share/grub/unicode.pf2"))
187
99ae9ceb 188 (mlet* %store-monad ((image (grub-background-image config)))
6b173ac0
MW
189 (return (and image
190 #~(format #$port "
191function setup_gfxterm {~a}
99ae9ceb 192
ccc2678b 193# Set 'root' to the partition that contains /gnu/store.
6b779207 194~a
ccc2678b 195
6b779207 196if loadfont ~a; then
6b173ac0 197 setup_gfxterm
99ae9ceb
LC
198fi
199
200insmod png
201if background_image ~a; then
202 set color_normal=~a
203 set color_highlight=~a
204else
205 set menu_color_normal=cyan/blue
206 set menu_color_highlight=white/blue
207fi~%"
6b173ac0 208 #$setup-gfxterm-body
6b779207
LC
209 #$(grub-root-search root-fs font-file)
210 #$font-file
211
6b173ac0
MW
212 #$image
213 #$(theme-colors grub-theme-color-normal)
214 #$(theme-colors grub-theme-color-highlight))))))
99ae9ceb
LC
215
216\f
217;;;
218;;; Configuration file.
219;;;
220
6b779207
LC
221(define (grub-root-search root-fs file)
222 "Return the GRUB 'search' command to look for ROOT-FS, which contains FILE,
223a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
224code."
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
fe6e3fe2
LC
239 #:key
240 (system (%current-system))
241 (old-entries '()))
d5b429ab 242 "Return the GRUB configuration file corresponding to CONFIG, a
6b779207
LC
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
245corresponding to old generations of the system."
c448bf74
MW
246 (define linux-image-name
247 (if (string-prefix? "mips" system)
248 "vmlinuz"
249 "bzImage"))
250
d5b429ab
LC
251 (define all-entries
252 (append entries (grub-configuration-menu-entries config)))
253
f6a7b21d 254 (define entry->gexp
0ded70f3
LC
255 (match-lambda
256 (($ <menu-entry> label linux arguments initrd)
f6a7b21d 257 #~(format port "menuentry ~s {
6b779207 258 ~a
c448bf74 259 linux ~a/~a ~a
d9f0a237 260 initrd ~a
0ded70f3 261}~%"
f6a7b21d 262 #$label
6b779207
LC
263 #$(grub-root-search store-fs
264 #~(string-append #$linux "/"
265 #$linux-image-name))
c448bf74 266 #$linux #$linux-image-name (string-join (list #$@arguments))
f6a7b21d
LC
267 #$initrd))))
268
6b779207 269 (mlet %store-monad ((sugar (eye-candy config store-fs system #~port)))
99ae9ceb
LC
270 (define builder
271 #~(call-with-output-file #$output
272 (lambda (port)
273 #$sugar
274 (format port "
f6a7b21d 275set default=~a
6c777cf8 276set timeout=~a~%"
99ae9ceb 277 #$(grub-configuration-default-entry config)
6c777cf8 278 #$(grub-configuration-timeout config))
99ae9ceb
LC
279 #$@(map entry->gexp all-entries)
280
281 #$@(if (pair? old-entries)
282 #~((format port "
fe6e3fe2 283submenu \"GNU system, old configurations...\" {~%")
99ae9ceb
LC
284 #$@(map entry->gexp old-entries)
285 (format port "}~%"))
286 #~()))))
0ded70f3 287
99ae9ceb 288 (gexp->derivation "grub.cfg" builder)))
0ded70f3
LC
289
290;;; grub.scm ends here