channels.scm 48 KB

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