package.scm 46 KB

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