profiles.scm 86 KB

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