gnu.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019 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. (define-module (gnu)
  21. #:use-module (guix i18n)
  22. #:use-module (guix utils)
  23. #:use-module (srfi srfi-34)
  24. #:use-module (srfi srfi-35)
  25. #:use-module (ice-9 match)
  26. #:use-module (guix packages)
  27. #:use-module (gnu packages)
  28. #:use-module (gnu services)
  29. #:export (use-package-modules
  30. use-service-modules
  31. use-system-modules))
  32. ;;; Commentary:
  33. ;;;
  34. ;;; This composite module re-exports core parts the (gnu …) public modules.
  35. ;;;
  36. ;;; Code:
  37. (eval-when (eval load compile)
  38. (begin
  39. (define %public-modules
  40. '((gnu system)
  41. (gnu system mapped-devices)
  42. (gnu system file-systems)
  43. (gnu bootloader)
  44. (gnu bootloader grub)
  45. (gnu system keyboard)
  46. (gnu system pam)
  47. (gnu system shadow) ; 'user-account'
  48. (gnu system linux-initrd)
  49. (gnu system nss)
  50. (gnu services)
  51. (gnu services base)
  52. (gnu packages)
  53. (gnu packages base)
  54. (guix gexp))) ; so gexps can be used
  55. (for-each (let ((i (module-public-interface (current-module))))
  56. (lambda (m)
  57. (module-use! i (resolve-interface m))))
  58. %public-modules)))
  59. (define (%try-use-modules modules location make-hint)
  60. "Attempt to load all of MODULES. Report errors as coming from LOCATION, a
  61. <location> record, and use MAKE-HINT to produce a fix hint."
  62. (define (location->string loc)
  63. (match loc
  64. (#f "")
  65. (($ <location> file line column)
  66. (format #f "~a:~a:~a: " file line column))))
  67. (for-each (lambda (module)
  68. (catch 'misc-error
  69. (lambda ()
  70. (process-use-modules `((,module))))
  71. (lambda _
  72. (raise
  73. (apply
  74. make-compound-condition
  75. (condition
  76. (&message
  77. (message (format #f (G_ "module ~a not found")
  78. module))))
  79. (condition
  80. (&error-location (location location)))
  81. (or (and=> (make-hint module) list)
  82. '()))))))
  83. modules))
  84. (define (package-module-hint module)
  85. (define last-name
  86. (match module
  87. ((_ ... last)
  88. (symbol->string last))))
  89. (match (find-packages-by-name last-name)
  90. (()
  91. (condition
  92. (&fix-hint
  93. (hint (G_ "\
  94. You may use @command{guix package --show=foo | grep location} to search
  95. for the location of package @code{foo}.
  96. If you get the line @code{location: gnu/packages/bar.scm:174:2},
  97. add @code{bar} to the @code{use-package-modules} form.")))))
  98. ((package _ ...)
  99. (condition
  100. (&fix-hint
  101. (hint (format #f (G_ "\
  102. Try adding @code{(use-package-modules ~a)}.")
  103. (basename (location-file (package-location package))
  104. ".scm"))))))))
  105. (define (service-module-hint module)
  106. (define last-name
  107. (match module
  108. ((_ ... last)
  109. last)))
  110. (match (lookup-service-types last-name)
  111. (()
  112. (condition
  113. (&fix-hint
  114. (hint (format #f (G_ "\
  115. You may use @command{guix system search ~a} to search for a service
  116. matching @code{~a}.
  117. If you get the line @code{location: gnu/services/foo.scm:188:2},
  118. add @code{foo} to the @code{use-service-modules} form.")
  119. last-name last-name)))))
  120. ((package _ ...)
  121. (condition
  122. (&fix-hint
  123. (hint (format #f (G_ "\
  124. Try adding @code{(use-service-modules ~a)}.")
  125. (basename (location-file (service-type-location package))
  126. ".scm"))))))))
  127. (define-syntax-rule (try-use-modules hint modules ...)
  128. (eval-when (expand load eval)
  129. (%try-use-modules '(modules ...)
  130. (source-properties->location
  131. (current-source-location))
  132. hint)))
  133. (define-syntax-rule (use-package-modules module ...)
  134. (try-use-modules package-module-hint
  135. (gnu packages module) ...))
  136. (define-syntax-rule (use-service-modules module ...)
  137. (try-use-modules service-module-hint
  138. (gnu services module) ...))
  139. (define-syntax-rule (use-system-modules module ...)
  140. (try-use-modules (const #f) ;no hint
  141. (gnu system module) ...))
  142. ;;; gnu.scm ends here