profiles: Generalize 'canonicalize-profile'.
[jackhill/guix/guix.git] / guix / scripts.scm
CommitLineData
88981dd3 1;;; GNU Guix --- Functional package management for GNU
75e24d7b 2;;; Copyright © 2013, 2014, 2015, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
88981dd3 3;;; Copyright © 2014 Deck Pickard <deck.r.pickard@gmail.com>
a82a201a 4;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
88981dd3
AK
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (guix scripts)
a82a201a 22 #:use-module (guix grafts)
88981dd3
AK
23 #:use-module (guix utils)
24 #:use-module (guix ui)
430505eb
AK
25 #:use-module (guix store)
26 #:use-module (guix monads)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
88981dd3 29 #:use-module (srfi srfi-1)
7fd952e0 30 #:use-module (srfi srfi-19)
88981dd3
AK
31 #:use-module (srfi srfi-37)
32 #:use-module (ice-9 match)
33 #:export (args-fold*
430505eb
AK
34 parse-command-line
35 maybe-build
ad8b83bd 36 build-package
7fd952e0
LC
37 build-package-source
38 %distro-age-warning
39 warn-about-old-distro))
88981dd3
AK
40
41;;; Commentary:
42;;;
43;;; General code for Guix scripts.
44;;;
45;;; Code:
46
47(define (args-fold* options unrecognized-option-proc operand-proc . seeds)
48 "A wrapper on top of `args-fold' that does proper user-facing error
49reporting."
50 (catch 'misc-error
51 (lambda ()
52 (apply args-fold options unrecognized-option-proc
53 operand-proc seeds))
54 (lambda (key proc msg args . rest)
55 ;; XXX: MSG is not i18n'd.
69daee23 56 (leave (G_ "invalid argument: ~a~%")
88981dd3
AK
57 (apply format #f msg args)))))
58
59(define (environment-build-options)
60 "Return additional build options passed as environment variables."
61 (arguments-from-environment-variable "GUIX_BUILD_OPTIONS"))
62
63(define %default-argument-handler
64 ;; The default handler for non-option command-line arguments.
65 (lambda (arg result)
66 (alist-cons 'argument arg result)))
67
68(define* (parse-command-line args options seeds
69 #:key
a1ff7e1d 70 (build-options? #t)
88981dd3 71 (argument-handler %default-argument-handler))
a1ff7e1d
LC
72 "Parse the command-line arguments ARGS according to OPTIONS (a list of
73SRFI-37 options) and return the result, seeded by SEEDS. When BUILD-OPTIONS?
74is true, also pass arguments passed via the 'GUIX_BUILD_OPTIONS' environment
75variable. Command-line options take precedence those passed via
76'GUIX_BUILD_OPTIONS'.
88981dd3
AK
77
78ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
79parameter of 'args-fold'."
80 (define (parse-options-from args seeds)
81 ;; Actual parsing takes place here.
82 (apply args-fold* args options
83 (lambda (opt name arg . rest)
69daee23 84 (leave (G_ "~A: unrecognized option~%") name))
88981dd3
AK
85 argument-handler
86 seeds))
87
88 (call-with-values
89 (lambda ()
a1ff7e1d
LC
90 (if build-options?
91 (parse-options-from (environment-build-options) seeds)
92 (apply values seeds)))
88981dd3
AK
93 (lambda seeds
94 ;; ARGS take precedence over what the environment variable specifies.
95 (parse-options-from args seeds))))
96
430505eb
AK
97(define* (maybe-build drvs
98 #:key dry-run? use-substitutes?)
99 "Show what will/would be built, and actually build DRVS, unless DRY-RUN? is
100true."
101 (with-monad %store-monad
102 (>>= (show-what-to-build* drvs
103 #:dry-run? dry-run?
104 #:use-substitutes? use-substitutes?)
105 (lambda (_)
106 (if dry-run?
107 (return #f)
108 (built-derivations drvs))))))
109
110(define* (build-package package
111 #:key dry-run? (use-substitutes? #t)
112 #:allow-other-keys
113 #:rest build-options)
114 "Build PACKAGE using BUILD-OPTIONS acceptable by 'set-build-options'.
115Show what and how will/would be built."
a82a201a 116 (mlet %store-monad ((grafting? ((lift0 %graft? %store-monad))))
430505eb
AK
117 (apply set-build-options*
118 #:use-substitutes? use-substitutes?
119 (strip-keyword-arguments '(#:dry-run?) build-options))
a82a201a
AK
120 (mlet %store-monad ((derivation (package->derivation
121 package #:graft? (and (not dry-run?)
122 grafting?))))
430505eb
AK
123 (mbegin %store-monad
124 (maybe-build (list derivation)
125 #:use-substitutes? use-substitutes?
126 #:dry-run? dry-run?)
127 (return (show-derivation-outputs derivation))))))
128
ad8b83bd
AK
129(define* (build-package-source package
130 #:key dry-run? (use-substitutes? #t)
131 #:allow-other-keys
132 #:rest build-options)
133 "Build PACKAGE source using BUILD-OPTIONS."
134 (mbegin %store-monad
135 (apply set-build-options*
136 #:use-substitutes? use-substitutes?
137 (strip-keyword-arguments '(#:dry-run?) build-options))
138 (mlet %store-monad ((derivation (origin->derivation
139 (package-source package))))
140 (mbegin %store-monad
141 (maybe-build (list derivation)
142 #:use-substitutes? use-substitutes?
143 #:dry-run? dry-run?)
144 (return (show-derivation-outputs derivation))))))
145
7fd952e0
LC
146(define %distro-age-warning
147 ;; The age (in seconds) above which we warn that the distro is too old.
148 (make-parameter (match (and=> (getenv "GUIX_DISTRO_AGE_WARNING")
149 string->duration)
150 (#f (* 7 24 3600))
151 (age (time-second age)))))
152
153(define* (warn-about-old-distro #:optional (old (%distro-age-warning))
154 #:key (suggested-command
155 "guix package -u"))
156 "Emit a warning if Guix is older than OLD seconds."
157 (let-syntax ((false-if-not-found
158 (syntax-rules ()
159 ((_ exp)
160 (catch 'system-error
161 (lambda ()
162 exp)
163 (lambda args
164 (if (= ENOENT (system-error-errno args))
165 #f
166 (apply throw args))))))))
167 (define (seconds->days seconds)
168 (round (/ seconds (* 3600 24))))
169
170 (define age
171 (match (false-if-not-found
e06ca952 172 (lstat (string-append (config-directory #:ensure? #f)
75e24d7b 173 "/current")))
7fd952e0
LC
174 (#f #f)
175 (stat (- (time-second (current-time time-utc))
176 (stat:mtime stat)))))
177
178 (when (and age (>= age old))
bc0e6c93
LC
179 (warning (N_ "Your Guix installation is ~a day old.\n"
180 "Your Guix installation is ~a days old.\n"
7fd952e0
LC
181 (seconds->days age))
182 (seconds->days age)))
183 (when (or (not age) (>= age old))
184 (warning (G_ "Consider running 'guix pull' followed by
185'~a' to get up-to-date packages and security updates.\n")
186 suggested-command)
187 (newline (guix-warning-port)))))
188
88981dd3 189;;; scripts.scm ends here