search-paths.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  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. (define-module (guix search-paths)
  19. #:use-module (guix records)
  20. #:use-module (guix build utils)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-26)
  23. #:use-module (ice-9 match)
  24. #:export (<search-path-specification>
  25. search-path-specification
  26. search-path-specification?
  27. search-path-specification-variable
  28. search-path-specification-files
  29. search-path-specification-separator
  30. search-path-specification-file-type
  31. search-path-specification-file-pattern
  32. $PATH
  33. search-path-specification->sexp
  34. sexp->search-path-specification
  35. string-tokenize*
  36. evaluate-search-paths
  37. environment-variable-definition
  38. search-path-definition
  39. set-search-paths))
  40. ;;; Commentary:
  41. ;;;
  42. ;;; This module defines "search path specifications", which allow packages to
  43. ;;; declare environment variables that they use to define search paths. For
  44. ;;; instance, GCC has the 'CPATH' variable, Guile has the 'GUILE_LOAD_PATH'
  45. ;;; variable, etc.
  46. ;;;
  47. ;;; Code:
  48. ;; The specification of a search path.
  49. (define-record-type* <search-path-specification>
  50. search-path-specification make-search-path-specification
  51. search-path-specification?
  52. (variable search-path-specification-variable) ;string
  53. (files search-path-specification-files) ;list of strings
  54. (separator search-path-specification-separator ;string | #f
  55. (default ":"))
  56. (file-type search-path-specification-file-type ;symbol
  57. (default 'directory))
  58. (file-pattern search-path-specification-file-pattern ;#f | string
  59. (default #f)))
  60. (define $PATH
  61. ;; The 'PATH' variable. This variable is a bit special: it is not attached
  62. ;; to any package in particular.
  63. (search-path-specification
  64. (variable "PATH")
  65. (files '("bin" "sbin"))))
  66. (define (search-path-specification->sexp spec)
  67. "Return an sexp representing SPEC, a <search-path-specification>. The sexp
  68. corresponds to the arguments expected by `set-path-environment-variable'."
  69. ;; Note that this sexp format is used both by build systems and in
  70. ;; (guix profiles), so think twice before you change it.
  71. (match spec
  72. (($ <search-path-specification> variable files separator type pattern)
  73. `(,variable ,files ,separator ,type ,pattern))))
  74. (define (sexp->search-path-specification sexp)
  75. "Convert SEXP, which is as returned by 'search-path-specification->sexp', to
  76. a <search-path-specification> object."
  77. (match sexp
  78. ((variable files separator type pattern)
  79. (search-path-specification
  80. (variable variable)
  81. (files files)
  82. (separator separator)
  83. (file-type type)
  84. (file-pattern pattern)))))
  85. (define-syntax-rule (with-null-error-port exp)
  86. "Evaluate EXP with the error port pointing to the bit bucket."
  87. (with-error-to-port (%make-void-port "w")
  88. (lambda () exp)))
  89. ;; XXX: This procedure used to be in (guix utils) but since we want to be able
  90. ;; to use (guix search-paths) on the build side, we want to avoid the
  91. ;; dependency on (guix utils), and so this procedure is back here for now.
  92. (define (string-tokenize* string separator)
  93. "Return the list of substrings of STRING separated by SEPARATOR. This is
  94. like `string-tokenize', but SEPARATOR is a string."
  95. (define (index string what)
  96. (let loop ((string string)
  97. (offset 0))
  98. (cond ((string-null? string)
  99. #f)
  100. ((string-prefix? what string)
  101. offset)
  102. (else
  103. (loop (string-drop string 1) (+ 1 offset))))))
  104. (define len
  105. (string-length separator))
  106. (let loop ((string string)
  107. (result '()))
  108. (cond ((index string separator)
  109. =>
  110. (lambda (offset)
  111. (loop (string-drop string (+ offset len))
  112. (cons (substring string 0 offset)
  113. result))))
  114. (else
  115. (reverse (cons string result))))))
  116. (define* (evaluate-search-paths search-paths directories
  117. #:optional (getenv (const #f)))
  118. "Evaluate SEARCH-PATHS, a list of search-path specifications, for
  119. DIRECTORIES, a list of directory names, and return a list of
  120. specification/value pairs. Use GETENV to determine the current settings and
  121. report only settings not already effective."
  122. (define (search-path-definition spec)
  123. (match spec
  124. (($ <search-path-specification> variable files #f type pattern)
  125. ;; Separator is #f so return the first match.
  126. (match (with-null-error-port
  127. (search-path-as-list files directories
  128. #:type type
  129. #:pattern pattern))
  130. (()
  131. #f)
  132. ((head . _)
  133. (let ((value (getenv variable)))
  134. (if (and value (string=? value head))
  135. #f ;VARIABLE already set appropriately
  136. (cons spec head))))))
  137. (($ <search-path-specification> variable files separator
  138. type pattern)
  139. (let* ((values (or (and=> (getenv variable)
  140. (cut string-tokenize* <> separator))
  141. '()))
  142. ;; XXX: Silence 'find-files' when it stumbles upon non-existent
  143. ;; directories (see
  144. ;; <http://lists.gnu.org/archive/html/guix-devel/2015-01/msg00269.html>.)
  145. (path (with-null-error-port
  146. (search-path-as-list files directories
  147. #:type type
  148. #:pattern pattern))))
  149. (if (every (cut member <> values) path)
  150. #f ;VARIABLE is already set appropriately
  151. (cons spec (string-join path separator)))))))
  152. (filter-map search-path-definition search-paths))
  153. (define* (environment-variable-definition variable value
  154. #:key
  155. (kind 'exact)
  156. (separator ":"))
  157. "Return a the definition of VARIABLE to VALUE in Bash syntax.
  158. KIND can be either 'exact (return the definition of VARIABLE=VALUE),
  159. 'prefix (return the definition where VALUE is added as a prefix to VARIABLE's
  160. current value), or 'suffix (return the definition where VALUE is added as a
  161. suffix to VARIABLE's current value.) In the case of 'prefix and 'suffix,
  162. SEPARATOR is used as the separator between VARIABLE's current value and its
  163. prefix/suffix."
  164. (match (if (not separator) 'exact kind)
  165. ('exact
  166. (format #f "export ~a=\"~a\"" variable value))
  167. ('prefix
  168. (format #f "export ~a=\"~a${~a:+~a}$~a\""
  169. variable value variable separator variable))
  170. ('suffix
  171. (format #f "export ~a=\"$~a${~a:+~a}~a\""
  172. variable variable variable separator value))))
  173. (define* (search-path-definition search-path value
  174. #:key (kind 'exact))
  175. "Similar to 'environment-variable-definition', but applied to a
  176. <search-path-specification>."
  177. (match search-path
  178. (($ <search-path-specification> variable _ separator)
  179. (environment-variable-definition variable value
  180. #:kind kind
  181. #:separator separator))))
  182. (define* (set-search-paths search-paths directories
  183. #:key (setenv setenv))
  184. "Set the search path environment variables specified by SEARCH-PATHS for the
  185. given directories."
  186. (for-each (match-lambda
  187. ((spec . value)
  188. (setenv (search-path-specification-variable spec)
  189. value)))
  190. (evaluate-search-paths search-paths directories)))
  191. ;;; search-paths.scm ends here