channels.scm 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019, 2020 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 git-authenticate)
  24. #:use-module ((guix openpgp)
  25. #:select (openpgp-public-key-fingerprint
  26. openpgp-format-fingerprint))
  27. #:use-module (guix base16)
  28. #:use-module (guix records)
  29. #:use-module (guix gexp)
  30. #:use-module (guix modules)
  31. #:use-module (guix discovery)
  32. #:use-module (guix monads)
  33. #:use-module (guix profiles)
  34. #:use-module (guix packages)
  35. #:use-module (guix progress)
  36. #:use-module (guix derivations)
  37. #:use-module (guix combinators)
  38. #:use-module (guix diagnostics)
  39. #:use-module (guix sets)
  40. #:use-module (guix store)
  41. #:use-module (guix i18n)
  42. #:use-module (srfi srfi-1)
  43. #:use-module (srfi srfi-2)
  44. #:use-module (srfi srfi-9)
  45. #:use-module (srfi srfi-11)
  46. #:use-module (srfi srfi-26)
  47. #:use-module (srfi srfi-34)
  48. #:use-module (srfi srfi-35)
  49. #:autoload (guix self) (whole-package make-config.scm)
  50. #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
  51. #:autoload (guix quirks) (%quirks %patches applicable-patch? apply-patch)
  52. #:use-module (ice-9 format)
  53. #:use-module (ice-9 match)
  54. #:use-module (ice-9 vlist)
  55. #:use-module ((ice-9 rdelim) #:select (read-string))
  56. #:use-module ((rnrs bytevectors) #:select (bytevector=?))
  57. #:export (channel
  58. channel?
  59. channel-name
  60. channel-url
  61. channel-branch
  62. channel-commit
  63. channel-introduction
  64. channel-location
  65. channel-introduction?
  66. make-channel-introduction
  67. channel-introduction-first-signed-commit
  68. channel-introduction-first-commit-signer
  69. openpgp-fingerprint->bytevector
  70. openpgp-fingerprint
  71. %default-guix-channel
  72. %default-channels
  73. guix-channel?
  74. channel-instance?
  75. channel-instance-channel
  76. channel-instance-commit
  77. channel-instance-checkout
  78. authenticate-channel
  79. latest-channel-instances
  80. checkout->channel-instance
  81. latest-channel-derivation
  82. channel-instances->manifest
  83. %channel-profile-hooks
  84. channel-instances->derivation
  85. ensure-forward-channel-update
  86. profile-channels
  87. channel-news-entry?
  88. channel-news-entry-commit
  89. channel-news-entry-tag
  90. channel-news-entry-title
  91. channel-news-entry-body
  92. channel-news-for-commit))
  93. ;;; Commentary:
  94. ;;;
  95. ;;; This module implements "channels." A channel is usually a source of
  96. ;;; package definitions. There's a special channel, the 'guix' channel, that
  97. ;;; provides all of Guix, including its commands and its documentation.
  98. ;;; User-defined channels are expected to typically provide a bunch of .scm
  99. ;;; files meant to be added to the '%package-search-path'.
  100. ;;;
  101. ;;; This module provides tools to fetch and update channels from a Git
  102. ;;; repository and to build them.
  103. ;;;
  104. ;;; Code:
  105. (define-record-type* <channel> channel make-channel
  106. channel?
  107. (name channel-name)
  108. (url channel-url)
  109. (branch channel-branch (default "master"))
  110. (commit channel-commit (default #f))
  111. (introduction channel-introduction (default #f))
  112. (location channel-location
  113. (default (current-source-location)) (innate)))
  114. ;; Channel introductions. A "channel introduction" provides a commit/signer
  115. ;; pair that specifies the first commit of the authentication process as well
  116. ;; as its signer's fingerprint. Introductions are used to bootstrap trust in
  117. ;; a channel.
  118. (define-record-type <channel-introduction>
  119. (%make-channel-introduction first-signed-commit first-commit-signer)
  120. channel-introduction?
  121. (first-signed-commit channel-introduction-first-signed-commit) ;hex string
  122. (first-commit-signer channel-introduction-first-commit-signer)) ;bytevector
  123. (define (make-channel-introduction commit signer)
  124. "Return a new channel introduction: COMMIT is the introductory where
  125. authentication starts, and SIGNER is the OpenPGP fingerprint (a bytevector) of
  126. the signer of that commit."
  127. (%make-channel-introduction commit signer))
  128. (define (openpgp-fingerprint->bytevector str)
  129. "Convert STR, an OpenPGP fingerprint (hexadecimal string with whitespace),
  130. to the corresponding bytevector."
  131. (base16-string->bytevector
  132. (string-downcase (string-filter char-set:hex-digit str))))
  133. (define-syntax openpgp-fingerprint
  134. (lambda (s)
  135. "Convert STR, an OpenPGP fingerprint (hexadecimal string with whitespace),
  136. to the corresponding bytevector."
  137. (syntax-case s ()
  138. ((_ str)
  139. (string? (syntax->datum #'str))
  140. (openpgp-fingerprint->bytevector (syntax->datum #'str)))
  141. ((_ str)
  142. #'(openpgp-fingerprint->bytevector str)))))
  143. (define %guix-channel-introduction
  144. ;; Introduction of the official 'guix channel. The chosen commit is the
  145. ;; first one that introduces '.guix-authorizations' on the 'staging'
  146. ;; branch that was eventually merged in 'master'. Any branch starting
  147. ;; before that commit cannot be merged or it will be rejected by 'guix pull'
  148. ;; & co.
  149. (make-channel-introduction
  150. "9edb3f66fd807b096b48283debdcddccfea34bad" ;2020-05-26
  151. (openpgp-fingerprint ;mbakke
  152. "BBB0 2DDF 2CEA F6A8 0D1D E643 A2A0 6DF2 A33A 54FA")))
  153. (define %default-channel-url
  154. ;; URL of the default 'guix' channel.
  155. "https://git.savannah.gnu.org/git/guix.git")
  156. (define %default-guix-channel
  157. (channel
  158. (name 'guix)
  159. (branch "master")
  160. (url %default-channel-url)
  161. (introduction %guix-channel-introduction)))
  162. (define %default-channels
  163. ;; Default list of channels.
  164. (list %default-guix-channel))
  165. (define (guix-channel? channel)
  166. "Return true if CHANNEL is the 'guix' channel."
  167. (eq? 'guix (channel-name channel)))
  168. (define (ensure-default-introduction chan)
  169. "If CHAN represents the \"official\" 'guix' channel and lacks an
  170. introduction, add it."
  171. (if (and (guix-channel? chan)
  172. (not (channel-introduction chan))
  173. (string=? (channel-url chan) %default-channel-url))
  174. (channel (inherit chan)
  175. (introduction %guix-channel-introduction))
  176. chan))
  177. (define-record-type <channel-instance>
  178. (channel-instance channel commit checkout)
  179. channel-instance?
  180. (channel channel-instance-channel)
  181. (commit channel-instance-commit)
  182. (checkout channel-instance-checkout))
  183. (define-record-type <channel-metadata>
  184. (channel-metadata directory dependencies news-file keyring-reference url)
  185. channel-metadata?
  186. (directory channel-metadata-directory) ;string with leading slash
  187. (dependencies channel-metadata-dependencies) ;list of <channel>
  188. (news-file channel-metadata-news-file) ;string | #f
  189. (keyring-reference channel-metadata-keyring-reference) ;string
  190. (url channel-metadata-url)) ;string | #f
  191. (define %default-keyring-reference
  192. ;; Default value of the 'keyring-reference' field.
  193. "keyring")
  194. (define (channel-reference channel)
  195. "Return the \"reference\" for CHANNEL, an sexp suitable for
  196. 'latest-repository-commit'."
  197. (match (channel-commit channel)
  198. (#f `(branch . ,(channel-branch channel)))
  199. (commit `(commit . ,(channel-commit channel)))))
  200. (define sexp->channel-introduction
  201. (match-lambda
  202. (('channel-introduction ('version 0)
  203. ('commit commit) ('signer signer)
  204. _ ...)
  205. (make-channel-introduction commit (openpgp-fingerprint signer)))
  206. (x #f)))
  207. (define (read-channel-metadata port)
  208. "Read from PORT channel metadata in the format expected for the
  209. '.guix-channel' file. Return a <channel-metadata> record, or raise an error
  210. if valid metadata could not be read from PORT."
  211. (match (read port)
  212. (('channel ('version 0) properties ...)
  213. (let ((directory (and=> (assoc-ref properties 'directory) first))
  214. (dependencies (or (assoc-ref properties 'dependencies) '()))
  215. (news-file (and=> (assoc-ref properties 'news-file) first))
  216. (url (and=> (assoc-ref properties 'url) first))
  217. (keyring-reference
  218. (or (and=> (assoc-ref properties 'keyring-reference) first)
  219. %default-keyring-reference)))
  220. (channel-metadata
  221. (cond ((not directory) "/") ;directory
  222. ((string-prefix? "/" directory) directory)
  223. (else (string-append "/" directory)))
  224. (map (lambda (item) ;dependencies
  225. (let ((get (lambda* (key #:optional default)
  226. (or (and=> (assoc-ref item key) first) default))))
  227. (and-let* ((name (get 'name))
  228. (url (get 'url))
  229. (branch (get 'branch "master")))
  230. (channel
  231. (name name)
  232. (branch branch)
  233. (url url)
  234. (commit (get 'commit))
  235. (introduction (and=> (get 'introduction)
  236. sexp->channel-introduction))))))
  237. dependencies)
  238. news-file
  239. keyring-reference
  240. url)))
  241. ((and ('channel ('version version) _ ...) sexp)
  242. (raise (condition
  243. (&message (message "unsupported '.guix-channel' version"))
  244. (&error-location
  245. (location (source-properties->location
  246. (source-properties sexp)))))))
  247. (sexp
  248. (raise (condition
  249. (&message (message "invalid '.guix-channel' file"))
  250. (&error-location
  251. (location (source-properties->location
  252. (source-properties sexp)))))))))
  253. (define (read-channel-metadata-from-source source)
  254. "Return a channel-metadata record read from channel's SOURCE/.guix-channel
  255. description file, or return the default channel-metadata record if that file
  256. doesn't exist."
  257. (catch 'system-error
  258. (lambda ()
  259. (call-with-input-file (string-append source "/.guix-channel")
  260. read-channel-metadata))
  261. (lambda args
  262. (if (= ENOENT (system-error-errno args))
  263. (channel-metadata "/" '() #f %default-keyring-reference #f)
  264. (apply throw args)))))
  265. (define (channel-instance-metadata instance)
  266. "Return a channel-metadata record read from the channel INSTANCE's
  267. description file or its default value."
  268. (read-channel-metadata-from-source (channel-instance-checkout instance)))
  269. (define (channel-instance-dependencies instance)
  270. "Return the list of channels that are declared as dependencies for the given
  271. channel INSTANCE."
  272. (channel-metadata-dependencies (channel-instance-metadata instance)))
  273. (define (apply-patches checkout commit patches)
  274. "Apply the matching PATCHES to CHECKOUT, modifying files in place. The
  275. result is unspecified."
  276. (let loop ((patches patches))
  277. (match patches
  278. (() #t)
  279. ((patch rest ...)
  280. (when (applicable-patch? patch checkout commit)
  281. (apply-patch patch checkout))
  282. (loop rest)))))
  283. (define commit-short-id
  284. (compose (cut string-take <> 7) oid->string commit-id))
  285. (define* (authenticate-channel channel checkout commit
  286. #:key (keyring-reference-prefix "origin/"))
  287. "Authenticate the given COMMIT of CHANNEL, available at CHECKOUT, a
  288. directory containing a CHANNEL checkout. Raise an error if authentication
  289. fails."
  290. (define intro
  291. (channel-introduction channel))
  292. (define cache-key
  293. (string-append "channels/" (symbol->string (channel-name channel))))
  294. (define keyring-reference
  295. (channel-metadata-keyring-reference
  296. (read-channel-metadata-from-source checkout)))
  297. (define (make-reporter start-commit end-commit commits)
  298. (format (current-error-port)
  299. (G_ "Authenticating channel '~a', commits ~a to ~a (~h new \
  300. commits)...~%")
  301. (channel-name channel)
  302. (commit-short-id start-commit)
  303. (commit-short-id end-commit)
  304. (length commits))
  305. (progress-reporter/bar (length commits)))
  306. ;; XXX: Too bad we need to re-open CHECKOUT.
  307. (with-repository checkout repository
  308. (authenticate-repository repository
  309. (string->oid
  310. (channel-introduction-first-signed-commit intro))
  311. (channel-introduction-first-commit-signer intro)
  312. #:end (string->oid commit)
  313. #:keyring-reference
  314. (string-append keyring-reference-prefix
  315. keyring-reference)
  316. #:make-reporter make-reporter
  317. #:cache-key cache-key)))
  318. (define* (latest-channel-instance store channel
  319. #:key (patches %patches)
  320. starting-commit
  321. (authenticate? #f)
  322. (validate-pull
  323. ensure-forward-channel-update))
  324. "Return the latest channel instance for CHANNEL. When STARTING-COMMIT is
  325. true, call VALIDATE-PULL with CHANNEL, STARTING-COMMIT, the target commit, and
  326. their relation. When AUTHENTICATE? is false, CHANNEL is not authenticated."
  327. (define (dot-git? file stat)
  328. (and (string=? (basename file) ".git")
  329. (eq? 'directory (stat:type stat))))
  330. (let-values (((channel)
  331. (ensure-default-introduction channel))
  332. ((checkout commit relation)
  333. (update-cached-checkout (channel-url channel)
  334. #:ref (channel-reference channel)
  335. #:starting-commit starting-commit)))
  336. (when relation
  337. (validate-pull channel starting-commit commit relation))
  338. (if authenticate?
  339. (if (channel-introduction channel)
  340. (authenticate-channel channel checkout commit)
  341. ;; TODO: Warn for all the channels once the authentication interface
  342. ;; is public.
  343. (when (guix-channel? channel)
  344. (raise (make-compound-condition
  345. (formatted-message (G_ "channel '~a' lacks an \
  346. introduction and cannot be authenticated~%")
  347. (channel-name channel))
  348. (condition
  349. (&fix-hint
  350. (hint (G_ "Add the missing introduction to your
  351. channels file to address the issue. Alternatively, you can pass
  352. @option{--disable-authentication}, at the risk of running unauthenticated and
  353. thus potentially malicious code."))))))))
  354. (warning (G_ "channel authentication disabled~%")))
  355. (when (guix-channel? channel)
  356. ;; Apply the relevant subset of PATCHES directly in CHECKOUT. This is
  357. ;; safe to do because 'switch-to-ref' eventually does a hard reset.
  358. (apply-patches checkout commit patches))
  359. (let* ((name (url+commit->name (channel-url channel) commit))
  360. (checkout (add-to-store store name #t "sha256" checkout
  361. #:select? (negate dot-git?))))
  362. (channel-instance channel commit checkout))))
  363. (define (ensure-forward-channel-update channel start commit relation)
  364. "Raise an error if RELATION is not 'ancestor, meaning that START is not an
  365. ancestor of COMMIT, unless CHANNEL specifies a commit.
  366. This procedure implements a channel update policy meant to be used as a
  367. #:validate-pull argument."
  368. (match relation
  369. ('ancestor #t)
  370. ('self #t)
  371. (_
  372. (raise (make-compound-condition
  373. (condition
  374. (&message (message
  375. (format #f (G_ "\
  376. aborting update of channel '~a' to commit ~a, which is not a descendant of ~a")
  377. (channel-name channel)
  378. commit start))))
  379. ;; If the user asked for a specific commit, they might want
  380. ;; that to happen nevertheless, so tell them about the
  381. ;; relevant 'guix pull' option.
  382. (if (channel-commit channel)
  383. (condition
  384. (&fix-hint
  385. (hint (G_ "Use @option{--allow-downgrades} to force
  386. this downgrade."))))
  387. (condition
  388. (&fix-hint
  389. (hint (G_ "This could indicate that the channel has
  390. been tampered with and is trying to force a roll-back, preventing you from
  391. getting the latest updates. If you think this is not the case, explicitly
  392. allow non-forward updates."))))))))))
  393. (define (channel-instance-primary-url instance)
  394. "Return the primary URL advertised for INSTANCE, or #f if there is no such
  395. information."
  396. (channel-metadata-url (channel-instance-metadata instance)))
  397. (define* (latest-channel-instances store channels
  398. #:key
  399. (current-channels '())
  400. (authenticate? #t)
  401. (validate-pull
  402. ensure-forward-channel-update))
  403. "Return a list of channel instances corresponding to the latest checkouts of
  404. CHANNELS and the channels on which they depend.
  405. When AUTHENTICATE? is true, authenticate the subset of CHANNELS that has a
  406. \"channel introduction\".
  407. CURRENT-CHANNELS is the list of currently used channels. It is compared
  408. against the newly-fetched instances of CHANNELS, and VALIDATE-PULL is called
  409. for each channel update and can choose to emit warnings or raise an error,
  410. depending on the policy it implements."
  411. ;; Only process channels that are unique, or that are more specific than a
  412. ;; previous channel specification.
  413. (define (ignore? channel others)
  414. (member channel others
  415. (lambda (a b)
  416. (and (eq? (channel-name a) (channel-name b))
  417. (or (channel-commit b)
  418. (not (or (channel-commit a)
  419. (channel-commit b))))))))
  420. (define (current-commit name)
  421. ;; Return the current commit for channel NAME.
  422. (any (lambda (channel)
  423. (and (eq? (channel-name channel) name)
  424. (channel-commit channel)))
  425. current-channels))
  426. (let loop ((channels channels)
  427. (previous-channels '()))
  428. ;; Accumulate a list of instances. A list of processed channels is also
  429. ;; accumulated to decide on duplicate channel specifications.
  430. (define-values (resulting-channels instances)
  431. (fold2 (lambda (channel previous-channels instances)
  432. (if (ignore? channel previous-channels)
  433. (values previous-channels instances)
  434. (begin
  435. (format (current-error-port)
  436. (G_ "Updating channel '~a' from Git repository at '~a'...~%")
  437. (channel-name channel)
  438. (channel-url channel))
  439. (let* ((current (current-commit (channel-name channel)))
  440. (instance
  441. (latest-channel-instance store channel
  442. #:authenticate?
  443. authenticate?
  444. #:validate-pull
  445. validate-pull
  446. #:starting-commit
  447. current)))
  448. (when authenticate?
  449. ;; CHANNEL is authenticated so we can trust the
  450. ;; primary URL advertised in its metadata and warn
  451. ;; about possibly stale mirrors.
  452. (let ((primary-url (channel-instance-primary-url
  453. instance)))
  454. (unless (or (not primary-url)
  455. (channel-commit channel)
  456. (string=? primary-url (channel-url channel)))
  457. (warning (G_ "pulled channel '~a' from a mirror \
  458. of ~a, which might be stale~%")
  459. (channel-name channel)
  460. primary-url))))
  461. (let-values (((new-instances new-channels)
  462. (loop (channel-instance-dependencies instance)
  463. previous-channels)))
  464. (values (append (cons channel new-channels)
  465. previous-channels)
  466. (append (cons instance new-instances)
  467. instances)))))))
  468. previous-channels
  469. '() ;instances
  470. channels))
  471. (let ((instance-name (compose channel-name channel-instance-channel)))
  472. ;; Remove all earlier channel specifications if they are followed by a
  473. ;; more specific one.
  474. (values (delete-duplicates instances
  475. (lambda (a b)
  476. (eq? (instance-name a) (instance-name b))))
  477. resulting-channels))))
  478. (define* (checkout->channel-instance checkout
  479. #:key commit
  480. (url checkout) (name 'guix))
  481. "Return a channel instance for CHECKOUT, which is assumed to be a checkout
  482. of COMMIT at URL. Use NAME as the channel name."
  483. (let* ((commit (or commit (make-string 40 #\0)))
  484. (channel (channel (name name)
  485. (commit commit)
  486. (url url))))
  487. (channel-instance channel commit checkout)))
  488. (define %self-build-file
  489. ;; The file containing code to build Guix. This serves the same purpose as
  490. ;; a makefile, and, similarly, is intended to always keep this name.
  491. "build-aux/build-self.scm")
  492. (define %pull-version
  493. ;; This is the version of the 'guix pull' protocol. It specifies what's
  494. ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
  495. ;; place a set of compiled Guile modules in ~/.config/guix/latest.
  496. 1)
  497. (define (standard-module-derivation name source core dependencies)
  498. "Return a derivation that builds with CORE, a Guix instance, the Scheme
  499. modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
  500. objects. The assumption is that SOURCE contains package modules to be added
  501. to '%package-module-path'."
  502. (let* ((metadata (read-channel-metadata-from-source source))
  503. (directory (channel-metadata-directory metadata)))
  504. (define build
  505. ;; This is code that we'll run in CORE, a Guix instance, with its own
  506. ;; modules and so on. That way, we make sure these modules are built for
  507. ;; the right Guile version, with the right dependencies, and that they get
  508. ;; to see the right (gnu packages …) modules.
  509. (with-extensions dependencies
  510. #~(begin
  511. (use-modules (guix build compile)
  512. (guix build utils)
  513. (srfi srfi-26))
  514. (define go
  515. (string-append #$output "/lib/guile/" (effective-version)
  516. "/site-ccache"))
  517. (define scm
  518. (string-append #$output "/share/guile/site/"
  519. (effective-version)))
  520. (let* ((subdir #$directory)
  521. (source (string-append #$source subdir)))
  522. (compile-files source go (find-files source "\\.scm$"))
  523. (mkdir-p (dirname scm))
  524. (symlink (string-append #$source subdir) scm))
  525. scm)))
  526. (gexp->derivation-in-inferior name build core)))
  527. (define* (guile-for-source source #:optional (quirks %quirks))
  528. "Return the Guile package to use when building SOURCE or #f if the default
  529. '%guile-for-build' should be good enough."
  530. (let loop ((quirks quirks))
  531. (match quirks
  532. (()
  533. #f)
  534. (((predicate . guile) rest ...)
  535. (if (predicate source) (guile) (loop rest))))))
  536. (define (call-with-guile guile thunk)
  537. (lambda (store)
  538. (values (parameterize ((%guile-for-build
  539. (if guile
  540. (package-derivation store guile)
  541. (%guile-for-build))))
  542. (run-with-store store (thunk)))
  543. store)))
  544. (define-syntax-rule (with-guile guile exp ...)
  545. "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
  546. EXP, a series of monadic expressions."
  547. (call-with-guile guile (lambda ()
  548. (mbegin %store-monad exp ...))))
  549. (define (with-trivial-build-handler mvalue)
  550. "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
  551. that unconditionally resumes the continuation."
  552. (lambda (store)
  553. (with-build-handler (lambda (continue . _)
  554. (continue #t))
  555. (values (run-with-store store mvalue)
  556. store))))
  557. (define* (build-from-source name source
  558. #:key core verbose? commit
  559. (dependencies '()))
  560. "Return a derivation to build Guix from SOURCE, using the self-build script
  561. contained therein; use COMMIT as the version string. When CORE is true, build
  562. package modules under SOURCE using CORE, an instance of Guix."
  563. ;; Running the self-build script makes it easier to update the build
  564. ;; procedure: the self-build script of the Guix-to-be-installed contains the
  565. ;; right dependencies, build procedure, etc., which the Guix-in-use may not
  566. ;; be know.
  567. (define script
  568. (string-append source "/" %self-build-file))
  569. (if (file-exists? script)
  570. (let ((build (save-module-excursion
  571. (lambda ()
  572. ;; Disable deprecation warnings; it's OK for SCRIPT to
  573. ;; use deprecated APIs and the user doesn't have to know
  574. ;; about it.
  575. (parameterize ((guix-warning-port
  576. (%make-void-port "w")))
  577. (primitive-load script)))))
  578. (guile (guile-for-source source)))
  579. ;; BUILD must be a monadic procedure of at least one argument: the
  580. ;; source tree.
  581. ;;
  582. ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
  583. ;; the future we'll fall back to a previous version of the protocol
  584. ;; when that happens.
  585. (with-guile guile
  586. ;; BUILD is usually quite costly. Install a "trivial" build handler
  587. ;; so we don't bounce an outer build-accumulator handler that could
  588. ;; cause us to redo half of the BUILD computation several times just
  589. ;; to realize it gives the same result.
  590. (with-trivial-build-handler
  591. (build source #:verbose? verbose? #:version commit
  592. #:pull-version %pull-version))))
  593. ;; Build a set of modules that extend Guix using the standard method.
  594. (standard-module-derivation name source core dependencies)))
  595. (define* (build-channel-instance instance
  596. #:optional core (dependencies '()))
  597. "Return, as a monadic value, the derivation for INSTANCE, a channel
  598. instance. DEPENDENCIES is a list of extensions providing Guile modules that
  599. INSTANCE depends on."
  600. (build-from-source (symbol->string
  601. (channel-name (channel-instance-channel instance)))
  602. (channel-instance-checkout instance)
  603. #:commit (channel-instance-commit instance)
  604. #:core core
  605. #:dependencies dependencies))
  606. (define (resolve-dependencies instances)
  607. "Return a procedure that, given one of the elements of INSTANCES, returns
  608. list of instances it depends on."
  609. (define channel-instance-name
  610. (compose channel-name channel-instance-channel))
  611. (define table ;map a name to an instance
  612. (fold (lambda (instance table)
  613. (vhash-consq (channel-instance-name instance)
  614. instance table))
  615. vlist-null
  616. instances))
  617. (define edges
  618. (fold (lambda (instance edges)
  619. (fold (lambda (channel edges)
  620. (let ((name (channel-name channel)))
  621. (match (vhash-assq name table)
  622. ((_ . target)
  623. (vhash-consq instance target edges)))))
  624. edges
  625. (channel-instance-dependencies instance)))
  626. vlist-null
  627. instances))
  628. (lambda (instance)
  629. (vhash-foldq* cons '() instance edges)))
  630. (define (channel-instance-derivations instances)
  631. "Return the list of derivations to build INSTANCES, in the same order as
  632. INSTANCES."
  633. (define core-instance
  634. ;; The 'guix' channel is treated specially: it's an implicit dependency of
  635. ;; all the other channels.
  636. (find (lambda (instance)
  637. (guix-channel? (channel-instance-channel instance)))
  638. instances))
  639. (define edges
  640. (resolve-dependencies instances))
  641. (define (instance->derivation instance)
  642. (mlet %store-monad ((system (current-system)))
  643. (mcached (if (eq? instance core-instance)
  644. (build-channel-instance instance)
  645. (mlet %store-monad ((core (instance->derivation core-instance))
  646. (deps (mapm %store-monad instance->derivation
  647. (edges instance))))
  648. (build-channel-instance instance core deps)))
  649. instance
  650. system)))
  651. (unless core-instance
  652. (let ((loc (and=> (any (compose channel-location channel-instance-channel)
  653. instances)
  654. source-properties->location)))
  655. (raise (apply make-compound-condition
  656. (condition
  657. (&message (message "'guix' channel is lacking")))
  658. (condition
  659. (&fix-hint (hint (G_ "Make sure your list of channels
  660. contains one channel named @code{guix} providing the core of Guix."))))
  661. (if loc
  662. (list (condition (&error-location (location loc))))
  663. '())))))
  664. (mapm %store-monad instance->derivation instances))
  665. (define (whole-package-for-legacy name modules)
  666. "Return a full-blown Guix package for MODULES, a derivation that builds Guix
  667. modules in the old ~/.config/guix/latest style."
  668. (define packages
  669. (resolve-interface '(gnu packages guile)))
  670. (define modules+compiled
  671. ;; Since MODULES contains both .scm and .go files at its root, re-bundle
  672. ;; it so that it has share/guile/site and lib/guile, which is what
  673. ;; 'whole-package' expects.
  674. (computed-file (derivation-name modules)
  675. (with-imported-modules '((guix build utils))
  676. #~(begin
  677. (use-modules (guix build utils))
  678. (define version
  679. (effective-version))
  680. (define share
  681. (string-append #$output "/share/guile/site"))
  682. (define lib
  683. (string-append #$output "/lib/guile/" version))
  684. (mkdir-p share) (mkdir-p lib)
  685. (symlink #$modules (string-append share "/" version))
  686. (symlink #$modules (string-append lib "/site-ccache"))))))
  687. (letrec-syntax ((list (syntax-rules (->)
  688. ((_)
  689. '())
  690. ((_ (module -> variable) rest ...)
  691. (cons (module-ref (resolve-interface
  692. '(gnu packages module))
  693. 'variable)
  694. (list rest ...)))
  695. ((_ variable rest ...)
  696. (cons (module-ref packages 'variable)
  697. (list rest ...))))))
  698. (whole-package name modules+compiled
  699. ;; In the "old style", %SELF-BUILD-FILE would simply return a
  700. ;; derivation that builds modules. We have to infer what the
  701. ;; dependencies of these modules were.
  702. (list guile-json-3 guile-git guile-bytestructures
  703. (ssh -> guile-ssh) (tls -> gnutls))
  704. #:guile (default-guile))))
  705. (define (old-style-guix? drv)
  706. "Return true if DRV corresponds to a ~/.config/guix/latest style of
  707. derivation."
  708. ;; Here we rely on a gross historical fact: that derivations produced by the
  709. ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
  710. ;; dated May 30, 2018) did not depend on "guix-command.drv".
  711. (not (find (lambda (input)
  712. (string=? "guix-command"
  713. (derivation-name
  714. (derivation-input-derivation input))))
  715. (derivation-inputs drv))))
  716. (define (channel-instances->manifest instances)
  717. "Return a profile manifest with entries for all of INSTANCES, a list of
  718. channel instances."
  719. (define (instance->entry instance drv)
  720. (let* ((commit (channel-instance-commit instance))
  721. (channel (channel-instance-channel instance))
  722. (intro (channel-introduction channel)))
  723. (manifest-entry
  724. (name (symbol->string (channel-name channel)))
  725. (version (string-take commit 7))
  726. (item (if (guix-channel? channel)
  727. (if (old-style-guix? drv)
  728. (whole-package-for-legacy (string-append name "-" version)
  729. drv)
  730. drv)
  731. drv))
  732. (properties
  733. `((source (repository
  734. (version 0)
  735. (url ,(channel-url channel))
  736. (branch ,(channel-branch channel))
  737. (commit ,commit)
  738. ,@(if intro
  739. `((introduction
  740. (channel-introduction
  741. (version 0)
  742. (commit
  743. ,(channel-introduction-first-signed-commit
  744. intro))
  745. (signer
  746. ,(openpgp-format-fingerprint
  747. (channel-introduction-first-commit-signer
  748. intro))))))
  749. '()))))))))
  750. (mlet* %store-monad ((derivations (channel-instance-derivations instances))
  751. (entries -> (map instance->entry instances derivations)))
  752. (return (manifest entries))))
  753. (define (package-cache-file manifest)
  754. "Build a package cache file for the instance in MANIFEST. This is meant to
  755. be used as a profile hook."
  756. (let ((profile (profile (content manifest) (hooks '()))))
  757. (define build
  758. #~(begin
  759. (use-modules (gnu packages))
  760. (if (defined? 'generate-package-cache)
  761. (begin
  762. ;; Delegate package cache generation to the inferior.
  763. (format (current-error-port)
  764. "Generating package cache for '~a'...~%"
  765. #$profile)
  766. (generate-package-cache #$output))
  767. (mkdir #$output))))
  768. (gexp->derivation-in-inferior "guix-package-cache" build
  769. profile
  770. ;; If the Guix in PROFILE is too old and
  771. ;; lacks 'guix repl', don't build the cache
  772. ;; instead of failing.
  773. #:silent-failure? #t
  774. #:properties '((type . profile-hook)
  775. (hook . package-cache))
  776. #:local-build? #t)))
  777. (define %channel-profile-hooks
  778. ;; The default channel profile hooks.
  779. (cons package-cache-file %default-profile-hooks))
  780. (define (channel-instances->derivation instances)
  781. "Return the derivation of the profile containing INSTANCES, a list of
  782. channel instances."
  783. (mlet %store-monad ((manifest (channel-instances->manifest instances)))
  784. (profile-derivation manifest
  785. #:hooks %channel-profile-hooks)))
  786. (define latest-channel-instances*
  787. (store-lift latest-channel-instances))
  788. (define* (latest-channel-derivation #:optional (channels %default-channels)
  789. #:key
  790. (current-channels '())
  791. (validate-pull
  792. ensure-forward-channel-update))
  793. "Return as a monadic value the derivation that builds the profile for the
  794. latest instances of CHANNELS. CURRENT-CHANNELS and VALIDATE-PULL are passed
  795. to 'latest-channel-instances'."
  796. (mlet %store-monad ((instances
  797. (latest-channel-instances* channels
  798. #:current-channels
  799. current-channels
  800. #:validate-pull
  801. validate-pull)))
  802. (channel-instances->derivation instances)))
  803. (define (profile-channels profile)
  804. "Return the list of channels corresponding to entries in PROFILE. If
  805. PROFILE is not a profile created by 'guix pull', return the empty list."
  806. (filter-map (lambda (entry)
  807. (match (assq 'source (manifest-entry-properties entry))
  808. (('source ('repository ('version 0)
  809. ('url url)
  810. ('branch branch)
  811. ('commit commit)
  812. rest ...))
  813. (channel (name (string->symbol
  814. (manifest-entry-name entry)))
  815. (url url)
  816. (commit commit)
  817. (introduction
  818. (match (assq 'introduction rest)
  819. (#f #f)
  820. (('introduction intro)
  821. (sexp->channel-introduction intro))))))
  822. ;; No channel information for this manifest entry.
  823. ;; XXX: Pre-0.15.0 Guix did not provide that information,
  824. ;; but there's not much we can do in that case.
  825. (_ #f)))
  826. ;; Show most recently installed packages last.
  827. (reverse
  828. (manifest-entries (profile-manifest profile)))))
  829. ;;;
  830. ;;; News.
  831. ;;;
  832. ;; Channel news.
  833. (define-record-type <channel-news>
  834. (channel-news entries)
  835. channel-news?
  836. (entries channel-news-entries)) ;list of <channel-news-entry>
  837. ;; News entry, associated with a specific commit of the channel.
  838. (define-record-type <channel-news-entry>
  839. (channel-news-entry commit tag title body)
  840. channel-news-entry?
  841. (commit channel-news-entry-commit) ;hex string | #f
  842. (tag channel-news-entry-tag) ;#f | string
  843. (title channel-news-entry-title) ;list of language tag/string pairs
  844. (body channel-news-entry-body)) ;list of language tag/string pairs
  845. (define (sexp->channel-news-entry entry)
  846. "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
  847. (define (pair language message)
  848. (cons (symbol->string language) message))
  849. (match entry
  850. (('entry ((and (or 'commit 'tag) type) commit-or-tag)
  851. ('title ((? symbol? title-tags) (? string? titles)) ...)
  852. ('body ((? symbol? body-tags) (? string? bodies)) ...)
  853. _ ...)
  854. (channel-news-entry (and (eq? type 'commit) commit-or-tag)
  855. (and (eq? type 'tag) commit-or-tag)
  856. (map pair title-tags titles)
  857. (map pair body-tags bodies)))
  858. (_
  859. (raise (condition
  860. (&message (message "invalid channel news entry"))
  861. (&error-location
  862. (location (source-properties->location
  863. (source-properties entry)))))))))
  864. (define (read-channel-news port)
  865. "Read a channel news feed from PORT and return it as a <channel-news>
  866. record."
  867. (match (false-if-exception (read port))
  868. (('channel-news ('version 0) entries ...)
  869. (channel-news (map sexp->channel-news-entry entries)))
  870. (('channel-news ('version version) _ ...)
  871. ;; This is an unsupported version from the future. There's nothing wrong
  872. ;; with that (the user may simply need to upgrade the 'guix' channel to
  873. ;; be able to read it), so silently ignore it.
  874. (channel-news '()))
  875. (#f
  876. (raise (condition
  877. (&message (message "syntactically invalid channel news file")))))
  878. (sexp
  879. (raise (condition
  880. (&message (message "invalid channel news file"))
  881. (&error-location
  882. (location (source-properties->location
  883. (source-properties sexp)))))))))
  884. (define (resolve-channel-news-entry-tag repository entry)
  885. "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
  886. ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
  887. the field its 'tag' refers to. A 'git-error' exception is raised if the tag
  888. cannot be found."
  889. (if (channel-news-entry-commit entry)
  890. entry
  891. (let* ((tag (channel-news-entry-tag entry))
  892. (reference (string-append "refs/tags/" tag))
  893. (oid (reference-name->oid repository reference)))
  894. (channel-news-entry (oid->string oid) tag
  895. (channel-news-entry-title entry)
  896. (channel-news-entry-body entry)))))
  897. (define* (channel-news-for-commit channel new #:optional old)
  898. "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
  899. NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
  900. (catch 'git-error
  901. (lambda ()
  902. (let* ((checkout (update-cached-checkout (channel-url channel)
  903. #:ref `(commit . ,new)))
  904. (metadata (read-channel-metadata-from-source checkout))
  905. (news-file (channel-metadata-news-file metadata))
  906. (news-file (and news-file
  907. (string-append checkout "/" news-file))))
  908. (if (and news-file (file-exists? news-file))
  909. (with-repository checkout repository
  910. (let* ((news (call-with-input-file news-file
  911. read-channel-news))
  912. (entries (map (lambda (entry)
  913. (resolve-channel-news-entry-tag repository
  914. entry))
  915. (channel-news-entries news))))
  916. (if old
  917. (let* ((new (commit-lookup repository (string->oid new)))
  918. (old (commit-lookup repository (string->oid old)))
  919. (commits (list->set
  920. (map (compose oid->string commit-id)
  921. (commit-difference new old)))))
  922. (filter (lambda (entry)
  923. (set-contains? commits
  924. (channel-news-entry-commit entry)))
  925. entries))
  926. entries)))
  927. '())))
  928. (lambda (key error . rest)
  929. ;; If commit NEW or commit OLD cannot be found, then something must be
  930. ;; wrong (for example, the history of CHANNEL was rewritten and these
  931. ;; commits no longer exist upstream), so quietly return the empty list.
  932. (if (= GIT_ENOTFOUND (git-error-code error))
  933. '()
  934. (apply throw key error rest)))))
  935. ;;; Local Variables:
  936. ;;; eval: (put 'with-guile 'scheme-indent-function 1)
  937. ;;; End: