channels.scm 47 KB

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