gnu.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019, 2020 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 diagnostics)
  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. (formatted-message (G_ "module ~a not found")
  76. module)
  77. (condition
  78. (&error-location (location location)))
  79. (or (and=> (make-hint module) list)
  80. '()))))))
  81. modules))
  82. (define (package-module-hint module)
  83. (define last-name
  84. (match module
  85. ((_ ... last)
  86. (symbol->string last))))
  87. (match (find-packages-by-name last-name)
  88. (()
  89. (condition
  90. (&fix-hint
  91. (hint (G_ "\
  92. You may use @command{guix package --show=foo | grep location} to search
  93. for the location of package @code{foo}.
  94. If you get the line @code{location: gnu/packages/bar.scm:174:2},
  95. add @code{bar} to the @code{use-package-modules} form.")))))
  96. ((package _ ...)
  97. (condition
  98. (&fix-hint
  99. (hint (format #f (G_ "\
  100. Try adding @code{(use-package-modules ~a)}.")
  101. (basename (location-file (package-location package))
  102. ".scm"))))))))
  103. (define (service-module-hint module)
  104. (define last-name
  105. (match module
  106. ((_ ... last)
  107. last)))
  108. (match (lookup-service-types last-name)
  109. (()
  110. (condition
  111. (&fix-hint
  112. (hint (format #f (G_ "\
  113. You may use @command{guix system search ~a} to search for a service
  114. matching @code{~a}.
  115. If you get the line @code{location: gnu/services/foo.scm:188:2},
  116. add @code{foo} to the @code{use-service-modules} form.")
  117. last-name last-name)))))
  118. ((package _ ...)
  119. (condition
  120. (&fix-hint
  121. (hint (format #f (G_ "\
  122. Try adding @code{(use-service-modules ~a)}.")
  123. (basename (location-file (service-type-location package))
  124. ".scm"))))))))
  125. (define-syntax-rule (try-use-modules hint modules ...)
  126. (eval-when (expand load eval)
  127. (%try-use-modules '(modules ...)
  128. (source-properties->location
  129. (current-source-location))
  130. hint)))
  131. (define-syntax-rule (use-package-modules module ...)
  132. (try-use-modules package-module-hint
  133. (gnu packages module) ...))
  134. (define-syntax-rule (use-service-modules module ...)
  135. (try-use-modules service-module-hint
  136. (gnu services module) ...))
  137. (define-syntax-rule (use-system-modules module ...)
  138. (try-use-modules (const #f) ;no hint
  139. (gnu system module) ...))
  140. ;;; gnu.scm ends here