services: mate-desktop: Deprecate the 'mate-desktop-service' procedure.
[jackhill/guix/guix.git] / guix / describe.scm
CommitLineData
fe634eaf 1;;; GNU Guix --- Functional package management for GNU
2cb658a9 2;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
fe634eaf
LC
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
19(define-module (guix describe)
20 #:use-module (guix memoization)
21 #:use-module (guix profiles)
2cb658a9
LC
22 #:use-module (guix packages)
23 #:use-module ((guix utils) #:select (location-file))
24 #:use-module ((guix store) #:select (%store-prefix))
fe634eaf
LC
25 #:use-module (srfi srfi-1)
26 #:use-module (ice-9 match)
bd747018
LC
27 #:export (current-profile
28 current-profile-entries
2cb658a9
LC
29 package-path-entries
30
31 package-provenance))
fe634eaf
LC
32
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
43(define current-profile
44 (mlambda ()
45 "Return the profile (created by 'guix pull') the calling process lives in,
46or #f if this is not applicable."
47 (match (command-line)
48 ((program . _)
49 (and (string-suffix? "/bin/guix" program)
50 ;; Note: We want to do _lexical dot-dot resolution_. Using ".."
51 ;; for real would instead take us into the /gnu/store directory
52 ;; that ~/.config/guix/current/bin points to, whereas we want to
53 ;; obtain ~/.config/guix/current.
54 (let ((candidate (dirname (dirname program))))
55 (and (file-exists? (string-append candidate "/manifest"))
56 candidate)))))))
57
58(define current-profile-entries
59 (mlambda ()
60 "Return the list of entries in the 'guix pull' profile the calling process
61lives in, or #f if this is not applicable."
62 (match (current-profile)
63 (#f '())
64 (profile
65 (let ((manifest (profile-manifest profile)))
66 (manifest-entries manifest))))))
67
68(define package-path-entries
69 (mlambda ()
70 "Return a list of package path entries to be added to the package search
71path. These entries are taken from the 'guix pull' profile the calling
72process lives in, when applicable."
73 ;; Filter out Guix itself.
74 (filter-map (lambda (entry)
75 (and (not (string=? (manifest-entry-name entry)
76 "guix"))
77 (string-append (manifest-entry-item entry)
78 "/share/guile/site/"
79 (effective-version))))
80 (current-profile-entries))))
2cb658a9
LC
81
82(define (package-provenance package)
83 "Return the provenance of PACKAGE as an sexp for use as the 'provenance'
84property of manifest entries, or #f if it could not be determined."
85 (define (entry-source entry)
86 (match (assq 'source
87 (manifest-entry-properties entry))
88 (('source value) value)
89 (_ #f)))
90
91 (match (and=> (package-location package) location-file)
92 (#f #f)
93 (file
94 (let ((file (if (string-prefix? "/" file)
95 file
96 (search-path %load-path file))))
97 (and file
98 (string-prefix? (%store-prefix) file)
99
100 ;; Always store information about the 'guix' channel and
101 ;; optionally about the specific channel FILE comes from.
102 (or (let ((main (and=> (find (lambda (entry)
103 (string=? "guix"
104 (manifest-entry-name entry)))
105 (current-profile-entries))
106 entry-source))
107 (extra (any (lambda (entry)
108 (let ((item (manifest-entry-item entry)))
109 (and (string-prefix? item file)
110 (entry-source entry))))
111 (current-profile-entries))))
112 (and main
113 `(,main
114 ,@(if extra (list extra) '()))))))))))