profiles.scm 81 KB

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