channels.scm 50 KB

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