You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

350 lines
13 KiB

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