1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix describe)
20 #:use-module (guix memoization)
21 #:use-module (guix profiles)
22 #:use-module (guix packages)
23 #:use-module ((guix utils) #:select (location-file))
24 #:use-module ((guix store) #:select (%store-prefix store-path?))
25 #:use-module ((guix config) #:select (%state-directory))
26 #:autoload (guix channels) (channel-name
28 manifest-entry-channel)
29 #:use-module (srfi srfi-1)
30 #:use-module (ice-9 match)
31 #:export (current-profile
33 current-profile-entries
39 manifest-entry-with-provenance
40 manifest-entry-provenance))
44 ;;; This module provides supporting code to allow a Guix instance to find, at
45 ;;; run time, which profile it's in (profiles created by 'guix pull'). That
46 ;;; allows it to read meta-information about itself (e.g., repository URL and
47 ;;; commit ID) and to find other channels available in the same profile. It's
48 ;;; a bit like ELPA's pkg-info.el.
52 (define initial-program-arguments
53 ;; Save the initial program arguments. This allows us to see the "real"
54 ;; 'guix' program, even if 'guix repl -s' calls 'set-program-arguments'
58 (define current-profile
60 "Return the profile (created by 'guix pull') the calling process lives in,
61 or #f if this is not applicable."
62 (match initial-program-arguments
64 (and (string-suffix? "/bin/guix" program)
65 ;; Note: We want to do _lexical dot-dot resolution_. Using ".."
66 ;; for real would instead take us into the /gnu/store directory
67 ;; that ~/.config/guix/current/bin points to, whereas we want to
68 ;; obtain ~/.config/guix/current.
69 (let ((candidate (dirname (dirname program))))
70 (and (file-exists? (string-append candidate "/manifest"))
73 (define (current-profile-date)
74 "Return the creation date of the current profile (produced by 'guix pull'),
75 as a number of seconds since the Epoch, or #f if it could not be determined."
76 ;; Normally 'current-profile' will return ~/.config/guix/current. We need
77 ;; to 'readlink' once to get '/var/guix/…/guix-profile', whose mtime is the
78 ;; piece of information we're looking for.
79 (let loop ((profile (current-profile)))
84 (if (string-prefix? %state-directory file)
85 (and=> (lstat file) stat:mtime)
88 (let ((target (readlink file)))
89 (loop (if (string-prefix? "/" target)
91 (string-append (dirname file) "/" target)))))
94 (define (channel-metadata)
95 "Return the 'guix' channel metadata sexp from (guix config) if available;
97 ;; Older 'build-self.scm' would create a (guix config) file without the
98 ;; '%channel-metadata' variable. Thus, properly deal with a lack of
100 (let ((module (resolve-interface '(guix config))))
101 (and=> (module-variable module '%channel-metadata) variable-ref)))
103 (define current-profile-entries
105 "Return the list of entries in the 'guix pull' profile the calling process
106 lives in, or the empty list if this is not applicable."
107 (match (current-profile)
110 (let ((manifest (profile-manifest profile)))
111 (manifest-entries manifest))))))
113 (define current-channel-entries
115 "Return manifest entries corresponding to extra channels--i.e., not the
117 (remove (lambda (entry)
118 (or (string=? (manifest-entry-name entry) "guix")
120 ;; If ENTRY lacks the 'source' property, it's not an entry
121 ;; from 'guix pull'. See <https://bugs.gnu.org/48778>.
122 (not (assq 'source (manifest-entry-properties entry)))))
123 (current-profile-entries))))
125 (define current-channels
127 "Return the list of channels currently available, including the 'guix'
128 channel. Return the empty list if this information is missing."
129 (define (build-time-metadata)
130 (match (channel-metadata)
132 (sexp (or (and=> (sexp->channel sexp 'guix) list) '()))))
134 (match (current-profile-entries)
136 ;; As a fallback, if we're not running from a profile, use 'guix'
137 ;; channel metadata from (guix config).
138 (build-time-metadata))
140 (match (filter-map manifest-entry-channel entries)
142 ;; This profile lacks provenance metadata, so fall back to
143 ;; build-time metadata as returned by 'channel-metadata'.
144 (build-time-metadata))
148 (define (package-path-entries)
149 "Return two values: the list of package path entries to be added to the
150 package search path, and the list to be added to %LOAD-COMPILED-PATH. These
151 entries are taken from the 'guix pull' profile the calling process lives in,
153 ;; Filter out Guix itself.
154 (unzip2 (map (lambda (entry)
155 (list (string-append (manifest-entry-item entry)
158 (string-append (manifest-entry-item entry)
159 "/lib/guile/" (effective-version)
161 (current-channel-entries))))
163 (define (package-channels package)
164 "Return the list of channels providing PACKAGE or an empty list if it could
166 (match (and=> (package-location package) location-file)
169 (let ((file (if (string-prefix? "/" file)
171 (search-path %load-path file))))
173 (string-prefix? (%store-prefix) file))
176 (let ((item (manifest-entry-item entry)))
177 (and (or (string-prefix? item file)
178 (string=? "guix" (manifest-entry-name entry)))
179 (manifest-entry-channel entry))))
180 (current-profile-entries))
183 (define (package-provenance package)
184 "Return the provenance of PACKAGE as an sexp for use as the 'provenance'
185 property of manifest entries, or #f if it could not be determined."
186 (define (entry-source entry)
188 (manifest-entry-properties entry))
189 (('source value) value)
192 (let* ((channels (package-channels package))
193 (names (map (compose symbol->string channel-name) channels)))
194 ;; Always store information about the 'guix' channel and
195 ;; optionally about the specific channel FILE comes from.
196 (or (let ((main (and=> (find (lambda (entry)
198 (manifest-entry-name entry)))
199 (current-profile-entries))
201 (extra (any (lambda (entry)
202 (let ((item (manifest-entry-item entry))
203 (name (manifest-entry-name entry)))
204 (and (member name names)
205 (not (string=? name "guix"))
206 (entry-source entry))))
207 (current-profile-entries))))
210 ,@(if extra (list extra) '())))))))
212 (define (manifest-entry-with-provenance entry)
213 "Return ENTRY with an additional 'provenance' property if it's not already
215 (let ((properties (manifest-entry-properties entry)))
216 (if (assq 'provenance properties)
218 (let ((item (manifest-entry-item entry)))
222 (match (and (package? item) (package-provenance item))
224 (sexp `((provenance ,@sexp)
225 ,@properties)))))))))
227 (define (manifest-entry-provenance entry)
228 "Return the list of channels ENTRY comes from. Return the empty list if
229 that information is missing."
230 (match (assq-ref (manifest-entry-properties entry) 'provenance)
232 ;; XXX: Until recently, channel sexps lacked the channel name. For
233 ;; entries created by 'manifest-entry-with-provenance', the first sexp
234 ;; is known to be the 'guix channel, and for the other ones, invent a
235 ;; fallback name (it's OK as the name is just a "pet name").
236 (match (sexp->channel main 'guix)
239 (let loop ((extras extras)
241 (channels (list channel)))
246 (let* ((name (string->symbol
247 (format #f "channel~a" counter)))
248 (extra (sexp->channel head name)))
250 (loop tail (+ 1 counter) (cons extra channels))
251 (loop tail counter channels)))))))))