profiles.scm 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 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 © 2014, 2016 Alex Kost <alezost@gmail.com>
  5. ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
  6. ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
  7. ;;; Copyright © 2016, 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
  8. ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
  9. ;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
  10. ;;; Copyright © 2017 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  11. ;;; Copyright © 2019 Kyle Meyer <kyle@kyleam.com>
  12. ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  13. ;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
  14. ;;;
  15. ;;; This file is part of GNU Guix.
  16. ;;;
  17. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  18. ;;; under the terms of the GNU General Public License as published by
  19. ;;; the Free Software Foundation; either version 3 of the License, or (at
  20. ;;; your option) any later version.
  21. ;;;
  22. ;;; GNU Guix is distributed in the hope that it will be useful, but
  23. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. ;;; GNU General Public License for more details.
  26. ;;;
  27. ;;; You should have received a copy of the GNU General Public License
  28. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  29. (define-module (guix profiles)
  30. #:use-module ((guix config) #:select (%state-directory))
  31. #:use-module ((guix utils) #:hide (package-name->name+version))
  32. #:use-module ((guix build utils)
  33. #:select (package-name->name+version mkdir-p))
  34. #:use-module ((guix diagnostics) #:select (&fix-hint))
  35. #:use-module (guix i18n)
  36. #:use-module (guix records)
  37. #:use-module (guix packages)
  38. #:use-module (guix derivations)
  39. #:use-module (guix search-paths)
  40. #:use-module (guix gexp)
  41. #:use-module (guix modules)
  42. #:use-module (guix monads)
  43. #:use-module (guix store)
  44. #:use-module (ice-9 vlist)
  45. #:use-module (ice-9 match)
  46. #:use-module (ice-9 regex)
  47. #:use-module (ice-9 ftw)
  48. #:use-module (ice-9 format)
  49. #:use-module (srfi srfi-1)
  50. #:use-module (srfi srfi-9)
  51. #:use-module (srfi srfi-11)
  52. #:use-module (srfi srfi-19)
  53. #:use-module (srfi srfi-26)
  54. #:use-module (srfi srfi-34)
  55. #:use-module (srfi srfi-35)
  56. #:export (&profile-error
  57. profile-error?
  58. profile-error-profile
  59. &profile-not-found-error
  60. profile-not-found-error?
  61. &profile-collision-error
  62. profile-collision-error?
  63. profile-collision-error-entry
  64. profile-collision-error-conflict
  65. &missing-generation-error
  66. missing-generation-error?
  67. missing-generation-error-generation
  68. &unmatched-pattern-error
  69. unmatched-pattern-error?
  70. unmatched-pattern-error-pattern
  71. unmatched-pattern-error-manifest
  72. manifest make-manifest
  73. manifest?
  74. manifest-entries
  75. manifest-transitive-entries
  76. <manifest-entry> ; FIXME: eventually make it internal
  77. manifest-entry
  78. manifest-entry?
  79. manifest-entry-name
  80. manifest-entry-version
  81. manifest-entry-output
  82. manifest-entry-item
  83. manifest-entry-dependencies
  84. manifest-entry-search-paths
  85. manifest-entry-parent
  86. manifest-entry-properties
  87. lower-manifest-entry
  88. manifest-entry=?
  89. manifest-pattern
  90. manifest-pattern?
  91. manifest-pattern-name
  92. manifest-pattern-version
  93. manifest-pattern-output
  94. concatenate-manifests
  95. map-manifest-entries
  96. manifest-remove
  97. manifest-add
  98. manifest-lookup
  99. manifest-installed?
  100. manifest-matching-entries
  101. manifest-search-paths
  102. check-for-collisions
  103. manifest->code
  104. manifest-transaction
  105. manifest-transaction?
  106. manifest-transaction-install
  107. manifest-transaction-remove
  108. manifest-transaction-install-entry
  109. manifest-transaction-remove-pattern
  110. manifest-transaction-null?
  111. manifest-transaction-removal-candidate?
  112. manifest-perform-transaction
  113. manifest-transaction-effects
  114. profile-manifest
  115. package->manifest-entry
  116. packages->manifest
  117. ca-certificate-bundle
  118. %default-profile-hooks
  119. profile-derivation
  120. profile-search-paths
  121. profile
  122. profile?
  123. profile-name
  124. profile-content
  125. profile-hooks
  126. profile-locales?
  127. profile-allow-collisions?
  128. profile-relative-symlinks?
  129. generation-number
  130. generation-profile
  131. generation-numbers
  132. profile-generations
  133. relative-generation-spec->number
  134. relative-generation
  135. previous-generation-number
  136. generation-time
  137. generation-file-name
  138. switch-to-generation
  139. roll-back
  140. delete-generation
  141. %user-profile-directory
  142. %profile-directory
  143. %current-profile
  144. ensure-profile-directory
  145. canonicalize-profile
  146. user-friendly-profile
  147. linux-module-database))
  148. ;;; Commentary:
  149. ;;;
  150. ;;; Tools to create and manipulate profiles---i.e., the representation of a
  151. ;;; set of installed packages.
  152. ;;;
  153. ;;; Code:
  154. ;;;
  155. ;;; Condition types.
  156. ;;;
  157. (define-condition-type &profile-error &error
  158. profile-error?
  159. (profile profile-error-profile))
  160. (define-condition-type &profile-not-found-error &profile-error
  161. profile-not-found-error?)
  162. (define-condition-type &profile-collision-error &error
  163. profile-collision-error?
  164. (entry profile-collision-error-entry) ;<manifest-entry>
  165. (conflict profile-collision-error-conflict)) ;<manifest-entry>
  166. (define-condition-type &unmatched-pattern-error &error
  167. unmatched-pattern-error?
  168. (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
  169. (manifest unmatched-pattern-error-manifest)) ;<manifest>
  170. (define-condition-type &missing-generation-error &profile-error
  171. missing-generation-error?
  172. (generation missing-generation-error-generation))
  173. ;;;
  174. ;;; Manifests.
  175. ;;;
  176. (define-record-type <manifest>
  177. (manifest entries)
  178. manifest?
  179. (entries manifest-entries)) ; list of <manifest-entry>
  180. ;; Convenient alias, to avoid name clashes.
  181. (define make-manifest manifest)
  182. (define-record-type* <manifest-entry> manifest-entry
  183. make-manifest-entry
  184. manifest-entry?
  185. (name manifest-entry-name) ; string
  186. (version manifest-entry-version) ; string
  187. (output manifest-entry-output ; string
  188. (default "out"))
  189. (item manifest-entry-item) ; package | file-like | store path
  190. (dependencies manifest-entry-dependencies ; <manifest-entry>*
  191. (default '()))
  192. (search-paths manifest-entry-search-paths ; search-path-specification*
  193. (default '()))
  194. (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
  195. (default (delay #f)))
  196. (properties manifest-entry-properties ; list of symbol/value pairs
  197. (default '())))
  198. (define-record-type* <manifest-pattern> manifest-pattern
  199. make-manifest-pattern
  200. manifest-pattern?
  201. (name manifest-pattern-name) ; string
  202. (version manifest-pattern-version ; string | #f
  203. (default #f))
  204. (output manifest-pattern-output ; string | #f
  205. (default "out")))
  206. (define (list=? = lst1 lst2)
  207. "Return true if LST1 and LST2 have the same length and their elements are
  208. pairwise equal per =."
  209. (match lst1
  210. (()
  211. (null? lst2))
  212. ((head1 . tail1)
  213. (match lst2
  214. ((head2 . tail2)
  215. (and (= head1 head2) (list=? = tail1 tail2)))
  216. (()
  217. #f)))))
  218. (define (manifest-entry=? entry1 entry2)
  219. "Return true if ENTRY1 is equivalent to ENTRY2, ignoring their 'properties'
  220. field."
  221. (match entry1
  222. (($ <manifest-entry> name1 version1 output1 item1 dependencies1 paths1)
  223. (match entry2
  224. (($ <manifest-entry> name2 version2 output2 item2 dependencies2 paths2)
  225. (and (string=? name1 name2)
  226. (string=? version1 version2)
  227. (string=? output1 output2)
  228. (equal? item1 item2) ;XXX: could be <package> vs. store item
  229. (equal? paths1 paths2)
  230. (list=? manifest-entry=? dependencies1 dependencies2)))))))
  231. (define (manifest-transitive-entries manifest)
  232. "Return the entries of MANIFEST along with their propagated inputs,
  233. recursively."
  234. (let loop ((entries (manifest-entries manifest))
  235. (result '())
  236. (visited vlist-null)) ;compare with 'manifest-entry=?'
  237. (match entries
  238. (()
  239. (reverse result))
  240. ((head . tail)
  241. (if (vhash-assoc head visited manifest-entry=?)
  242. (loop tail result visited)
  243. (loop (append (manifest-entry-dependencies head)
  244. tail)
  245. (cons head result)
  246. (vhash-cons head #t visited)))))))
  247. (define (profile-manifest profile)
  248. "Return the PROFILE's manifest."
  249. (let ((file (string-append profile "/manifest")))
  250. (if (file-exists? file)
  251. (call-with-input-file file read-manifest)
  252. (manifest '()))))
  253. (define (manifest-entry-lookup manifest)
  254. "Return a lookup procedure for the entries of MANIFEST. The lookup
  255. procedure takes two arguments: the entry name and output."
  256. (define mapping
  257. (let loop ((entries (manifest-entries manifest))
  258. (mapping vlist-null))
  259. (fold (lambda (entry result)
  260. (vhash-cons (cons (manifest-entry-name entry)
  261. (manifest-entry-output entry))
  262. entry
  263. (loop (manifest-entry-dependencies entry)
  264. result)))
  265. mapping
  266. entries)))
  267. (lambda (name output)
  268. (match (vhash-assoc (cons name output) mapping)
  269. ((_ . entry) entry)
  270. (#f #f))))
  271. (define* (lower-manifest-entry entry system #:key target)
  272. "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
  273. file name."
  274. (define (recurse entry)
  275. (mapm/accumulate-builds (lambda (entry)
  276. (lower-manifest-entry entry system
  277. #:target target))
  278. (manifest-entry-dependencies entry)))
  279. (let ((item (manifest-entry-item entry)))
  280. (if (string? item)
  281. (with-monad %store-monad
  282. (return entry))
  283. (mlet %store-monad ((drv (lower-object item system
  284. #:target target))
  285. (dependencies (recurse entry))
  286. (output -> (manifest-entry-output entry)))
  287. (return (manifest-entry
  288. (inherit entry)
  289. (item (derivation->output-path drv output))
  290. (dependencies dependencies)))))))
  291. (define* (check-for-collisions manifest system #:key target)
  292. "Check whether the entries of MANIFEST conflict with one another; raise a
  293. '&profile-collision-error' when a conflict is encountered."
  294. (define lookup
  295. (manifest-entry-lookup manifest))
  296. (define candidates
  297. (filter-map (lambda (entry)
  298. (let ((other (lookup (manifest-entry-name entry)
  299. (manifest-entry-output entry))))
  300. (and other (list entry other))))
  301. (manifest-transitive-entries manifest)))
  302. (define lower-pair
  303. (match-lambda
  304. ((first second)
  305. (mlet %store-monad ((first (lower-manifest-entry first system
  306. #:target target))
  307. (second (lower-manifest-entry second system
  308. #:target target)))
  309. (return (list first second))))))
  310. ;; Start by lowering CANDIDATES "in parallel".
  311. (mlet %store-monad ((lst (mapm/accumulate-builds lower-pair candidates)))
  312. (foldm %store-monad
  313. (lambda (entries result)
  314. (match entries
  315. ((first second)
  316. (if (string=? (manifest-entry-item first)
  317. (manifest-entry-item second))
  318. (return result)
  319. (raise (condition
  320. (&profile-collision-error
  321. (entry first)
  322. (conflict second))))))))
  323. #t
  324. lst)))
  325. (define (default-properties package)
  326. "Return the default properties of a manifest entry for PACKAGE."
  327. ;; Preserve transformation options by default.
  328. (match (assq-ref (package-properties package) 'transformations)
  329. (#f '())
  330. (transformations `((transformations . ,transformations)))))
  331. (define* (package->manifest-entry package #:optional (output "out")
  332. #:key (parent (delay #f))
  333. (properties (default-properties package)))
  334. "Return a manifest entry for the OUTPUT of package PACKAGE."
  335. ;; For each dependency, keep a promise pointing to its "parent" entry.
  336. (letrec* ((deps (map (match-lambda
  337. ((label package)
  338. (package->manifest-entry package
  339. #:parent (delay entry)))
  340. ((label package output)
  341. (package->manifest-entry package output
  342. #:parent (delay entry))))
  343. (package-propagated-inputs package)))
  344. (entry (manifest-entry
  345. (name (package-name package))
  346. (version (package-version package))
  347. (output output)
  348. (item package)
  349. (dependencies (delete-duplicates deps))
  350. (search-paths
  351. (package-transitive-native-search-paths package))
  352. (parent parent)
  353. (properties properties))))
  354. entry))
  355. (define (packages->manifest packages)
  356. "Return a list of manifest entries, one for each item listed in PACKAGES.
  357. Elements of PACKAGES can be either package objects or package/string tuples
  358. denoting a specific output of a package."
  359. (define inferiors-loaded?
  360. ;; This hack allows us to provide seamless integration for inferior
  361. ;; packages while not having a hard dependency on (guix inferior).
  362. (resolve-module '(guix inferior) #f #f #:ensure #f))
  363. (define (inferior->entry)
  364. (module-ref (resolve-interface '(guix inferior))
  365. 'inferior-package->manifest-entry))
  366. (manifest
  367. (delete-duplicates
  368. (map (match-lambda
  369. (((? package? package) output)
  370. (package->manifest-entry package output))
  371. ((? package? package)
  372. (package->manifest-entry package))
  373. ((thing output)
  374. (if inferiors-loaded?
  375. ((inferior->entry) thing output)
  376. (throw 'wrong-type-arg 'packages->manifest
  377. "Wrong package object: ~S" (list thing) (list thing))))
  378. (thing
  379. (if inferiors-loaded?
  380. ((inferior->entry) thing)
  381. (throw 'wrong-type-arg 'packages->manifest
  382. "Wrong package object: ~S" (list thing) (list thing)))))
  383. packages)
  384. manifest-entry=?)))
  385. (define (manifest->gexp manifest)
  386. "Return a representation of MANIFEST as a gexp."
  387. (define (entry->gexp entry)
  388. (match entry
  389. (($ <manifest-entry> name version output (? string? path)
  390. (deps ...) (search-paths ...) _ (properties ...))
  391. #~(#$name #$version #$output #$path
  392. (propagated-inputs #$(map entry->gexp deps))
  393. (search-paths #$(map search-path-specification->sexp
  394. search-paths))
  395. (properties . #$properties)))
  396. (($ <manifest-entry> name version output package
  397. (deps ...) (search-paths ...) _ (properties ...))
  398. #~(#$name #$version #$output
  399. (ungexp package (or output "out"))
  400. (propagated-inputs #$(map entry->gexp deps))
  401. (search-paths #$(map search-path-specification->sexp
  402. search-paths))
  403. (properties . #$properties)))))
  404. (match manifest
  405. (($ <manifest> (entries ...))
  406. #~(manifest (version 3)
  407. (packages #$(map entry->gexp entries))))))
  408. (define (find-package name version)
  409. "Return a package from the distro matching NAME and possibly VERSION. This
  410. procedure is here for backward-compatibility and will eventually vanish."
  411. (define find-best-packages-by-name ;break abstractions
  412. (module-ref (resolve-interface '(gnu packages))
  413. 'find-best-packages-by-name))
  414. ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
  415. ;; former traverses the module tree only once and then allows for efficient
  416. ;; access via a vhash.
  417. (match (find-best-packages-by-name name version)
  418. ((p _ ...) p)
  419. (_
  420. (match (find-best-packages-by-name name #f)
  421. ((p _ ...) p)
  422. (_ #f)))))
  423. (define (sexp->manifest sexp)
  424. "Parse SEXP as a manifest."
  425. (define (infer-search-paths name version)
  426. ;; Infer the search path specifications for NAME-VERSION by looking up a
  427. ;; same-named package in the distro. Useful for the old manifest formats
  428. ;; that did not store search path info.
  429. (let ((package (find-package name version)))
  430. (if package
  431. (package-native-search-paths package)
  432. '())))
  433. (define (infer-dependency item parent)
  434. ;; Return a <manifest-entry> for ITEM.
  435. (let-values (((name version)
  436. (package-name->name+version
  437. (store-path-package-name item))))
  438. (manifest-entry
  439. (name name)
  440. (version version)
  441. (item item)
  442. (parent parent))))
  443. (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
  444. (match sexp
  445. ((name version output path
  446. ('propagated-inputs deps)
  447. ('search-paths search-paths)
  448. extra-stuff ...)
  449. ;; For each of DEPS, keep a promise pointing to ENTRY.
  450. (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
  451. deps))
  452. (entry (manifest-entry
  453. (name name)
  454. (version version)
  455. (output output)
  456. (item path)
  457. (dependencies deps*)
  458. (search-paths (map sexp->search-path-specification
  459. search-paths))
  460. (parent parent)
  461. (properties (or (assoc-ref extra-stuff 'properties)
  462. '())))))
  463. entry))))
  464. (match sexp
  465. (('manifest ('version 0)
  466. ('packages ((name version output path) ...)))
  467. (manifest
  468. (map (lambda (name version output path)
  469. (manifest-entry
  470. (name name)
  471. (version version)
  472. (output output)
  473. (item path)
  474. (search-paths (infer-search-paths name version))))
  475. name version output path)))
  476. ;; Version 1 adds a list of propagated inputs to the
  477. ;; name/version/output/path tuples.
  478. (('manifest ('version 1)
  479. ('packages ((name version output path deps) ...)))
  480. (manifest
  481. (map (lambda (name version output path deps)
  482. ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
  483. ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
  484. ;; such lists.
  485. (let ((deps (match deps
  486. (((labels directories) ...)
  487. directories)
  488. ((directories ...)
  489. directories))))
  490. (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
  491. deps))
  492. (entry (manifest-entry
  493. (name name)
  494. (version version)
  495. (output output)
  496. (item path)
  497. (dependencies deps*)
  498. (search-paths
  499. (infer-search-paths name version)))))
  500. entry)))
  501. name version output path deps)))
  502. ;; Version 2 adds search paths and is slightly more verbose.
  503. (('manifest ('version 2 minor-version ...)
  504. ('packages ((name version output path
  505. ('propagated-inputs deps)
  506. ('search-paths search-paths)
  507. extra-stuff ...)
  508. ...)))
  509. (manifest
  510. (map (lambda (name version output path deps search-paths)
  511. (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
  512. deps))
  513. (entry (manifest-entry
  514. (name name)
  515. (version version)
  516. (output output)
  517. (item path)
  518. (dependencies deps*)
  519. (search-paths
  520. (map sexp->search-path-specification
  521. search-paths)))))
  522. entry))
  523. name version output path deps search-paths)))
  524. ;; Version 3 represents DEPS as full-blown manifest entries.
  525. (('manifest ('version 3 minor-version ...)
  526. ('packages (entries ...)))
  527. (manifest (map sexp->manifest-entry entries)))
  528. (_
  529. (raise (condition
  530. (&message (message "unsupported manifest format")))))))
  531. (define (read-manifest port)
  532. "Return the packages listed in MANIFEST."
  533. (sexp->manifest (read port)))
  534. (define (concatenate-manifests lst)
  535. "Concatenate the manifests listed in LST and return the resulting manifest."
  536. (manifest (append-map manifest-entries lst)))
  537. (define (map-manifest-entries proc manifest)
  538. "Apply PROC to all the entries of MANIFEST and return a new manifest."
  539. (make-manifest
  540. (map proc (manifest-entries manifest))))
  541. (define (entry-predicate pattern)
  542. "Return a procedure that returns #t when passed a manifest entry that
  543. matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
  544. are ignored."
  545. (match pattern
  546. (($ <manifest-pattern> name version output)
  547. (match-lambda
  548. (($ <manifest-entry> entry-name entry-version entry-output)
  549. (and (string=? entry-name name)
  550. (or (not entry-output) (not output)
  551. (string=? entry-output output))
  552. (or (not version)
  553. (string=? entry-version version))))))))
  554. (define (manifest-remove manifest patterns)
  555. "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
  556. must be a manifest-pattern."
  557. (define (remove-entry pattern lst)
  558. (remove (entry-predicate pattern) lst))
  559. (make-manifest (fold remove-entry
  560. (manifest-entries manifest)
  561. patterns)))
  562. (define (manifest-add manifest entries)
  563. "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
  564. Remove MANIFEST entries that have the same name and output as ENTRIES."
  565. (define (same-entry? entry name output)
  566. (match entry
  567. (($ <manifest-entry> entry-name _ entry-output _)
  568. (and (equal? name entry-name)
  569. (equal? output entry-output)))))
  570. (make-manifest
  571. (fold (lambda (entry result) ;XXX: quadratic
  572. (match entry
  573. (($ <manifest-entry> name _ out _)
  574. (cons entry
  575. (remove (cut same-entry? <> name out)
  576. result)))))
  577. (manifest-entries manifest)
  578. entries)))
  579. (define (manifest-lookup manifest pattern)
  580. "Return the first item of MANIFEST that matches PATTERN, or #f if there is
  581. no match.."
  582. (find (entry-predicate pattern)
  583. (manifest-entries manifest)))
  584. (define (manifest-installed? manifest pattern)
  585. "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
  586. #f otherwise."
  587. (->bool (manifest-lookup manifest pattern)))
  588. (define (manifest-matching-entries manifest patterns)
  589. "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
  590. an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
  591. of PATTERNS."
  592. (fold-right (lambda (pattern matches)
  593. (match (filter (entry-predicate pattern)
  594. (manifest-entries manifest))
  595. (()
  596. (raise (condition
  597. (&unmatched-pattern-error
  598. (pattern pattern)
  599. (manifest manifest)))))
  600. (lst
  601. (append lst matches))))
  602. '()
  603. patterns))
  604. (define (manifest-search-paths manifest)
  605. "Return the list of search path specifications that apply to MANIFEST,
  606. including the search path specification for $PATH."
  607. (delete-duplicates
  608. (cons $PATH
  609. (append-map manifest-entry-search-paths
  610. (manifest-entries manifest)))))
  611. (define* (manifest->code manifest
  612. #:key (entry-package-version (const "")))
  613. "Return an sexp representing code to build an approximate version of
  614. MANIFEST; the code is wrapped in a top-level 'begin' form. Call
  615. ENTRY-PACKAGE-VERSION to determine the version number to use in the spec for a
  616. given entry; it can be set to 'manifest-entry-version' for fully-specified
  617. version numbers, or to some other procedure to disambiguate versions for
  618. packages for which several versions are available."
  619. (define (entry-transformations entry)
  620. ;; Return the transformations that apply to ENTRY.
  621. (assoc-ref (manifest-entry-properties entry) 'transformations))
  622. (define transformation-procedures
  623. ;; List of transformation options/procedure name pairs.
  624. (let loop ((entries (manifest-entries manifest))
  625. (counter 1)
  626. (result '()))
  627. (match entries
  628. (() result)
  629. ((entry . tail)
  630. (match (entry-transformations entry)
  631. (#f
  632. (loop tail counter result))
  633. (options
  634. (if (assoc-ref result options)
  635. (loop tail counter result)
  636. (loop tail (+ 1 counter)
  637. (alist-cons options
  638. (string->symbol
  639. (format #f "transform~a" counter))
  640. result)))))))))
  641. (define (qualified-name entry)
  642. ;; Return the name of ENTRY possibly with "@" followed by a version.
  643. (match (entry-package-version entry)
  644. ("" (manifest-entry-name entry))
  645. (version (string-append (manifest-entry-name entry)
  646. "@" version))))
  647. (if (null? transformation-procedures)
  648. `(begin ;simplest case
  649. (specifications->manifest
  650. (list ,@(map (lambda (entry)
  651. (match (manifest-entry-output entry)
  652. ("out" (qualified-name entry))
  653. (output (string-append (qualified-name entry)
  654. ":" output))))
  655. (manifest-entries manifest)))))
  656. (let* ((transform (lambda (options exp)
  657. (if (not options)
  658. exp
  659. (let ((proc (assoc-ref transformation-procedures
  660. options)))
  661. `(,proc ,exp))))))
  662. `(begin ;transformations apply
  663. (use-modules (guix transformations))
  664. ,@(map (match-lambda
  665. ((options . name)
  666. `(define ,name
  667. (options->transformation ',options))))
  668. transformation-procedures)
  669. (packages->manifest
  670. (list ,@(map (lambda (entry)
  671. (define options
  672. (entry-transformations entry))
  673. (define name
  674. (qualified-name entry))
  675. (match (manifest-entry-output entry)
  676. ("out"
  677. (transform options
  678. `(specification->package ,name)))
  679. (output
  680. `(list ,(transform
  681. options
  682. `(specification->package ,name))
  683. ,output))))
  684. (manifest-entries manifest))))))))
  685. ;;;
  686. ;;; Manifest transactions.
  687. ;;;
  688. (define-record-type* <manifest-transaction> manifest-transaction
  689. make-manifest-transaction
  690. manifest-transaction?
  691. (install manifest-transaction-install ; list of <manifest-entry>
  692. (default '()))
  693. (remove manifest-transaction-remove ; list of <manifest-pattern>
  694. (default '())))
  695. (define (manifest-transaction-install-entry entry transaction)
  696. "Augment TRANSACTION's set of installed packages with ENTRY, a
  697. <manifest-entry>."
  698. (manifest-transaction
  699. (inherit transaction)
  700. (install
  701. (cons entry (manifest-transaction-install transaction)))))
  702. (define (manifest-transaction-remove-pattern pattern transaction)
  703. "Add PATTERN to TRANSACTION's list of packages to remove."
  704. (manifest-transaction
  705. (inherit transaction)
  706. (remove
  707. (cons pattern (manifest-transaction-remove transaction)))))
  708. (define (manifest-transaction-null? transaction)
  709. "Return true if TRANSACTION has no effect---i.e., it neither installs nor
  710. remove software."
  711. (match transaction
  712. (($ <manifest-transaction> () ()) #t)
  713. (($ <manifest-transaction> _ _) #f)))
  714. (define (manifest-transaction-removal-candidate? entry transaction)
  715. "Return true if ENTRY is a candidate for removal in TRANSACTION."
  716. (any (lambda (pattern)
  717. ((entry-predicate pattern) entry))
  718. (manifest-transaction-remove transaction)))
  719. (define (manifest-transaction-effects manifest transaction)
  720. "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
  721. the list of packages that would be removed, installed, upgraded, or downgraded
  722. when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
  723. where the head is the entry being upgraded and the tail is the entry that will
  724. replace it."
  725. (define (manifest-entry->pattern entry)
  726. (manifest-pattern
  727. (name (manifest-entry-name entry))
  728. (output (manifest-entry-output entry))))
  729. (define manifest-entry-pair=?
  730. (match-lambda*
  731. (((m1a . m2a) (m1b . m2b))
  732. (and (manifest-entry=? m1a m1b)
  733. (manifest-entry=? m2a m2b)))
  734. (_ #f)))
  735. (let loop ((input (manifest-transaction-install transaction))
  736. (install '())
  737. (upgrade '())
  738. (downgrade '()))
  739. (match input
  740. (()
  741. (let ((remove (manifest-transaction-remove transaction)))
  742. (values (delete-duplicates
  743. (manifest-matching-entries manifest remove)
  744. manifest-entry=?)
  745. (delete-duplicates (reverse install) manifest-entry=?)
  746. (delete-duplicates
  747. (reverse upgrade)
  748. manifest-entry-pair=?)
  749. (delete-duplicates
  750. (reverse downgrade)
  751. manifest-entry-pair=?))))
  752. ((entry rest ...)
  753. ;; Check whether installing ENTRY corresponds to the installation of a
  754. ;; new package or to an upgrade.
  755. ;; XXX: When the exact same output directory is installed, we're not
  756. ;; really upgrading anything. Add a check for that case.
  757. (let* ((pattern (manifest-entry->pattern entry))
  758. (previous (manifest-lookup manifest pattern))
  759. (newer? (and previous
  760. (version>=? (manifest-entry-version entry)
  761. (manifest-entry-version previous)))))
  762. (loop rest
  763. (if previous install (cons entry install))
  764. (if (and previous newer?)
  765. (alist-cons previous entry upgrade)
  766. upgrade)
  767. (if (and previous (not newer?))
  768. (alist-cons previous entry downgrade)
  769. downgrade)))))))
  770. (define (manifest-perform-transaction manifest transaction)
  771. "Perform TRANSACTION on MANIFEST and return the new manifest."
  772. (let ((install (manifest-transaction-install transaction))
  773. (remove (manifest-transaction-remove transaction)))
  774. (manifest-add (manifest-remove manifest remove)
  775. install)))
  776. ;;;
  777. ;;; Profiles.
  778. ;;;
  779. (define (manifest-inputs manifest)
  780. "Return a list of <gexp-input> objects for MANIFEST."
  781. (define entry->input
  782. (match-lambda
  783. (($ <manifest-entry> name version output thing deps)
  784. ;; THING may be a package or a file name. In the latter case, assume
  785. ;; it's already valid.
  786. (cons (gexp-input thing output)
  787. (append-map entry->input deps)))))
  788. (append-map entry->input (manifest-entries manifest)))
  789. (define* (manifest-lookup-package manifest name #:optional version)
  790. "Return as a monadic value the first package or store path referenced by
  791. MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
  792. if not found."
  793. ;; Return as a monadic value the package or store path referenced by the
  794. ;; manifest ENTRY, or #f if not referenced.
  795. (define (entry-lookup-package entry)
  796. (define (find-among-inputs inputs)
  797. (find (lambda (input)
  798. (and (package? input)
  799. (equal? name (package-name input))
  800. (if version
  801. (string-prefix? version (package-version input))
  802. #t)))
  803. inputs))
  804. (define (find-among-store-items items)
  805. (find (lambda (item)
  806. (let-values (((name* version*)
  807. (package-name->name+version
  808. (store-path-package-name item))))
  809. (and (string=? name name*)
  810. (if version
  811. (string-prefix? version version*)
  812. #t))))
  813. items))
  814. (with-monad %store-monad
  815. (match (manifest-entry-item entry)
  816. ((? package? package)
  817. (match (cons (list (package-name package) package)
  818. (package-transitive-inputs package))
  819. (((labels inputs . _) ...)
  820. (return (find-among-inputs inputs)))))
  821. ((? string? item)
  822. (mlet %store-monad ((refs (references* item)))
  823. (return (find-among-store-items refs))))
  824. (item
  825. ;; XXX: ITEM might be a 'computed-file' or anything like that, in
  826. ;; which case we don't know what to do. The fix may be to check
  827. ;; references once ITEM is compiled, as proposed at
  828. ;; <https://bugs.gnu.org/29927>.
  829. (return #f)))))
  830. (anym %store-monad
  831. entry-lookup-package (manifest-entries manifest)))
  832. (define (info-dir-file manifest)
  833. "Return a derivation that builds the 'dir' file for all the entries of
  834. MANIFEST."
  835. (define texinfo ;lazy reference
  836. (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
  837. (define gzip ;lazy reference
  838. (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
  839. (define glibc-utf8-locales ;lazy reference
  840. (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
  841. (define build
  842. (with-imported-modules '((guix build utils))
  843. #~(begin
  844. (use-modules (guix build utils)
  845. (srfi srfi-1) (srfi srfi-26)
  846. (ice-9 ftw))
  847. (define (info-file? file)
  848. (or (string-suffix? ".info" file)
  849. (string-suffix? ".info.gz" file)))
  850. (define (info-files top)
  851. (let ((infodir (string-append top "/share/info")))
  852. (map (cut string-append infodir "/" <>)
  853. (or (scandir infodir info-file?) '()))))
  854. (define (info-file-language file)
  855. (let* ((base (if (string-suffix? ".gz" file)
  856. (basename file ".info.gz")
  857. (basename file ".info")))
  858. (dot (string-rindex base #\.)))
  859. (if dot
  860. (string-drop base (+ 1 dot))
  861. "en")))
  862. (define (install-info info)
  863. (let ((language (info-file-language info)))
  864. ;; We need to choose a valid locale for $LANGUAGE to be honored.
  865. (setenv "LC_ALL" "en_US.utf8")
  866. (setenv "LANGUAGE" language)
  867. (zero?
  868. (system* #+(file-append texinfo "/bin/install-info")
  869. "--silent" info
  870. (apply string-append #$output "/share/info/dir"
  871. (if (string=? "en" language)
  872. '("")
  873. `("." ,language)))))))
  874. (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
  875. (setenv "GUIX_LOCPATH"
  876. #+(file-append glibc-utf8-locales "/lib/locale"))
  877. (mkdir-p (string-append #$output "/share/info"))
  878. (exit (every install-info
  879. (append-map info-files
  880. '#$(manifest-inputs manifest)))))))
  881. (gexp->derivation "info-dir" build
  882. #:local-build? #t
  883. #:substitutable? #f
  884. #:properties
  885. `((type . profile-hook)
  886. (hook . info-dir))))
  887. (define (ghc-package-cache-file manifest)
  888. "Return a derivation that builds the GHC 'package.cache' file for all the
  889. entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
  890. (define ghc ;lazy reference
  891. (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
  892. (define build
  893. (with-imported-modules '((guix build utils))
  894. #~(begin
  895. (use-modules (guix build utils)
  896. (srfi srfi-1) (srfi srfi-26)
  897. (ice-9 ftw))
  898. (define ghc-name-version
  899. (let* ((base (basename #+ghc)))
  900. (string-drop base
  901. (+ 1 (string-index base #\-)))))
  902. (define db-subdir
  903. (string-append "lib/" ghc-name-version "/package.conf.d"))
  904. (define db-dir
  905. (string-append #$output "/" db-subdir))
  906. (define (conf-files top)
  907. (let ((db (string-append top "/" db-subdir)))
  908. (if (file-exists? db)
  909. (find-files db "\\.conf$")
  910. '())))
  911. (define (copy-conf-file conf)
  912. (let ((base (basename conf)))
  913. (copy-file conf (string-append db-dir "/" base))))
  914. (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
  915. (for-each copy-conf-file
  916. (append-map conf-files
  917. (delete-duplicates
  918. '#$(manifest-inputs manifest))))
  919. (let ((success
  920. (zero?
  921. (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
  922. (string-append "--package-db=" db-dir)))))
  923. (for-each delete-file (find-files db-dir "\\.conf$"))
  924. (exit success)))))
  925. (with-monad %store-monad
  926. ;; Don't depend on GHC when there's nothing to do.
  927. (if (any (cut string-prefix? "ghc" <>)
  928. (map manifest-entry-name (manifest-entries manifest)))
  929. (gexp->derivation "ghc-package-cache" build
  930. #:local-build? #t
  931. #:substitutable? #f
  932. #:properties
  933. `((type . profile-hook)
  934. (hook . ghc-package-cache)))
  935. (return #f))))
  936. (define (ca-certificate-bundle manifest)
  937. "Return a derivation that builds a single-file bundle containing the CA
  938. certificates in the /etc/ssl/certs sub-directories of the packages in
  939. MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
  940. ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
  941. ;; for a discussion.
  942. (define glibc-utf8-locales ;lazy reference
  943. (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
  944. (define build
  945. (with-imported-modules '((guix build utils))
  946. #~(begin
  947. (use-modules (guix build utils)
  948. (rnrs io ports)
  949. (srfi srfi-1)
  950. (srfi srfi-26)
  951. (ice-9 ftw)
  952. (ice-9 match))
  953. (define (pem-file? file)
  954. (string-suffix? ".pem" file))
  955. (define (ca-files top)
  956. (let ((cert-dir (string-append top "/etc/ssl/certs")))
  957. (map (cut string-append cert-dir "/" <>)
  958. (or (scandir cert-dir pem-file?) '()))))
  959. (define (concatenate-files files result)
  960. "Make RESULT the concatenation of all of FILES."
  961. (define (dump file port)
  962. (display (call-with-input-file file get-string-all)
  963. port)
  964. (newline port)) ;required, see <https://bugs.debian.org/635570>
  965. (call-with-output-file result
  966. (lambda (port)
  967. (for-each (cut dump <> port) files))))
  968. ;; Some file names in the NSS certificates are UTF-8 encoded so
  969. ;; install a UTF-8 locale.
  970. (setenv "LOCPATH"
  971. (string-append #+glibc-utf8-locales "/lib/locale/"
  972. #+(version-major+minor
  973. (package-version glibc-utf8-locales))))
  974. (setlocale LC_ALL "en_US.utf8")
  975. (match (append-map ca-files '#$(manifest-inputs manifest))
  976. (()
  977. ;; Since there are no CA files, just create an empty directory. Do
  978. ;; not create the etc/ssl/certs sub-directory, since that would
  979. ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
  980. ;; defined.
  981. (mkdir #$output)
  982. #t)
  983. ((ca-files ...)
  984. (let ((result (string-append #$output "/etc/ssl/certs")))
  985. (mkdir-p result)
  986. (concatenate-files ca-files
  987. (string-append result
  988. "/ca-certificates.crt"))
  989. #t))))))
  990. (gexp->derivation "ca-certificate-bundle" build
  991. #:local-build? #t
  992. #:substitutable? #f
  993. #:properties
  994. `((type . profile-hook)
  995. (hook . ca-certificate-bundle))))
  996. (define (glib-schemas manifest)
  997. "Return a derivation that unions all schemas from manifest entries and
  998. creates the Glib 'gschemas.compiled' file."
  999. (define glib ; lazy reference
  1000. (module-ref (resolve-interface '(gnu packages glib)) 'glib))
  1001. (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
  1002. ;; XXX: Can't use glib-compile-schemas corresponding
  1003. ;; to the glib referenced by 'manifest'. Because
  1004. ;; '%glib' can be either a package or store path, and
  1005. ;; there's no way to get the "bin" output for the later.
  1006. (glib-compile-schemas
  1007. -> #~(string-append #+glib:bin
  1008. "/bin/glib-compile-schemas")))
  1009. (define build
  1010. (with-imported-modules '((guix build utils)
  1011. (guix build union)
  1012. (guix build profiles)
  1013. (guix search-paths)
  1014. (guix records))
  1015. #~(begin
  1016. (use-modules (guix build utils)
  1017. (guix build union)
  1018. (guix build profiles)
  1019. (srfi srfi-26))
  1020. (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
  1021. (schemadirs (filter file-exists?
  1022. (map (cut string-append <> "/share/glib-2.0/schemas")
  1023. '#$(manifest-inputs manifest)))))
  1024. ;; Union all the schemas.
  1025. (mkdir-p (string-append #$output "/share/glib-2.0"))
  1026. (union-build destdir schemadirs
  1027. #:log-port (%make-void-port "w"))
  1028. (let ((dir destdir))
  1029. (when (file-is-directory? dir)
  1030. (ensure-writable-directory dir)
  1031. (invoke #+glib-compile-schemas
  1032. (string-append "--targetdir=" dir)
  1033. dir)))))))
  1034. ;; Don't run the hook when there's nothing to do.
  1035. (if %glib
  1036. (gexp->derivation "glib-schemas" build
  1037. #:local-build? #t
  1038. #:substitutable? #f
  1039. #:properties
  1040. `((type . profile-hook)
  1041. (hook . glib-schemas)))
  1042. (return #f))))
  1043. (define (gtk-icon-themes manifest)
  1044. "Return a derivation that unions all icon themes from manifest entries and
  1045. creates the GTK+ 'icon-theme.cache' file for each theme."
  1046. (define gtk+ ; lazy reference
  1047. (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
  1048. (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
  1049. ;; XXX: Can't use gtk-update-icon-cache corresponding
  1050. ;; to the gtk+ referenced by 'manifest'. Because
  1051. ;; '%gtk+' can be either a package or store path, and
  1052. ;; there's no way to get the "bin" output for the later.
  1053. (gtk-update-icon-cache
  1054. -> #~(string-append #+gtk+:bin
  1055. "/bin/gtk-update-icon-cache")))
  1056. (define build
  1057. (with-imported-modules '((guix build utils)
  1058. (guix build union)
  1059. (guix build profiles)
  1060. (guix search-paths)
  1061. (guix records))
  1062. #~(begin
  1063. (use-modules (guix build utils)
  1064. (guix build union)
  1065. (guix build profiles)
  1066. (srfi srfi-26)
  1067. (ice-9 ftw))
  1068. (let* ((destdir (string-append #$output "/share/icons"))
  1069. (icondirs (filter file-exists?
  1070. (map (cut string-append <> "/share/icons")
  1071. '#$(manifest-inputs manifest)))))
  1072. ;; Union all the icons.
  1073. (mkdir-p (string-append #$output "/share"))
  1074. (union-build destdir icondirs
  1075. #:log-port (%make-void-port "w"))
  1076. ;; Update the 'icon-theme.cache' file for each icon theme.
  1077. (for-each
  1078. (lambda (theme)
  1079. (let ((dir (string-append destdir "/" theme)))
  1080. ;; Occasionally DESTDIR contains plain files, such as
  1081. ;; "abiword_48.png". Ignore these.
  1082. (when (file-is-directory? dir)
  1083. (ensure-writable-directory dir)
  1084. (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
  1085. (scandir destdir (negate (cut member <> '("." "..")))))))))
  1086. ;; Don't run the hook when there's nothing to do.
  1087. (if %gtk+
  1088. (gexp->derivation "gtk-icon-themes" build
  1089. #:local-build? #t
  1090. #:substitutable? #f
  1091. #:properties
  1092. `((type . profile-hook)
  1093. (hook . gtk-icon-themes)))
  1094. (return #f))))
  1095. (define (gtk-im-modules manifest)
  1096. "Return a derivation that builds the cache files for input method modules
  1097. for both major versions of GTK+."
  1098. (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
  1099. (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
  1100. (define (build gtk gtk-version query)
  1101. (let ((major (string-take gtk-version 1)))
  1102. (with-imported-modules '((guix build utils)
  1103. (guix build union)
  1104. (guix build profiles)
  1105. (guix search-paths)
  1106. (guix records))
  1107. #~(begin
  1108. (use-modules (guix build utils)
  1109. (guix build union)
  1110. (guix build profiles)
  1111. (ice-9 popen)
  1112. (srfi srfi-1)
  1113. (srfi srfi-26))
  1114. (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
  1115. #$gtk-version))
  1116. (destdir (string-append #$output prefix))
  1117. (moddirs (cons (string-append #$gtk prefix "/immodules")
  1118. (filter file-exists?
  1119. (map (cut string-append <> prefix "/immodules")
  1120. '#$(manifest-inputs manifest)))))
  1121. (modules (append-map (cut find-files <> "\\.so$")
  1122. moddirs)))
  1123. ;; Generate a new immodules cache file.
  1124. (mkdir-p (string-append #$output prefix))
  1125. (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
  1126. (outfile (string-append #$output prefix
  1127. "/immodules-gtk" #$major ".cache")))
  1128. (dynamic-wind
  1129. (const #t)
  1130. (lambda ()
  1131. (call-with-output-file outfile
  1132. (lambda (out)
  1133. (while (not (eof-object? (peek-char pipe)))
  1134. (write-char (read-char pipe) out))))
  1135. #t)
  1136. (lambda ()
  1137. (close-pipe pipe)))))))))
  1138. ;; Don't run the hook when there's nothing to do.
  1139. (let* ((pkg-gtk+ (module-ref ; lazy reference
  1140. (resolve-interface '(gnu packages gtk)) 'gtk+))
  1141. (pkg-gtk+2 (module-ref ; lazy reference
  1142. (resolve-interface '(gnu packages gtk)) 'gtk+-2))
  1143. (gexp #~(begin
  1144. #$(if gtk+
  1145. (build
  1146. gtk+ "3.0.0"
  1147. ;; Use 'gtk-query-immodules-3.0' from the 'bin'
  1148. ;; output of latest gtk+ package.
  1149. #~(string-append
  1150. #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
  1151. #t)
  1152. #$(if gtk+-2
  1153. (build
  1154. gtk+-2 "2.10.0"
  1155. #~(string-append
  1156. #$pkg-gtk+2:bin "/bin/gtk-query-immodules-2.0"))
  1157. #t))))
  1158. (if (or gtk+ gtk+-2)
  1159. (gexp->derivation "gtk-im-modules" gexp
  1160. #:local-build? #t
  1161. #:substitutable? #f
  1162. #:properties
  1163. `((type . profile-hook)
  1164. (hook . gtk-im-modules)))
  1165. (return #f)))))
  1166. (define (linux-module-database manifest)
  1167. "Return a derivation that unites all the kernel modules of the manifest
  1168. and creates the dependency graph of all these kernel modules.
  1169. This is meant to be used as a profile hook."
  1170. (define kmod ; lazy reference
  1171. (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
  1172. (define guile-zlib
  1173. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  1174. (define build
  1175. (with-imported-modules (source-module-closure
  1176. '((guix build utils)
  1177. (gnu build linux-modules)))
  1178. (with-extensions (list guile-zlib)
  1179. #~(begin
  1180. (use-modules (ice-9 ftw)
  1181. (ice-9 match)
  1182. (srfi srfi-1) ; append-map
  1183. (gnu build linux-modules))
  1184. (let* ((inputs '#$(manifest-inputs manifest))
  1185. (module-directories
  1186. (map (lambda (directory)
  1187. (string-append directory "/lib/modules"))
  1188. inputs))
  1189. (directory-entries
  1190. (lambda (directory)
  1191. (or (scandir directory
  1192. (lambda (basename)
  1193. (not (string-prefix? "." basename))))
  1194. '())))
  1195. ;; Note: Should usually result in one entry.
  1196. (versions (delete-duplicates
  1197. (append-map directory-entries
  1198. module-directories))))
  1199. (match versions
  1200. ((version)
  1201. (let ((old-path (getenv "PATH")))
  1202. (setenv "PATH" #+(file-append kmod "/bin"))
  1203. (make-linux-module-directory inputs version #$output)
  1204. (setenv "PATH" old-path)))
  1205. (()
  1206. ;; Nothing here, maybe because this is a kernel with
  1207. ;; CONFIG_MODULES=n.
  1208. (mkdir #$output))
  1209. (_ (error "Specified Linux kernel and Linux kernel modules
  1210. are not all of the same version"))))))))
  1211. (gexp->derivation "linux-module-database" build
  1212. #:local-build? #t
  1213. #:substitutable? #f
  1214. #:properties
  1215. `((type . profile-hook)
  1216. (hook . linux-module-database))))
  1217. (define (xdg-desktop-database manifest)
  1218. "Return a derivation that builds the @file{mimeinfo.cache} database from
  1219. desktop files. It's used to query what applications can handle a given
  1220. MIME type."
  1221. (define desktop-file-utils ; lazy reference
  1222. (module-ref (resolve-interface '(gnu packages freedesktop))
  1223. 'desktop-file-utils))
  1224. (mlet %store-monad ((glib
  1225. (manifest-lookup-package
  1226. manifest "glib")))
  1227. (define build
  1228. (with-imported-modules '((guix build utils)
  1229. (guix build union))
  1230. #~(begin
  1231. (use-modules (srfi srfi-26)
  1232. (guix build utils)
  1233. (guix build union))
  1234. (let* ((destdir (string-append #$output "/share/applications"))
  1235. (appdirs (filter file-exists?
  1236. (map (cut string-append <>
  1237. "/share/applications")
  1238. '#$(manifest-inputs manifest))))
  1239. (update-desktop-database (string-append
  1240. #+desktop-file-utils
  1241. "/bin/update-desktop-database")))
  1242. (mkdir-p (string-append #$output "/share"))
  1243. (union-build destdir appdirs
  1244. #:log-port (%make-void-port "w"))
  1245. (exit (zero? (system* update-desktop-database destdir)))))))
  1246. ;; Don't run the hook when 'glib' is not referenced.
  1247. (if glib
  1248. (gexp->derivation "xdg-desktop-database" build
  1249. #:local-build? #t
  1250. #:substitutable? #f
  1251. #:properties
  1252. `((type . profile-hook)
  1253. (hook . xdg-desktop-database)))
  1254. (return #f))))
  1255. (define (xdg-mime-database manifest)
  1256. "Return a derivation that builds the @file{mime.cache} database from manifest
  1257. entries. It's used to query the MIME type of a given file."
  1258. (define shared-mime-info ; lazy reference
  1259. (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
  1260. (mlet %store-monad ((glib (manifest-lookup-package manifest "glib")))
  1261. (define build
  1262. (with-imported-modules '((guix build utils)
  1263. (guix build union))
  1264. #~(begin
  1265. (use-modules (guix build utils)
  1266. (guix build union)
  1267. (srfi srfi-26)
  1268. (ice-9 match))
  1269. (let* ((datadir (string-append #$output "/share"))
  1270. (destdir (string-append datadir "/mime"))
  1271. (pkgdirs (filter file-exists?
  1272. (map (cut string-append <>
  1273. "/share/mime/packages")
  1274. (cons #+shared-mime-info
  1275. '#$(manifest-inputs manifest))))))
  1276. (match pkgdirs
  1277. ((shared-mime-info)
  1278. ;; PKGDIRS contains nothing but 'shared-mime-info', which
  1279. ;; already contains its database, so nothing to do.
  1280. (mkdir-p datadir)
  1281. (symlink #$(file-append shared-mime-info "/share/mime")
  1282. destdir))
  1283. (_
  1284. ;; PKGDIRS contains additional packages providing
  1285. ;; 'share/mime/packages' (very few packages do so) so rebuild
  1286. ;; the database. TODO: Find a way to avoid reprocessing
  1287. ;; 'shared-mime-info', which is the most expensive one.
  1288. (mkdir-p destdir)
  1289. (union-build (string-append destdir "/packages") pkgdirs
  1290. #:log-port (%make-void-port "w"))
  1291. (setenv "XDG_DATA_HOME" datadir)
  1292. (invoke #+(file-append shared-mime-info
  1293. "/bin/update-mime-database")
  1294. destdir)))))))
  1295. ;; Don't run the hook when there are no GLib based applications.
  1296. (if glib
  1297. (gexp->derivation "xdg-mime-database" build
  1298. #:local-build? #t
  1299. #:substitutable? #f
  1300. #:properties
  1301. `((type . profile-hook)
  1302. (hook . xdg-mime-database)))
  1303. (return #f))))
  1304. ;; Several font packages may install font files into same directory, so
  1305. ;; fonts.dir and fonts.scale file should be generated here, instead of in
  1306. ;; packages.
  1307. (define (fonts-dir-file manifest)
  1308. "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
  1309. files for the fonts of the @var{manifest} entries."
  1310. (define mkfontscale
  1311. (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
  1312. (define mkfontdir
  1313. (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
  1314. (define build
  1315. #~(begin
  1316. (use-modules (srfi srfi-26)
  1317. (guix build utils)
  1318. (guix build union))
  1319. (let ((fonts-dirs (filter file-exists?
  1320. (map (cut string-append <>
  1321. "/share/fonts")
  1322. '#$(manifest-inputs manifest)))))
  1323. (mkdir #$output)
  1324. (if (null? fonts-dirs)
  1325. (exit #t)
  1326. (let* ((share-dir (string-append #$output "/share"))
  1327. (fonts-dir (string-append share-dir "/fonts"))
  1328. (mkfontscale (string-append #+mkfontscale
  1329. "/bin/mkfontscale"))
  1330. (mkfontdir (string-append #+mkfontdir
  1331. "/bin/mkfontdir"))
  1332. (empty-file? (lambda (filename)
  1333. (call-with-ascii-input-file filename
  1334. (lambda (p)
  1335. (eqv? #\0 (read-char p))))))
  1336. (fonts-dir-file "fonts.dir")
  1337. (fonts-scale-file "fonts.scale"))
  1338. (mkdir-p share-dir)
  1339. ;; Create all sub-directories, because we may create fonts.dir
  1340. ;; and fonts.scale files in the sub-directories.
  1341. (union-build fonts-dir fonts-dirs
  1342. #:log-port (%make-void-port "w")
  1343. #:create-all-directories? #t)
  1344. (let ((directories (find-files fonts-dir
  1345. (lambda (file stat)
  1346. (eq? 'directory (stat:type stat)))
  1347. #:directories? #t)))
  1348. (for-each (lambda (dir)
  1349. (with-directory-excursion dir
  1350. (when (file-exists? fonts-scale-file)
  1351. (delete-file fonts-scale-file))
  1352. (when (file-exists? fonts-dir-file)
  1353. (delete-file fonts-dir-file))
  1354. (unless (and (zero? (system* mkfontscale))
  1355. (zero? (system* mkfontdir)))
  1356. (exit #f))
  1357. (when (and (file-exists? fonts-scale-file)
  1358. (empty-file? fonts-scale-file))
  1359. (delete-file fonts-scale-file))
  1360. (when (and (file-exists? fonts-dir-file)
  1361. (empty-file? fonts-dir-file))
  1362. (delete-file fonts-dir-file))))
  1363. directories)))))))
  1364. (gexp->derivation "fonts-dir" build
  1365. #:modules '((guix build utils)
  1366. (guix build union)
  1367. (srfi srfi-26))
  1368. #:local-build? #t
  1369. #:substitutable? #f
  1370. #:properties
  1371. `((type . profile-hook)
  1372. (hook . fonts-dir))))
  1373. (define (manual-database manifest)
  1374. "Return a derivation that builds the manual page database (\"mandb\") for
  1375. the entries in MANIFEST."
  1376. (define gdbm-ffi
  1377. (module-ref (resolve-interface '(gnu packages guile))
  1378. 'guile-gdbm-ffi))
  1379. (define guile-zlib
  1380. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  1381. (define modules
  1382. (delete '(guix config)
  1383. (source-module-closure `((guix build utils)
  1384. (guix man-db)))))
  1385. (define build
  1386. (with-imported-modules modules
  1387. (with-extensions (list gdbm-ffi ;for (guix man-db)
  1388. guile-zlib)
  1389. #~(begin
  1390. (use-modules (guix man-db)
  1391. (guix build utils)
  1392. (ice-9 threads)
  1393. (srfi srfi-1)
  1394. (srfi srfi-19))
  1395. (define (print-string msg)
  1396. (display msg)
  1397. (force-output))
  1398. (define-syntax-rule (print fmt args ...)
  1399. ;; Build up the string and display it at once.
  1400. (print-string (format #f fmt args ...)))
  1401. (define (compute-entry directory count total)
  1402. (print "\r[~3d/~3d] building list of man-db entries..."
  1403. count total)
  1404. (let ((man (string-append directory "/share/man")))
  1405. (if (directory-exists? man)
  1406. (mandb-entries man)
  1407. '())))
  1408. (define (compute-entries)
  1409. ;; This is the most expensive part (I/O and CPU, due to
  1410. ;; decompression), so report progress as we traverse INPUTS.
  1411. ;; Cap at 4 threads because we don't see any speedup beyond that
  1412. ;; on an SSD laptop.
  1413. (let* ((inputs '#$(manifest-inputs manifest))
  1414. (total (length inputs))
  1415. (threads (min (parallel-job-count) 4)))
  1416. (concatenate
  1417. (n-par-map threads compute-entry inputs
  1418. (iota total 1)
  1419. (make-list total total)))))
  1420. (define man-directory
  1421. (string-append #$output "/share/man"))
  1422. (mkdir-p man-directory)
  1423. (format #t "Creating manual page database...~%")
  1424. (force-output)
  1425. (let* ((start (current-time))
  1426. (entries (compute-entries))
  1427. (_ (write-mandb-database (string-append man-directory
  1428. "/index.db")
  1429. entries))
  1430. (duration (time-difference (current-time) start)))
  1431. (newline)
  1432. (format #t "~a entries processed in ~,1f s~%"
  1433. (length entries)
  1434. (+ (time-second duration)
  1435. (* (time-nanosecond duration) (expt 10 -9))))
  1436. (force-output))))))
  1437. (gexp->derivation "manual-database" build
  1438. ;; Work around GDBM 1.13 issue whereby uninitialized bytes
  1439. ;; get written to disk:
  1440. ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
  1441. #:env-vars `(("MALLOC_PERTURB_" . "1"))
  1442. #:substitutable? #f
  1443. #:local-build? #t
  1444. #:properties
  1445. `((type . profile-hook)
  1446. (hook . manual-database))))
  1447. (define (texlive-configuration manifest)
  1448. "Return a derivation that builds a TeXlive configuration for the entries in
  1449. MANIFEST."
  1450. (define entry->texlive-input
  1451. (match-lambda
  1452. (($ <manifest-entry> name version output thing deps)
  1453. (if (string-prefix? "texlive-" name)
  1454. (cons (gexp-input thing output)
  1455. (append-map entry->texlive-input deps))
  1456. '()))))
  1457. (define build
  1458. (with-imported-modules '((guix build utils)
  1459. (guix build union))
  1460. #~(begin
  1461. (use-modules (guix build utils)
  1462. (guix build union))
  1463. ;; Build a modifiable union of all texlive inputs. We do this so
  1464. ;; that TeX live can resolve the parent and grandparent directories
  1465. ;; correctly. There might be a more elegant way to accomplish this.
  1466. (union-build #$output
  1467. '#$(append-map entry->texlive-input
  1468. (manifest-entries manifest))
  1469. #:create-all-directories? #t
  1470. #:log-port (%make-void-port "w"))
  1471. (let ((texmf.cnf (string-append
  1472. #$output
  1473. "/share/texmf-dist/web2c/texmf.cnf")))
  1474. (when (file-exists? texmf.cnf)
  1475. (substitute* texmf.cnf
  1476. (("^TEXMFROOT = .*")
  1477. (string-append "TEXMFROOT = " #$output "/share\n"))
  1478. (("^TEXMF = .*")
  1479. "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
  1480. #t)))
  1481. (with-monad %store-monad
  1482. (if (any (cut string-prefix? "texlive-" <>)
  1483. (map manifest-entry-name (manifest-entries manifest)))
  1484. (gexp->derivation "texlive-configuration" build
  1485. #:substitutable? #f
  1486. #:local-build? #t
  1487. #:properties
  1488. `((type . profile-hook)
  1489. (hook . texlive-configuration)))
  1490. (return #f))))
  1491. (define %default-profile-hooks
  1492. ;; This is the list of derivation-returning procedures that are called by
  1493. ;; default when making a non-empty profile.
  1494. (list info-dir-file
  1495. manual-database
  1496. fonts-dir-file
  1497. ghc-package-cache-file
  1498. ca-certificate-bundle
  1499. glib-schemas
  1500. gtk-icon-themes
  1501. gtk-im-modules
  1502. texlive-configuration
  1503. xdg-desktop-database
  1504. xdg-mime-database))
  1505. (define* (profile-derivation manifest
  1506. #:key
  1507. (name "profile")
  1508. (hooks %default-profile-hooks)
  1509. (locales? #t)
  1510. (allow-collisions? #f)
  1511. (relative-symlinks? #f)
  1512. system target)
  1513. "Return a derivation that builds a profile (aka. 'user environment') with
  1514. the given MANIFEST. The profile includes additional derivations returned by
  1515. the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
  1516. Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
  1517. entries in MANIFEST collide (for instance if there are two same-name packages
  1518. with a different version number.)
  1519. When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
  1520. a dependency on the 'glibc-utf8-locales' package.
  1521. When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
  1522. This is one of the things to do for the result to be relocatable.
  1523. When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
  1524. are cross-built for TARGET."
  1525. (mlet* %store-monad ((system (if system
  1526. (return system)
  1527. (current-system)))
  1528. (target (if target
  1529. (return target)
  1530. (current-target-system)))
  1531. (ok? (if allow-collisions?
  1532. (return #t)
  1533. (check-for-collisions manifest system
  1534. #:target target)))
  1535. (extras (if (null? (manifest-entries manifest))
  1536. (return '())
  1537. (mapm/accumulate-builds (lambda (hook)
  1538. (hook manifest))
  1539. hooks))))
  1540. (define inputs
  1541. (append (filter-map (lambda (drv)
  1542. (and (derivation? drv)
  1543. (gexp-input drv)))
  1544. extras)
  1545. (manifest-inputs manifest)))
  1546. (define glibc-utf8-locales ;lazy reference
  1547. (module-ref (resolve-interface '(gnu packages base))
  1548. 'glibc-utf8-locales))
  1549. (define set-utf8-locale
  1550. ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
  1551. ;; install a UTF-8 locale.
  1552. #~(begin
  1553. (setenv "LOCPATH"
  1554. #$(file-append glibc-utf8-locales "/lib/locale/"
  1555. (version-major+minor
  1556. (package-version glibc-utf8-locales))))
  1557. (setlocale LC_ALL "en_US.utf8")))
  1558. (define builder
  1559. (with-imported-modules '((guix build profiles)
  1560. (guix build union)
  1561. (guix build utils)
  1562. (guix search-paths)
  1563. (guix records))
  1564. #~(begin
  1565. (use-modules (guix build profiles)
  1566. (guix search-paths)
  1567. (srfi srfi-1))
  1568. (let ((line (cond-expand (guile-2.2 'line)
  1569. (else _IOLBF)))) ;Guile 2.0
  1570. (setvbuf (current-output-port) line)
  1571. (setvbuf (current-error-port) line))
  1572. #+(if locales? set-utf8-locale #t)
  1573. (define search-paths
  1574. ;; Search paths of MANIFEST's packages, converted back to their
  1575. ;; record form.
  1576. (map sexp->search-path-specification
  1577. (delete-duplicates
  1578. '#$(map search-path-specification->sexp
  1579. (manifest-search-paths manifest)))))
  1580. (build-profile #$output '#$inputs
  1581. #:symlink #$(if relative-symlinks?
  1582. #~symlink-relative
  1583. #~symlink)
  1584. #:manifest '#$(manifest->gexp manifest)
  1585. #:search-paths search-paths))))
  1586. (gexp->derivation name builder
  1587. #:system system
  1588. #:target target
  1589. ;; Don't complain about _IO* on Guile 2.2.
  1590. #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
  1591. ;; Not worth offloading.
  1592. #:local-build? #t
  1593. ;; Disable substitution because it would trigger a
  1594. ;; connection to the substitute server, which is likely
  1595. ;; to have no substitute to offer.
  1596. #:substitutable? #f
  1597. #:properties `((type . profile)
  1598. (profile
  1599. (count
  1600. . ,(length
  1601. (manifest-entries manifest))))))))
  1602. ;; Declarative profile.
  1603. (define-record-type* <profile> profile make-profile
  1604. profile?
  1605. (name profile-name (default "profile")) ;string
  1606. (content profile-content) ;<manifest>
  1607. (hooks profile-hooks ;list of procedures
  1608. (default %default-profile-hooks))
  1609. (locales? profile-locales? ;Boolean
  1610. (default #t))
  1611. (allow-collisions? profile-allow-collisions? ;Boolean
  1612. (default #f))
  1613. (relative-symlinks? profile-relative-symlinks? ;Boolean
  1614. (default #f)))
  1615. (define-gexp-compiler (profile-compiler (profile <profile>) system target)
  1616. "Compile PROFILE to a derivation."
  1617. (match profile
  1618. (($ <profile> name manifest hooks
  1619. locales? allow-collisions? relative-symlinks?)
  1620. (profile-derivation manifest
  1621. #:name name
  1622. #:hooks hooks
  1623. #:locales? locales?
  1624. #:allow-collisions? allow-collisions?
  1625. #:relative-symlinks? relative-symlinks?
  1626. #:system system #:target target))))
  1627. (define* (profile-search-paths profile
  1628. #:optional (manifest (profile-manifest profile))
  1629. #:key (getenv (const #f)))
  1630. "Read the manifest of PROFILE and evaluate the values of search path
  1631. environment variables required by PROFILE; return a list of
  1632. specification/value pairs. If MANIFEST is not #f, it is assumed to be the
  1633. manifest of PROFILE, which avoids rereading it.
  1634. Use GETENV to determine the current settings and report only settings not
  1635. already effective."
  1636. (evaluate-search-paths (manifest-search-paths manifest)
  1637. (list profile) getenv))
  1638. (define (profile-regexp profile)
  1639. "Return a regular expression that matches PROFILE's name and number."
  1640. (make-regexp (string-append "^" (regexp-quote (basename profile))
  1641. "-([0-9]+)")))
  1642. (define (generation-number profile)
  1643. "Return PROFILE's number or 0. An absolute file name must be used."
  1644. (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
  1645. (basename (readlink profile))))
  1646. (compose string->number (cut match:substring <> 1)))
  1647. 0))
  1648. (define %profile-generation-rx
  1649. ;; Regexp that matches profile generation.
  1650. (make-regexp "(.*)-([0-9]+)-link$"))
  1651. (define (generation-profile file)
  1652. "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
  1653. return its corresponding profile---e.g., \"guix-profile\". Otherwise return
  1654. #f."
  1655. (match (regexp-exec %profile-generation-rx file)
  1656. (#f #f)
  1657. (m (let ((profile (match:substring m 1)))
  1658. (and (file-exists? (string-append profile "/manifest"))
  1659. profile)))))
  1660. (define (generation-numbers profile)
  1661. "Return the sorted list of generation numbers of PROFILE, or '(0) if no
  1662. former profiles were found."
  1663. (match (scandir (dirname profile)
  1664. (cute regexp-exec (profile-regexp profile) <>))
  1665. (#f ; no profile directory
  1666. '(0))
  1667. (() ; no profiles
  1668. '(0))
  1669. ((profiles ...) ; former profiles around
  1670. (sort (map (compose string->number
  1671. (cut match:substring <> 1)
  1672. (cute regexp-exec (profile-regexp profile) <>))
  1673. profiles)
  1674. <))))
  1675. (define (profile-generations profile)
  1676. "Return a list of PROFILE's generations."
  1677. (let ((generations (generation-numbers profile)))
  1678. (if (equal? generations '(0))
  1679. '()
  1680. generations)))
  1681. (define (relative-generation-spec->number profile spec)
  1682. "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
  1683. may be a N, -N, or +N, where N is a number. If the spec is N, then the number
  1684. returned is N. If it is -N, then the number returned is the profile's current
  1685. generation number minus N. If it is +N, then the number returned is the
  1686. profile's current generation number plus N. Return #f if there is no such
  1687. generation."
  1688. (let ((number (string->number spec)))
  1689. (and number
  1690. (case (string-ref spec 0)
  1691. ((#\+ #\-)
  1692. (relative-generation profile number))
  1693. (else (if (memv number (profile-generations profile))
  1694. number
  1695. #f))))))
  1696. (define* (relative-generation profile shift #:optional
  1697. (current (generation-number profile)))
  1698. "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
  1699. SHIFT is a positive or negative number.
  1700. Return #f if there is no such generation."
  1701. (let* ((abs-shift (abs shift))
  1702. (numbers (profile-generations profile))
  1703. (from-current (memq current
  1704. (if (negative? shift)
  1705. (reverse numbers)
  1706. numbers))))
  1707. (and from-current
  1708. (< abs-shift (length from-current))
  1709. (list-ref from-current abs-shift))))
  1710. (define* (previous-generation-number profile #:optional
  1711. (number (generation-number profile)))
  1712. "Return the number of the generation before generation NUMBER of
  1713. PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
  1714. case when generations have been deleted (there are \"holes\")."
  1715. (or (relative-generation profile -1 number)
  1716. 0))
  1717. (define (generation-file-name profile generation)
  1718. "Return the file name for PROFILE's GENERATION."
  1719. (format #f "~a-~a-link" profile generation))
  1720. (define (generation-time profile number)
  1721. "Return the creation time of a generation in the UTC format."
  1722. (make-time time-utc 0
  1723. (stat:ctime (stat (generation-file-name profile number)))))
  1724. (define (link-to-empty-profile store generation)
  1725. "Link GENERATION, a string, to the empty profile. An error is raised if
  1726. that fails."
  1727. (let* ((drv (run-with-store store
  1728. (profile-derivation (manifest '())
  1729. #:locales? #f)))
  1730. (prof (derivation->output-path drv "out")))
  1731. (build-derivations store (list drv))
  1732. (switch-symlinks generation prof)))
  1733. (define (switch-to-generation profile number)
  1734. "Atomically switch PROFILE to the generation NUMBER. Return the number of
  1735. the generation that was current before switching."
  1736. (let ((current (generation-number profile))
  1737. (generation (generation-file-name profile number)))
  1738. (cond ((not (file-exists? profile))
  1739. (raise (condition (&profile-not-found-error
  1740. (profile profile)))))
  1741. ((not (file-exists? generation))
  1742. (raise (condition (&missing-generation-error
  1743. (profile profile)
  1744. (generation number)))))
  1745. (else
  1746. (switch-symlinks profile (basename generation))
  1747. current))))
  1748. (define (switch-to-previous-generation profile)
  1749. "Atomically switch PROFILE to the previous generation. Return the former
  1750. generation number and the current one."
  1751. (let ((previous (previous-generation-number profile)))
  1752. (values (switch-to-generation profile previous)
  1753. previous)))
  1754. (define (roll-back store profile)
  1755. "Roll back to the previous generation of PROFILE. Return the number of the
  1756. generation that was current before switching and the new generation number."
  1757. (let* ((number (generation-number profile))
  1758. (previous-number (previous-generation-number profile number))
  1759. (previous-generation (generation-file-name profile previous-number)))
  1760. (cond ((not (file-exists? profile)) ;invalid profile
  1761. (raise (condition (&profile-not-found-error
  1762. (profile profile)))))
  1763. ((zero? number) ;empty profile
  1764. (values number number))
  1765. ((or (zero? previous-number) ;going to emptiness
  1766. (not (file-exists? previous-generation)))
  1767. (link-to-empty-profile store previous-generation)
  1768. (switch-to-previous-generation profile))
  1769. (else ;anything else
  1770. (switch-to-previous-generation profile)))))
  1771. (define (delete-generation store profile number)
  1772. "Delete generation with NUMBER from PROFILE. Return the file name of the
  1773. generation that has been deleted, or #f if nothing was done (for instance
  1774. because the NUMBER is zero.)"
  1775. (define (delete-and-return)
  1776. (let ((generation (generation-file-name profile number)))
  1777. (delete-file generation)
  1778. generation))
  1779. (let* ((current-number (generation-number profile))
  1780. (previous-number (previous-generation-number profile number))
  1781. (previous-generation (generation-file-name profile previous-number)))
  1782. (cond ((zero? number) #f) ;do not delete generation 0
  1783. ((and (= number current-number)
  1784. (not (file-exists? previous-generation)))
  1785. (link-to-empty-profile store previous-generation)
  1786. (switch-to-previous-generation profile)
  1787. (delete-and-return))
  1788. ((= number current-number)
  1789. (roll-back store profile)
  1790. (delete-and-return))
  1791. (else
  1792. (delete-and-return)))))
  1793. (define %user-profile-directory
  1794. (and=> (getenv "HOME")
  1795. (cut string-append <> "/.guix-profile")))
  1796. (define %profile-directory
  1797. (string-append %state-directory "/profiles/"
  1798. (or (and=> (or (getenv "USER")
  1799. (getenv "LOGNAME")
  1800. (false-if-exception
  1801. (passwd:name (getpwuid (getuid)))))
  1802. (cut string-append "per-user/" <>))
  1803. "default")))
  1804. (define %current-profile
  1805. ;; Call it `guix-profile', not `profile', to allow Guix profiles to
  1806. ;; coexist with Nix profiles.
  1807. (string-append %profile-directory "/guix-profile"))
  1808. (define (ensure-profile-directory)
  1809. "Attempt to create /…/profiles/per-user/$USER if needed. Nowadays this is
  1810. taken care of by the daemon."
  1811. (let ((s (stat %profile-directory #f)))
  1812. (unless (and s (eq? 'directory (stat:type s)))
  1813. (catch 'system-error
  1814. (lambda ()
  1815. (mkdir-p %profile-directory))
  1816. (lambda args
  1817. ;; Often, we cannot create %PROFILE-DIRECTORY because its
  1818. ;; parent directory is root-owned and we're running
  1819. ;; unprivileged.
  1820. (raise (condition
  1821. (&message
  1822. (message
  1823. (format #f
  1824. (G_ "while creating directory `~a': ~a")
  1825. %profile-directory
  1826. (strerror (system-error-errno args)))))
  1827. (&fix-hint
  1828. (hint
  1829. (format #f (G_ "Please create the @file{~a} directory, \
  1830. with you as the owner.")
  1831. %profile-directory))))))))
  1832. ;; Bail out if it's not owned by the user.
  1833. (unless (or (not s) (= (stat:uid s) (getuid)))
  1834. (raise (condition
  1835. (&message
  1836. (message
  1837. (format #f (G_ "directory `~a' is not owned by you")
  1838. %profile-directory)))
  1839. (&fix-hint
  1840. (hint
  1841. (format #f (G_ "Please change the owner of @file{~a} \
  1842. to user ~s.")
  1843. %profile-directory (or (getenv "USER")
  1844. (getenv "LOGNAME")
  1845. (getuid))))))))))
  1846. (define (canonicalize-profile profile)
  1847. "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
  1848. Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
  1849. as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
  1850. ;; Trim trailing slashes so 'readlink' can do its job.
  1851. (let ((profile (string-trim-right profile #\/)))
  1852. (catch 'system-error
  1853. (lambda ()
  1854. (let ((target (readlink profile)))
  1855. (if (string=? (dirname target) %profile-directory)
  1856. target
  1857. profile)))
  1858. (const profile))))
  1859. (define %known-shorthand-profiles
  1860. ;; Known shorthand forms for profiles that the user manipulates.
  1861. (list (string-append (config-directory #:ensure? #f) "/current")
  1862. %user-profile-directory))
  1863. (define (user-friendly-profile profile)
  1864. "Return either ~/.guix-profile or ~/.config/guix/current if that's what
  1865. PROFILE refers to, directly or indirectly, or PROFILE."
  1866. (or (find (lambda (shorthand)
  1867. (and shorthand
  1868. (let ((target (false-if-exception
  1869. (readlink shorthand))))
  1870. (and target (string=? target profile)))))
  1871. %known-shorthand-profiles)
  1872. profile))
  1873. ;;; profiles.scm ends here