package.scm 45 KB

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