package.scm 46 KB

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