gnu: Improve error reporting of the use-.*modules macros.
[jackhill/guix/guix.git] / gnu.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Joshua S. Grant <jgrant@parenthetical.io>
4 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu)
22 #:use-module (guix i18n)
23 #:use-module (guix utils)
24 #:use-module (srfi srfi-34)
25 #:use-module (srfi srfi-35)
26 #:use-module (ice-9 match)
27 #:use-module (guix packages)
28 #:use-module (gnu packages)
29 #:use-module (gnu services)
30 #:export (use-package-modules
31 use-service-modules
32 use-system-modules))
33
34 ;;; Commentary:
35 ;;;
36 ;;; This composite module re-exports core parts the (gnu …) public modules.
37 ;;;
38 ;;; Code:
39
40 (eval-when (eval load compile)
41 (begin
42 (define %public-modules
43 '((gnu system)
44 (gnu system mapped-devices)
45 (gnu system file-systems)
46 (gnu bootloader)
47 (gnu bootloader grub)
48 (gnu system pam)
49 (gnu system shadow) ; 'user-account'
50 (gnu system linux-initrd)
51 (gnu system nss)
52 (gnu services)
53 (gnu services base)
54 (gnu packages)
55 (gnu packages base)
56 (guix gexp))) ; so gexps can be used
57
58 (for-each (let ((i (module-public-interface (current-module))))
59 (lambda (m)
60 (module-use! i (resolve-interface m))))
61 %public-modules)))
62
63 (define (%try-use-modules modules location make-hint)
64 "Attempt to load all of MODULES. Report errors as coming from LOCATION, a
65 <location> record, and use MAKE-HINT to produce a fix hint."
66 (define (location->string loc)
67 (match loc
68 (#f "")
69 (($ <location> file line column)
70 (format #f "~a:~a:~a: " file line column))))
71
72 (for-each (lambda (module)
73 (catch 'misc-error
74 (lambda ()
75 (process-use-modules `((,module))))
76 (lambda _
77 (raise
78 (apply
79 make-compound-condition
80 (condition
81 (&message
82 (message (format #f (G_ "module ~a not found")
83 module))))
84 (condition
85 (&error-location (location location)))
86 (or (and=> (make-hint module) list)
87 '()))))))
88 modules))
89
90 (define (package-module-hint module)
91 (define last-name
92 (match module
93 ((_ ... last)
94 (symbol->string last))))
95
96 (match (find-packages-by-name last-name)
97 (()
98 (condition
99 (&fix-hint
100 (hint (G_ "\
101 You may use @command{guix package --show=foo | grep location} to search
102 for the location of package @code{foo}.
103 If you get the line @code{location: gnu/packages/bar.scm:174:2},
104 add @code{bar} to the @code{use-package-modules} form.")))))
105 ((package _ ...)
106 (condition
107 (&fix-hint
108 (hint (format #f (G_ "\
109 Try adding @code{(use-package-modules ~a)}.")
110 (basename (location-file (package-location package))
111 ".scm"))))))))
112
113 (define (service-module-hint module)
114 (define last-name
115 (match module
116 ((_ ... last)
117 last)))
118
119 (match (lookup-service-types last-name)
120 (()
121 (condition
122 (&fix-hint
123 (hint (format #f (G_ "\
124 You may use @command{guix system search ~a} to search for a service
125 matching @code{~a}.
126 If you get the line @code{location: gnu/services/foo.scm:188:2},
127 add @code{foo} to the @code{use-service-modules} form.")
128 last-name last-name)))))
129 ((package _ ...)
130 (condition
131 (&fix-hint
132 (hint (format #f (G_ "\
133 Try adding @code{(use-service-modules ~a)}.")
134 (basename (location-file (service-type-location package))
135 ".scm"))))))))
136
137 (define-syntax-rule (try-use-modules hint modules ...)
138 (eval-when (expand load eval)
139 (%try-use-modules '(modules ...)
140 (source-properties->location
141 (current-source-location))
142 hint)))
143
144 (define-syntax-rule (use-package-modules module ...)
145 (try-use-modules package-module-hint
146 (gnu packages module) ...))
147
148 (define-syntax-rule (use-service-modules module ...)
149 (try-use-modules service-module-hint
150 (gnu services module) ...))
151
152 (define-syntax-rule (use-system-modules module ...)
153 (try-use-modules (const #f) ;no hint
154 (gnu system module) ...))
155
156 ;;; gnu.scm ends here