package.scm 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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. transaction-upgrade-entry ;mostly for testing
  70. (%options . %package-options)
  71. (%default-options . %package-default-options)
  72. guix-package*))
  73. (define %store
  74. (make-parameter #f))
  75. ;;;
  76. ;;; Profiles.
  77. ;;;
  78. (define (ensure-default-profile)
  79. "Ensure the default profile symlink and directory exist and are writable."
  80. (ensure-profile-directory)
  81. ;; Try to create ~/.guix-profile if it doesn't exist yet.
  82. (when (and %user-profile-directory
  83. %current-profile
  84. (not (false-if-exception
  85. (lstat %user-profile-directory))))
  86. (catch 'system-error
  87. (lambda ()
  88. (symlink %current-profile %user-profile-directory))
  89. (const #t))))
  90. (define (delete-generations store profile generations)
  91. "Delete GENERATIONS from PROFILE.
  92. GENERATIONS is a list of generation numbers."
  93. (for-each (cut delete-generation* store profile <>)
  94. generations))
  95. (define (delete-matching-generations store profile pattern)
  96. "Delete from PROFILE all the generations matching PATTERN. PATTERN must be
  97. a string denoting a set of generations: the empty list means \"all generations
  98. but the current one\", a number designates a generation, and other patterns
  99. denote ranges as interpreted by 'matching-generations'."
  100. (let ((current (generation-number profile)))
  101. (cond ((not (file-exists? profile)) ; XXX: race condition
  102. (raise (condition (&profile-not-found-error
  103. (profile profile)))))
  104. ((not pattern)
  105. (delete-generations store profile
  106. (delv current (profile-generations profile))))
  107. ;; Do not delete the zeroth generation.
  108. ((equal? 0 (string->number pattern))
  109. #t)
  110. ;; If PATTERN is a duration, match generations that are
  111. ;; older than the specified duration.
  112. ((matching-generations pattern profile
  113. #:duration-relation >)
  114. =>
  115. (lambda (numbers)
  116. (when (memv current numbers)
  117. (warning (G_ "not removing generation ~a, which is current~%")
  118. current))
  119. ;; Make sure we don't inadvertently remove the current
  120. ;; generation.
  121. (let ((numbers (delv current numbers)))
  122. (when (null-list? numbers)
  123. (leave (G_ "no matching generation~%")))
  124. (delete-generations store profile numbers)))))))
  125. (define* (build-and-use-profile store profile manifest
  126. #:key
  127. (hooks %default-profile-hooks)
  128. allow-collisions?
  129. bootstrap?)
  130. "Build a new generation of PROFILE, a file name, using the packages
  131. specified in MANIFEST, a manifest object. When ALLOW-COLLISIONS? is true,
  132. do not treat collisions in MANIFEST as an error. HOOKS is a list of \"profile
  133. hooks\" run when building the profile."
  134. (let* ((prof-drv (run-with-store store
  135. (profile-derivation manifest
  136. #:allow-collisions? allow-collisions?
  137. #:hooks (if bootstrap? '() hooks)
  138. #:locales? (not bootstrap?))))
  139. (prof (derivation->output-path prof-drv)))
  140. (cond
  141. ((and (file-exists? profile)
  142. (and=> (readlink* profile) (cut string=? prof <>)))
  143. (format (current-error-port) (G_ "nothing to be done~%")))
  144. (else
  145. (let* ((number (generation-number profile))
  146. ;; Always use NUMBER + 1 for the new profile, possibly
  147. ;; overwriting a "previous future generation".
  148. (name (generation-file-name profile (+ 1 number))))
  149. (and (build-derivations store (list prof-drv))
  150. (let* ((entries (manifest-entries manifest))
  151. (count (length entries)))
  152. (switch-symlinks name prof)
  153. (switch-symlinks profile (basename name))
  154. (unless (string=? profile %current-profile)
  155. (register-gc-root store name))
  156. (display-search-path-hint entries profile)))
  157. (warn-about-disk-space profile))))))
  158. ;;;
  159. ;;; Package specifications.
  160. ;;;
  161. (define (find-packages-by-description regexps)
  162. "Return a list of pairs: packages whose name, synopsis, description,
  163. or output matches at least one of REGEXPS sorted by relevance, and its
  164. non-zero relevance score."
  165. (let ((matches (fold-packages (lambda (package result)
  166. (if (package-superseded package)
  167. result
  168. (match (package-relevance package
  169. regexps)
  170. ((? zero?)
  171. result)
  172. (score
  173. (cons (cons package score)
  174. result)))))
  175. '())))
  176. (sort matches
  177. (lambda (m1 m2)
  178. (match m1
  179. ((package1 . score1)
  180. (match m2
  181. ((package2 . score2)
  182. (if (= score1 score2)
  183. (string>? (package-full-name package1)
  184. (package-full-name package2))
  185. (> score1 score2))))))))))
  186. (define (transaction-upgrade-entry store entry transaction)
  187. "Return a variant of TRANSACTION that accounts for the upgrade of ENTRY, a
  188. <manifest-entry>."
  189. (define (lower-manifest-entry* entry)
  190. (run-with-store store
  191. (lower-manifest-entry entry (%current-system))))
  192. (define (supersede old new)
  193. (info (G_ "package '~a' has been superseded by '~a'~%")
  194. (manifest-entry-name old) (package-name new))
  195. (manifest-transaction-install-entry
  196. (package->manifest-entry* new (manifest-entry-output old))
  197. (manifest-transaction-remove-pattern
  198. (manifest-pattern
  199. (name (manifest-entry-name old))
  200. (version (manifest-entry-version old))
  201. (output (manifest-entry-output old)))
  202. transaction)))
  203. (define (upgrade entry transform)
  204. (match entry
  205. (($ <manifest-entry> name version output (? string? path))
  206. (match (find-best-packages-by-name name #f)
  207. ((pkg . rest)
  208. (let* ((pkg (transform pkg))
  209. (candidate-version (package-version pkg)))
  210. (match (package-superseded pkg)
  211. ((? package? new)
  212. (supersede entry new))
  213. (#f
  214. (case (version-compare candidate-version version)
  215. ((>)
  216. (manifest-transaction-install-entry
  217. (package->manifest-entry* pkg output)
  218. transaction))
  219. ((<)
  220. transaction)
  221. ((=)
  222. (let* ((new (package->manifest-entry* pkg output)))
  223. ;; Here we want to determine whether the NEW actually
  224. ;; differs from ENTRY, but we need to intercept
  225. ;; 'build-things' calls because they would prevent us from
  226. ;; displaying the list of packages to install/upgrade
  227. ;; upfront. Thus, if lowering NEW triggers a build (due
  228. ;; to grafts), assume NEW differs from ENTRY.
  229. (if (with-build-handler (const #f)
  230. (manifest-entry=? (lower-manifest-entry* new)
  231. entry))
  232. transaction
  233. (manifest-transaction-install-entry
  234. new transaction)))))))))
  235. (()
  236. (warning (G_ "package '~a' no longer exists~%") name)
  237. transaction)))))
  238. (if (manifest-transaction-removal-candidate? entry transaction)
  239. transaction
  240. ;; Upgrade ENTRY, preserving transformation options listed in its
  241. ;; properties.
  242. (let ((transform (options->transformation
  243. (or (assq-ref (manifest-entry-properties entry)
  244. 'transformations)
  245. '()))))
  246. (upgrade entry transform))))
  247. ;;;
  248. ;;; Search paths.
  249. ;;;
  250. (define* (search-path-environment-variables entries profiles
  251. #:optional (getenv getenv)
  252. #:key (kind 'exact))
  253. "Return environment variable definitions that may be needed for the use of
  254. ENTRIES, a list of manifest entries, in PROFILES. Use GETENV to determine the
  255. current settings and report only settings not already effective. KIND
  256. must be one of 'exact, 'prefix, or 'suffix, depending on the kind of search
  257. path definition to be returned."
  258. (let ((search-paths (delete-duplicates
  259. (cons $PATH
  260. (append-map manifest-entry-search-paths
  261. entries)))))
  262. (filter-map (match-lambda
  263. ((spec . value)
  264. (let ((variable (search-path-specification-variable spec))
  265. (sep (search-path-specification-separator spec)))
  266. (environment-variable-definition variable value
  267. #:separator sep
  268. #:kind kind))))
  269. (evaluate-search-paths search-paths profiles
  270. getenv))))
  271. (define (absolutize file)
  272. "Return an absolute file name equivalent to FILE, but without resolving
  273. symlinks like 'canonicalize-path' would do."
  274. (if (string-prefix? "/" file)
  275. file
  276. (string-append (getcwd) "/" file)))
  277. (define (display-search-path-hint entries profile)
  278. "Display a hint on how to set environment variables to use ENTRIES, a list
  279. of manifest entries, in the context of PROFILE."
  280. (let* ((profile (user-friendly-profile (absolutize profile)))
  281. (settings (search-path-environment-variables entries (list profile)
  282. #:kind 'prefix)))
  283. (unless (null? settings)
  284. (display-hint (format #f (G_ "Consider setting the necessary environment
  285. variables by running:
  286. @example
  287. GUIX_PROFILE=\"~a\"
  288. . \"$GUIX_PROFILE/etc/profile\"
  289. @end example
  290. Alternately, see @command{guix package --search-paths -p ~s}.")
  291. profile profile)))))
  292. ;;;
  293. ;;; Export a manifest.
  294. ;;;
  295. (define* (export-manifest manifest
  296. #:optional (port (current-output-port)))
  297. "Write to PORT a manifest corresponding to MANIFEST."
  298. (define (version-spec entry)
  299. (let ((name (manifest-entry-name entry)))
  300. (match (map package-version (find-packages-by-name name))
  301. ((_)
  302. ;; A single version of NAME is available, so do not specify the
  303. ;; version number, even if the available version doesn't match ENTRY.
  304. "")
  305. (versions
  306. ;; If ENTRY uses the latest version, don't specify any version.
  307. ;; Otherwise return the shortest unique version prefix. Note that
  308. ;; this is based on the currently available packages, which could
  309. ;; differ from the packages available in the revision that was used
  310. ;; to build MANIFEST.
  311. (let ((current (manifest-entry-version entry)))
  312. (if (every (cut version>? current <>)
  313. (delete current versions))
  314. ""
  315. (version-unique-prefix (manifest-entry-version entry)
  316. versions)))))))
  317. (match (manifest->code manifest
  318. #:entry-package-version version-spec)
  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 transaction)
  652. "Given MANIFEST, the current manifest, and OPTS, the result of 'args-fold',
  653. return an variant of TRANSACTION that accounts for the specified installations
  654. and upgrades."
  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* 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* package output))))
  676. (('install . obj)
  677. (leave (G_ "cannot install non-package object: ~s~%")
  678. obj))
  679. (_
  680. #f))
  681. opts))
  682. (fold manifest-transaction-install-entry
  683. upgraded
  684. to-install))
  685. (define (options->removable options manifest transaction)
  686. "Given options, return a variant of TRANSACTION augmented with the list of
  687. patterns of packages to remove."
  688. (fold (lambda (opt transaction)
  689. (match opt
  690. (('remove . spec)
  691. (call-with-values
  692. (lambda ()
  693. (package-specification->name+version+output spec))
  694. (lambda (name version output)
  695. (manifest-transaction-remove-pattern
  696. (manifest-pattern
  697. (name name)
  698. (version version)
  699. (output output))
  700. transaction))))
  701. (_ transaction)))
  702. transaction
  703. options))
  704. (define (register-gc-root store profile)
  705. "Register PROFILE, a profile generation symlink, as a GC root, unless it
  706. doesn't need it."
  707. (define absolute
  708. ;; We must pass the daemon an absolute file name for PROFILE. However, we
  709. ;; cannot use (canonicalize-path profile) because that would return us the
  710. ;; target of PROFILE in the store; using a store item as an indirect root
  711. ;; would mean that said store item will always remain live, which is not
  712. ;; what we want here.
  713. (if (string-prefix? "/" profile)
  714. profile
  715. (string-append (getcwd) "/" profile)))
  716. (add-indirect-root store absolute))
  717. ;;;
  718. ;;; Queries and actions.
  719. ;;;
  720. (define (process-query opts)
  721. "Process any query specified by OPTS. Return #t when a query was actually
  722. processed, #f otherwise."
  723. (let* ((profiles (delete-duplicates
  724. (match (filter-map (match-lambda
  725. (('profile . p) p)
  726. (_ #f))
  727. opts)
  728. (() (list %current-profile))
  729. (lst (reverse lst)))))
  730. (profile (match profiles
  731. ((head tail ...) head))))
  732. (match (assoc-ref opts 'query)
  733. (('list-generations pattern)
  734. (define (list-generation display-function number)
  735. (unless (zero? number)
  736. (display-generation profile number)
  737. (display-function profile number)
  738. (newline)))
  739. (define (diff-profiles profile numbers)
  740. (unless (null-list? (cdr numbers))
  741. (display-profile-content-diff profile (car numbers) (cadr numbers))
  742. (diff-profiles profile (cdr numbers))))
  743. (leave-on-EPIPE
  744. (cond ((not (file-exists? profile)) ; XXX: race condition
  745. (raise (condition (&profile-not-found-error
  746. (profile profile)))))
  747. ((not pattern)
  748. (match (profile-generations profile)
  749. (()
  750. #t)
  751. ((first rest ...)
  752. (list-generation display-profile-content first)
  753. (diff-profiles profile (cons first rest)))))
  754. ((matching-generations pattern profile)
  755. =>
  756. (lambda (numbers)
  757. (if (null-list? numbers)
  758. (exit 1)
  759. (begin
  760. (list-generation display-profile-content (car numbers))
  761. (diff-profiles profile numbers)))))))
  762. #t)
  763. (('list-installed regexp)
  764. (let* ((regexp (and regexp (make-regexp* regexp regexp/icase)))
  765. (manifest (concatenate-manifests
  766. (map profile-manifest profiles)))
  767. (installed (manifest-entries manifest)))
  768. (leave-on-EPIPE
  769. (let ((rows (filter-map
  770. (match-lambda
  771. (($ <manifest-entry> name version output path _)
  772. (and (regexp-exec regexp name)
  773. (list name (or version "?") output path))))
  774. installed)))
  775. ;; Show most recently installed packages last.
  776. (pretty-print-table (reverse rows)))))
  777. #t)
  778. (('list-available regexp)
  779. (let* ((regexp (and regexp (make-regexp* regexp regexp/icase)))
  780. (available (fold-available-packages
  781. (lambda* (name version result
  782. #:key outputs location
  783. supported? deprecated?
  784. #:allow-other-keys)
  785. (if (and supported? (not deprecated?))
  786. (if regexp
  787. (if (regexp-exec regexp name)
  788. (cons `(,name ,version
  789. ,outputs ,location)
  790. result)
  791. result)
  792. (cons `(,name ,version
  793. ,outputs ,location)
  794. result))
  795. result))
  796. '())))
  797. (leave-on-EPIPE
  798. (let ((rows (map (match-lambda
  799. ((name version outputs location)
  800. (list name version (string-join outputs ",")
  801. (location->string location))))
  802. (sort available
  803. (match-lambda*
  804. (((name1 . _) (name2 . _))
  805. (string<? name1 name2)))))))
  806. (pretty-print-table rows)))
  807. #t))
  808. (('list-profiles _)
  809. (let ((profiles (delete-duplicates
  810. (filter-map (lambda (root)
  811. (and (or (zero? (getuid))
  812. (user-owned? root))
  813. (generation-profile root)))
  814. (gc-roots)))))
  815. (leave-on-EPIPE
  816. (for-each (lambda (profile)
  817. (display (user-friendly-profile profile))
  818. (newline))
  819. (sort profiles string<?)))))
  820. (('search _)
  821. (let* ((patterns (filter-map (match-lambda
  822. (('query 'search rx) rx)
  823. (_ #f))
  824. opts))
  825. (regexps (map (cut make-regexp* <> regexp/icase) patterns))
  826. (matches (find-packages-by-description regexps)))
  827. (leave-on-EPIPE
  828. (display-search-results matches (current-output-port)))
  829. #t))
  830. (('show _)
  831. (let ((requested-names
  832. (filter-map (match-lambda
  833. (('query 'show requested-name) requested-name)
  834. (_ #f))
  835. opts)))
  836. (for-each
  837. (lambda (requested-name)
  838. (let-values (((name version)
  839. (package-name->name+version requested-name)))
  840. (match (remove package-superseded
  841. (find-packages-by-name name version))
  842. (()
  843. (leave (G_ "~a~@[@~a~]: package not found~%") name version))
  844. (packages
  845. (leave-on-EPIPE
  846. (for-each (cute package->recutils <> (current-output-port))
  847. packages))))))
  848. requested-names))
  849. #t)
  850. (('search-paths kind)
  851. (let* ((manifests (map profile-manifest profiles))
  852. (entries (append-map manifest-transitive-entries
  853. manifests))
  854. (profiles (map user-friendly-profile profiles))
  855. (settings (search-path-environment-variables entries profiles
  856. (const #f)
  857. #:kind kind)))
  858. (format #t "~{~a~%~}" settings)
  859. #t))
  860. (('export-manifest)
  861. (let* ((manifest (concatenate-manifests
  862. (map profile-manifest profiles))))
  863. (export-manifest manifest (current-output-port))
  864. #t))
  865. (('export-channels)
  866. (let ((manifest (concatenate-manifests
  867. (map profile-manifest profiles))))
  868. (export-channels manifest (current-output-port))
  869. #t))
  870. (_ #f))))
  871. (define* (roll-back-action store profile arg opts
  872. #:key dry-run?)
  873. "Roll back PROFILE to its previous generation."
  874. (unless dry-run?
  875. (roll-back* store profile)))
  876. (define* (switch-generation-action store profile spec opts
  877. #:key dry-run?)
  878. "Switch PROFILE to the generation specified by SPEC."
  879. (unless dry-run?
  880. (let ((number (relative-generation-spec->number profile spec)))
  881. (if number
  882. (switch-to-generation* profile number)
  883. (leave (G_ "cannot switch to generation '~a'~%") spec)))))
  884. (define* (delete-generations-action store profile pattern opts
  885. #:key dry-run?)
  886. "Delete PROFILE's generations that match PATTERN."
  887. (unless dry-run?
  888. (delete-matching-generations store profile pattern)))
  889. (define (load-manifest file)
  890. "Load the user-profile manifest (Scheme code) from FILE and return it."
  891. (let ((user-module (make-user-module '((guix profiles) (gnu)))))
  892. (load* file user-module)))
  893. (define %actions
  894. ;; List of actions that may be processed. The car of each pair is the
  895. ;; action's symbol in the option list; the cdr is the action's procedure.
  896. `((roll-back? . ,roll-back-action)
  897. (switch-generation . ,switch-generation-action)
  898. (delete-generations . ,delete-generations-action)))
  899. (define (process-actions store opts)
  900. "Process any install/remove/upgrade action from OPTS."
  901. (define dry-run? (assoc-ref opts 'dry-run?))
  902. (define bootstrap? (assoc-ref opts 'bootstrap?))
  903. (define substitutes? (assoc-ref opts 'substitutes?))
  904. (define allow-collisions? (assoc-ref opts 'allow-collisions?))
  905. (define profile (or (assoc-ref opts 'profile) %current-profile))
  906. (define transform (options->transformation opts))
  907. (define (transform-entry entry)
  908. (let ((item (transform (manifest-entry-item entry))))
  909. (manifest-entry-with-transformations
  910. (manifest-entry
  911. (inherit entry)
  912. (item item)
  913. (version (if (package? item)
  914. (package-version item)
  915. (manifest-entry-version entry)))))))
  916. (when (equal? profile %current-profile)
  917. ;; Normally the daemon created %CURRENT-PROFILE when we connected, unless
  918. ;; it's a version that lacks the fix for <https://bugs.gnu.org/37744>
  919. ;; (aka. CVE-2019-18192). Ensure %CURRENT-PROFILE exists so that
  920. ;; 'with-profile-lock' can create its lock file below.
  921. (ensure-default-profile))
  922. ;; First, acquire a lock on the profile, to ensure only one guix process
  923. ;; is modifying it at a time.
  924. (with-profile-lock profile
  925. ;; Then, process roll-backs, generation removals, etc.
  926. (for-each (match-lambda
  927. ((key . arg)
  928. (and=> (assoc-ref %actions key)
  929. (lambda (proc)
  930. (proc store profile arg opts
  931. #:dry-run? dry-run?)))))
  932. opts)
  933. ;; Then, process normal package removal/installation/upgrade.
  934. (let* ((files (filter-map (match-lambda
  935. (('manifest . file) file)
  936. (_ #f))
  937. opts))
  938. (manifest (match files
  939. (() (profile-manifest profile))
  940. (_ (map-manifest-entries
  941. manifest-entry-with-provenance
  942. (concatenate-manifests
  943. (map load-manifest files))))))
  944. (step1 (options->removable opts manifest
  945. (manifest-transaction)))
  946. (step2 (options->installable opts manifest step1))
  947. (step3 (manifest-transaction
  948. (inherit step2)
  949. (install (map transform-entry
  950. (manifest-transaction-install step2)))))
  951. (new (manifest-perform-transaction manifest step3))
  952. (trans (if (null? files)
  953. step3
  954. (fold manifest-transaction-install-entry
  955. step3
  956. (manifest-entries manifest)))))
  957. (warn-about-old-distro)
  958. (when (and (null? files) (manifest-transaction-null? trans)
  959. (not (any (match-lambda
  960. ((key . _) (assoc-ref %actions key)))
  961. opts)))
  962. ;; We can reach this point because the user did not specify any action
  963. ;; (as in "guix package"), did not specify any package (as in "guix
  964. ;; install"), or because there's nothing to upgrade (as when running
  965. ;; "guix upgrade" on an up-to-date profile). We cannot distinguish
  966. ;; among these here; all we can say is that there's nothing to do.
  967. (warning (G_ "nothing to do~%")))
  968. (unless (manifest-transaction-null? trans)
  969. ;; When '--manifest' is used, display information about TRANS as if we
  970. ;; were starting from an empty profile.
  971. (show-manifest-transaction store
  972. (if (null? files)
  973. manifest
  974. (make-manifest '()))
  975. trans
  976. #:dry-run? dry-run?)
  977. (build-and-use-profile store profile new
  978. #:allow-collisions? allow-collisions?
  979. #:bootstrap? bootstrap?)))))
  980. ;;;
  981. ;;; Entry point.
  982. ;;;
  983. (define-command (guix-package . args)
  984. (synopsis "manage packages and profiles")
  985. (define (handle-argument arg result arg-handler)
  986. ;; Process non-option argument ARG by calling back ARG-HANDLER.
  987. (if arg-handler
  988. (arg-handler arg result)
  989. (leave (G_ "~A: extraneous argument~%") arg)))
  990. (define opts
  991. (parse-command-line args %options (list %default-options #f)
  992. #:argument-handler handle-argument))
  993. (guix-package* opts))
  994. (define (guix-package* opts)
  995. "Run the 'guix package' command on OPTS, an alist resulting for command-line
  996. option processing with 'parse-command-line'."
  997. (with-error-handling
  998. (or (process-query opts)
  999. (parameterize ((%store (open-connection))
  1000. (%graft? (assoc-ref opts 'graft?)))
  1001. (with-status-verbosity (assoc-ref opts 'verbosity)
  1002. (set-build-options-from-command-line (%store) opts)
  1003. (with-build-handler (build-notifier #:use-substitutes?
  1004. (assoc-ref opts 'substitutes?)
  1005. #:verbosity
  1006. (assoc-ref opts 'verbosity)
  1007. #:dry-run?
  1008. (assoc-ref opts 'dry-run?))
  1009. (parameterize ((%guile-for-build
  1010. (package-derivation
  1011. (%store)
  1012. (if (assoc-ref opts 'bootstrap?)
  1013. %bootstrap-guile
  1014. (default-guile)))))
  1015. (process-actions (%store) opts))))))))