package.scm 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2013, 2015 Mark H Weaver <mhw@netris.org>
  5. ;;; Copyright © 2014, 2016 Alex Kost <alezost@gmail.com>
  6. ;;; Copyright © 2016 Roel Janssen <roel@gnu.org>
  7. ;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch>
  8. ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
  9. ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
  10. ;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
  11. ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
  12. ;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
  13. ;;;
  14. ;;; This file is part of GNU Guix.
  15. ;;;
  16. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  17. ;;; under the terms of the GNU General Public License as published by
  18. ;;; the Free Software Foundation; either version 3 of the License, or (at
  19. ;;; your option) any later version.
  20. ;;;
  21. ;;; GNU Guix is distributed in the hope that it will be useful, but
  22. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ;;; GNU General Public License for more details.
  25. ;;;
  26. ;;; You should have received a copy of the GNU General Public License
  27. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  28. (define-module (guix scripts package)
  29. #:use-module (guix ui)
  30. #:use-module ((guix status) #:select (with-status-verbosity))
  31. #:use-module ((guix build syscalls) #:select (terminal-rows))
  32. #:use-module (guix store)
  33. #:use-module (guix grafts)
  34. #:use-module (guix derivations)
  35. #:use-module (guix packages)
  36. #:use-module (guix profiles)
  37. #:use-module (guix search-paths)
  38. #:autoload (guix import json) (json->scheme-file)
  39. #:use-module (guix monads)
  40. #:use-module (guix utils)
  41. #:use-module (guix config)
  42. #:use-module (guix scripts)
  43. #:use-module (guix scripts build)
  44. #:use-module (guix transformations)
  45. #:autoload (guix describe) (manifest-entry-provenance
  46. manifest-entry-with-provenance)
  47. #:autoload (guix channels) (channel-name channel-commit channel->code)
  48. #:autoload (guix store roots) (gc-roots user-owned?)
  49. #:use-module ((guix build utils)
  50. #:select (directory-exists? mkdir-p))
  51. #:use-module (ice-9 format)
  52. #:use-module (ice-9 match)
  53. #:autoload (ice-9 pretty-print) (pretty-print)
  54. #:use-module (ice-9 regex)
  55. #:use-module (ice-9 vlist)
  56. #:use-module (srfi srfi-1)
  57. #:use-module (srfi srfi-11)
  58. #:use-module (srfi srfi-26)
  59. #:use-module (srfi srfi-34)
  60. #:use-module (srfi srfi-35)
  61. #:use-module (srfi srfi-37)
  62. #:use-module (gnu packages)
  63. #:autoload (gnu packages bootstrap) (%bootstrap-guile)
  64. #:export (build-and-use-profile
  65. delete-generations
  66. delete-matching-generations
  67. guix-package
  68. search-path-environment-variables
  69. manifest-entry-version-prefix
  70. transaction-upgrade-entry ;mostly for testing
  71. (%options . %package-options)
  72. (%default-options . %package-default-options)
  73. guix-package*))
  74. (define %store
  75. (make-parameter #f))
  76. ;;;
  77. ;;; Profiles.
  78. ;;;
  79. (define (ensure-default-profile)
  80. "Ensure the default profile symlink and directory exist and are writable."
  81. (ensure-profile-directory)
  82. ;; Try to create ~/.guix-profile if it doesn't exist yet.
  83. (when (and %user-profile-directory
  84. %current-profile
  85. (not (false-if-exception
  86. (lstat %user-profile-directory))))
  87. (catch 'system-error
  88. (lambda ()
  89. (symlink %current-profile %user-profile-directory))
  90. (const #t))))
  91. (define (delete-generations store profile generations)
  92. "Delete GENERATIONS from PROFILE.
  93. GENERATIONS is a list of generation numbers."
  94. (for-each (cut delete-generation* store profile <>)
  95. generations))
  96. (define (delete-matching-generations store profile pattern)
  97. "Delete from PROFILE all the generations matching PATTERN. PATTERN must be
  98. a string denoting a set of generations: the empty list means \"all generations
  99. but the current one\", a number designates a generation, and other patterns
  100. denote ranges as interpreted by 'matching-generations'."
  101. (let ((current (generation-number profile)))
  102. (cond ((not (file-exists? profile)) ; XXX: race condition
  103. (raise (condition (&profile-not-found-error
  104. (profile profile)))))
  105. ((not pattern)
  106. (delete-generations store profile
  107. (delv current (profile-generations profile))))
  108. ;; Do not delete the zeroth generation.
  109. ((equal? 0 (string->number pattern))
  110. #t)
  111. ;; If PATTERN is a duration, match generations that are
  112. ;; older than the specified duration.
  113. ((matching-generations pattern profile
  114. #:duration-relation >)
  115. =>
  116. (lambda (numbers)
  117. (when (memv current numbers)
  118. (warning (G_ "not removing generation ~a, which is current~%")
  119. current))
  120. ;; Make sure we don't inadvertently remove the current
  121. ;; generation.
  122. (let ((numbers (delv current numbers)))
  123. (when (null-list? numbers)
  124. (leave (G_ "no matching generation~%")))
  125. (delete-generations store profile numbers)))))))
  126. (define* (build-and-use-profile store profile manifest
  127. #:key
  128. dry-run?
  129. (hooks %default-profile-hooks)
  130. allow-collisions?
  131. bootstrap?)
  132. "Build a new generation of PROFILE, a file name, using the packages
  133. specified in MANIFEST, a manifest object. When ALLOW-COLLISIONS? is true,
  134. do not treat collisions in MANIFEST as an error. HOOKS is a list of \"profile
  135. hooks\" run when building the profile."
  136. (let* ((prof-drv (run-with-store store
  137. (profile-derivation manifest
  138. #:allow-collisions? allow-collisions?
  139. #:hooks (if bootstrap? '() hooks)
  140. #:locales? (not bootstrap?))))
  141. (prof (derivation->output-path prof-drv)))
  142. (cond
  143. (dry-run? #t)
  144. ((and (file-exists? profile)
  145. (and=> (readlink* profile) (cut string=? prof <>)))
  146. (format (current-error-port) (G_ "nothing to be done~%")))
  147. (else
  148. (let* ((number (generation-number profile))
  149. ;; Always use NUMBER + 1 for the new profile, possibly
  150. ;; overwriting a "previous future generation".
  151. (name (generation-file-name profile (+ 1 number))))
  152. (and (build-derivations store (list prof-drv))
  153. (let* ((entries (manifest-entries manifest))
  154. (count (length entries)))
  155. (switch-symlinks name prof)
  156. (switch-symlinks profile (basename name))
  157. (unless (string=? profile %current-profile)
  158. (register-gc-root store name))
  159. (display-search-path-hint entries profile)))
  160. (warn-about-disk-space profile))))))
  161. ;;;
  162. ;;; Package specifications.
  163. ;;;
  164. (define (find-packages-by-description regexps)
  165. "Return a list of pairs: packages whose name, synopsis, description,
  166. or output matches at least one of REGEXPS sorted by relevance, and its
  167. non-zero relevance score."
  168. (let ((matches (fold-packages (lambda (package result)
  169. (if (package-superseded package)
  170. result
  171. (match (package-relevance package
  172. regexps)
  173. ((? zero?)
  174. result)
  175. (score
  176. (cons (cons package score)
  177. result)))))
  178. '())))
  179. (sort matches
  180. (lambda (m1 m2)
  181. (match m1
  182. ((package1 . score1)
  183. (match m2
  184. ((package2 . score2)
  185. (if (= score1 score2)
  186. (string>? (package-full-name package1)
  187. (package-full-name package2))
  188. (> score1 score2))))))))))
  189. (define (transaction-upgrade-entry store entry transaction)
  190. "Return a variant of TRANSACTION that accounts for the upgrade of ENTRY, a
  191. <manifest-entry>."
  192. (define (lower-manifest-entry* entry)
  193. (run-with-store store
  194. (lower-manifest-entry entry (%current-system))))
  195. (define (supersede old new)
  196. (info (G_ "package '~a' has been superseded by '~a'~%")
  197. (manifest-entry-name old) (package-name new))
  198. (manifest-transaction-install-entry
  199. (package->manifest-entry* new (manifest-entry-output old))
  200. (manifest-transaction-remove-pattern
  201. (manifest-pattern
  202. (name (manifest-entry-name old))
  203. (version (manifest-entry-version old))
  204. (output (manifest-entry-output old)))
  205. transaction)))
  206. (define (upgrade entry transform)
  207. (match entry
  208. (($ <manifest-entry> name version output (? string? path))
  209. (match (find-best-packages-by-name name #f)
  210. ((pkg . rest)
  211. (let* ((pkg (transform pkg))
  212. (candidate-version (package-version pkg)))
  213. (match (package-superseded pkg)
  214. ((? package? new)
  215. (supersede entry new))
  216. (#f
  217. (case (version-compare candidate-version version)
  218. ((>)
  219. (manifest-transaction-install-entry
  220. (package->manifest-entry* pkg output)
  221. transaction))
  222. ((<)
  223. transaction)
  224. ((=)
  225. (let* ((new (package->manifest-entry* pkg output)))
  226. ;; Here we want to determine whether the NEW actually
  227. ;; differs from ENTRY, but we need to intercept
  228. ;; 'build-things' calls because they would prevent us from
  229. ;; displaying the list of packages to install/upgrade
  230. ;; upfront. Thus, if lowering NEW triggers a build (due
  231. ;; to grafts), assume NEW differs from ENTRY.
  232. (if (with-build-handler (const #f)
  233. (manifest-entry=? (lower-manifest-entry* new)
  234. entry))
  235. transaction
  236. (manifest-transaction-install-entry
  237. new transaction)))))))))
  238. (()
  239. (warning (G_ "package '~a' no longer exists~%") name)
  240. transaction)))))
  241. (if (manifest-transaction-removal-candidate? entry transaction)
  242. transaction
  243. ;; Upgrade ENTRY, preserving transformation options listed in its
  244. ;; properties.
  245. (let ((transform (options->transformation
  246. (or (assq-ref (manifest-entry-properties entry)
  247. 'transformations)
  248. '()))))
  249. (upgrade entry transform))))
  250. ;;;
  251. ;;; Search paths.
  252. ;;;
  253. (define* (search-path-environment-variables entries profiles
  254. #:optional (getenv getenv)
  255. #:key (kind 'exact))
  256. "Return environment variable definitions that may be needed for the use of
  257. ENTRIES, a list of manifest entries, in PROFILES. Use GETENV to determine the
  258. current settings and report only settings not already effective. KIND
  259. must be one of 'exact, 'prefix, or 'suffix, depending on the kind of search
  260. path definition to be returned."
  261. (let ((search-paths (delete-duplicates
  262. (cons $PATH
  263. (append-map manifest-entry-search-paths
  264. entries)))))
  265. (filter-map (match-lambda
  266. ((spec . value)
  267. (let ((variable (search-path-specification-variable spec))
  268. (sep (search-path-specification-separator spec)))
  269. (environment-variable-definition variable value
  270. #:separator sep
  271. #:kind kind))))
  272. (evaluate-search-paths search-paths profiles
  273. getenv))))
  274. (define (absolutize file)
  275. "Return an absolute file name equivalent to FILE, but without resolving
  276. symlinks like 'canonicalize-path' would do."
  277. (if (string-prefix? "/" file)
  278. file
  279. (string-append (getcwd) "/" file)))
  280. (define (display-search-path-hint entries profile)
  281. "Display a hint on how to set environment variables to use ENTRIES, a list
  282. of manifest entries, in the context of PROFILE."
  283. (let* ((profile (user-friendly-profile (absolutize profile)))
  284. (settings (search-path-environment-variables entries (list profile)
  285. #:kind 'prefix)))
  286. (unless (null? settings)
  287. (display-hint (format #f (G_ "Consider setting the necessary environment
  288. variables by running:
  289. @example
  290. GUIX_PROFILE=\"~a\"
  291. . \"$GUIX_PROFILE/etc/profile\"
  292. @end example
  293. Alternately, see @command{guix package --search-paths -p ~s}.")
  294. profile profile)))))
  295. ;;;
  296. ;;; Export a manifest.
  297. ;;;
  298. (define (manifest-entry-version-prefix entry)
  299. "Search among all the versions of ENTRY's package that are available, and
  300. return the shortest unambiguous version prefix for this package. If only one
  301. version of ENTRY's package is available, return the empty string."
  302. (package-unique-version-prefix (manifest-entry-name entry)
  303. (manifest-entry-version entry)))
  304. (define* (export-manifest manifest
  305. #:optional (port (current-output-port)))
  306. "Write to PORT a manifest corresponding to MANIFEST."
  307. (match (manifest->code manifest
  308. #:entry-package-version
  309. manifest-entry-version-prefix)
  310. (('begin exp ...)
  311. (format port (G_ "\
  312. ;; This \"manifest\" file can be passed to 'guix package -m' to reproduce
  313. ;; the content of your profile. This is \"symbolic\": it only specifies
  314. ;; package names. To reproduce the exact same profile, you also need to
  315. ;; capture the channels being used, as returned by \"guix describe\".
  316. ;; See the \"Replicating Guix\" section in the manual.\n"))
  317. (for-each (lambda (exp)
  318. (newline port)
  319. (pretty-print exp port))
  320. exp))))
  321. (define (channel=? a b)
  322. (and (channel-commit a) (channel-commit b)
  323. (string=? (channel-commit a) (channel-commit b))))
  324. (define* (export-channels manifest
  325. #:optional (port (current-output-port)))
  326. (define channels
  327. (delete-duplicates
  328. (append-map manifest-entry-provenance (manifest-entries manifest))
  329. channel=?))
  330. (define channel-names
  331. (delete-duplicates (map channel-name channels)))
  332. (define table
  333. (fold (lambda (channel table)
  334. (vhash-consq (channel-name channel) channel table))
  335. vlist-null
  336. channels))
  337. (when (null? channels)
  338. (leave (G_ "no provenance information for this profile~%")))
  339. (format port (G_ "\
  340. ;; This channel file can be passed to 'guix pull -C' or to
  341. ;; 'guix time-machine -C' to obtain the Guix revision that was
  342. ;; used to populate this profile.\n"))
  343. (newline port)
  344. (display "(list\n" port)
  345. (for-each (lambda (name)
  346. (define indent " ")
  347. (match (vhash-foldq* cons '() name table)
  348. ((channel extra ...)
  349. (unless (null? extra)
  350. (display indent port)
  351. (format port (G_ "\
  352. ;; Note: these other commits were also used to install \
  353. some of the packages in this profile:~%"))
  354. (for-each (lambda (channel)
  355. (format port "~a;; ~s~%"
  356. indent (channel-commit channel)))
  357. extra))
  358. (pretty-print (channel->code channel) port
  359. #:per-line-prefix indent))))
  360. channel-names)
  361. (display ")\n" port)
  362. #t)
  363. ;;;
  364. ;;; Command-line options.
  365. ;;;
  366. (define %default-options
  367. ;; Alist of default option values.
  368. `((verbosity . 1)
  369. (debug . 0)
  370. (graft? . #t)
  371. (substitutes? . #t)
  372. (offload? . #t)
  373. (print-build-trace? . #t)
  374. (print-extended-build-trace? . #t)
  375. (multiplexed-build-output? . #t)))
  376. (define (show-help)
  377. (display (G_ "Usage: guix package [OPTION]...
  378. Install, remove, or upgrade packages in a single transaction.\n"))
  379. (display (G_ "
  380. -i, --install PACKAGE ...
  381. install PACKAGEs"))
  382. (display (G_ "
  383. -e, --install-from-expression=EXP
  384. install the package EXP evaluates to"))
  385. (display (G_ "
  386. -f, --install-from-file=FILE
  387. install the package that the code within FILE
  388. evaluates to"))
  389. (display (G_ "
  390. -r, --remove PACKAGE ...
  391. remove PACKAGEs"))
  392. (display (G_ "
  393. -u, --upgrade[=REGEXP] upgrade all the installed packages matching REGEXP"))
  394. (display (G_ "
  395. -m, --manifest=FILE create a new profile generation with the manifest
  396. from FILE"))
  397. (display (G_ "
  398. --do-not-upgrade[=REGEXP] do not upgrade any packages matching REGEXP"))
  399. (display (G_ "
  400. --roll-back roll back to the previous generation"))
  401. (display (G_ "
  402. --search-paths[=KIND]
  403. display needed environment variable definitions"))
  404. (display (G_ "
  405. -l, --list-generations[=PATTERN]
  406. list generations matching PATTERN"))
  407. (display (G_ "
  408. -d, --delete-generations[=PATTERN]
  409. delete generations matching PATTERN"))
  410. (display (G_ "
  411. -S, --switch-generation=PATTERN
  412. switch to a generation matching PATTERN"))
  413. (display (G_ "
  414. --export-manifest print a manifest for the chosen profile"))
  415. (display (G_ "
  416. --export-channels print channels for the chosen profile"))
  417. (display (G_ "
  418. -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
  419. (display (G_ "
  420. --list-profiles list the user's profiles"))
  421. (newline)
  422. (display (G_ "
  423. --allow-collisions do not treat collisions in the profile as an error"))
  424. (display (G_ "
  425. --bootstrap use the bootstrap Guile to build the profile"))
  426. (display (G_ "
  427. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  428. (newline)
  429. (display (G_ "
  430. -s, --search=REGEXP search in synopsis and description using REGEXP"))
  431. (display (G_ "
  432. -I, --list-installed[=REGEXP]
  433. list installed packages matching REGEXP"))
  434. (display (G_ "
  435. -A, --list-available[=REGEXP]
  436. list available packages matching REGEXP"))
  437. (display (G_ "
  438. --show=PACKAGE show details about PACKAGE"))
  439. (newline)
  440. (show-build-options-help)
  441. (newline)
  442. (show-transformation-options-help)
  443. (newline)
  444. (display (G_ "
  445. -h, --help display this help and exit"))
  446. (display (G_ "
  447. -V, --version display version information and exit"))
  448. (newline)
  449. (show-bug-report-information))
  450. (define %options
  451. ;; Specification of the command-line options.
  452. (cons* (option '(#\h "help") #f #f
  453. (lambda args
  454. (show-help)
  455. (exit 0)))
  456. (option '(#\V "version") #f #f
  457. (lambda args
  458. (show-version-and-exit "guix package")))
  459. (option '(#\i "install") #f #t
  460. (lambda (opt name arg result arg-handler)
  461. (let arg-handler ((arg arg) (result result))
  462. (values (if arg
  463. (alist-cons 'install arg result)
  464. result)
  465. arg-handler))))
  466. (option '(#\e "install-from-expression") #t #f
  467. (lambda (opt name arg result arg-handler)
  468. (values (alist-cons 'install (read/eval-package-expression arg)
  469. result)
  470. #f)))
  471. (option '(#\f "install-from-file") #t #f
  472. (lambda (opt name arg result arg-handler)
  473. (values (alist-cons 'install
  474. (let ((file (or (and (string-suffix? ".json" arg)
  475. (json->scheme-file arg))
  476. arg)))
  477. (load* file (make-user-module '())))
  478. result)
  479. #f)))
  480. (option '(#\r "remove") #f #t
  481. (lambda (opt name arg result arg-handler)
  482. (let arg-handler ((arg arg) (result result))
  483. (values (if arg
  484. (alist-cons 'remove arg result)
  485. result)
  486. arg-handler))))
  487. (option '(#\u "upgrade") #f #t
  488. (lambda (opt name arg result arg-handler)
  489. (when (and arg (string-prefix? "-" arg))
  490. (warning (G_ "upgrade regexp '~a' looks like a \
  491. command-line option~%")
  492. arg)
  493. (warning (G_ "is this intended?~%")))
  494. (let arg-handler ((arg arg) (result result))
  495. (values (alist-cons 'upgrade arg
  496. ;; Delete any prior "upgrade all"
  497. ;; command, or else "--upgrade gcc"
  498. ;; would upgrade everything.
  499. (delete '(upgrade . #f) result))
  500. arg-handler))))
  501. (option '("do-not-upgrade") #f #t
  502. (lambda (opt name arg result arg-handler)
  503. (let arg-handler ((arg arg) (result result))
  504. (values (if arg
  505. (alist-cons 'do-not-upgrade arg result)
  506. result)
  507. arg-handler))))
  508. (option '("roll-back" "rollback") #f #f
  509. (lambda (opt name arg result arg-handler)
  510. (values (alist-cons 'roll-back? #t result)
  511. #f)))
  512. (option '(#\m "manifest") #t #f
  513. (lambda (opt name arg result arg-handler)
  514. (values (alist-cons 'manifest arg result)
  515. arg-handler)))
  516. (option '(#\l "list-generations") #f #t
  517. (lambda (opt name arg result arg-handler)
  518. (values (cons `(query list-generations ,arg)
  519. result)
  520. #f)))
  521. (option '("list-profiles") #f #f
  522. (lambda (opt name arg result arg-handler)
  523. (values (cons `(query list-profiles #t)
  524. result)
  525. #f)))
  526. (option '(#\d "delete-generations") #f #t
  527. (lambda (opt name arg result arg-handler)
  528. (values (alist-cons 'delete-generations arg
  529. result)
  530. #f)))
  531. (option '(#\S "switch-generation") #t #f
  532. (lambda (opt name arg result arg-handler)
  533. (values (alist-cons 'switch-generation arg result)
  534. #f)))
  535. (option '("search-paths") #f #t
  536. (lambda (opt name arg result arg-handler)
  537. (let ((kind (match arg
  538. ((or "exact" "prefix" "suffix")
  539. (string->symbol arg))
  540. (#f
  541. 'exact)
  542. (x
  543. (leave (G_ "~a: unsupported \
  544. kind of search path~%")
  545. x)))))
  546. (values (cons `(query search-paths ,kind)
  547. result)
  548. #f))))
  549. (option '("export-manifest") #f #f
  550. (lambda (opt name arg result arg-handler)
  551. (values (cons `(query export-manifest) result)
  552. #f)))
  553. (option '("export-channels") #f #f
  554. (lambda (opt name arg result arg-handler)
  555. (values (cons `(query export-channels) result)
  556. #f)))
  557. (option '(#\p "profile") #t #f
  558. (lambda (opt name arg result arg-handler)
  559. (values (alist-cons 'profile (canonicalize-profile arg)
  560. result)
  561. #f)))
  562. (option '(#\n "dry-run") #f #f
  563. (lambda (opt name arg result arg-handler)
  564. (values (alist-cons 'dry-run? #t result)
  565. #f)))
  566. (option '(#\v "verbosity") #t #f
  567. (lambda (opt name arg result arg-handler)
  568. (let ((level (string->number* arg)))
  569. (values (alist-cons 'verbosity level
  570. (alist-delete 'verbosity result))
  571. #f))))
  572. (option '("bootstrap") #f #f
  573. (lambda (opt name arg result arg-handler)
  574. (values (alist-cons 'bootstrap? #t result)
  575. #f)))
  576. (option '("verbose") #f #f ;deprecated
  577. (lambda (opt name arg result arg-handler)
  578. (values (alist-cons 'verbosity 2
  579. (alist-delete 'verbosity
  580. result))
  581. #f)))
  582. (option '("allow-collisions") #f #f
  583. (lambda (opt name arg result arg-handler)
  584. (values (alist-cons 'allow-collisions? #t result)
  585. #f)))
  586. (option '(#\s "search") #t #f
  587. (lambda (opt name arg result arg-handler)
  588. (values (cons `(query search ,(or arg ""))
  589. result)
  590. #f)))
  591. (option '(#\I "list-installed") #f #t
  592. (lambda (opt name arg result arg-handler)
  593. (values (cons `(query list-installed ,(or arg ""))
  594. result)
  595. #f)))
  596. (option '(#\A "list-available") #f #t
  597. (lambda (opt name arg result arg-handler)
  598. (values (cons `(query list-available ,(or arg ""))
  599. result)
  600. #f)))
  601. (option '("show") #t #t
  602. (lambda (opt name arg result arg-handler)
  603. (values (cons `(query show ,arg)
  604. result)
  605. #f)))
  606. (append %transformation-options
  607. %standard-build-options)))
  608. (define (options->upgrade-predicate opts)
  609. "Return a predicate based on the upgrade/do-not-upgrade regexps in OPTS
  610. that, given a package name, returns true if the package is a candidate for
  611. upgrading, #f otherwise."
  612. (define upgrade-regexps
  613. (filter-map (match-lambda
  614. (('upgrade . regexp)
  615. (make-regexp* (or regexp "") regexp/icase))
  616. (_ #f))
  617. opts))
  618. (define do-not-upgrade-regexps
  619. (filter-map (match-lambda
  620. (('do-not-upgrade . regexp)
  621. (make-regexp* regexp regexp/icase))
  622. (_ #f))
  623. opts))
  624. (lambda (name)
  625. (and (any (cut regexp-exec <> name) upgrade-regexps)
  626. (not (any (cut regexp-exec <> name) do-not-upgrade-regexps)))))
  627. (define (store-item->manifest-entry item)
  628. "Return a manifest entry for ITEM, a \"/gnu/store/...\" file name."
  629. (let-values (((name version)
  630. (package-name->name+version (store-path-package-name item)
  631. #\-)))
  632. (manifest-entry
  633. (name name)
  634. (version version)
  635. (output "out") ;XXX: wild guess
  636. (item item))))
  637. (define (package->manifest-entry* package output)
  638. "Like 'package->manifest-entry', but attach PACKAGE provenance meta-data to
  639. the resulting manifest entry."
  640. (manifest-entry-with-provenance
  641. (package->manifest-entry package output)))
  642. (define (options->installable opts manifest transaction)
  643. "Given MANIFEST, the current manifest, and OPTS, the result of 'args-fold',
  644. return an variant of TRANSACTION that accounts for the specified installations
  645. and upgrades."
  646. (define upgrade?
  647. (options->upgrade-predicate opts))
  648. (define upgraded
  649. (fold (lambda (entry transaction)
  650. (if (upgrade? (manifest-entry-name entry))
  651. (transaction-upgrade-entry (%store) entry transaction)
  652. transaction))
  653. transaction
  654. (manifest-entries manifest)))
  655. (define to-install
  656. (filter-map (match-lambda
  657. (('install . (? package? p))
  658. ;; When given a package via `-e', install the first of its
  659. ;; outputs (XXX).
  660. (package->manifest-entry* p "out"))
  661. (('install . (? string? spec))
  662. (if (store-path? spec)
  663. (store-item->manifest-entry spec)
  664. (let-values (((package output)
  665. (specification->package+output spec)))
  666. (package->manifest-entry* package output))))
  667. (('install . obj)
  668. (leave (G_ "cannot install non-package object: ~s~%")
  669. obj))
  670. (_
  671. #f))
  672. opts))
  673. (fold manifest-transaction-install-entry
  674. upgraded
  675. to-install))
  676. (define (options->removable options manifest transaction)
  677. "Given options, return a variant of TRANSACTION augmented with the list of
  678. patterns of packages to remove."
  679. (fold (lambda (opt transaction)
  680. (match opt
  681. (('remove . spec)
  682. (call-with-values
  683. (lambda ()
  684. (package-specification->name+version+output spec))
  685. (lambda (name version output)
  686. (manifest-transaction-remove-pattern
  687. (manifest-pattern
  688. (name name)
  689. (version version)
  690. (output output))
  691. transaction))))
  692. (_ transaction)))
  693. transaction
  694. options))
  695. (define (register-gc-root store profile)
  696. "Register PROFILE, a profile generation symlink, as a GC root, unless it
  697. doesn't need it."
  698. (define absolute
  699. ;; We must pass the daemon an absolute file name for PROFILE. However, we
  700. ;; cannot use (canonicalize-path profile) because that would return us the
  701. ;; target of PROFILE in the store; using a store item as an indirect root
  702. ;; would mean that said store item will always remain live, which is not
  703. ;; what we want here.
  704. (if (string-prefix? "/" profile)
  705. profile
  706. (string-append (getcwd) "/" profile)))
  707. (add-indirect-root store absolute))
  708. ;;;
  709. ;;; Queries and actions.
  710. ;;;
  711. (define (process-query opts)
  712. "Process any query specified by OPTS. Return #t when a query was actually
  713. processed, #f otherwise."
  714. (let* ((profiles (delete-duplicates
  715. (match (filter-map (match-lambda
  716. (('profile . p) p)
  717. (_ #f))
  718. opts)
  719. (() (list %current-profile))
  720. (lst (reverse lst)))))
  721. (profile (match profiles
  722. ((head tail ...) head))))
  723. (match (assoc-ref opts 'query)
  724. (('list-generations pattern)
  725. (define (list-generation display-function number)
  726. (unless (zero? number)
  727. (display-generation profile number)
  728. (display-function profile number)
  729. (newline)))
  730. (define (diff-profiles profile numbers)
  731. (unless (null-list? (cdr numbers))
  732. (display-profile-content-diff profile (car numbers) (cadr numbers))
  733. (diff-profiles profile (cdr numbers))))
  734. (leave-on-EPIPE
  735. (cond ((not (file-exists? profile)) ; XXX: race condition
  736. (raise (condition (&profile-not-found-error
  737. (profile profile)))))
  738. ((not pattern)
  739. (match (profile-generations profile)
  740. (()
  741. #t)
  742. ((first rest ...)
  743. (list-generation display-profile-content first)
  744. (diff-profiles profile (cons first rest)))))
  745. ((matching-generations pattern profile)
  746. =>
  747. (lambda (numbers)
  748. (if (null-list? numbers)
  749. (exit 1)
  750. (begin
  751. (list-generation display-profile-content (car numbers))
  752. (diff-profiles profile numbers)))))))
  753. #t)
  754. (('list-installed regexp)
  755. (let* ((regexp (and regexp (make-regexp* regexp regexp/icase)))
  756. (manifest (concatenate-manifests
  757. (map profile-manifest profiles)))
  758. (installed (manifest-entries manifest)))
  759. (leave-on-EPIPE
  760. (let ((rows (filter-map
  761. (match-lambda
  762. (($ <manifest-entry> name version output path _)
  763. (and (regexp-exec regexp name)
  764. (list name (or version "?") output path))))
  765. installed)))
  766. ;; Show most recently installed packages last.
  767. (pretty-print-table (reverse rows)))))
  768. #t)
  769. (('list-available regexp)
  770. (let* ((regexp (and regexp (make-regexp* regexp regexp/icase)))
  771. (available (fold-available-packages
  772. (lambda* (name version result
  773. #:key outputs location
  774. supported? deprecated?
  775. #:allow-other-keys)
  776. (if (and supported? (not deprecated?))
  777. (if regexp
  778. (if (regexp-exec regexp name)
  779. (cons `(,name ,version
  780. ,outputs ,location)
  781. result)
  782. result)
  783. (cons `(,name ,version
  784. ,outputs ,location)
  785. result))
  786. result))
  787. '())))
  788. (leave-on-EPIPE
  789. (let ((rows (map (match-lambda
  790. ((name version outputs location)
  791. (list name version (string-join outputs ",")
  792. (location->string location))))
  793. (sort available
  794. (match-lambda*
  795. (((name1 . _) (name2 . _))
  796. (string<? name1 name2)))))))
  797. (pretty-print-table rows)))
  798. #t))
  799. (('list-profiles _)
  800. (let ((profiles (delete-duplicates
  801. (filter-map (lambda (root)
  802. (and (or (zero? (getuid))
  803. (user-owned? root))
  804. (generation-profile root)))
  805. (gc-roots)))))
  806. (leave-on-EPIPE
  807. (for-each (lambda (profile)
  808. (display (user-friendly-profile profile))
  809. (newline))
  810. (sort profiles string<?)))))
  811. (('search _)
  812. (let* ((patterns (filter-map (match-lambda
  813. (('query 'search rx) rx)
  814. (_ #f))
  815. opts))
  816. (regexps (map (cut make-regexp* <> regexp/icase) patterns))
  817. (matches (find-packages-by-description regexps)))
  818. (leave-on-EPIPE
  819. (display-search-results matches (current-output-port)))
  820. #t))
  821. (('show _)
  822. (let ((requested-names
  823. (filter-map (match-lambda
  824. (('query 'show requested-name) requested-name)
  825. (_ #f))
  826. opts)))
  827. (for-each
  828. (lambda (requested-name)
  829. (let-values (((name version)
  830. (package-name->name+version requested-name)))
  831. (match (remove package-superseded
  832. (find-packages-by-name name version))
  833. (()
  834. (leave (G_ "~a~@[@~a~]: package not found~%") name version))
  835. (packages
  836. (leave-on-EPIPE
  837. (for-each (cute package->recutils <> (current-output-port))
  838. packages))))))
  839. requested-names))
  840. #t)
  841. (('search-paths kind)
  842. (let* ((manifests (map profile-manifest profiles))
  843. (entries (append-map manifest-transitive-entries
  844. manifests))
  845. (profiles (map user-friendly-profile profiles))
  846. (settings (search-path-environment-variables entries profiles
  847. (const #f)
  848. #:kind kind)))
  849. (format #t "~{~a~%~}" settings)
  850. #t))
  851. (('export-manifest)
  852. (let* ((manifest (concatenate-manifests
  853. (map profile-manifest profiles))))
  854. (export-manifest manifest (current-output-port))
  855. #t))
  856. (('export-channels)
  857. (let ((manifest (concatenate-manifests
  858. (map profile-manifest profiles))))
  859. (export-channels manifest (current-output-port))
  860. #t))
  861. (_ #f))))
  862. (define* (roll-back-action store profile arg opts
  863. #:key dry-run?)
  864. "Roll back PROFILE to its previous generation."
  865. (unless dry-run?
  866. (roll-back* store profile)))
  867. (define* (switch-generation-action store profile spec opts
  868. #:key dry-run?)
  869. "Switch PROFILE to the generation specified by SPEC."
  870. (unless dry-run?
  871. (let ((number (relative-generation-spec->number profile spec)))
  872. (if number
  873. (switch-to-generation* profile number)
  874. (leave (G_ "cannot switch to generation '~a'~%") spec)))))
  875. (define* (delete-generations-action store profile pattern opts
  876. #:key dry-run?)
  877. "Delete PROFILE's generations that match PATTERN."
  878. (unless dry-run?
  879. (delete-matching-generations store profile pattern)))
  880. (define (load-manifest file)
  881. "Load the user-profile manifest (Scheme code) from FILE and return it."
  882. (let ((user-module (make-user-module '((guix profiles) (gnu)))))
  883. (load* file user-module)))
  884. (define %actions
  885. ;; List of actions that may be processed. The car of each pair is the
  886. ;; action's symbol in the option list; the cdr is the action's procedure.
  887. `((roll-back? . ,roll-back-action)
  888. (switch-generation . ,switch-generation-action)
  889. (delete-generations . ,delete-generations-action)))
  890. (define (process-actions store opts)
  891. "Process any install/remove/upgrade action from OPTS."
  892. (define dry-run? (assoc-ref opts 'dry-run?))
  893. (define bootstrap? (assoc-ref opts 'bootstrap?))
  894. (define substitutes? (assoc-ref opts 'substitutes?))
  895. (define allow-collisions? (assoc-ref opts 'allow-collisions?))
  896. (define profile (or (assoc-ref opts 'profile) %current-profile))
  897. (define transform (options->transformation opts))
  898. (define (transform-entry entry)
  899. (let ((item (transform (manifest-entry-item entry))))
  900. (manifest-entry-with-transformations
  901. (manifest-entry
  902. (inherit entry)
  903. (item item)
  904. (version (if (package? item)
  905. (package-version item)
  906. (manifest-entry-version entry)))))))
  907. (when (equal? profile %current-profile)
  908. ;; Normally the daemon created %CURRENT-PROFILE when we connected, unless
  909. ;; it's a version that lacks the fix for <https://bugs.gnu.org/37744>
  910. ;; (aka. CVE-2019-18192). Ensure %CURRENT-PROFILE exists so that
  911. ;; 'with-profile-lock' can create its lock file below.
  912. (ensure-default-profile))
  913. ;; First, acquire a lock on the profile, to ensure only one guix process
  914. ;; is modifying it at a time.
  915. (with-profile-lock profile
  916. ;; Then, process roll-backs, generation removals, etc.
  917. (for-each (match-lambda
  918. ((key . arg)
  919. (and=> (assoc-ref %actions key)
  920. (lambda (proc)
  921. (proc store profile arg opts
  922. #:dry-run? dry-run?)))))
  923. opts)
  924. ;; Then, process normal package removal/installation/upgrade.
  925. (let* ((files (filter-map (match-lambda
  926. (('manifest . file) file)
  927. (_ #f))
  928. opts))
  929. (manifest (match files
  930. (() (profile-manifest profile))
  931. (_ (map-manifest-entries
  932. manifest-entry-with-provenance
  933. (concatenate-manifests
  934. (map load-manifest files))))))
  935. (step1 (options->removable opts manifest
  936. (manifest-transaction)))
  937. (step2 (options->installable opts manifest step1))
  938. (step3 (manifest-transaction
  939. (inherit step2)
  940. (install (map transform-entry
  941. (manifest-transaction-install step2)))))
  942. (new (manifest-perform-transaction manifest step3))
  943. (trans (if (null? files)
  944. step3
  945. (fold manifest-transaction-install-entry
  946. step3
  947. (manifest-entries manifest)))))
  948. (warn-about-old-distro)
  949. (when (and (null? files) (manifest-transaction-null? trans)
  950. (not (any (match-lambda
  951. ((key . _) (assoc-ref %actions key)))
  952. opts)))
  953. ;; We can reach this point because the user did not specify any action
  954. ;; (as in "guix package"), did not specify any package (as in "guix
  955. ;; install"), or because there's nothing to upgrade (as when running
  956. ;; "guix upgrade" on an up-to-date profile). We cannot distinguish
  957. ;; among these here; all we can say is that there's nothing to do.
  958. (warning (G_ "nothing to do~%")))
  959. (unless (manifest-transaction-null? trans)
  960. ;; When '--manifest' is used, display information about TRANS as if we
  961. ;; were starting from an empty profile.
  962. (show-manifest-transaction store
  963. (if (null? files)
  964. manifest
  965. (make-manifest '()))
  966. trans
  967. #:dry-run? dry-run?)
  968. (build-and-use-profile store profile new
  969. #:dry-run? dry-run?
  970. #:allow-collisions? allow-collisions?
  971. #:bootstrap? bootstrap?)))))
  972. ;;;
  973. ;;; Entry point.
  974. ;;;
  975. (define-command (guix-package . args)
  976. (synopsis "manage packages and profiles")
  977. (define (handle-argument arg result arg-handler)
  978. ;; Process non-option argument ARG by calling back ARG-HANDLER.
  979. (if arg-handler
  980. (arg-handler arg result)
  981. (leave (G_ "~A: extraneous argument~%") arg)))
  982. (define opts
  983. (parse-command-line args %options (list %default-options #f)
  984. #:argument-handler handle-argument))
  985. (guix-package* opts))
  986. (define (guix-package* opts)
  987. "Run the 'guix package' command on OPTS, an alist resulting for command-line
  988. option processing with 'parse-command-line'."
  989. (with-error-handling
  990. (or (process-query opts)
  991. (parameterize ((%store (open-connection))
  992. (%graft? (assoc-ref opts 'graft?)))
  993. (with-status-verbosity (assoc-ref opts 'verbosity)
  994. (set-build-options-from-command-line (%store) opts)
  995. (with-build-handler (build-notifier #:use-substitutes?
  996. (assoc-ref opts 'substitutes?)
  997. #:verbosity
  998. (assoc-ref opts 'verbosity)
  999. #:dry-run?
  1000. (assoc-ref opts 'dry-run?))
  1001. (parameterize ((%guile-for-build
  1002. (package-derivation
  1003. (%store)
  1004. (if (assoc-ref opts 'bootstrap?)
  1005. %bootstrap-guile
  1006. (default-guile)))))
  1007. (process-actions (%store) opts))))))))