package.scm 41 KB

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