describe.scm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019 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 describe)
  19. #:use-module (guix memoization)
  20. #:use-module (guix profiles)
  21. #:use-module (guix packages)
  22. #:use-module ((guix utils) #:select (location-file))
  23. #:use-module ((guix store) #:select (%store-prefix store-path?))
  24. #:use-module ((guix config) #:select (%state-directory))
  25. #:use-module (srfi srfi-1)
  26. #:use-module (ice-9 match)
  27. #:export (current-profile
  28. current-profile-date
  29. current-profile-entries
  30. package-path-entries
  31. package-provenance
  32. manifest-entry-with-provenance))
  33. ;;; Commentary:
  34. ;;;
  35. ;;; This module provides supporting code to allow a Guix instance to find, at
  36. ;;; run time, which profile it's in (profiles created by 'guix pull'). That
  37. ;;; allows it to read meta-information about itself (e.g., repository URL and
  38. ;;; commit ID) and to find other channels available in the same profile. It's
  39. ;;; a bit like ELPA's pkg-info.el.
  40. ;;;
  41. ;;; Code:
  42. (define current-profile
  43. (mlambda ()
  44. "Return the profile (created by 'guix pull') the calling process lives in,
  45. or #f if this is not applicable."
  46. (match (command-line)
  47. ((program . _)
  48. (and (string-suffix? "/bin/guix" program)
  49. ;; Note: We want to do _lexical dot-dot resolution_. Using ".."
  50. ;; for real would instead take us into the /gnu/store directory
  51. ;; that ~/.config/guix/current/bin points to, whereas we want to
  52. ;; obtain ~/.config/guix/current.
  53. (let ((candidate (dirname (dirname program))))
  54. (and (file-exists? (string-append candidate "/manifest"))
  55. candidate)))))))
  56. (define (current-profile-date)
  57. "Return the creation date of the current profile (produced by 'guix pull'),
  58. as a number of seconds since the Epoch, or #f if it could not be determined."
  59. ;; Normally 'current-profile' will return ~/.config/guix/current. We need
  60. ;; to 'readlink' once to get '/var/guix/…/guix-profile', whose mtime is the
  61. ;; piece of information we're looking for.
  62. (let loop ((profile (current-profile)))
  63. (match profile
  64. (#f #f)
  65. ((? store-path?) #f)
  66. (file
  67. (if (string-prefix? %state-directory file)
  68. (and=> (lstat file) stat:mtime)
  69. (catch 'system-error
  70. (lambda ()
  71. (let ((target (readlink file)))
  72. (loop (if (string-prefix? "/" target)
  73. target
  74. (string-append (dirname file) "/" target)))))
  75. (const #f)))))))
  76. (define current-profile-entries
  77. (mlambda ()
  78. "Return the list of entries in the 'guix pull' profile the calling process
  79. lives in, or #f if this is not applicable."
  80. (match (current-profile)
  81. (#f '())
  82. (profile
  83. (let ((manifest (profile-manifest profile)))
  84. (manifest-entries manifest))))))
  85. (define current-channel-entries
  86. (mlambda ()
  87. "Return manifest entries corresponding to extra channels--i.e., not the
  88. 'guix' channel."
  89. (remove (lambda (entry)
  90. (string=? (manifest-entry-name entry) "guix"))
  91. (current-profile-entries))))
  92. (define (package-path-entries)
  93. "Return two values: the list of package path entries to be added to the
  94. package search path, and the list to be added to %LOAD-COMPILED-PATH. These
  95. entries are taken from the 'guix pull' profile the calling process lives in,
  96. when applicable."
  97. ;; Filter out Guix itself.
  98. (unzip2 (map (lambda (entry)
  99. (list (string-append (manifest-entry-item entry)
  100. "/share/guile/site/"
  101. (effective-version))
  102. (string-append (manifest-entry-item entry)
  103. "/lib/guile/" (effective-version)
  104. "/site-ccache")))
  105. (current-channel-entries))))
  106. (define (package-provenance package)
  107. "Return the provenance of PACKAGE as an sexp for use as the 'provenance'
  108. property of manifest entries, or #f if it could not be determined."
  109. (define (entry-source entry)
  110. (match (assq 'source
  111. (manifest-entry-properties entry))
  112. (('source value) value)
  113. (_ #f)))
  114. (match (and=> (package-location package) location-file)
  115. (#f #f)
  116. (file
  117. (let ((file (if (string-prefix? "/" file)
  118. file
  119. (search-path %load-path file))))
  120. (and file
  121. (string-prefix? (%store-prefix) file)
  122. ;; Always store information about the 'guix' channel and
  123. ;; optionally about the specific channel FILE comes from.
  124. (or (let ((main (and=> (find (lambda (entry)
  125. (string=? "guix"
  126. (manifest-entry-name entry)))
  127. (current-profile-entries))
  128. entry-source))
  129. (extra (any (lambda (entry)
  130. (let ((item (manifest-entry-item entry)))
  131. (and (string-prefix? item file)
  132. (entry-source entry))))
  133. (current-profile-entries))))
  134. (and main
  135. `(,main
  136. ,@(if extra (list extra) '()))))))))))
  137. (define (manifest-entry-with-provenance entry)
  138. "Return ENTRY with an additional 'provenance' property if it's not already
  139. there."
  140. (let ((properties (manifest-entry-properties entry)))
  141. (if (assq 'properties properties)
  142. entry
  143. (let ((item (manifest-entry-item entry)))
  144. (manifest-entry
  145. (inherit entry)
  146. (properties
  147. (match (and (package? item) (package-provenance item))
  148. (#f properties)
  149. (sexp `((provenance ,@sexp)
  150. ,@properties)))))))))