package.scm 46 KB

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