describe.scm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019, 2020 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 initial-program-arguments
  43. ;; Save the initial program arguments. This allows us to see the "real"
  44. ;; 'guix' program, even if 'guix repl -s' calls 'set-program-arguments'
  45. ;; later on.
  46. (program-arguments))
  47. (define current-profile
  48. (mlambda ()
  49. "Return the profile (created by 'guix pull') the calling process lives in,
  50. or #f if this is not applicable."
  51. (match initial-program-arguments
  52. ((program . _)
  53. (and (string-suffix? "/bin/guix" program)
  54. ;; Note: We want to do _lexical dot-dot resolution_. Using ".."
  55. ;; for real would instead take us into the /gnu/store directory
  56. ;; that ~/.config/guix/current/bin points to, whereas we want to
  57. ;; obtain ~/.config/guix/current.
  58. (let ((candidate (dirname (dirname program))))
  59. (and (file-exists? (string-append candidate "/manifest"))
  60. candidate)))))))
  61. (define (current-profile-date)
  62. "Return the creation date of the current profile (produced by 'guix pull'),
  63. as a number of seconds since the Epoch, or #f if it could not be determined."
  64. ;; Normally 'current-profile' will return ~/.config/guix/current. We need
  65. ;; to 'readlink' once to get '/var/guix/…/guix-profile', whose mtime is the
  66. ;; piece of information we're looking for.
  67. (let loop ((profile (current-profile)))
  68. (match profile
  69. (#f #f)
  70. ((? store-path?) #f)
  71. (file
  72. (if (string-prefix? %state-directory file)
  73. (and=> (lstat file) stat:mtime)
  74. (catch 'system-error
  75. (lambda ()
  76. (let ((target (readlink file)))
  77. (loop (if (string-prefix? "/" target)
  78. target
  79. (string-append (dirname file) "/" target)))))
  80. (const #f)))))))
  81. (define current-profile-entries
  82. (mlambda ()
  83. "Return the list of entries in the 'guix pull' profile the calling process
  84. lives in, or #f if this is not applicable."
  85. (match (current-profile)
  86. (#f '())
  87. (profile
  88. (let ((manifest (profile-manifest profile)))
  89. (manifest-entries manifest))))))
  90. (define current-channel-entries
  91. (mlambda ()
  92. "Return manifest entries corresponding to extra channels--i.e., not the
  93. 'guix' channel."
  94. (remove (lambda (entry)
  95. (string=? (manifest-entry-name entry) "guix"))
  96. (current-profile-entries))))
  97. (define (package-path-entries)
  98. "Return two values: the list of package path entries to be added to the
  99. package search path, and the list to be added to %LOAD-COMPILED-PATH. These
  100. entries are taken from the 'guix pull' profile the calling process lives in,
  101. when applicable."
  102. ;; Filter out Guix itself.
  103. (unzip2 (map (lambda (entry)
  104. (list (string-append (manifest-entry-item entry)
  105. "/share/guile/site/"
  106. (effective-version))
  107. (string-append (manifest-entry-item entry)
  108. "/lib/guile/" (effective-version)
  109. "/site-ccache")))
  110. (current-channel-entries))))
  111. (define (package-provenance package)
  112. "Return the provenance of PACKAGE as an sexp for use as the 'provenance'
  113. property of manifest entries, or #f if it could not be determined."
  114. (define (entry-source entry)
  115. (match (assq 'source
  116. (manifest-entry-properties entry))
  117. (('source value) value)
  118. (_ #f)))
  119. (match (and=> (package-location package) location-file)
  120. (#f #f)
  121. (file
  122. (let ((file (if (string-prefix? "/" file)
  123. file
  124. (search-path %load-path file))))
  125. (and file
  126. (string-prefix? (%store-prefix) file)
  127. ;; Always store information about the 'guix' channel and
  128. ;; optionally about the specific channel FILE comes from.
  129. (or (let ((main (and=> (find (lambda (entry)
  130. (string=? "guix"
  131. (manifest-entry-name entry)))
  132. (current-profile-entries))
  133. entry-source))
  134. (extra (any (lambda (entry)
  135. (let ((item (manifest-entry-item entry)))
  136. (and (string-prefix? item file)
  137. (entry-source entry))))
  138. (current-profile-entries))))
  139. (and main
  140. `(,main
  141. ,@(if extra (list extra) '()))))))))))
  142. (define (manifest-entry-with-provenance entry)
  143. "Return ENTRY with an additional 'provenance' property if it's not already
  144. there."
  145. (let ((properties (manifest-entry-properties entry)))
  146. (if (assq 'properties properties)
  147. entry
  148. (let ((item (manifest-entry-item entry)))
  149. (manifest-entry
  150. (inherit entry)
  151. (properties
  152. (match (and (package? item) (package-provenance item))
  153. (#f properties)
  154. (sexp `((provenance ,@sexp)
  155. ,@properties)))))))))