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 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* (package->manifest-entry package #:optional (output "out")
  326. #:key (parent (delay #f))
  327. (properties '()))
  328. "Return a manifest entry for the OUTPUT of package PACKAGE."
  329. ;; For each dependency, keep a promise pointing to its "parent" entry.
  330. (letrec* ((deps (map (match-lambda
  331. ((label package)
  332. (package->manifest-entry package
  333. #:parent (delay entry)))
  334. ((label package output)
  335. (package->manifest-entry package output
  336. #:parent (delay entry))))
  337. (package-propagated-inputs package)))
  338. (entry (manifest-entry
  339. (name (package-name package))
  340. (version (package-version package))
  341. (output output)
  342. (item package)
  343. (dependencies (delete-duplicates deps))
  344. (search-paths
  345. (package-transitive-native-search-paths package))
  346. (parent parent)
  347. (properties properties))))
  348. entry))
  349. (define (packages->manifest packages)
  350. "Return a list of manifest entries, one for each item listed in PACKAGES.
  351. Elements of PACKAGES can be either package objects or package/string tuples
  352. denoting a specific output of a package."
  353. (define inferiors-loaded?
  354. ;; This hack allows us to provide seamless integration for inferior
  355. ;; packages while not having a hard dependency on (guix inferior).
  356. (resolve-module '(guix inferior) #f #f #:ensure #f))
  357. (define (inferior->entry)
  358. (module-ref (resolve-interface '(guix inferior))
  359. 'inferior-package->manifest-entry))
  360. (manifest
  361. (delete-duplicates
  362. (map (match-lambda
  363. (((? package? package) output)
  364. (package->manifest-entry package output))
  365. ((? package? package)
  366. (package->manifest-entry package))
  367. ((thing output)
  368. (if inferiors-loaded?
  369. ((inferior->entry) thing output)
  370. (throw 'wrong-type-arg 'packages->manifest
  371. "Wrong package object: ~S" (list thing) (list thing))))
  372. (thing
  373. (if inferiors-loaded?
  374. ((inferior->entry) thing)
  375. (throw 'wrong-type-arg 'packages->manifest
  376. "Wrong package object: ~S" (list thing) (list thing)))))
  377. packages)
  378. manifest-entry=?)))
  379. (define (manifest->gexp manifest)
  380. "Return a representation of MANIFEST as a gexp."
  381. (define (entry->gexp entry)
  382. (match entry
  383. (($ <manifest-entry> name version output (? string? path)
  384. (deps ...) (search-paths ...) _ (properties ...))
  385. #~(#$name #$version #$output #$path
  386. (propagated-inputs #$(map entry->gexp deps))
  387. (search-paths #$(map search-path-specification->sexp
  388. search-paths))
  389. (properties . #$properties)))
  390. (($ <manifest-entry> name version output package
  391. (deps ...) (search-paths ...) _ (properties ...))
  392. #~(#$name #$version #$output
  393. (ungexp package (or output "out"))
  394. (propagated-inputs #$(map entry->gexp deps))
  395. (search-paths #$(map search-path-specification->sexp
  396. search-paths))
  397. (properties . #$properties)))))
  398. (match manifest
  399. (($ <manifest> (entries ...))
  400. #~(manifest (version 3)
  401. (packages #$(map entry->gexp entries))))))
  402. (define (find-package name version)
  403. "Return a package from the distro matching NAME and possibly VERSION. This
  404. procedure is here for backward-compatibility and will eventually vanish."
  405. (define find-best-packages-by-name ;break abstractions
  406. (module-ref (resolve-interface '(gnu packages))
  407. 'find-best-packages-by-name))
  408. ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
  409. ;; former traverses the module tree only once and then allows for efficient
  410. ;; access via a vhash.
  411. (match (find-best-packages-by-name name version)
  412. ((p _ ...) p)
  413. (_
  414. (match (find-best-packages-by-name name #f)
  415. ((p _ ...) p)
  416. (_ #f)))))
  417. (define (sexp->manifest sexp)
  418. "Parse SEXP as a manifest."
  419. (define (infer-search-paths name version)
  420. ;; Infer the search path specifications for NAME-VERSION by looking up a
  421. ;; same-named package in the distro. Useful for the old manifest formats
  422. ;; that did not store search path info.
  423. (let ((package (find-package name version)))
  424. (if package
  425. (package-native-search-paths package)
  426. '())))
  427. (define (infer-dependency item parent)
  428. ;; Return a <manifest-entry> for ITEM.
  429. (let-values (((name version)
  430. (package-name->name+version
  431. (store-path-package-name item))))
  432. (manifest-entry
  433. (name name)
  434. (version version)
  435. (item item)
  436. (parent parent))))
  437. (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
  438. (match sexp
  439. ((name version output path
  440. ('propagated-inputs deps)
  441. ('search-paths search-paths)
  442. extra-stuff ...)
  443. ;; For each of DEPS, keep a promise pointing to ENTRY.
  444. (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
  445. deps))
  446. (entry (manifest-entry
  447. (name name)
  448. (version version)
  449. (output output)
  450. (item path)
  451. (dependencies deps*)
  452. (search-paths (map sexp->search-path-specification
  453. search-paths))
  454. (parent parent)
  455. (properties (or (assoc-ref extra-stuff 'properties)
  456. '())))))
  457. entry))))
  458. (match sexp
  459. (('manifest ('version 0)
  460. ('packages ((name version output path) ...)))
  461. (manifest
  462. (map (lambda (name version output path)
  463. (manifest-entry
  464. (name name)
  465. (version version)
  466. (output output)
  467. (item path)
  468. (search-paths (infer-search-paths name version))))
  469. name version output path)))
  470. ;; Version 1 adds a list of propagated inputs to the
  471. ;; name/version/output/path tuples.
  472. (('manifest ('version 1)
  473. ('packages ((name version output path deps) ...)))
  474. (manifest
  475. (map (lambda (name version output path deps)
  476. ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
  477. ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
  478. ;; such lists.
  479. (let ((deps (match deps
  480. (((labels directories) ...)
  481. directories)
  482. ((directories ...)
  483. directories))))
  484. (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
  485. deps))
  486. (entry (manifest-entry
  487. (name name)
  488. (version version)
  489. (output output)
  490. (item path)
  491. (dependencies deps*)
  492. (search-paths
  493. (infer-search-paths name version)))))
  494. entry)))
  495. name version output path deps)))
  496. ;; Version 2 adds search paths and is slightly more verbose.
  497. (('manifest ('version 2 minor-version ...)
  498. ('packages ((name version output path
  499. ('propagated-inputs deps)
  500. ('search-paths search-paths)
  501. extra-stuff ...)
  502. ...)))
  503. (manifest
  504. (map (lambda (name version output path deps search-paths)
  505. (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
  506. deps))
  507. (entry (manifest-entry
  508. (name name)
  509. (version version)
  510. (output output)
  511. (item path)
  512. (dependencies deps*)
  513. (search-paths
  514. (map sexp->search-path-specification
  515. search-paths)))))
  516. entry))
  517. name version output path deps search-paths)))
  518. ;; Version 3 represents DEPS as full-blown manifest entries.
  519. (('manifest ('version 3 minor-version ...)
  520. ('packages (entries ...)))
  521. (manifest (map sexp->manifest-entry entries)))
  522. (_
  523. (raise (condition
  524. (&message (message "unsupported manifest format")))))))
  525. (define (read-manifest port)
  526. "Return the packages listed in MANIFEST."
  527. (sexp->manifest (read port)))
  528. (define (concatenate-manifests lst)
  529. "Concatenate the manifests listed in LST and return the resulting manifest."
  530. (manifest (append-map manifest-entries lst)))
  531. (define (map-manifest-entries proc manifest)
  532. "Apply PROC to all the entries of MANIFEST and return a new manifest."
  533. (make-manifest
  534. (map proc (manifest-entries manifest))))
  535. (define (entry-predicate pattern)
  536. "Return a procedure that returns #t when passed a manifest entry that
  537. matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
  538. are ignored."
  539. (match pattern
  540. (($ <manifest-pattern> name version output)
  541. (match-lambda
  542. (($ <manifest-entry> entry-name entry-version entry-output)
  543. (and (string=? entry-name name)
  544. (or (not entry-output) (not output)
  545. (string=? entry-output output))
  546. (or (not version)
  547. (string=? entry-version version))))))))
  548. (define (manifest-remove manifest patterns)
  549. "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
  550. must be a manifest-pattern."
  551. (define (remove-entry pattern lst)
  552. (remove (entry-predicate pattern) lst))
  553. (make-manifest (fold remove-entry
  554. (manifest-entries manifest)
  555. patterns)))
  556. (define (manifest-add manifest entries)
  557. "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
  558. Remove MANIFEST entries that have the same name and output as ENTRIES."
  559. (define (same-entry? entry name output)
  560. (match entry
  561. (($ <manifest-entry> entry-name _ entry-output _)
  562. (and (equal? name entry-name)
  563. (equal? output entry-output)))))
  564. (make-manifest
  565. (fold (lambda (entry result) ;XXX: quadratic
  566. (match entry
  567. (($ <manifest-entry> name _ out _)
  568. (cons entry
  569. (remove (cut same-entry? <> name out)
  570. result)))))
  571. (manifest-entries manifest)
  572. entries)))
  573. (define (manifest-lookup manifest pattern)
  574. "Return the first item of MANIFEST that matches PATTERN, or #f if there is
  575. no match.."
  576. (find (entry-predicate pattern)
  577. (manifest-entries manifest)))
  578. (define (manifest-installed? manifest pattern)
  579. "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
  580. #f otherwise."
  581. (->bool (manifest-lookup manifest pattern)))
  582. (define (manifest-matching-entries manifest patterns)
  583. "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
  584. an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
  585. of PATTERNS."
  586. (fold-right (lambda (pattern matches)
  587. (match (filter (entry-predicate pattern)
  588. (manifest-entries manifest))
  589. (()
  590. (raise (condition
  591. (&unmatched-pattern-error
  592. (pattern pattern)
  593. (manifest manifest)))))
  594. (lst
  595. (append lst matches))))
  596. '()
  597. patterns))
  598. (define (manifest-search-paths manifest)
  599. "Return the list of search path specifications that apply to MANIFEST,
  600. including the search path specification for $PATH."
  601. (delete-duplicates
  602. (cons $PATH
  603. (append-map manifest-entry-search-paths
  604. (manifest-entries manifest)))))
  605. (define* (manifest->code manifest
  606. #:key (entry-package-version (const "")))
  607. "Return an sexp representing code to build an approximate version of
  608. MANIFEST; the code is wrapped in a top-level 'begin' form. Call
  609. ENTRY-PACKAGE-VERSION to determine the version number to use in the spec for a
  610. given entry; it can be set to 'manifest-entry-version' for fully-specified
  611. version numbers, or to some other procedure to disambiguate versions for
  612. packages for which several versions are available."
  613. (define (entry-transformations entry)
  614. ;; Return the transformations that apply to ENTRY.
  615. (assoc-ref (manifest-entry-properties entry) 'transformations))
  616. (define transformation-procedures
  617. ;; List of transformation options/procedure name pairs.
  618. (let loop ((entries (manifest-entries manifest))
  619. (counter 1)
  620. (result '()))
  621. (match entries
  622. (() result)
  623. ((entry . tail)
  624. (match (entry-transformations entry)
  625. (#f
  626. (loop tail counter result))
  627. (options
  628. (if (assoc-ref result options)
  629. (loop tail counter result)
  630. (loop tail (+ 1 counter)
  631. (alist-cons options
  632. (string->symbol
  633. (format #f "transform~a" counter))
  634. result)))))))))
  635. (define (qualified-name entry)
  636. ;; Return the name of ENTRY possibly with "@" followed by a version.
  637. (match (entry-package-version entry)
  638. ("" (manifest-entry-name entry))
  639. (version (string-append (manifest-entry-name entry)
  640. "@" version))))
  641. (if (null? transformation-procedures)
  642. `(begin ;simplest case
  643. (specifications->manifest
  644. (list ,@(map (lambda (entry)
  645. (match (manifest-entry-output entry)
  646. ("out" (qualified-name entry))
  647. (output (string-append (qualified-name entry)
  648. ":" output))))
  649. (manifest-entries manifest)))))
  650. (let* ((transform (lambda (options exp)
  651. (if (not options)
  652. exp
  653. (let ((proc (assoc-ref transformation-procedures
  654. options)))
  655. `(,proc ,exp))))))
  656. `(begin ;transformations apply
  657. (use-modules (guix transformations))
  658. ,@(map (match-lambda
  659. ((options . name)
  660. `(define ,name
  661. (options->transformation ',options))))
  662. transformation-procedures)
  663. (packages->manifest
  664. (list ,@(map (lambda (entry)
  665. (define options
  666. (entry-transformations entry))
  667. (define name
  668. (qualified-name entry))
  669. (match (manifest-entry-output entry)
  670. ("out"
  671. (transform options
  672. `(specification->package ,name)))
  673. (output
  674. `(list ,(transform
  675. options
  676. `(specification->package ,name))
  677. ,output))))
  678. (manifest-entries manifest))))))))
  679. ;;;
  680. ;;; Manifest transactions.
  681. ;;;
  682. (define-record-type* <manifest-transaction> manifest-transaction
  683. make-manifest-transaction
  684. manifest-transaction?
  685. (install manifest-transaction-install ; list of <manifest-entry>
  686. (default '()))
  687. (remove manifest-transaction-remove ; list of <manifest-pattern>
  688. (default '())))
  689. (define (manifest-transaction-install-entry entry transaction)
  690. "Augment TRANSACTION's set of installed packages with ENTRY, a
  691. <manifest-entry>."
  692. (manifest-transaction
  693. (inherit transaction)
  694. (install
  695. (cons entry (manifest-transaction-install transaction)))))
  696. (define (manifest-transaction-remove-pattern pattern transaction)
  697. "Add PATTERN to TRANSACTION's list of packages to remove."
  698. (manifest-transaction
  699. (inherit transaction)
  700. (remove
  701. (cons pattern (manifest-transaction-remove transaction)))))
  702. (define (manifest-transaction-null? transaction)
  703. "Return true if TRANSACTION has no effect---i.e., it neither installs nor
  704. remove software."
  705. (match transaction
  706. (($ <manifest-transaction> () ()) #t)
  707. (($ <manifest-transaction> _ _) #f)))
  708. (define (manifest-transaction-removal-candidate? entry transaction)
  709. "Return true if ENTRY is a candidate for removal in TRANSACTION."
  710. (any (lambda (pattern)
  711. ((entry-predicate pattern) entry))
  712. (manifest-transaction-remove transaction)))
  713. (define (manifest-transaction-effects manifest transaction)
  714. "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
  715. the list of packages that would be removed, installed, upgraded, or downgraded
  716. when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
  717. where the head is the entry being upgraded and the tail is the entry that will
  718. replace it."
  719. (define (manifest-entry->pattern entry)
  720. (manifest-pattern
  721. (name (manifest-entry-name entry))
  722. (output (manifest-entry-output entry))))
  723. (define manifest-entry-pair=?
  724. (match-lambda*
  725. (((m1a . m2a) (m1b . m2b))
  726. (and (manifest-entry=? m1a m1b)
  727. (manifest-entry=? m2a m2b)))
  728. (_ #f)))
  729. (let loop ((input (manifest-transaction-install transaction))
  730. (install '())
  731. (upgrade '())
  732. (downgrade '()))
  733. (match input
  734. (()
  735. (let ((remove (manifest-transaction-remove transaction)))
  736. (values (delete-duplicates
  737. (manifest-matching-entries manifest remove)
  738. manifest-entry=?)
  739. (delete-duplicates (reverse install) manifest-entry=?)
  740. (delete-duplicates
  741. (reverse upgrade)
  742. manifest-entry-pair=?)
  743. (delete-duplicates
  744. (reverse downgrade)
  745. manifest-entry-pair=?))))
  746. ((entry rest ...)
  747. ;; Check whether installing ENTRY corresponds to the installation of a
  748. ;; new package or to an upgrade.
  749. ;; XXX: When the exact same output directory is installed, we're not
  750. ;; really upgrading anything. Add a check for that case.
  751. (let* ((pattern (manifest-entry->pattern entry))
  752. (previous (manifest-lookup manifest pattern))
  753. (newer? (and previous
  754. (version>=? (manifest-entry-version entry)
  755. (manifest-entry-version previous)))))
  756. (loop rest
  757. (if previous install (cons entry install))
  758. (if (and previous newer?)
  759. (alist-cons previous entry upgrade)
  760. upgrade)
  761. (if (and previous (not newer?))
  762. (alist-cons previous entry downgrade)
  763. downgrade)))))))
  764. (define (manifest-perform-transaction manifest transaction)
  765. "Perform TRANSACTION on MANIFEST and return the new manifest."
  766. (let ((install (manifest-transaction-install transaction))
  767. (remove (manifest-transaction-remove transaction)))
  768. (manifest-add (manifest-remove manifest remove)
  769. install)))
  770. ;;;
  771. ;;; Profiles.
  772. ;;;
  773. (define (manifest-inputs manifest)
  774. "Return a list of <gexp-input> objects for MANIFEST."
  775. (define entry->input
  776. (match-lambda
  777. (($ <manifest-entry> name version output thing deps)
  778. ;; THING may be a package or a file name. In the latter case, assume
  779. ;; it's already valid.
  780. (cons (gexp-input thing output)
  781. (append-map entry->input deps)))))
  782. (append-map entry->input (manifest-entries manifest)))
  783. (define* (manifest-lookup-package manifest name #:optional version)
  784. "Return as a monadic value the first package or store path referenced by
  785. MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
  786. if not found."
  787. ;; Return as a monadic value the package or store path referenced by the
  788. ;; manifest ENTRY, or #f if not referenced.
  789. (define (entry-lookup-package entry)
  790. (define (find-among-inputs inputs)
  791. (find (lambda (input)
  792. (and (package? input)
  793. (equal? name (package-name input))
  794. (if version
  795. (string-prefix? version (package-version input))
  796. #t)))
  797. inputs))
  798. (define (find-among-store-items items)
  799. (find (lambda (item)
  800. (let-values (((name* version*)
  801. (package-name->name+version
  802. (store-path-package-name item))))
  803. (and (string=? name name*)
  804. (if version
  805. (string-prefix? version version*)
  806. #t))))
  807. items))
  808. (with-monad %store-monad
  809. (match (manifest-entry-item entry)
  810. ((? package? package)
  811. (match (cons (list (package-name package) package)
  812. (package-transitive-inputs package))
  813. (((labels inputs . _) ...)
  814. (return (find-among-inputs inputs)))))
  815. ((? string? item)
  816. (mlet %store-monad ((refs (references* item)))
  817. (return (find-among-store-items refs))))
  818. (item
  819. ;; XXX: ITEM might be a 'computed-file' or anything like that, in
  820. ;; which case we don't know what to do. The fix may be to check
  821. ;; references once ITEM is compiled, as proposed at
  822. ;; <https://bugs.gnu.org/29927>.
  823. (return #f)))))
  824. (anym %store-monad
  825. entry-lookup-package (manifest-entries manifest)))
  826. (define (info-dir-file manifest)
  827. "Return a derivation that builds the 'dir' file for all the entries of
  828. MANIFEST."
  829. (define texinfo ;lazy reference
  830. (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
  831. (define gzip ;lazy reference
  832. (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
  833. (define glibc-utf8-locales ;lazy reference
  834. (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
  835. (define build
  836. (with-imported-modules '((guix build utils))
  837. #~(begin
  838. (use-modules (guix build utils)
  839. (srfi srfi-1) (srfi srfi-26)
  840. (ice-9 ftw))
  841. (define (info-file? file)
  842. (or (string-suffix? ".info" file)
  843. (string-suffix? ".info.gz" file)))
  844. (define (info-files top)
  845. (let ((infodir (string-append top "/share/info")))
  846. (map (cut string-append infodir "/" <>)
  847. (or (scandir infodir info-file?) '()))))
  848. (define (info-file-language file)
  849. (let* ((base (if (string-suffix? ".gz" file)
  850. (basename file ".info.gz")
  851. (basename file ".info")))
  852. (dot (string-rindex base #\.)))
  853. (if dot
  854. (string-drop base (+ 1 dot))
  855. "en")))
  856. (define (install-info info)
  857. (let ((language (info-file-language info)))
  858. ;; We need to choose a valid locale for $LANGUAGE to be honored.
  859. (setenv "LC_ALL" "en_US.utf8")
  860. (setenv "LANGUAGE" language)
  861. (zero?
  862. (system* #+(file-append texinfo "/bin/install-info")
  863. "--silent" info
  864. (apply string-append #$output "/share/info/dir"
  865. (if (string=? "en" language)
  866. '("")
  867. `("." ,language)))))))
  868. (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
  869. (setenv "GUIX_LOCPATH"
  870. #+(file-append glibc-utf8-locales "/lib/locale"))
  871. (mkdir-p (string-append #$output "/share/info"))
  872. (exit (every install-info
  873. (append-map info-files
  874. '#$(manifest-inputs manifest)))))))
  875. (gexp->derivation "info-dir" build
  876. #:local-build? #t
  877. #:substitutable? #f
  878. #:properties
  879. `((type . profile-hook)
  880. (hook . info-dir))))
  881. (define (ghc-package-cache-file manifest)
  882. "Return a derivation that builds the GHC 'package.cache' file for all the
  883. entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
  884. (define ghc ;lazy reference
  885. (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
  886. (define build
  887. (with-imported-modules '((guix build utils))
  888. #~(begin
  889. (use-modules (guix build utils)
  890. (srfi srfi-1) (srfi srfi-26)
  891. (ice-9 ftw))
  892. (define ghc-name-version
  893. (let* ((base (basename #+ghc)))
  894. (string-drop base
  895. (+ 1 (string-index base #\-)))))
  896. (define db-subdir
  897. (string-append "lib/" ghc-name-version "/package.conf.d"))
  898. (define db-dir
  899. (string-append #$output "/" db-subdir))
  900. (define (conf-files top)
  901. (let ((db (string-append top "/" db-subdir)))
  902. (if (file-exists? db)
  903. (find-files db "\\.conf$")
  904. '())))
  905. (define (copy-conf-file conf)
  906. (let ((base (basename conf)))
  907. (copy-file conf (string-append db-dir "/" base))))
  908. (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
  909. (for-each copy-conf-file
  910. (append-map conf-files
  911. (delete-duplicates
  912. '#$(manifest-inputs manifest))))
  913. (let ((success
  914. (zero?
  915. (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
  916. (string-append "--package-db=" db-dir)))))
  917. (for-each delete-file (find-files db-dir "\\.conf$"))
  918. (exit success)))))
  919. (with-monad %store-monad
  920. ;; Don't depend on GHC when there's nothing to do.
  921. (if (any (cut string-prefix? "ghc" <>)
  922. (map manifest-entry-name (manifest-entries manifest)))
  923. (gexp->derivation "ghc-package-cache" build
  924. #:local-build? #t
  925. #:substitutable? #f
  926. #:properties
  927. `((type . profile-hook)
  928. (hook . ghc-package-cache)))
  929. (return #f))))
  930. (define (ca-certificate-bundle manifest)
  931. "Return a derivation that builds a single-file bundle containing the CA
  932. certificates in the /etc/ssl/certs sub-directories of the packages in
  933. MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
  934. ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
  935. ;; for a discussion.
  936. (define glibc-utf8-locales ;lazy reference
  937. (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
  938. (define build
  939. (with-imported-modules '((guix build utils))
  940. #~(begin
  941. (use-modules (guix build utils)
  942. (rnrs io ports)
  943. (srfi srfi-1)
  944. (srfi srfi-26)
  945. (ice-9 ftw)
  946. (ice-9 match))
  947. (define (pem-file? file)
  948. (string-suffix? ".pem" file))
  949. (define (ca-files top)
  950. (let ((cert-dir (string-append top "/etc/ssl/certs")))
  951. (map (cut string-append cert-dir "/" <>)
  952. (or (scandir cert-dir pem-file?) '()))))
  953. (define (concatenate-files files result)
  954. "Make RESULT the concatenation of all of FILES."
  955. (define (dump file port)
  956. (display (call-with-input-file file get-string-all)
  957. port)
  958. (newline port)) ;required, see <https://bugs.debian.org/635570>
  959. (call-with-output-file result
  960. (lambda (port)
  961. (for-each (cut dump <> port) files))))
  962. ;; Some file names in the NSS certificates are UTF-8 encoded so
  963. ;; install a UTF-8 locale.
  964. (setenv "LOCPATH"
  965. (string-append #+glibc-utf8-locales "/lib/locale/"
  966. #+(version-major+minor
  967. (package-version glibc-utf8-locales))))
  968. (setlocale LC_ALL "en_US.utf8")
  969. (match (append-map ca-files '#$(manifest-inputs manifest))
  970. (()
  971. ;; Since there are no CA files, just create an empty directory. Do
  972. ;; not create the etc/ssl/certs sub-directory, since that would
  973. ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
  974. ;; defined.
  975. (mkdir #$output)
  976. #t)
  977. ((ca-files ...)
  978. (let ((result (string-append #$output "/etc/ssl/certs")))
  979. (mkdir-p result)
  980. (concatenate-files ca-files
  981. (string-append result
  982. "/ca-certificates.crt"))
  983. #t))))))
  984. (gexp->derivation "ca-certificate-bundle" build
  985. #:local-build? #t
  986. #:substitutable? #f
  987. #:properties
  988. `((type . profile-hook)
  989. (hook . ca-certificate-bundle))))
  990. (define (glib-schemas manifest)
  991. "Return a derivation that unions all schemas from manifest entries and
  992. creates the Glib 'gschemas.compiled' file."
  993. (define glib ; lazy reference
  994. (module-ref (resolve-interface '(gnu packages glib)) 'glib))
  995. (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
  996. ;; XXX: Can't use glib-compile-schemas corresponding
  997. ;; to the glib referenced by 'manifest'. Because
  998. ;; '%glib' can be either a package or store path, and
  999. ;; there's no way to get the "bin" output for the later.
  1000. (glib-compile-schemas
  1001. -> #~(string-append #+glib:bin
  1002. "/bin/glib-compile-schemas")))
  1003. (define build
  1004. (with-imported-modules '((guix build utils)
  1005. (guix build union)
  1006. (guix build profiles)
  1007. (guix search-paths)
  1008. (guix records))
  1009. #~(begin
  1010. (use-modules (guix build utils)
  1011. (guix build union)
  1012. (guix build profiles)
  1013. (srfi srfi-26))
  1014. (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
  1015. (schemadirs (filter file-exists?
  1016. (map (cut string-append <> "/share/glib-2.0/schemas")
  1017. '#$(manifest-inputs manifest)))))
  1018. ;; Union all the schemas.
  1019. (mkdir-p (string-append #$output "/share/glib-2.0"))
  1020. (union-build destdir schemadirs
  1021. #:log-port (%make-void-port "w"))
  1022. (let ((dir destdir))
  1023. (when (file-is-directory? dir)
  1024. (ensure-writable-directory dir)
  1025. (invoke #+glib-compile-schemas
  1026. (string-append "--targetdir=" dir)
  1027. dir)))))))
  1028. ;; Don't run the hook when there's nothing to do.
  1029. (if %glib
  1030. (gexp->derivation "glib-schemas" build
  1031. #:local-build? #t
  1032. #:substitutable? #f
  1033. #:properties
  1034. `((type . profile-hook)
  1035. (hook . glib-schemas)))
  1036. (return #f))))
  1037. (define (gtk-icon-themes manifest)
  1038. "Return a derivation that unions all icon themes from manifest entries and
  1039. creates the GTK+ 'icon-theme.cache' file for each theme."
  1040. (define gtk+ ; lazy reference
  1041. (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
  1042. (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
  1043. ;; XXX: Can't use gtk-update-icon-cache corresponding
  1044. ;; to the gtk+ referenced by 'manifest'. Because
  1045. ;; '%gtk+' can be either a package or store path, and
  1046. ;; there's no way to get the "bin" output for the later.
  1047. (gtk-update-icon-cache
  1048. -> #~(string-append #+gtk+:bin
  1049. "/bin/gtk-update-icon-cache")))
  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. (ice-9 ftw))
  1062. (let* ((destdir (string-append #$output "/share/icons"))
  1063. (icondirs (filter file-exists?
  1064. (map (cut string-append <> "/share/icons")
  1065. '#$(manifest-inputs manifest)))))
  1066. ;; Union all the icons.
  1067. (mkdir-p (string-append #$output "/share"))
  1068. (union-build destdir icondirs
  1069. #:log-port (%make-void-port "w"))
  1070. ;; Update the 'icon-theme.cache' file for each icon theme.
  1071. (for-each
  1072. (lambda (theme)
  1073. (let ((dir (string-append destdir "/" theme)))
  1074. ;; Occasionally DESTDIR contains plain files, such as
  1075. ;; "abiword_48.png". Ignore these.
  1076. (when (file-is-directory? dir)
  1077. (ensure-writable-directory dir)
  1078. (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
  1079. (scandir destdir (negate (cut member <> '("." "..")))))))))
  1080. ;; Don't run the hook when there's nothing to do.
  1081. (if %gtk+
  1082. (gexp->derivation "gtk-icon-themes" build
  1083. #:local-build? #t
  1084. #:substitutable? #f
  1085. #:properties
  1086. `((type . profile-hook)
  1087. (hook . gtk-icon-themes)))
  1088. (return #f))))
  1089. (define (gtk-im-modules manifest)
  1090. "Return a derivation that builds the cache files for input method modules
  1091. for both major versions of GTK+."
  1092. (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
  1093. (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
  1094. (define (build gtk gtk-version query)
  1095. (let ((major (string-take gtk-version 1)))
  1096. (with-imported-modules '((guix build utils)
  1097. (guix build union)
  1098. (guix build profiles)
  1099. (guix search-paths)
  1100. (guix records))
  1101. #~(begin
  1102. (use-modules (guix build utils)
  1103. (guix build union)
  1104. (guix build profiles)
  1105. (ice-9 popen)
  1106. (srfi srfi-1)
  1107. (srfi srfi-26))
  1108. (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
  1109. #$gtk-version))
  1110. (destdir (string-append #$output prefix))
  1111. (moddirs (cons (string-append #$gtk prefix "/immodules")
  1112. (filter file-exists?
  1113. (map (cut string-append <> prefix "/immodules")
  1114. '#$(manifest-inputs manifest)))))
  1115. (modules (append-map (cut find-files <> "\\.so$")
  1116. moddirs)))
  1117. ;; Generate a new immodules cache file.
  1118. (mkdir-p (string-append #$output prefix))
  1119. (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
  1120. (outfile (string-append #$output prefix
  1121. "/immodules-gtk" #$major ".cache")))
  1122. (dynamic-wind
  1123. (const #t)
  1124. (lambda ()
  1125. (call-with-output-file outfile
  1126. (lambda (out)
  1127. (while (not (eof-object? (peek-char pipe)))
  1128. (write-char (read-char pipe) out))))
  1129. #t)
  1130. (lambda ()
  1131. (close-pipe pipe)))))))))
  1132. ;; Don't run the hook when there's nothing to do.
  1133. (let* ((pkg-gtk+ (module-ref ; lazy reference
  1134. (resolve-interface '(gnu packages gtk)) 'gtk+))
  1135. (pkg-gtk+2 (module-ref ; lazy reference
  1136. (resolve-interface '(gnu packages gtk)) 'gtk+-2))
  1137. (gexp #~(begin
  1138. #$(if gtk+
  1139. (build
  1140. gtk+ "3.0.0"
  1141. ;; Use 'gtk-query-immodules-3.0' from the 'bin'
  1142. ;; output of latest gtk+ package.
  1143. #~(string-append
  1144. #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
  1145. #t)
  1146. #$(if gtk+-2
  1147. (build
  1148. gtk+-2 "2.10.0"
  1149. #~(string-append
  1150. #$pkg-gtk+2:bin "/bin/gtk-query-immodules-2.0"))
  1151. #t))))
  1152. (if (or gtk+ gtk+-2)
  1153. (gexp->derivation "gtk-im-modules" gexp
  1154. #:local-build? #t
  1155. #:substitutable? #f
  1156. #:properties
  1157. `((type . profile-hook)
  1158. (hook . gtk-im-modules)))
  1159. (return #f)))))
  1160. (define (linux-module-database manifest)
  1161. "Return a derivation that unites all the kernel modules of the manifest
  1162. and creates the dependency graph of all these kernel modules.
  1163. This is meant to be used as a profile hook."
  1164. (define kmod ; lazy reference
  1165. (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
  1166. (define guile-zlib
  1167. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  1168. (define build
  1169. (with-imported-modules (source-module-closure
  1170. '((guix build utils)
  1171. (gnu build linux-modules)))
  1172. (with-extensions (list guile-zlib)
  1173. #~(begin
  1174. (use-modules (ice-9 ftw)
  1175. (ice-9 match)
  1176. (srfi srfi-1) ; append-map
  1177. (gnu build linux-modules))
  1178. (let* ((inputs '#$(manifest-inputs manifest))
  1179. (module-directories
  1180. (map (lambda (directory)
  1181. (string-append directory "/lib/modules"))
  1182. inputs))
  1183. (directory-entries
  1184. (lambda (directory)
  1185. (or (scandir directory
  1186. (lambda (basename)
  1187. (not (string-prefix? "." basename))))
  1188. '())))
  1189. ;; Note: Should usually result in one entry.
  1190. (versions (delete-duplicates
  1191. (append-map directory-entries
  1192. module-directories))))
  1193. (match versions
  1194. ((version)
  1195. (let ((old-path (getenv "PATH")))
  1196. (setenv "PATH" #+(file-append kmod "/bin"))
  1197. (make-linux-module-directory inputs version #$output)
  1198. (setenv "PATH" old-path)))
  1199. (()
  1200. ;; Nothing here, maybe because this is a kernel with
  1201. ;; CONFIG_MODULES=n.
  1202. (mkdir #$output))
  1203. (_ (error "Specified Linux kernel and Linux kernel modules
  1204. are not all of the same version"))))))))
  1205. (gexp->derivation "linux-module-database" build
  1206. #:local-build? #t
  1207. #:substitutable? #f
  1208. #:properties
  1209. `((type . profile-hook)
  1210. (hook . linux-module-database))))
  1211. (define (xdg-desktop-database manifest)
  1212. "Return a derivation that builds the @file{mimeinfo.cache} database from
  1213. desktop files. It's used to query what applications can handle a given
  1214. MIME type."
  1215. (define desktop-file-utils ; lazy reference
  1216. (module-ref (resolve-interface '(gnu packages freedesktop))
  1217. 'desktop-file-utils))
  1218. (mlet %store-monad ((glib
  1219. (manifest-lookup-package
  1220. manifest "glib")))
  1221. (define build
  1222. (with-imported-modules '((guix build utils)
  1223. (guix build union))
  1224. #~(begin
  1225. (use-modules (srfi srfi-26)
  1226. (guix build utils)
  1227. (guix build union))
  1228. (let* ((destdir (string-append #$output "/share/applications"))
  1229. (appdirs (filter file-exists?
  1230. (map (cut string-append <>
  1231. "/share/applications")
  1232. '#$(manifest-inputs manifest))))
  1233. (update-desktop-database (string-append
  1234. #+desktop-file-utils
  1235. "/bin/update-desktop-database")))
  1236. (mkdir-p (string-append #$output "/share"))
  1237. (union-build destdir appdirs
  1238. #:log-port (%make-void-port "w"))
  1239. (exit (zero? (system* update-desktop-database destdir)))))))
  1240. ;; Don't run the hook when 'glib' is not referenced.
  1241. (if glib
  1242. (gexp->derivation "xdg-desktop-database" build
  1243. #:local-build? #t
  1244. #:substitutable? #f
  1245. #:properties
  1246. `((type . profile-hook)
  1247. (hook . xdg-desktop-database)))
  1248. (return #f))))
  1249. (define (xdg-mime-database manifest)
  1250. "Return a derivation that builds the @file{mime.cache} database from manifest
  1251. entries. It's used to query the MIME type of a given file."
  1252. (define shared-mime-info ; lazy reference
  1253. (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
  1254. (mlet %store-monad ((glib (manifest-lookup-package manifest "glib")))
  1255. (define build
  1256. (with-imported-modules '((guix build utils)
  1257. (guix build union))
  1258. #~(begin
  1259. (use-modules (guix build utils)
  1260. (guix build union)
  1261. (srfi srfi-26)
  1262. (ice-9 match))
  1263. (let* ((datadir (string-append #$output "/share"))
  1264. (destdir (string-append datadir "/mime"))
  1265. (pkgdirs (filter file-exists?
  1266. (map (cut string-append <>
  1267. "/share/mime/packages")
  1268. (cons #+shared-mime-info
  1269. '#$(manifest-inputs manifest))))))
  1270. (match pkgdirs
  1271. ((shared-mime-info)
  1272. ;; PKGDIRS contains nothing but 'shared-mime-info', which
  1273. ;; already contains its database, so nothing to do.
  1274. (mkdir-p datadir)
  1275. (symlink #$(file-append shared-mime-info "/share/mime")
  1276. destdir))
  1277. (_
  1278. ;; PKGDIRS contains additional packages providing
  1279. ;; 'share/mime/packages' (very few packages do so) so rebuild
  1280. ;; the database. TODO: Find a way to avoid reprocessing
  1281. ;; 'shared-mime-info', which is the most expensive one.
  1282. (mkdir-p destdir)
  1283. (union-build (string-append destdir "/packages") pkgdirs
  1284. #:log-port (%make-void-port "w"))
  1285. (setenv "XDG_DATA_HOME" datadir)
  1286. (invoke #+(file-append shared-mime-info
  1287. "/bin/update-mime-database")
  1288. destdir)))))))
  1289. ;; Don't run the hook when there are no GLib based applications.
  1290. (if glib
  1291. (gexp->derivation "xdg-mime-database" build
  1292. #:local-build? #t
  1293. #:substitutable? #f
  1294. #:properties
  1295. `((type . profile-hook)
  1296. (hook . xdg-mime-database)))
  1297. (return #f))))
  1298. ;; Several font packages may install font files into same directory, so
  1299. ;; fonts.dir and fonts.scale file should be generated here, instead of in
  1300. ;; packages.
  1301. (define (fonts-dir-file manifest)
  1302. "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
  1303. files for the fonts of the @var{manifest} entries."
  1304. (define mkfontscale
  1305. (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
  1306. (define mkfontdir
  1307. (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
  1308. (define build
  1309. #~(begin
  1310. (use-modules (srfi srfi-26)
  1311. (guix build utils)
  1312. (guix build union))
  1313. (let ((fonts-dirs (filter file-exists?
  1314. (map (cut string-append <>
  1315. "/share/fonts")
  1316. '#$(manifest-inputs manifest)))))
  1317. (mkdir #$output)
  1318. (if (null? fonts-dirs)
  1319. (exit #t)
  1320. (let* ((share-dir (string-append #$output "/share"))
  1321. (fonts-dir (string-append share-dir "/fonts"))
  1322. (mkfontscale (string-append #+mkfontscale
  1323. "/bin/mkfontscale"))
  1324. (mkfontdir (string-append #+mkfontdir
  1325. "/bin/mkfontdir"))
  1326. (empty-file? (lambda (filename)
  1327. (call-with-ascii-input-file filename
  1328. (lambda (p)
  1329. (eqv? #\0 (read-char p))))))
  1330. (fonts-dir-file "fonts.dir")
  1331. (fonts-scale-file "fonts.scale"))
  1332. (mkdir-p share-dir)
  1333. ;; Create all sub-directories, because we may create fonts.dir
  1334. ;; and fonts.scale files in the sub-directories.
  1335. (union-build fonts-dir fonts-dirs
  1336. #:log-port (%make-void-port "w")
  1337. #:create-all-directories? #t)
  1338. (let ((directories (find-files fonts-dir
  1339. (lambda (file stat)
  1340. (eq? 'directory (stat:type stat)))
  1341. #:directories? #t)))
  1342. (for-each (lambda (dir)
  1343. (with-directory-excursion dir
  1344. (when (file-exists? fonts-scale-file)
  1345. (delete-file fonts-scale-file))
  1346. (when (file-exists? fonts-dir-file)
  1347. (delete-file fonts-dir-file))
  1348. (unless (and (zero? (system* mkfontscale))
  1349. (zero? (system* mkfontdir)))
  1350. (exit #f))
  1351. (when (and (file-exists? fonts-scale-file)
  1352. (empty-file? fonts-scale-file))
  1353. (delete-file fonts-scale-file))
  1354. (when (and (file-exists? fonts-dir-file)
  1355. (empty-file? fonts-dir-file))
  1356. (delete-file fonts-dir-file))))
  1357. directories)))))))
  1358. (gexp->derivation "fonts-dir" build
  1359. #:modules '((guix build utils)
  1360. (guix build union)
  1361. (srfi srfi-26))
  1362. #:local-build? #t
  1363. #:substitutable? #f
  1364. #:properties
  1365. `((type . profile-hook)
  1366. (hook . fonts-dir))))
  1367. (define (manual-database manifest)
  1368. "Return a derivation that builds the manual page database (\"mandb\") for
  1369. the entries in MANIFEST."
  1370. (define gdbm-ffi
  1371. (module-ref (resolve-interface '(gnu packages guile))
  1372. 'guile-gdbm-ffi))
  1373. (define guile-zlib
  1374. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  1375. (define modules
  1376. (delete '(guix config)
  1377. (source-module-closure `((guix build utils)
  1378. (guix man-db)))))
  1379. (define build
  1380. (with-imported-modules modules
  1381. (with-extensions (list gdbm-ffi ;for (guix man-db)
  1382. guile-zlib)
  1383. #~(begin
  1384. (use-modules (guix man-db)
  1385. (guix build utils)
  1386. (ice-9 threads)
  1387. (srfi srfi-1)
  1388. (srfi srfi-19))
  1389. (define (print-string msg)
  1390. (display msg)
  1391. (force-output))
  1392. (define-syntax-rule (print fmt args ...)
  1393. ;; Build up the string and display it at once.
  1394. (print-string (format #f fmt args ...)))
  1395. (define (compute-entry directory count total)
  1396. (print "\r[~3d/~3d] building list of man-db entries..."
  1397. count total)
  1398. (let ((man (string-append directory "/share/man")))
  1399. (if (directory-exists? man)
  1400. (mandb-entries man)
  1401. '())))
  1402. (define (compute-entries)
  1403. ;; This is the most expensive part (I/O and CPU, due to
  1404. ;; decompression), so report progress as we traverse INPUTS.
  1405. ;; Cap at 4 threads because we don't see any speedup beyond that
  1406. ;; on an SSD laptop.
  1407. (let* ((inputs '#$(manifest-inputs manifest))
  1408. (total (length inputs))
  1409. (threads (min (parallel-job-count) 4)))
  1410. (concatenate
  1411. (n-par-map threads compute-entry inputs
  1412. (iota total 1)
  1413. (make-list total total)))))
  1414. (define man-directory
  1415. (string-append #$output "/share/man"))
  1416. (mkdir-p man-directory)
  1417. (format #t "Creating manual page database...~%")
  1418. (force-output)
  1419. (let* ((start (current-time))
  1420. (entries (compute-entries))
  1421. (_ (write-mandb-database (string-append man-directory
  1422. "/index.db")
  1423. entries))
  1424. (duration (time-difference (current-time) start)))
  1425. (newline)
  1426. (format #t "~a entries processed in ~,1f s~%"
  1427. (length entries)
  1428. (+ (time-second duration)
  1429. (* (time-nanosecond duration) (expt 10 -9))))
  1430. (force-output))))))
  1431. (gexp->derivation "manual-database" build
  1432. ;; Work around GDBM 1.13 issue whereby uninitialized bytes
  1433. ;; get written to disk:
  1434. ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
  1435. #:env-vars `(("MALLOC_PERTURB_" . "1"))
  1436. #:substitutable? #f
  1437. #:local-build? #t
  1438. #:properties
  1439. `((type . profile-hook)
  1440. (hook . manual-database))))
  1441. (define (texlive-configuration manifest)
  1442. "Return a derivation that builds a TeXlive configuration for the entries in
  1443. MANIFEST."
  1444. (define entry->texlive-input
  1445. (match-lambda
  1446. (($ <manifest-entry> name version output thing deps)
  1447. (if (string-prefix? "texlive-" name)
  1448. (cons (gexp-input thing output)
  1449. (append-map entry->texlive-input deps))
  1450. '()))))
  1451. (define build
  1452. (with-imported-modules '((guix build utils)
  1453. (guix build union))
  1454. #~(begin
  1455. (use-modules (guix build utils)
  1456. (guix build union))
  1457. ;; Build a modifiable union of all texlive inputs. We do this so
  1458. ;; that TeX live can resolve the parent and grandparent directories
  1459. ;; correctly. There might be a more elegant way to accomplish this.
  1460. (union-build #$output
  1461. '#$(append-map entry->texlive-input
  1462. (manifest-entries manifest))
  1463. #:create-all-directories? #t
  1464. #:log-port (%make-void-port "w"))
  1465. (let ((texmf.cnf (string-append
  1466. #$output
  1467. "/share/texmf-dist/web2c/texmf.cnf")))
  1468. (when (file-exists? texmf.cnf)
  1469. (substitute* texmf.cnf
  1470. (("^TEXMFROOT = .*")
  1471. (string-append "TEXMFROOT = " #$output "/share\n"))
  1472. (("^TEXMF = .*")
  1473. "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
  1474. #t)))
  1475. (with-monad %store-monad
  1476. (if (any (cut string-prefix? "texlive-" <>)
  1477. (map manifest-entry-name (manifest-entries manifest)))
  1478. (gexp->derivation "texlive-configuration" build
  1479. #:substitutable? #f
  1480. #:local-build? #t
  1481. #:properties
  1482. `((type . profile-hook)
  1483. (hook . texlive-configuration)))
  1484. (return #f))))
  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. glib-schemas
  1494. gtk-icon-themes
  1495. gtk-im-modules
  1496. texlive-configuration
  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