profiles.scm 95 KB

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