profiles.scm 96 KB

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