1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix scripts describe)
21 #:use-module ((guix ui) #:hide (display-profile-content))
22 #:use-module (guix channels)
23 #:use-module (guix scripts)
24 #:use-module (guix describe)
25 #:use-module (guix profiles)
26 #:use-module ((guix scripts pull) #:select (display-profile-content))
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-37)
31 #:use-module (ice-9 match)
32 #:autoload (ice-9 pretty-print) (pretty-print)
33 #:export (guix-describe))
37 ;;; Command-line options.
41 ;; Specifications of the command-line options.
42 (list (option '(#\f "format") #t #f
43 (lambda (opt name arg result)
44 (unless (member arg '("human" "channels" "json" "recutils"))
45 (leave (G_ "~a: unsupported output format~%") arg))
46 (alist-cons 'format (string->symbol arg) result)))
47 (option '(#\p "profile") #t #f
48 (lambda (opt name arg result)
49 (alist-cons 'profile (canonicalize-profile arg)
51 (option '(#\h "help") #f #f
55 (option '(#\V "version") #f #f
57 (show-version-and-exit "guix describe")))))
59 (define %default-options
60 ;; Alist of default option values.
64 (display (G_ "Usage: guix describe [OPTION]...
65 Display information about the channels currently in use.\n"))
67 -f, --format=FORMAT display information in the given FORMAT"))
69 -p, --profile=PROFILE display information about PROFILE"))
72 -h, --help display this help and exit"))
74 -V, --version display version information and exit"))
76 (show-bug-report-information))
78 (define (display-package-search-path fmt)
79 "Display GUIX_PACKAGE_PATH, if it is set, according to FMT."
80 (match (getenv "GUIX_PACKAGE_PATH")
85 (format #t "~%GUIX_PACKAGE_PATH=\"~a\"~%" string))
87 (format #t (G_ "~%;; warning: GUIX_PACKAGE_PATH=\"~a\"~%")
90 (warning (G_ "'GUIX_PACKAGE_PATH' is set but it is not captured~%")))))))
92 (define (channel->sexp channel)
94 (name ',(channel-name channel))
95 (url ,(channel-url channel))
96 (commit ,(channel-commit channel))))
98 (define (channel->json channel)
99 (scm->json-string `((name . ,(channel-name channel))
100 (url . ,(channel-url channel))
101 (commit . ,(channel-commit channel)))))
103 (define (channel->recutils channel port)
104 (format port "name: ~a~%" (channel-name channel))
105 (format port "url: ~a~%" (channel-url channel))
106 (format port "commit: ~a~%" (channel-commit channel)))
108 (define (display-checkout-info fmt)
109 "Display information about the current checkout according to FMT, a symbol
110 denoting the requested format. Exit if the current directory does not lie
111 within a Git checkout."
112 (let* ((program (car (command-line)))
113 (directory (catch 'git-error
115 (repository-discover (dirname program)))
117 (leave (G_ "failed to determine origin~%")))))
118 (repository (repository-open directory))
119 (head (repository-head repository))
120 (commit (oid->string (reference-target head))))
123 (format #t (G_ "Git checkout:~%"))
124 (format #t (G_ " repository: ~a~%") (dirname directory))
125 (format #t (G_ " branch: ~a~%") (reference-shorthand head))
126 (format #t (G_ " commit: ~a~%") commit))
128 (pretty-print `(list ,(channel->sexp (channel (name 'guix)
129 (url (dirname directory))
132 (display (channel->json (channel (name 'guix)
133 (url (dirname directory))
137 (channel->recutils (channel (name 'guix)
138 (url (dirname directory))
140 (current-output-port))))
141 (display-package-search-path fmt)))
143 (define (display-profile-info profile fmt)
144 "Display information about PROFILE, a profile as created by (guix channels),
145 in the format specified by FMT."
147 (generation-number profile))
151 (match (assq 'source (manifest-entry-properties entry))
152 (('source ('repository ('version 0)
157 (channel (name (string->symbol (manifest-entry-name entry)))
161 ;; Pre-0.15.0 Guix does not provide that information,
162 ;; so there's not much we can do in that case.
163 (_ (channel (name 'guix)
167 ;; Show most recently installed packages last.
173 (generation-file-name profile number)))))))
177 (display-profile-content profile number))
179 (pretty-print `(list ,@(map channel->sexp channels))))
181 (format #t "[~a]~%" (string-join (map channel->json channels) ",")))
183 (format #t "~{~a~%~}"
184 (map (lambda (channel)
185 (with-output-to-string
187 (channel->recutils channel (current-output-port)))))
189 (display-package-search-path fmt))
196 (define (guix-describe . args)
197 (let* ((opts (args-fold* args %options
198 (lambda (opt name arg result)
199 (leave (G_ "~A: unrecognized option~%")
203 (format (assq-ref opts 'format))
204 (profile (or (assq-ref opts 'profile) (current-profile))))
208 (display-checkout-info format))
210 (display-profile-info (canonicalize-profile profile) format))))))