gnu: Add emacs-exec-path-from-shell.
[jackhill/guix/guix.git] / guix / scripts.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Deck Pickard <deck.r.pickard@gmail.com>
4 ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
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)
22 #:use-module (guix grafts)
23 #:use-module (guix utils)
24 #:use-module (guix ui)
25 #:use-module (guix store)
26 #:use-module (guix monads)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-19)
31 #:use-module (srfi srfi-37)
32 #:use-module (ice-9 match)
33 #:export (args-fold*
34 parse-command-line
35 maybe-build
36 build-package
37 build-package-source
38 %distro-age-warning
39 warn-about-old-distro))
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
49 reporting."
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.
56 (leave (G_ "invalid argument: ~a~%")
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
70 (build-options? #t)
71 (argument-handler %default-argument-handler))
72 "Parse the command-line arguments ARGS according to OPTIONS (a list of
73 SRFI-37 options) and return the result, seeded by SEEDS. When BUILD-OPTIONS?
74 is true, also pass arguments passed via the 'GUIX_BUILD_OPTIONS' environment
75 variable. Command-line options take precedence those passed via
76 'GUIX_BUILD_OPTIONS'.
77
78 ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
79 parameter 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)
84 (leave (G_ "~A: unrecognized option~%") name))
85 argument-handler
86 seeds))
87
88 (call-with-values
89 (lambda ()
90 (if build-options?
91 (parse-options-from (environment-build-options) seeds)
92 (apply values seeds)))
93 (lambda seeds
94 ;; ARGS take precedence over what the environment variable specifies.
95 (parse-options-from args seeds))))
96
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
100 true."
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'.
115 Show what and how will/would be built."
116 (mlet %store-monad ((grafting? ((lift0 %graft? %store-monad))))
117 (apply set-build-options*
118 #:use-substitutes? use-substitutes?
119 (strip-keyword-arguments '(#:dry-run?) build-options))
120 (mlet %store-monad ((derivation (package->derivation
121 package #:graft? (and (not dry-run?)
122 grafting?))))
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
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
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
172 (lstat (string-append (config-directory #:ensure? #f)
173 "/latest")))
174 (#f #f)
175 (stat (- (time-second (current-time time-utc))
176 (stat:mtime stat)))))
177
178 (when (and age (>= age old))
179 (warning (N_ "Your Guix installation is ~a day old.\n"
180 "Your Guix installation is ~a days old.\n"
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
189 ;;; scripts.scm ends here