channels.scm 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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 '()))
  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."
  592. (define name
  593. (symbol->string
  594. (channel-name (channel-instance-channel instance))))
  595. (define source
  596. (channel-instance-checkout instance))
  597. (define commit
  598. (channel-instance-commit instance))
  599. ;; Running the self-build script makes it easier to update the build
  600. ;; procedure: the self-build script of the Guix-to-be-installed contains the
  601. ;; right dependencies, build procedure, etc., which the Guix-in-use may not
  602. ;; know.
  603. (define script
  604. (string-append source "/" %self-build-file))
  605. (if (file-exists? script)
  606. (let ((build (save-module-excursion
  607. (lambda ()
  608. ;; Disable deprecation warnings; it's OK for SCRIPT to
  609. ;; use deprecated APIs and the user doesn't have to know
  610. ;; about it.
  611. (parameterize ((guix-warning-port
  612. (%make-void-port "w")))
  613. (primitive-load script)))))
  614. (guile (guile-for-source source)))
  615. ;; BUILD must be a monadic procedure of at least one argument: the
  616. ;; source tree.
  617. ;;
  618. ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
  619. ;; the future we'll fall back to a previous version of the protocol
  620. ;; when that happens.
  621. (with-guile guile
  622. ;; BUILD is usually quite costly. Install a "trivial" build handler
  623. ;; so we don't bounce an outer build-accumulator handler that could
  624. ;; cause us to redo half of the BUILD computation several times just
  625. ;; to realize it gives the same result.
  626. (with-trivial-build-handler
  627. (build source
  628. #:verbose? verbose? #:version commit
  629. #:channel-metadata (channel-instance->sexp instance)
  630. #:pull-version %pull-version))))
  631. ;; Build a set of modules that extend Guix using the standard method.
  632. (standard-module-derivation name source core dependencies)))
  633. (define* (build-channel-instance instance
  634. #:optional core (dependencies '()))
  635. "Return, as a monadic value, the derivation for INSTANCE, a channel
  636. instance. DEPENDENCIES is a list of extensions providing Guile modules that
  637. INSTANCE depends on."
  638. (build-from-source instance
  639. #:core core
  640. #:dependencies dependencies))
  641. (define (resolve-dependencies instances)
  642. "Return a procedure that, given one of the elements of INSTANCES, returns
  643. list of instances it depends on."
  644. (define channel-instance-name
  645. (compose channel-name channel-instance-channel))
  646. (define table ;map a name to an instance
  647. (fold (lambda (instance table)
  648. (vhash-consq (channel-instance-name instance)
  649. instance table))
  650. vlist-null
  651. instances))
  652. (define edges
  653. (fold (lambda (instance edges)
  654. (fold (lambda (channel edges)
  655. (let ((name (channel-name channel)))
  656. (match (vhash-assq name table)
  657. ((_ . target)
  658. (vhash-consq instance target edges)))))
  659. edges
  660. (channel-instance-dependencies instance)))
  661. vlist-null
  662. instances))
  663. (lambda (instance)
  664. (vhash-foldq* cons '() instance edges)))
  665. (define (channel-instance-derivations instances)
  666. "Return the list of derivations to build INSTANCES, in the same order as
  667. INSTANCES."
  668. (define core-instance
  669. ;; The 'guix' channel is treated specially: it's an implicit dependency of
  670. ;; all the other channels.
  671. (find (lambda (instance)
  672. (guix-channel? (channel-instance-channel instance)))
  673. instances))
  674. (define edges
  675. (resolve-dependencies instances))
  676. (define (instance->derivation instance)
  677. (mlet %store-monad ((system (current-system)))
  678. (mcached (if (eq? instance core-instance)
  679. (build-channel-instance instance)
  680. (mlet %store-monad ((core (instance->derivation core-instance))
  681. (deps (mapm %store-monad instance->derivation
  682. (edges instance))))
  683. (build-channel-instance instance core deps)))
  684. instance
  685. system)))
  686. (unless core-instance
  687. (let ((loc (and=> (any (compose channel-location channel-instance-channel)
  688. instances)
  689. source-properties->location)))
  690. (raise (apply make-compound-condition
  691. (condition
  692. (&message (message "'guix' channel is lacking")))
  693. (condition
  694. (&fix-hint (hint (G_ "Make sure your list of channels
  695. contains one channel named @code{guix} providing the core of Guix."))))
  696. (if loc
  697. (list (condition (&error-location (location loc))))
  698. '())))))
  699. (mapm %store-monad instance->derivation instances))
  700. (define (whole-package-for-legacy name modules)
  701. "Return a full-blown Guix package for MODULES, a derivation that builds Guix
  702. modules in the old ~/.config/guix/latest style."
  703. (define packages
  704. (resolve-interface '(gnu packages guile)))
  705. (define modules+compiled
  706. ;; Since MODULES contains both .scm and .go files at its root, re-bundle
  707. ;; it so that it has share/guile/site and lib/guile, which is what
  708. ;; 'whole-package' expects.
  709. (computed-file (derivation-name modules)
  710. (with-imported-modules '((guix build utils))
  711. #~(begin
  712. (use-modules (guix build utils))
  713. (define version
  714. (effective-version))
  715. (define share
  716. (string-append #$output "/share/guile/site"))
  717. (define lib
  718. (string-append #$output "/lib/guile/" version))
  719. (mkdir-p share) (mkdir-p lib)
  720. (symlink #$modules (string-append share "/" version))
  721. (symlink #$modules (string-append lib "/site-ccache"))))))
  722. (letrec-syntax ((list (syntax-rules (->)
  723. ((_)
  724. '())
  725. ((_ (module -> variable) rest ...)
  726. (cons (module-ref (resolve-interface
  727. '(gnu packages module))
  728. 'variable)
  729. (list rest ...)))
  730. ((_ variable rest ...)
  731. (cons (module-ref packages 'variable)
  732. (list rest ...))))))
  733. (whole-package name modules+compiled
  734. ;; In the "old style", %SELF-BUILD-FILE would simply return a
  735. ;; derivation that builds modules. We have to infer what the
  736. ;; dependencies of these modules were.
  737. (list guile-json-3 guile-git guile-bytestructures
  738. (ssh -> guile-ssh) (tls -> gnutls))
  739. #:guile (default-guile))))
  740. (define (old-style-guix? drv)
  741. "Return true if DRV corresponds to a ~/.config/guix/latest style of
  742. derivation."
  743. ;; Here we rely on a gross historical fact: that derivations produced by the
  744. ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
  745. ;; dated May 30, 2018) did not depend on "guix-command.drv".
  746. (not (find (lambda (input)
  747. (string=? "guix-command"
  748. (derivation-name
  749. (derivation-input-derivation input))))
  750. (derivation-inputs drv))))
  751. (define (channel-instance->sexp instance)
  752. "Return an sexp representation of INSTANCE, a channel instance."
  753. (let* ((commit (channel-instance-commit instance))
  754. (channel (channel-instance-channel instance))
  755. (intro (channel-introduction channel)))
  756. `(repository
  757. (version 0)
  758. (url ,(channel-url channel))
  759. (branch ,(channel-branch channel))
  760. (commit ,commit)
  761. (name ,(channel-name channel))
  762. ,@(if intro
  763. `((introduction
  764. (channel-introduction
  765. (version 0)
  766. (commit
  767. ,(channel-introduction-first-signed-commit
  768. intro))
  769. (signer
  770. ,(openpgp-format-fingerprint
  771. (channel-introduction-first-commit-signer
  772. intro))))))
  773. '()))))
  774. (define (channel-instances->manifest instances)
  775. "Return a profile manifest with entries for all of INSTANCES, a list of
  776. channel instances."
  777. (define (instance->entry instance drv)
  778. (let ((commit (channel-instance-commit instance))
  779. (channel (channel-instance-channel instance)))
  780. (manifest-entry
  781. (name (symbol->string (channel-name channel)))
  782. (version (string-take commit 7))
  783. (item (if (guix-channel? channel)
  784. (if (old-style-guix? drv)
  785. (whole-package-for-legacy (string-append name "-" version)
  786. drv)
  787. drv)
  788. drv))
  789. (properties
  790. `((source ,(channel-instance->sexp instance)))))))
  791. (mlet* %store-monad ((derivations (channel-instance-derivations instances))
  792. (entries -> (map instance->entry instances derivations)))
  793. (return (manifest entries))))
  794. (define (package-cache-file manifest)
  795. "Build a package cache file for the instance in MANIFEST. This is meant to
  796. be used as a profile hook."
  797. (let ((profile (profile (content manifest) (hooks '()))))
  798. (define build
  799. #~(begin
  800. (use-modules (gnu packages))
  801. (if (defined? 'generate-package-cache)
  802. (begin
  803. ;; Delegate package cache generation to the inferior.
  804. (format (current-error-port)
  805. "Generating package cache for '~a'...~%"
  806. #$profile)
  807. (generate-package-cache #$output))
  808. (mkdir #$output))))
  809. (gexp->derivation-in-inferior "guix-package-cache" build
  810. profile
  811. ;; If the Guix in PROFILE is too old and
  812. ;; lacks 'guix repl', don't build the cache
  813. ;; instead of failing.
  814. #:silent-failure? #t
  815. #:properties '((type . profile-hook)
  816. (hook . package-cache))
  817. #:local-build? #t)))
  818. (define %channel-profile-hooks
  819. ;; The default channel profile hooks.
  820. (cons package-cache-file %default-profile-hooks))
  821. (define (channel-instances->derivation instances)
  822. "Return the derivation of the profile containing INSTANCES, a list of
  823. channel instances."
  824. (mlet %store-monad ((manifest (channel-instances->manifest instances)))
  825. (profile-derivation manifest
  826. #:hooks %channel-profile-hooks)))
  827. (define latest-channel-instances*
  828. (store-lift latest-channel-instances))
  829. (define* (latest-channel-derivation #:optional (channels %default-channels)
  830. #:key
  831. (current-channels '())
  832. (validate-pull
  833. ensure-forward-channel-update))
  834. "Return as a monadic value the derivation that builds the profile for the
  835. latest instances of CHANNELS. CURRENT-CHANNELS and VALIDATE-PULL are passed
  836. to 'latest-channel-instances'."
  837. (mlet %store-monad ((instances
  838. (latest-channel-instances* channels
  839. #:current-channels
  840. current-channels
  841. #:validate-pull
  842. validate-pull)))
  843. (channel-instances->derivation instances)))
  844. (define* (sexp->channel sexp #:optional (name 'channel))
  845. "Read SEXP, a provenance sexp as created by 'channel-instance->sexp'; use
  846. NAME as the channel name if SEXP does not specify it. Return #f if the sexp
  847. does not have the expected structure."
  848. (match sexp
  849. (('repository ('version 0)
  850. ('url url)
  851. ('branch branch)
  852. ('commit commit)
  853. rest ...)
  854. ;; Historically channel sexps did not include the channel name. It's OK
  855. ;; for channels created by 'channel-instances->manifest' because the
  856. ;; entry name is the channel name, but it was missing for entries created
  857. ;; by 'manifest-entry-with-provenance'.
  858. (channel (name (match (assq 'name rest)
  859. (#f name)
  860. (('name name) name)))
  861. (url url)
  862. (branch branch)
  863. (commit commit)
  864. (introduction
  865. (match (assq 'introduction rest)
  866. (#f #f)
  867. (('introduction intro)
  868. (sexp->channel-introduction intro))))))
  869. (_ #f)))
  870. (define (manifest-entry-channel entry)
  871. "Return the channel ENTRY corresponds to, or #f if that information is
  872. missing or unreadable. ENTRY must be an entry created by
  873. 'channel-instances->manifest', with the 'source' property."
  874. (let ((name (string->symbol (manifest-entry-name entry))))
  875. (match (assq-ref (manifest-entry-properties entry) 'source)
  876. ((sexp)
  877. (sexp->channel sexp name))
  878. (_
  879. ;; No channel information for this manifest entry.
  880. ;; XXX: Pre-0.15.0 Guix did not provide that information,
  881. ;; but there's not much we can do in that case.
  882. #f))))
  883. (define (profile-channels profile)
  884. "Return the list of channels corresponding to entries in PROFILE. If
  885. PROFILE is not a profile created by 'guix pull', return the empty list."
  886. (filter-map manifest-entry-channel
  887. ;; Show most recently installed packages last.
  888. (reverse
  889. (manifest-entries (profile-manifest profile)))))
  890. (define* (channel->code channel #:key (include-introduction? #t))
  891. "Return code (an sexp) to build CHANNEL. When INCLUDE-INTRODUCTION? is
  892. true, include its introduction, if any."
  893. (let ((intro (and include-introduction?
  894. (channel-introduction channel))))
  895. `(channel
  896. (name ',(channel-name channel))
  897. (url ,(channel-url channel))
  898. (commit ,(channel-commit channel))
  899. ,@(if intro
  900. `((introduction (make-channel-introduction
  901. ,(channel-introduction-first-signed-commit intro)
  902. (openpgp-fingerprint
  903. ,(openpgp-format-fingerprint
  904. (channel-introduction-first-commit-signer
  905. intro))))))
  906. '()))))
  907. ;;;
  908. ;;; News.
  909. ;;;
  910. ;; Channel news.
  911. (define-record-type <channel-news>
  912. (channel-news entries)
  913. channel-news?
  914. (entries channel-news-entries)) ;list of <channel-news-entry>
  915. ;; News entry, associated with a specific commit of the channel.
  916. (define-record-type <channel-news-entry>
  917. (channel-news-entry commit tag title body)
  918. channel-news-entry?
  919. (commit channel-news-entry-commit) ;hex string | #f
  920. (tag channel-news-entry-tag) ;#f | string
  921. (title channel-news-entry-title) ;list of language tag/string pairs
  922. (body channel-news-entry-body)) ;list of language tag/string pairs
  923. (define (sexp->channel-news-entry entry)
  924. "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
  925. (define (pair language message)
  926. (cons (symbol->string language) message))
  927. (match entry
  928. (('entry ((and (or 'commit 'tag) type) commit-or-tag)
  929. ('title ((? symbol? title-tags) (? string? titles)) ...)
  930. ('body ((? symbol? body-tags) (? string? bodies)) ...)
  931. _ ...)
  932. (channel-news-entry (and (eq? type 'commit) commit-or-tag)
  933. (and (eq? type 'tag) commit-or-tag)
  934. (map pair title-tags titles)
  935. (map pair body-tags bodies)))
  936. (_
  937. (raise (condition
  938. (&message (message "invalid channel news entry"))
  939. (&error-location
  940. (location (source-properties->location
  941. (source-properties entry)))))))))
  942. (define (read-channel-news port)
  943. "Read a channel news feed from PORT and return it as a <channel-news>
  944. record."
  945. (match (false-if-exception (read port))
  946. (('channel-news ('version 0) entries ...)
  947. (channel-news (map sexp->channel-news-entry entries)))
  948. (('channel-news ('version version) _ ...)
  949. ;; This is an unsupported version from the future. There's nothing wrong
  950. ;; with that (the user may simply need to upgrade the 'guix' channel to
  951. ;; be able to read it), so silently ignore it.
  952. (channel-news '()))
  953. (#f
  954. (raise (condition
  955. (&message (message "syntactically invalid channel news file")))))
  956. (sexp
  957. (raise (condition
  958. (&message (message "invalid channel news file"))
  959. (&error-location
  960. (location (source-properties->location
  961. (source-properties sexp)))))))))
  962. (define (resolve-channel-news-entry-tag repository entry)
  963. "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
  964. ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
  965. the field its 'tag' refers to. A 'git-error' exception is raised if the tag
  966. cannot be found."
  967. (if (channel-news-entry-commit entry)
  968. entry
  969. (let* ((tag (channel-news-entry-tag entry))
  970. (reference (string-append "refs/tags/" tag))
  971. (oid (reference-name->oid repository reference)))
  972. (channel-news-entry (oid->string oid) tag
  973. (channel-news-entry-title entry)
  974. (channel-news-entry-body entry)))))
  975. (define* (channel-news-for-commit channel new #:optional old)
  976. "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
  977. NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
  978. (catch 'git-error
  979. (lambda ()
  980. (let* ((checkout (update-cached-checkout (channel-url channel)
  981. #:ref `(commit . ,new)))
  982. (metadata (read-channel-metadata-from-source checkout))
  983. (news-file (channel-metadata-news-file metadata))
  984. (news-file (and news-file
  985. (string-append checkout "/" news-file))))
  986. (if (and news-file (file-exists? news-file))
  987. (with-repository checkout repository
  988. (let* ((news (call-with-input-file news-file
  989. read-channel-news))
  990. (entries (map (lambda (entry)
  991. (resolve-channel-news-entry-tag repository
  992. entry))
  993. (channel-news-entries news))))
  994. (if old
  995. (let* ((new (commit-lookup repository (string->oid new)))
  996. (old (commit-lookup repository (string->oid old)))
  997. (commits (list->set
  998. (map (compose oid->string commit-id)
  999. (commit-difference new old)))))
  1000. (filter (lambda (entry)
  1001. (set-contains? commits
  1002. (channel-news-entry-commit entry)))
  1003. entries))
  1004. entries)))
  1005. '())))
  1006. (lambda (key error . rest)
  1007. ;; If commit NEW or commit OLD cannot be found, then something must be
  1008. ;; wrong (for example, the history of CHANNEL was rewritten and these
  1009. ;; commits no longer exist upstream), so quietly return the empty list.
  1010. (if (= GIT_ENOTFOUND (git-error-code error))
  1011. '()
  1012. (apply throw key error rest)))))
  1013. ;;; Local Variables:
  1014. ;;; eval: (put 'with-guile 'scheme-indent-function 1)
  1015. ;;; End: