channels.scm 48 KB

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