archive.scm 16 KB

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