profiles.scm 86 KB

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