channels.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix channels)
  21. #:use-module (git)
  22. #:use-module (guix git)
  23. #:use-module (guix records)
  24. #:use-module (guix gexp)
  25. #:use-module (guix modules)
  26. #:use-module (guix discovery)
  27. #:use-module (guix monads)
  28. #:use-module (guix profiles)
  29. #:use-module (guix packages)
  30. #:use-module (guix derivations)
  31. #:use-module (guix combinators)
  32. #:use-module (guix diagnostics)
  33. #:use-module (guix sets)
  34. #:use-module (guix store)
  35. #:use-module (guix i18n)
  36. #:use-module ((guix utils)
  37. #:select (source-properties->location
  38. &error-location))
  39. #:use-module (srfi srfi-1)
  40. #:use-module (srfi srfi-2)
  41. #:use-module (srfi srfi-9)
  42. #:use-module (srfi srfi-11)
  43. #:use-module (srfi srfi-26)
  44. #:use-module (srfi srfi-34)
  45. #:use-module (srfi srfi-35)
  46. #:autoload (guix self) (whole-package make-config.scm)
  47. #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
  48. #:use-module (ice-9 match)
  49. #:use-module (ice-9 vlist)
  50. #:use-module ((ice-9 rdelim) #:select (read-string))
  51. #:export (channel
  52. channel?
  53. channel-name
  54. channel-url
  55. channel-branch
  56. channel-commit
  57. channel-location
  58. %default-channels
  59. guix-channel?
  60. channel-instance?
  61. channel-instance-channel
  62. channel-instance-commit
  63. channel-instance-checkout
  64. latest-channel-instances
  65. checkout->channel-instance
  66. latest-channel-derivation
  67. channel-instances->manifest
  68. %channel-profile-hooks
  69. channel-instances->derivation
  70. profile-channels
  71. channel-news-entry?
  72. channel-news-entry-commit
  73. channel-news-entry-tag
  74. channel-news-entry-title
  75. channel-news-entry-body
  76. channel-news-for-commit))
  77. ;;; Commentary:
  78. ;;;
  79. ;;; This module implements "channels." A channel is usually a source of
  80. ;;; package definitions. There's a special channel, the 'guix' channel, that
  81. ;;; provides all of Guix, including its commands and its documentation.
  82. ;;; User-defined channels are expected to typically provide a bunch of .scm
  83. ;;; files meant to be added to the '%package-search-path'.
  84. ;;;
  85. ;;; This module provides tools to fetch and update channels from a Git
  86. ;;; repository and to build them.
  87. ;;;
  88. ;;; Code:
  89. (define-record-type* <channel> channel make-channel
  90. channel?
  91. (name channel-name)
  92. (url channel-url)
  93. (branch channel-branch (default "master"))
  94. (commit channel-commit (default #f))
  95. (location channel-location
  96. (default (current-source-location)) (innate)))
  97. (define %default-channels
  98. ;; Default list of channels.
  99. (list (channel
  100. (name 'guix)
  101. (branch "master")
  102. (url "https://git.savannah.gnu.org/git/guix.git"))))
  103. (define (guix-channel? channel)
  104. "Return true if CHANNEL is the 'guix' channel."
  105. (eq? 'guix (channel-name channel)))
  106. (define-record-type <channel-instance>
  107. (channel-instance channel commit checkout)
  108. channel-instance?
  109. (channel channel-instance-channel)
  110. (commit channel-instance-commit)
  111. (checkout channel-instance-checkout))
  112. (define-record-type <channel-metadata>
  113. (channel-metadata directory dependencies news-file)
  114. channel-metadata?
  115. (directory channel-metadata-directory) ;string with leading slash
  116. (dependencies channel-metadata-dependencies) ;list of <channel>
  117. (news-file channel-metadata-news-file)) ;string | #f
  118. (define (channel-reference channel)
  119. "Return the \"reference\" for CHANNEL, an sexp suitable for
  120. 'latest-repository-commit'."
  121. (match (channel-commit channel)
  122. (#f `(branch . ,(channel-branch channel)))
  123. (commit `(commit . ,(channel-commit channel)))))
  124. (define (read-channel-metadata port)
  125. "Read from PORT channel metadata in the format expected for the
  126. '.guix-channel' file. Return a <channel-metadata> record, or raise an error
  127. if valid metadata could not be read from PORT."
  128. (match (read port)
  129. (('channel ('version 0) properties ...)
  130. (let ((directory (and=> (assoc-ref properties 'directory) first))
  131. (dependencies (or (assoc-ref properties 'dependencies) '()))
  132. (news-file (and=> (assoc-ref properties 'news-file) first)))
  133. (channel-metadata
  134. (cond ((not directory) "/") ;directory
  135. ((string-prefix? "/" directory) directory)
  136. (else (string-append "/" directory)))
  137. (map (lambda (item) ;dependencies
  138. (let ((get (lambda* (key #:optional default)
  139. (or (and=> (assoc-ref item key) first) default))))
  140. (and-let* ((name (get 'name))
  141. (url (get 'url))
  142. (branch (get 'branch "master")))
  143. (channel
  144. (name name)
  145. (branch branch)
  146. (url url)
  147. (commit (get 'commit))))))
  148. dependencies)
  149. news-file))) ;news-file
  150. ((and ('channel ('version version) _ ...) sexp)
  151. (raise (condition
  152. (&message (message "unsupported '.guix-channel' version"))
  153. (&error-location
  154. (location (source-properties->location
  155. (source-properties sexp)))))))
  156. (sexp
  157. (raise (condition
  158. (&message (message "invalid '.guix-channel' file"))
  159. (&error-location
  160. (location (source-properties->location
  161. (source-properties sexp)))))))))
  162. (define (read-channel-metadata-from-source source)
  163. "Return a channel-metadata record read from channel's SOURCE/.guix-channel
  164. description file, or return the default channel-metadata record if that file
  165. doesn't exist."
  166. (catch 'system-error
  167. (lambda ()
  168. (call-with-input-file (string-append source "/.guix-channel")
  169. read-channel-metadata))
  170. (lambda args
  171. (if (= ENOENT (system-error-errno args))
  172. (channel-metadata "/" '() #f)
  173. (apply throw args)))))
  174. (define (channel-instance-metadata instance)
  175. "Return a channel-metadata record read from the channel INSTANCE's
  176. description file or its default value."
  177. (read-channel-metadata-from-source (channel-instance-checkout instance)))
  178. (define (channel-instance-dependencies instance)
  179. "Return the list of channels that are declared as dependencies for the given
  180. channel INSTANCE."
  181. (channel-metadata-dependencies (channel-instance-metadata instance)))
  182. (define* (latest-channel-instances store channels #:optional (previous-channels '()))
  183. "Return a list of channel instances corresponding to the latest checkouts of
  184. CHANNELS and the channels on which they depend. PREVIOUS-CHANNELS is a list
  185. of previously processed channels."
  186. ;; Only process channels that are unique, or that are more specific than a
  187. ;; previous channel specification.
  188. (define (ignore? channel others)
  189. (member channel others
  190. (lambda (a b)
  191. (and (eq? (channel-name a) (channel-name b))
  192. (or (channel-commit b)
  193. (not (or (channel-commit a)
  194. (channel-commit b))))))))
  195. ;; Accumulate a list of instances. A list of processed channels is also
  196. ;; accumulated to decide on duplicate channel specifications.
  197. (define-values (resulting-channels instances)
  198. (fold2 (lambda (channel previous-channels instances)
  199. (if (ignore? channel previous-channels)
  200. (values previous-channels instances)
  201. (begin
  202. (format (current-error-port)
  203. (G_ "Updating channel '~a' from Git repository at '~a'...~%")
  204. (channel-name channel)
  205. (channel-url channel))
  206. (let-values (((checkout commit)
  207. (latest-repository-commit store (channel-url channel)
  208. #:ref (channel-reference
  209. channel))))
  210. (let ((instance (channel-instance channel commit checkout)))
  211. (let-values (((new-instances new-channels)
  212. (latest-channel-instances
  213. store
  214. (channel-instance-dependencies instance)
  215. previous-channels)))
  216. (values (append (cons channel new-channels)
  217. previous-channels)
  218. (append (cons instance new-instances)
  219. instances))))))))
  220. previous-channels
  221. '() ;instances
  222. channels))
  223. (let ((instance-name (compose channel-name channel-instance-channel)))
  224. ;; Remove all earlier channel specifications if they are followed by a
  225. ;; more specific one.
  226. (values (delete-duplicates instances
  227. (lambda (a b)
  228. (eq? (instance-name a) (instance-name b))))
  229. resulting-channels)))
  230. (define* (checkout->channel-instance checkout
  231. #:key commit
  232. (url checkout) (name 'guix))
  233. "Return a channel instance for CHECKOUT, which is assumed to be a checkout
  234. of COMMIT at URL. Use NAME as the channel name."
  235. (let* ((commit (or commit (make-string 40 #\0)))
  236. (channel (channel (name name)
  237. (commit commit)
  238. (url url))))
  239. (channel-instance channel commit checkout)))
  240. (define %self-build-file
  241. ;; The file containing code to build Guix. This serves the same purpose as
  242. ;; a makefile, and, similarly, is intended to always keep this name.
  243. "build-aux/build-self.scm")
  244. (define %pull-version
  245. ;; This is the version of the 'guix pull' protocol. It specifies what's
  246. ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
  247. ;; place a set of compiled Guile modules in ~/.config/guix/latest.
  248. 1)
  249. (define (standard-module-derivation name source core dependencies)
  250. "Return a derivation that builds with CORE, a Guix instance, the Scheme
  251. modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
  252. objects. The assumption is that SOURCE contains package modules to be added
  253. to '%package-module-path'."
  254. (let* ((metadata (read-channel-metadata-from-source source))
  255. (directory (channel-metadata-directory metadata)))
  256. (define build
  257. ;; This is code that we'll run in CORE, a Guix instance, with its own
  258. ;; modules and so on. That way, we make sure these modules are built for
  259. ;; the right Guile version, with the right dependencies, and that they get
  260. ;; to see the right (gnu packages …) modules.
  261. (with-extensions dependencies
  262. #~(begin
  263. (use-modules (guix build compile)
  264. (guix build utils)
  265. (srfi srfi-26))
  266. (define go
  267. (string-append #$output "/lib/guile/" (effective-version)
  268. "/site-ccache"))
  269. (define scm
  270. (string-append #$output "/share/guile/site/"
  271. (effective-version)))
  272. (let* ((subdir #$directory)
  273. (source (string-append #$source subdir)))
  274. (compile-files source go (find-files source "\\.scm$"))
  275. (mkdir-p (dirname scm))
  276. (symlink (string-append #$source subdir) scm))
  277. scm)))
  278. (gexp->derivation-in-inferior name build core)))
  279. (define (syscalls-reexports-local-variables? source)
  280. "Return true if (guix build syscalls) contains the bug described at
  281. <https://bugs.gnu.org/36723>."
  282. (catch 'system-error
  283. (lambda ()
  284. (define content
  285. (call-with-input-file (string-append source
  286. "/guix/build/syscalls.scm")
  287. read-string))
  288. ;; The faulty code would use the 're-export' macro, causing the
  289. ;; 'AT_SYMLINK_NOFOLLOW' local variable to be re-exported when using
  290. ;; Guile > 2.2.4.
  291. (string-contains content "(re-export variable)"))
  292. (lambda args
  293. (if (= ENOENT (system-error-errno args))
  294. #f
  295. (apply throw args)))))
  296. (define (guile-2.2.4)
  297. (module-ref (resolve-interface '(gnu packages guile))
  298. 'guile-2.2.4))
  299. (define %quirks
  300. ;; List of predicate/package pairs. This allows us provide information
  301. ;; about specific Guile versions that old Guix revisions might need to use
  302. ;; just to be able to build and run the trampoline in %SELF-BUILD-FILE. See
  303. ;; <https://bugs.gnu.org/37506>
  304. `((,syscalls-reexports-local-variables? . ,guile-2.2.4)))
  305. (define* (guile-for-source source #:optional (quirks %quirks))
  306. "Return the Guile package to use when building SOURCE or #f if the default
  307. '%guile-for-build' should be good enough."
  308. (let loop ((quirks quirks))
  309. (match quirks
  310. (()
  311. #f)
  312. (((predicate . guile) rest ...)
  313. (if (predicate source) (guile) (loop rest))))))
  314. (define* (build-from-source name source
  315. #:key core verbose? commit
  316. (dependencies '()))
  317. "Return a derivation to build Guix from SOURCE, using the self-build script
  318. contained therein; use COMMIT as the version string. When CORE is true, build
  319. package modules under SOURCE using CORE, an instance of Guix."
  320. ;; Running the self-build script makes it easier to update the build
  321. ;; procedure: the self-build script of the Guix-to-be-installed contains the
  322. ;; right dependencies, build procedure, etc., which the Guix-in-use may not
  323. ;; be know.
  324. (define script
  325. (string-append source "/" %self-build-file))
  326. (if (file-exists? script)
  327. (let ((build (save-module-excursion
  328. (lambda ()
  329. ;; Disable deprecation warnings; it's OK for SCRIPT to
  330. ;; use deprecated APIs and the user doesn't have to know
  331. ;; about it.
  332. (parameterize ((guix-warning-port
  333. (%make-void-port "w")))
  334. (primitive-load script)))))
  335. (guile (guile-for-source source)))
  336. ;; BUILD must be a monadic procedure of at least one argument: the
  337. ;; source tree.
  338. ;;
  339. ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
  340. ;; the future we'll fall back to a previous version of the protocol
  341. ;; when that happens.
  342. (mbegin %store-monad
  343. (mwhen guile
  344. (set-guile-for-build guile))
  345. (build source #:verbose? verbose? #:version commit
  346. #:pull-version %pull-version)))
  347. ;; Build a set of modules that extend Guix using the standard method.
  348. (standard-module-derivation name source core dependencies)))
  349. (define* (build-channel-instance instance
  350. #:optional core (dependencies '()))
  351. "Return, as a monadic value, the derivation for INSTANCE, a channel
  352. instance. DEPENDENCIES is a list of extensions providing Guile modules that
  353. INSTANCE depends on."
  354. (build-from-source (symbol->string
  355. (channel-name (channel-instance-channel instance)))
  356. (channel-instance-checkout instance)
  357. #:commit (channel-instance-commit instance)
  358. #:core core
  359. #:dependencies dependencies))
  360. (define (resolve-dependencies instances)
  361. "Return a procedure that, given one of the elements of INSTANCES, returns
  362. list of instances it depends on."
  363. (define channel-instance-name
  364. (compose channel-name channel-instance-channel))
  365. (define table ;map a name to an instance
  366. (fold (lambda (instance table)
  367. (vhash-consq (channel-instance-name instance)
  368. instance table))
  369. vlist-null
  370. instances))
  371. (define edges
  372. (fold (lambda (instance edges)
  373. (fold (lambda (channel edges)
  374. (let ((name (channel-name channel)))
  375. (match (vhash-assq name table)
  376. ((_ . target)
  377. (vhash-consq instance target edges)))))
  378. edges
  379. (channel-instance-dependencies instance)))
  380. vlist-null
  381. instances))
  382. (lambda (instance)
  383. (vhash-foldq* cons '() instance edges)))
  384. (define (channel-instance-derivations instances)
  385. "Return the list of derivations to build INSTANCES, in the same order as
  386. INSTANCES."
  387. (define core-instance
  388. ;; The 'guix' channel is treated specially: it's an implicit dependency of
  389. ;; all the other channels.
  390. (find (lambda (instance)
  391. (guix-channel? (channel-instance-channel instance)))
  392. instances))
  393. (define edges
  394. (resolve-dependencies instances))
  395. (define (instance->derivation instance)
  396. (mlet %store-monad ((system (current-system)))
  397. (mcached (if (eq? instance core-instance)
  398. (build-channel-instance instance)
  399. (mlet %store-monad ((core (instance->derivation core-instance))
  400. (deps (mapm %store-monad instance->derivation
  401. (edges instance))))
  402. (build-channel-instance instance core deps)))
  403. instance
  404. system)))
  405. (unless core-instance
  406. (let ((loc (and=> (any (compose channel-location channel-instance-channel)
  407. instances)
  408. source-properties->location)))
  409. (raise (apply make-compound-condition
  410. (condition
  411. (&message (message "'guix' channel is lacking")))
  412. (if loc
  413. (list (condition (&error-location (location loc))))
  414. '())))))
  415. (mapm %store-monad instance->derivation instances))
  416. (define (whole-package-for-legacy name modules)
  417. "Return a full-blown Guix package for MODULES, a derivation that builds Guix
  418. modules in the old ~/.config/guix/latest style."
  419. (define packages
  420. (resolve-interface '(gnu packages guile)))
  421. (define modules+compiled
  422. ;; Since MODULES contains both .scm and .go files at its root, re-bundle
  423. ;; it so that it has share/guile/site and lib/guile, which is what
  424. ;; 'whole-package' expects.
  425. (computed-file (derivation-name modules)
  426. (with-imported-modules '((guix build utils))
  427. #~(begin
  428. (use-modules (guix build utils))
  429. (define version
  430. (effective-version))
  431. (define share
  432. (string-append #$output "/share/guile/site"))
  433. (define lib
  434. (string-append #$output "/lib/guile/" version))
  435. (mkdir-p share) (mkdir-p lib)
  436. (symlink #$modules (string-append share "/" version))
  437. (symlink #$modules (string-append lib "/site-ccache"))))))
  438. (letrec-syntax ((list (syntax-rules (->)
  439. ((_)
  440. '())
  441. ((_ (module -> variable) rest ...)
  442. (cons (module-ref (resolve-interface
  443. '(gnu packages module))
  444. 'variable)
  445. (list rest ...)))
  446. ((_ variable rest ...)
  447. (cons (module-ref packages 'variable)
  448. (list rest ...))))))
  449. (whole-package name modules+compiled
  450. ;; In the "old style", %SELF-BUILD-FILE would simply return a
  451. ;; derivation that builds modules. We have to infer what the
  452. ;; dependencies of these modules were.
  453. (list guile-json-3 guile-git guile-bytestructures
  454. (ssh -> guile-ssh) (tls -> gnutls)))))
  455. (define (old-style-guix? drv)
  456. "Return true if DRV corresponds to a ~/.config/guix/latest style of
  457. derivation."
  458. ;; Here we rely on a gross historical fact: that derivations produced by the
  459. ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
  460. ;; dated May 30, 2018) did not depend on "guix-command.drv".
  461. (not (find (lambda (input)
  462. (string=? "guix-command"
  463. (derivation-name
  464. (derivation-input-derivation input))))
  465. (derivation-inputs drv))))
  466. (define (channel-instances->manifest instances)
  467. "Return a profile manifest with entries for all of INSTANCES, a list of
  468. channel instances."
  469. (define (instance->entry instance drv)
  470. (let ((commit (channel-instance-commit instance))
  471. (channel (channel-instance-channel instance)))
  472. (manifest-entry
  473. (name (symbol->string (channel-name channel)))
  474. (version (string-take commit 7))
  475. (item (if (guix-channel? channel)
  476. (if (old-style-guix? drv)
  477. (whole-package-for-legacy (string-append name "-" version)
  478. drv)
  479. drv)
  480. drv))
  481. (properties
  482. `((source (repository
  483. (version 0)
  484. (url ,(channel-url channel))
  485. (branch ,(channel-branch channel))
  486. (commit ,commit))))))))
  487. (mlet* %store-monad ((derivations (channel-instance-derivations instances))
  488. (entries -> (map instance->entry instances derivations)))
  489. (return (manifest entries))))
  490. (define (package-cache-file manifest)
  491. "Build a package cache file for the instance in MANIFEST. This is meant to
  492. be used as a profile hook."
  493. (mlet %store-monad ((profile (profile-derivation manifest
  494. #:hooks '())))
  495. (define build
  496. #~(begin
  497. (use-modules (gnu packages))
  498. (if (defined? 'generate-package-cache)
  499. (begin
  500. ;; Delegate package cache generation to the inferior.
  501. (format (current-error-port)
  502. "Generating package cache for '~a'...~%"
  503. #$profile)
  504. (generate-package-cache #$output))
  505. (mkdir #$output))))
  506. (gexp->derivation-in-inferior "guix-package-cache" build
  507. profile
  508. ;; If the Guix in PROFILE is too old and
  509. ;; lacks 'guix repl', don't build the cache
  510. ;; instead of failing.
  511. #:silent-failure? #t
  512. #:properties '((type . profile-hook)
  513. (hook . package-cache))
  514. #:local-build? #t)))
  515. (define %channel-profile-hooks
  516. ;; The default channel profile hooks.
  517. (cons package-cache-file %default-profile-hooks))
  518. (define (channel-instances->derivation instances)
  519. "Return the derivation of the profile containing INSTANCES, a list of
  520. channel instances."
  521. (mlet %store-monad ((manifest (channel-instances->manifest instances)))
  522. (profile-derivation manifest
  523. #:hooks %channel-profile-hooks)))
  524. (define latest-channel-instances*
  525. (store-lift latest-channel-instances))
  526. (define* (latest-channel-derivation #:optional (channels %default-channels))
  527. "Return as a monadic value the derivation that builds the profile for the
  528. latest instances of CHANNELS."
  529. (mlet %store-monad ((instances (latest-channel-instances* channels)))
  530. (channel-instances->derivation instances)))
  531. (define (profile-channels profile)
  532. "Return the list of channels corresponding to entries in PROFILE. If
  533. PROFILE is not a profile created by 'guix pull', return the empty list."
  534. (filter-map (lambda (entry)
  535. (match (assq 'source (manifest-entry-properties entry))
  536. (('source ('repository ('version 0)
  537. ('url url)
  538. ('branch branch)
  539. ('commit commit)
  540. _ ...))
  541. (channel (name (string->symbol
  542. (manifest-entry-name entry)))
  543. (url url)
  544. (commit commit)))
  545. ;; No channel information for this manifest entry.
  546. ;; XXX: Pre-0.15.0 Guix did not provide that information,
  547. ;; but there's not much we can do in that case.
  548. (_ #f)))
  549. ;; Show most recently installed packages last.
  550. (reverse
  551. (manifest-entries (profile-manifest profile)))))
  552. ;;;
  553. ;;; News.
  554. ;;;
  555. ;; Channel news.
  556. (define-record-type <channel-news>
  557. (channel-news entries)
  558. channel-news?
  559. (entries channel-news-entries)) ;list of <channel-news-entry>
  560. ;; News entry, associated with a specific commit of the channel.
  561. (define-record-type <channel-news-entry>
  562. (channel-news-entry commit tag title body)
  563. channel-news-entry?
  564. (commit channel-news-entry-commit) ;hex string | #f
  565. (tag channel-news-entry-tag) ;#f | string
  566. (title channel-news-entry-title) ;list of language tag/string pairs
  567. (body channel-news-entry-body)) ;list of language tag/string pairs
  568. (define (sexp->channel-news-entry entry)
  569. "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
  570. (define (pair language message)
  571. (cons (symbol->string language) message))
  572. (match entry
  573. (('entry ((and (or 'commit 'tag) type) commit-or-tag)
  574. ('title ((? symbol? title-tags) (? string? titles)) ...)
  575. ('body ((? symbol? body-tags) (? string? bodies)) ...)
  576. _ ...)
  577. (channel-news-entry (and (eq? type 'commit) commit-or-tag)
  578. (and (eq? type 'tag) commit-or-tag)
  579. (map pair title-tags titles)
  580. (map pair body-tags bodies)))
  581. (_
  582. (raise (condition
  583. (&message (message "invalid channel news entry"))
  584. (&error-location
  585. (location (source-properties->location
  586. (source-properties entry)))))))))
  587. (define (read-channel-news port)
  588. "Read a channel news feed from PORT and return it as a <channel-news>
  589. record."
  590. (match (false-if-exception (read port))
  591. (('channel-news ('version 0) entries ...)
  592. (channel-news (map sexp->channel-news-entry entries)))
  593. (('channel-news ('version version) _ ...)
  594. ;; This is an unsupported version from the future. There's nothing wrong
  595. ;; with that (the user may simply need to upgrade the 'guix' channel to
  596. ;; be able to read it), so silently ignore it.
  597. (channel-news '()))
  598. (#f
  599. (raise (condition
  600. (&message (message "syntactically invalid channel news file")))))
  601. (sexp
  602. (raise (condition
  603. (&message (message "invalid channel news file"))
  604. (&error-location
  605. (location (source-properties->location
  606. (source-properties sexp)))))))))
  607. (define (resolve-channel-news-entry-tag repository entry)
  608. "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
  609. ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
  610. the field its 'tag' refers to. A 'git-error' exception is raised if the tag
  611. cannot be found."
  612. (if (channel-news-entry-commit entry)
  613. entry
  614. (let* ((tag (channel-news-entry-tag entry))
  615. (reference (string-append "refs/tags/" tag))
  616. (oid (reference-name->oid repository reference)))
  617. (channel-news-entry (oid->string oid) tag
  618. (channel-news-entry-title entry)
  619. (channel-news-entry-body entry)))))
  620. (define* (channel-news-for-commit channel new #:optional old)
  621. "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
  622. NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
  623. (catch 'git-error
  624. (lambda ()
  625. (let* ((checkout (update-cached-checkout (channel-url channel)
  626. #:ref `(commit . ,new)))
  627. (metadata (read-channel-metadata-from-source checkout))
  628. (news-file (channel-metadata-news-file metadata))
  629. (news-file (and news-file
  630. (string-append checkout "/" news-file))))
  631. (if (and news-file (file-exists? news-file))
  632. (with-repository checkout repository
  633. (let* ((news (call-with-input-file news-file
  634. read-channel-news))
  635. (entries (map (lambda (entry)
  636. (resolve-channel-news-entry-tag repository
  637. entry))
  638. (channel-news-entries news))))
  639. (if old
  640. (let* ((new (commit-lookup repository (string->oid new)))
  641. (old (commit-lookup repository (string->oid old)))
  642. (commits (list->set
  643. (map (compose oid->string commit-id)
  644. (commit-difference new old)))))
  645. (filter (lambda (entry)
  646. (set-contains? commits
  647. (channel-news-entry-commit entry)))
  648. entries))
  649. entries)))
  650. '())))
  651. (lambda (key error . rest)
  652. ;; If commit NEW or commit OLD cannot be found, then something must be
  653. ;; wrong (for example, the history of CHANNEL was rewritten and these
  654. ;; commits no longer exist upstream), so quietly return the empty list.
  655. (if (= GIT_ENOTFOUND (git-error-code error))
  656. '()
  657. (apply throw key error rest)))))