archive.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix scripts archive)
  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 packages)
  28. #:use-module (guix derivations)
  29. #:use-module (guix monads)
  30. #:use-module (guix ui)
  31. #:use-module (guix pki)
  32. #:use-module (gcrypt common)
  33. #:use-module (gcrypt pk-crypto)
  34. #:use-module (guix scripts)
  35. #:use-module (guix scripts build)
  36. #:use-module (gnu packages)
  37. #:use-module (ice-9 match)
  38. #:use-module (ice-9 format)
  39. #:use-module (ice-9 rdelim)
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-26)
  43. #:use-module (srfi srfi-37)
  44. #:use-module (ice-9 binary-ports)
  45. #:use-module (rnrs bytevectors)
  46. #:export (guix-archive
  47. options->derivations+files))
  48. ;;;
  49. ;;; Command-line options.
  50. ;;;
  51. (define %default-options
  52. ;; Alist of default option values.
  53. `((system . ,(%current-system))
  54. (substitutes? . #t)
  55. (offload? . #t)
  56. (graft? . #t)
  57. (print-build-trace? . #t)
  58. (print-extended-build-trace? . #t)
  59. (multiplexed-build-output? . #t)
  60. (verbosity . 3)
  61. (debug . 0)))
  62. (define (show-help)
  63. (display (G_ "Usage: guix archive [OPTION]... PACKAGE...
  64. Export/import one or more packages from/to the store.\n"))
  65. (display (G_ "
  66. --export export the specified files/packages to stdout"))
  67. (display (G_ "
  68. -r, --recursive combined with '--export', include dependencies"))
  69. (display (G_ "
  70. --import import from the archive passed on stdin"))
  71. (display (G_ "
  72. --missing print the files from stdin that are missing"))
  73. (display (G_ "
  74. -x, --extract=DIR extract the archive on stdin to DIR"))
  75. (display (G_ "
  76. -t, --list list the files in the archive on stdin"))
  77. (newline)
  78. (display (G_ "
  79. --generate-key[=PARAMETERS]
  80. generate a key pair with the given parameters"))
  81. (display (G_ "
  82. --authorize authorize imports signed by the public key on stdin"))
  83. (newline)
  84. (display (G_ "
  85. -e, --expression=EXPR build the package or derivation EXPR evaluates to"))
  86. (display (G_ "
  87. -S, --source build the packages' source derivations"))
  88. (display (G_ "
  89. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  90. (newline)
  91. (show-build-options-help)
  92. (newline)
  93. (show-cross-build-options-help)
  94. (newline)
  95. (show-native-build-options-help)
  96. (newline)
  97. (display (G_ "
  98. -h, --help display this help and exit"))
  99. (display (G_ "
  100. -V, --version display version information and exit"))
  101. (newline)
  102. (show-bug-report-information))
  103. (define %key-generation-parameters
  104. ;; Default key generation parameters. We prefer Ed25519, but it was
  105. ;; introduced in libgcrypt 1.6.0.
  106. (if (version>? (gcrypt-version) "1.6.0")
  107. "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))"
  108. "(genkey (rsa (nbits 4:4096)))"))
  109. (define %options
  110. ;; Specifications of the command-line options.
  111. (cons* (option '(#\h "help") #f #f
  112. (lambda args
  113. (show-help)
  114. (exit 0)))
  115. (option '(#\V "version") #f #f
  116. (lambda args
  117. (show-version-and-exit "guix build")))
  118. (option '("export") #f #f
  119. (lambda (opt name arg result)
  120. (alist-cons 'export #t result)))
  121. (option '(#\r "recursive") #f #f
  122. (lambda (opt name arg result)
  123. (alist-cons 'export-recursive? #t result)))
  124. (option '("import") #f #f
  125. (lambda (opt name arg result)
  126. (alist-cons 'import #t result)))
  127. (option '("missing") #f #f
  128. (lambda (opt name arg result)
  129. (alist-cons 'missing #t result)))
  130. (option '("extract" #\x) #t #f
  131. (lambda (opt name arg result)
  132. (alist-cons 'extract arg result)))
  133. (option '("list" #\t) #f #f
  134. (lambda (opt name arg result)
  135. (alist-cons 'list #t result)))
  136. (option '("generate-key") #f #t
  137. (lambda (opt name arg result)
  138. (catch 'gcry-error
  139. (lambda ()
  140. ;; XXX: Curve25519 was actually introduced in
  141. ;; libgcrypt 1.6.0.
  142. (let ((params
  143. (string->canonical-sexp
  144. (or arg %key-generation-parameters))))
  145. (alist-cons 'generate-key params result)))
  146. (lambda (key proc err)
  147. (leave (G_ "invalid key generation parameters: ~a: ~a~%")
  148. (error-source err)
  149. (error-string err))))))
  150. (option '("authorize") #f #f
  151. (lambda (opt name arg result)
  152. (alist-cons 'authorize #t result)))
  153. (option '(#\S "source") #f #f
  154. (lambda (opt name arg result)
  155. (alist-cons 'source? #t result)))
  156. (option '(#\e "expression") #t #f
  157. (lambda (opt name arg result)
  158. (alist-cons 'expression arg result)))
  159. (option '(#\v "verbosity") #t #f
  160. (lambda (opt name arg result)
  161. (let ((level (string->number* arg)))
  162. (alist-cons 'verbosity level
  163. (alist-delete 'verbosity result)))))
  164. (option '(#\n "dry-run") #f #f
  165. (lambda (opt name arg result)
  166. (alist-cons 'dry-run? #t result)))
  167. (append %standard-build-options
  168. %standard-cross-build-options
  169. %standard-native-build-options)))
  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. (define (options->derivations+files store opts)
  191. "Given OPTS, the result of 'args-fold', return a list of derivations to
  192. build and a list of store files to transfer."
  193. (define package->derivation
  194. (match (assoc-ref opts 'target)
  195. (#f package-derivation)
  196. (triplet
  197. (cut package-cross-derivation <> <> triplet <>))))
  198. (define src? (assoc-ref opts 'source?))
  199. (define sys (assoc-ref opts 'system))
  200. (fold2 (lambda (arg derivations files)
  201. (match arg
  202. (('expression . str)
  203. (let ((drv (derivation-from-expression store str
  204. package->derivation
  205. sys src?)))
  206. (values (cons drv derivations)
  207. (cons (derivation->output-path drv) files))))
  208. (('argument . (? store-path? file))
  209. (values derivations (cons file files)))
  210. (('argument . (? string? spec))
  211. (let-values (((p output)
  212. (specification->package+output spec)))
  213. (if src?
  214. (let* ((s (package-source p))
  215. (drv (package-source-derivation store s)))
  216. (values (cons drv derivations)
  217. (cons (derivation->output-path drv)
  218. files)))
  219. (let ((drv (package->derivation store p sys)))
  220. (values (cons drv derivations)
  221. (cons (derivation->output-path drv output)
  222. files))))))
  223. (_
  224. (values derivations files))))
  225. '()
  226. '()
  227. opts))
  228. ;;;
  229. ;;; Entry point.
  230. ;;;
  231. (define (export-from-store store opts)
  232. "Export the packages or derivations specified in OPTS from STORE. Write the
  233. resulting archive to the standard output port."
  234. (let-values (((drv files)
  235. (options->derivations+files store opts)))
  236. (when (null? files)
  237. (warning (G_ "no arguments specified; creating an empty archive~%")))
  238. (if (build-derivations store drv)
  239. (export-paths store files (current-output-port)
  240. #:recursive? (assoc-ref opts 'export-recursive?))
  241. (leave (G_ "unable to export the given packages~%")))))
  242. (define (generate-key-pair parameters)
  243. "Generate a key pair with PARAMETERS, a canonical sexp, and store it in the
  244. right place."
  245. (when (or (file-exists? %public-key-file)
  246. (file-exists? %private-key-file))
  247. (leave (G_ "key pair exists under '~a'; remove it first~%")
  248. (dirname %public-key-file)))
  249. (format (current-error-port)
  250. (G_ "Please wait while gathering entropy to generate the key pair;
  251. this may take time...~%"))
  252. (let* ((pair (catch 'gcry-error
  253. (lambda ()
  254. (generate-key parameters))
  255. (lambda (key proc err)
  256. (leave (G_ "key generation failed: ~a: ~a~%")
  257. (error-source err)
  258. (error-string err)))))
  259. (public (find-sexp-token pair 'public-key))
  260. (secret (find-sexp-token pair 'private-key)))
  261. ;; Create the following files as #o400.
  262. (umask #o266)
  263. (mkdir-p (dirname %public-key-file))
  264. (with-atomic-file-output %public-key-file
  265. (lambda (port)
  266. (display (canonical-sexp->string public) port)))
  267. (with-atomic-file-output %private-key-file
  268. (lambda (port)
  269. (display (canonical-sexp->string secret) port)))
  270. ;; Make the public key readable by everyone.
  271. (chmod %public-key-file #o444)))
  272. (define (authorize-key)
  273. "Authorize imports signed by the public key passed as an advanced sexp on
  274. the input port."
  275. (define (read-key)
  276. (catch 'gcry-error
  277. (lambda ()
  278. (string->canonical-sexp (read-string (current-input-port))))
  279. (lambda (key proc err)
  280. (leave (G_ "failed to read public key: ~a: ~a~%")
  281. (error-source err) (error-string err)))))
  282. ;; Warn about potentially volatile ACLs, but continue: system reconfiguration
  283. ;; might not be possible without (newly-authorized) substitutes.
  284. (let ((stat (false-if-exception (lstat %acl-file))))
  285. (when (and stat (eq? 'symlink (stat:type (lstat %acl-file))))
  286. (warning (G_ "replacing symbolic link ~a with a regular file~%")
  287. %acl-file)
  288. (when (string-prefix? (%store-prefix) (readlink %acl-file))
  289. (display-hint (G_ "On Guix System, add all @code{authorized-keys} to the
  290. @code{guix-service-type} service of your @code{operating-system} instead.")))))
  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. ('directory-complete
  315. #t)
  316. ('symlink
  317. (format #t "S ~a -> ~a~%" file content))
  318. ((or 'regular 'executable)
  319. (match content
  320. ((input . size)
  321. (format #t "~a ~60a ~10h B~%"
  322. (if (eq? type 'executable)
  323. "x" "r")
  324. file size)
  325. (consume-input input size))))))
  326. #t
  327. port
  328. ""))
  329. ;;;
  330. ;;; Entry point.
  331. ;;;
  332. (define-command (guix-archive . args)
  333. (category plumbing)
  334. (synopsis "manipulate, export, and import normalized archives (nars)")
  335. (define (lines port)
  336. ;; Return lines read from PORT.
  337. (let loop ((line (read-line port))
  338. (result '()))
  339. (if (eof-object? line)
  340. (reverse result)
  341. (loop (read-line port)
  342. (cons line result)))))
  343. (with-error-handling
  344. (let ((opts (parse-command-line args %options (list %default-options))))
  345. (parameterize ((%graft? (assoc-ref opts 'graft?)))
  346. (cond ((assoc-ref opts 'generate-key)
  347. =>
  348. generate-key-pair)
  349. ((assoc-ref opts 'authorize)
  350. (authorize-key))
  351. (else
  352. (with-status-verbosity (assoc-ref opts 'verbosity)
  353. (with-store store
  354. (set-build-options-from-command-line store opts)
  355. (with-build-handler
  356. (build-notifier #:use-substitutes?
  357. (assoc-ref opts 'substitutes?)
  358. #:verbosity
  359. (assoc-ref opts 'verbosity)
  360. #:dry-run?
  361. (assoc-ref opts 'dry-run?))
  362. (cond ((assoc-ref opts 'export)
  363. (export-from-store store opts))
  364. ((assoc-ref opts 'import)
  365. (import-paths store (current-input-port)))
  366. ((assoc-ref opts 'missing)
  367. (let* ((files (lines (current-input-port)))
  368. (missing (remove (cut valid-path? store <>)
  369. files)))
  370. (format #t "~{~a~%~}" missing)))
  371. ((assoc-ref opts 'list)
  372. (list-contents (current-input-port)))
  373. ((assoc-ref opts 'extract)
  374. =>
  375. (lambda (target)
  376. (restore-file (current-input-port) target)))
  377. (else
  378. (leave
  379. (G_ "either '--export' or '--import' \
  380. must be specified~%")))))))))))))