archive.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2019, 2020 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 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. (if (build-derivations store drv)
  244. (export-paths store files (current-output-port)
  245. #:recursive? (assoc-ref opts 'export-recursive?))
  246. (leave (G_ "unable to export the given packages~%")))))
  247. (define (generate-key-pair parameters)
  248. "Generate a key pair with PARAMETERS, a canonical sexp, and store it in the
  249. right place."
  250. (when (or (file-exists? %public-key-file)
  251. (file-exists? %private-key-file))
  252. (leave (G_ "key pair exists under '~a'; remove it first~%")
  253. (dirname %public-key-file)))
  254. (format (current-error-port)
  255. (G_ "Please wait while gathering entropy to generate the key pair;
  256. this may take time...~%"))
  257. (let* ((pair (catch 'gcry-error
  258. (lambda ()
  259. (generate-key parameters))
  260. (lambda (key proc err)
  261. (leave (G_ "key generation failed: ~a: ~a~%")
  262. (error-source err)
  263. (error-string err)))))
  264. (public (find-sexp-token pair 'public-key))
  265. (secret (find-sexp-token pair 'private-key)))
  266. ;; Create the following files as #o400.
  267. (umask #o266)
  268. (mkdir-p (dirname %public-key-file))
  269. (with-atomic-file-output %public-key-file
  270. (lambda (port)
  271. (display (canonical-sexp->string public) port)))
  272. (with-atomic-file-output %private-key-file
  273. (lambda (port)
  274. (display (canonical-sexp->string secret) port)))
  275. ;; Make the public key readable by everyone.
  276. (chmod %public-key-file #o444)))
  277. (define (authorize-key)
  278. "Authorize imports signed by the public key passed as an advanced sexp on
  279. the input port."
  280. (define (read-key)
  281. (catch 'gcry-error
  282. (lambda ()
  283. (string->canonical-sexp (read-string (current-input-port))))
  284. (lambda (key proc err)
  285. (leave (G_ "failed to read public key: ~a: ~a~%")
  286. (error-source err) (error-string err)))))
  287. (let ((key (read-key))
  288. (acl (current-acl)))
  289. (unless (eq? 'public-key (canonical-sexp-nth-data key 0))
  290. (leave (G_ "s-expression does not denote a public key~%")))
  291. ;; Add KEY to the ACL and write that.
  292. (let ((acl (public-keys->acl (cons key (acl->public-keys acl)))))
  293. (mkdir-p (dirname %acl-file))
  294. (with-atomic-file-output %acl-file
  295. (cut write-acl acl <>)))))
  296. (define (list-contents port)
  297. "Read a nar from PORT and print the list of files it contains to the current
  298. output port."
  299. (define (consume-input port size)
  300. (let ((bv (make-bytevector 32768)))
  301. (let loop ((total size))
  302. (unless (zero? total)
  303. (let ((n (get-bytevector-n! port bv 0
  304. (min total (bytevector-length bv)))))
  305. (loop (- total n)))))))
  306. (fold-archive (lambda (file type content result)
  307. (match type
  308. ('directory
  309. (format #t "D ~a~%" file))
  310. ('symlink
  311. (format #t "S ~a -> ~a~%" file content))
  312. ((or 'regular 'executable)
  313. (match content
  314. ((input . size)
  315. (format #t "~a ~60a ~10h B~%"
  316. (if (eq? type 'executable)
  317. "x" "r")
  318. file size)
  319. (consume-input input size))))))
  320. #t
  321. port
  322. ""))
  323. ;;;
  324. ;;; Entry point.
  325. ;;;
  326. (define-command (guix-archive . args)
  327. (category plumbing)
  328. (synopsis "manipulate, export, and import normalized archives (nars)")
  329. (define (lines port)
  330. ;; Return lines read from PORT.
  331. (let loop ((line (read-line port))
  332. (result '()))
  333. (if (eof-object? line)
  334. (reverse result)
  335. (loop (read-line port)
  336. (cons line result)))))
  337. (with-error-handling
  338. (let ((opts (parse-command-line args %options (list %default-options))))
  339. (parameterize ((%graft? (assoc-ref opts 'graft?)))
  340. (cond ((assoc-ref opts 'generate-key)
  341. =>
  342. generate-key-pair)
  343. ((assoc-ref opts 'authorize)
  344. (authorize-key))
  345. (else
  346. (with-status-verbosity (assoc-ref opts 'verbosity)
  347. (with-store store
  348. (set-build-options-from-command-line store opts)
  349. (with-build-handler
  350. (build-notifier #:use-substitutes?
  351. (assoc-ref opts 'substitutes?)
  352. #:verbosity
  353. (assoc-ref opts 'verbosity)
  354. #:dry-run?
  355. (assoc-ref opts 'dry-run?))
  356. (cond ((assoc-ref opts 'export)
  357. (export-from-store store opts))
  358. ((assoc-ref opts 'import)
  359. (import-paths store (current-input-port)))
  360. ((assoc-ref opts 'missing)
  361. (let* ((files (lines (current-input-port)))
  362. (missing (remove (cut valid-path? store <>)
  363. files)))
  364. (format #t "~{~a~%~}" missing)))
  365. ((assoc-ref opts 'list)
  366. (list-contents (current-input-port)))
  367. ((assoc-ref opts 'extract)
  368. =>
  369. (lambda (target)
  370. (restore-file (current-input-port) target)))
  371. (else
  372. (leave
  373. (G_ "either '--export' or '--import' \
  374. must be specified~%")))))))))))))