profiles.scm 92 KB

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