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.
 
 
 
 
 
 

337 lines
13 KiB

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