package.scm 41 KB

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