profiles.scm 82 KB

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