Merge branch 'master' into staging
[jackhill/guix/guix.git] / guix / scripts / archive.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
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 scripts archive)
20 #:use-module (guix config)
21 #:use-module (guix utils)
22 #:use-module (guix combinators)
23 #:use-module ((guix build utils) #:select (mkdir-p))
24 #:use-module ((guix serialization) #:select (restore-file))
25 #:use-module (guix store)
26 #:use-module (guix grafts)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
29 #:use-module (guix monads)
30 #:use-module (guix ui)
31 #:use-module (guix pki)
32 #:use-module (guix pk-crypto)
33 #:use-module (guix scripts)
34 #:use-module (guix scripts build)
35 #:use-module (gnu packages)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:use-module (ice-9 rdelim)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-37)
43 #:use-module (ice-9 binary-ports)
44 #:export (guix-archive
45 options->derivations+files))
46
47 \f
48 ;;;
49 ;;; Command-line options.
50 ;;;
51
52 (define %default-options
53 ;; Alist of default option values.
54 `((system . ,(%current-system))
55 (substitutes? . #t)
56 (graft? . #t)
57 (max-silent-time . 3600)
58 (verbosity . 0)))
59
60 (define (show-help)
61 (display (G_ "Usage: guix archive [OPTION]... PACKAGE...
62 Export/import one or more packages from/to the store.\n"))
63 (display (G_ "
64 --export export the specified files/packages to stdout"))
65 (display (G_ "
66 -r, --recursive combined with '--export', include dependencies"))
67 (display (G_ "
68 --import import from the archive passed on stdin"))
69 (display (G_ "
70 --missing print the files from stdin that are missing"))
71 (display (G_ "
72 -x, --extract=DIR extract the archive on stdin to DIR"))
73 (newline)
74 (display (G_ "
75 --generate-key[=PARAMETERS]
76 generate a key pair with the given parameters"))
77 (display (G_ "
78 --authorize authorize imports signed by the public key on stdin"))
79 (newline)
80 (display (G_ "
81 -e, --expression=EXPR build the package or derivation EXPR evaluates to"))
82 (display (G_ "
83 -S, --source build the packages' source derivations"))
84 (display (G_ "
85 -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
86 (display (G_ "
87 --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
88
89 (newline)
90 (show-build-options-help)
91
92 (newline)
93 (display (G_ "
94 -h, --help display this help and exit"))
95 (display (G_ "
96 -V, --version display version information and exit"))
97 (newline)
98 (show-bug-report-information))
99
100 (define %key-generation-parameters
101 ;; Default key generation parameters. We prefer Ed25519, but it was
102 ;; introduced in libgcrypt 1.6.0.
103 (if (version>? (gcrypt-version) "1.6.0")
104 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))"
105 "(genkey (rsa (nbits 4:4096)))"))
106
107 (define %options
108 ;; Specifications of the command-line options.
109 (cons* (option '(#\h "help") #f #f
110 (lambda args
111 (show-help)
112 (exit 0)))
113 (option '(#\V "version") #f #f
114 (lambda args
115 (show-version-and-exit "guix build")))
116
117 (option '("export") #f #f
118 (lambda (opt name arg result)
119 (alist-cons 'export #t result)))
120 (option '(#\r "recursive") #f #f
121 (lambda (opt name arg result)
122 (alist-cons 'export-recursive? #t result)))
123 (option '("import") #f #f
124 (lambda (opt name arg result)
125 (alist-cons 'import #t result)))
126 (option '("missing") #f #f
127 (lambda (opt name arg result)
128 (alist-cons 'missing #t result)))
129 (option '("extract" #\x) #t #f
130 (lambda (opt name arg result)
131 (alist-cons 'extract arg result)))
132 (option '("generate-key") #f #t
133 (lambda (opt name arg result)
134 (catch 'gcry-error
135 (lambda ()
136 ;; XXX: Curve25519 was actually introduced in
137 ;; libgcrypt 1.6.0.
138 (let ((params
139 (string->canonical-sexp
140 (or arg %key-generation-parameters))))
141 (alist-cons 'generate-key params result)))
142 (lambda (key proc err)
143 (leave (G_ "invalid key generation parameters: ~a: ~a~%")
144 (error-source err)
145 (error-string err))))))
146 (option '("authorize") #f #f
147 (lambda (opt name arg result)
148 (alist-cons 'authorize #t result)))
149
150 (option '(#\S "source") #f #f
151 (lambda (opt name arg result)
152 (alist-cons 'source? #t result)))
153 (option '(#\s "system") #t #f
154 (lambda (opt name arg result)
155 (alist-cons 'system arg
156 (alist-delete 'system result eq?))))
157 (option '("target") #t #f
158 (lambda (opt name arg result)
159 (alist-cons 'target arg
160 (alist-delete 'target result eq?))))
161 (option '(#\e "expression") #t #f
162 (lambda (opt name arg result)
163 (alist-cons 'expression arg result)))
164 (option '(#\n "dry-run") #f #f
165 (lambda (opt name arg result)
166 (alist-cons 'dry-run? #t (alist-cons 'graft? #f result))))
167
168 %standard-build-options))
169
170 (define (derivation-from-expression store str package-derivation
171 system source?)
172 "Read/eval STR and return the corresponding derivation path for SYSTEM.
173 When SOURCE? is true and STR evaluates to a package, return the derivation of
174 the package source; otherwise, use PACKAGE-DERIVATION to compute the
175 derivation of a package."
176 (match (read/eval str)
177 ((? package? p)
178 (if source?
179 (let ((source (package-source p)))
180 (if source
181 (package-source-derivation store source)
182 (leave (G_ "package `~a' has no source~%")
183 (package-name p))))
184 (package-derivation store p system)))
185 ((? procedure? proc)
186 (run-with-store store
187 (mbegin %store-monad
188 (set-guile-for-build (default-guile))
189 (proc)) #:system system))))
190
191 (define (options->derivations+files store opts)
192 "Given OPTS, the result of 'args-fold', return a list of derivations to
193 build and a list of store files to transfer."
194 (define package->derivation
195 (match (assoc-ref opts 'target)
196 (#f package-derivation)
197 (triplet
198 (cut package-cross-derivation <> <> triplet <>))))
199
200 (define src? (assoc-ref opts 'source?))
201 (define sys (assoc-ref opts 'system))
202
203 (fold2 (lambda (arg derivations files)
204 (match arg
205 (('expression . str)
206 (let ((drv (derivation-from-expression store str
207 package->derivation
208 sys src?)))
209 (values (cons drv derivations)
210 (cons (derivation->output-path drv) files))))
211 (('argument . (? store-path? file))
212 (values derivations (cons file files)))
213 (('argument . (? string? spec))
214 (let-values (((p output)
215 (specification->package+output spec)))
216 (if src?
217 (let* ((s (package-source p))
218 (drv (package-source-derivation store s)))
219 (values (cons drv derivations)
220 (cons (derivation->output-path drv)
221 files)))
222 (let ((drv (package->derivation store p sys)))
223 (values (cons drv derivations)
224 (cons (derivation->output-path drv output)
225 files))))))
226 (_
227 (values derivations files))))
228 '()
229 '()
230 opts))
231
232 \f
233 ;;;
234 ;;; Entry point.
235 ;;;
236
237 (define (export-from-store store opts)
238 "Export the packages or derivations specified in OPTS from STORE. Write the
239 resulting archive to the standard output port."
240 (let-values (((drv files)
241 (options->derivations+files store opts)))
242 (set-build-options-from-command-line store opts)
243 (show-what-to-build store drv
244 #:use-substitutes? (assoc-ref opts 'substitutes?)
245 #:dry-run? (assoc-ref opts 'dry-run?))
246
247 (if (or (assoc-ref opts 'dry-run?)
248 (build-derivations store drv))
249 (export-paths store files (current-output-port)
250 #:recursive? (assoc-ref opts 'export-recursive?))
251 (leave (G_ "unable to export the given packages~%")))))
252
253 (define (generate-key-pair parameters)
254 "Generate a key pair with PARAMETERS, a canonical sexp, and store it in the
255 right place."
256 (when (or (file-exists? %public-key-file)
257 (file-exists? %private-key-file))
258 (leave (G_ "key pair exists under '~a'; remove it first~%")
259 (dirname %public-key-file)))
260
261 (format (current-error-port)
262 (G_ "Please wait while gathering entropy to generate the key pair;
263 this may take time...~%"))
264
265 (let* ((pair (catch 'gcry-error
266 (lambda ()
267 (generate-key parameters))
268 (lambda (key proc err)
269 (leave (G_ "key generation failed: ~a: ~a~%")
270 (error-source err)
271 (error-string err)))))
272 (public (find-sexp-token pair 'public-key))
273 (secret (find-sexp-token pair 'private-key)))
274 ;; Create the following files as #o400.
275 (umask #o266)
276
277 (mkdir-p (dirname %public-key-file))
278 (with-atomic-file-output %public-key-file
279 (lambda (port)
280 (display (canonical-sexp->string public) port)))
281 (with-atomic-file-output %private-key-file
282 (lambda (port)
283 (display (canonical-sexp->string secret) port)))
284
285 ;; Make the public key readable by everyone.
286 (chmod %public-key-file #o444)))
287
288 (define (authorize-key)
289 "Authorize imports signed by the public key passed as an advanced sexp on
290 the input port."
291 (define (read-key)
292 (catch 'gcry-error
293 (lambda ()
294 (string->canonical-sexp (read-string (current-input-port))))
295 (lambda (key proc err)
296 (leave (G_ "failed to read public key: ~a: ~a~%")
297 (error-source err) (error-string err)))))
298
299 (let ((key (read-key))
300 (acl (current-acl)))
301 (unless (eq? 'public-key (canonical-sexp-nth-data key 0))
302 (leave (G_ "s-expression does not denote a public key~%")))
303
304 ;; Add KEY to the ACL and write that.
305 (let ((acl (public-keys->acl (cons key (acl->public-keys acl)))))
306 (mkdir-p (dirname %acl-file))
307 (with-atomic-file-output %acl-file
308 (cut write-acl acl <>)))))
309
310 (define (guix-archive . args)
311 (define (lines port)
312 ;; Return lines read from PORT.
313 (let loop ((line (read-line port))
314 (result '()))
315 (if (eof-object? line)
316 (reverse result)
317 (loop (read-line port)
318 (cons line result)))))
319
320 (with-error-handling
321 ;; Ask for absolute file names so that .drv file names passed from the
322 ;; user to 'read-derivation' are absolute when it returns.
323 (with-fluids ((%file-port-name-canonicalization 'absolute))
324 (let ((opts (parse-command-line args %options (list %default-options))))
325 (parameterize ((%graft? (assoc-ref opts 'graft?)))
326 (cond ((assoc-ref opts 'generate-key)
327 =>
328 generate-key-pair)
329 ((assoc-ref opts 'authorize)
330 (authorize-key))
331 (else
332 (with-store store
333 (cond ((assoc-ref opts 'export)
334 (export-from-store store opts))
335 ((assoc-ref opts 'import)
336 (import-paths store (current-input-port)))
337 ((assoc-ref opts 'missing)
338 (let* ((files (lines (current-input-port)))
339 (missing (remove (cut valid-path? store <>)
340 files)))
341 (format #t "~{~a~%~}" missing)))
342 ((assoc-ref opts 'extract)
343 =>
344 (lambda (target)
345 (restore-file (current-input-port) target)))
346 (else
347 (leave
348 (G_ "either '--export' or '--import' \
349 must be specified~%"))))))))))))