channels.scm 44 KB

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