You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

940 lines
39 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix self)
  19. #:use-module (guix config)
  20. #:use-module (guix i18n)
  21. #:use-module (guix modules)
  22. #:use-module (guix gexp)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix discovery)
  26. #:use-module (guix packages)
  27. #:use-module (guix sets)
  28. #:use-module (guix modules)
  29. #:use-module ((guix build utils) #:select (find-files))
  30. #:use-module ((guix build compile) #:select (%lightweight-optimizations))
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-9)
  33. #:use-module (ice-9 match)
  34. #:export (make-config.scm
  35. whole-package ;for internal use in 'guix pull'
  36. compiled-guix
  37. guix-derivation
  38. reload-guix))
  39. ;;;
  40. ;;; Dependency handling.
  41. ;;;
  42. (define* (false-if-wrong-guile package
  43. #:optional (guile-version (effective-version)))
  44. "Return #f if PACKAGE depends on the \"wrong\" major version of Guile (e.g.,
  45. 2.0 instead of 2.2), otherwise return PACKAGE."
  46. (let ((guile (any (match-lambda
  47. ((label (? package? dep) _ ...)
  48. (and (string=? (package-name dep) "guile")
  49. dep)))
  50. (package-direct-inputs package))))
  51. (and (or (not guile)
  52. (string-prefix? guile-version
  53. (package-version guile)))
  54. package)))
  55. (define (package-for-guile guile-version . names)
  56. "Return the package with one of the given NAMES that depends on
  57. GUILE-VERSION (\"2.0\" or \"2.2\"), or #f if none of the packages matches."
  58. (let loop ((names names))
  59. (match names
  60. (()
  61. #f)
  62. ((name rest ...)
  63. (match (specification->package name)
  64. (#f
  65. (loop rest))
  66. ((? package? package)
  67. (or (false-if-wrong-guile package guile-version)
  68. (loop rest))))))))
  69. (define specification->package
  70. ;; Use our own variant of that procedure because that of (gnu packages)
  71. ;; would traverse all the .scm files, which is wasteful.
  72. (let ((ref (lambda (module variable)
  73. (module-ref (resolve-interface module) variable))))
  74. (match-lambda
  75. ("guile" (ref '(gnu packages commencement) 'guile-final))
  76. ("guile-json" (ref '(gnu packages guile) 'guile-json))
  77. ("guile-ssh" (ref '(gnu packages ssh) 'guile-ssh))
  78. ("guile-git" (ref '(gnu packages guile) 'guile-git))
  79. ("guile-sqlite3" (ref '(gnu packages guile) 'guile-sqlite3))
  80. ("gnutls" (ref '(gnu packages tls) 'gnutls))
  81. ("libgcrypt" (ref '(gnu packages gnupg) 'libgcrypt))
  82. ("zlib" (ref '(gnu packages compression) 'zlib))
  83. ("gzip" (ref '(gnu packages compression) 'gzip))
  84. ("bzip2" (ref '(gnu packages compression) 'bzip2))
  85. ("xz" (ref '(gnu packages compression) 'xz))
  86. ("guile2.0-json" (ref '(gnu packages guile) 'guile2.0-json))
  87. ("guile2.0-ssh" (ref '(gnu packages ssh) 'guile2.0-ssh))
  88. ("guile2.0-git" (ref '(gnu packages guile) 'guile2.0-git))
  89. ;; XXX: No "guile2.0-sqlite3".
  90. ("guile2.0-gnutls" (ref '(gnu packages tls) 'gnutls/guile-2.0))
  91. (_ #f)))) ;no such package
  92. ;;;
  93. ;;; Derivations.
  94. ;;;
  95. ;; Node in a DAG of build tasks. Each node maps to a derivation, but it's
  96. ;; easier to express things this way.
  97. (define-record-type <node>
  98. (node name modules source dependencies compiled)
  99. node?
  100. (name node-name) ;string
  101. (modules node-modules) ;list of module names
  102. (source node-source) ;list of source files
  103. (dependencies node-dependencies) ;list of nodes
  104. (compiled node-compiled)) ;node -> lowerable object
  105. ;; File mappings are essentially an alist as passed to 'imported-files'.
  106. (define-record-type <file-mapping>
  107. (file-mapping name alist)
  108. file-mapping?
  109. (name file-mapping-name)
  110. (alist file-mapping-alist))
  111. (define-gexp-compiler (file-mapping-compiler (mapping <file-mapping>)
  112. system target)
  113. ;; Here we use 'imported-files', which can arrange to directly import all
  114. ;; the files instead of creating a derivation, when possible.
  115. (imported-files (map (match-lambda
  116. ((destination (? local-file? file))
  117. (cons destination
  118. (local-file-absolute-file-name file)))
  119. ((destination source)
  120. (cons destination source))) ;silliness
  121. (file-mapping-alist mapping))
  122. #:name (file-mapping-name mapping)
  123. #:system system))
  124. (define (node-fold proc init nodes)
  125. (let loop ((nodes nodes)
  126. (visited (setq))
  127. (result init))
  128. (match nodes
  129. (() result)
  130. ((head tail ...)
  131. (if (set-contains? visited head)
  132. (loop tail visited result)
  133. (loop tail (set-insert head visited)
  134. (proc head result)))))))
  135. (define (node-modules/recursive nodes)
  136. (node-fold (lambda (node modules)
  137. (append (node-modules node) modules))
  138. '()
  139. nodes))
  140. (define* (closure modules #:optional (except '()))
  141. (source-module-closure modules
  142. #:select?
  143. (match-lambda
  144. (('guix 'config)
  145. #f)
  146. ((and module
  147. (or ('guix _ ...) ('gnu _ ...)))
  148. (not (member module except)))
  149. (rest #f))))
  150. (define module->import
  151. ;; Return a file-name/file-like object pair for the specified module and
  152. ;; suitable for 'imported-files'.
  153. (match-lambda
  154. ((module '=> thing)
  155. (let ((file (module-name->file-name module)))
  156. (list file thing)))
  157. (module
  158. (let ((file (module-name->file-name module)))
  159. (list file
  160. (local-file (search-path %load-path file)))))))
  161. (define* (scheme-node name modules #:optional (dependencies '())
  162. #:key (extra-modules '()) (extra-files '())
  163. (extensions '())
  164. parallel? guile-for-build)
  165. "Return a node that builds the given Scheme MODULES, and depends on
  166. DEPENDENCIES (a list of nodes). EXTRA-MODULES is a list of additional modules
  167. added to the source, and EXTRA-FILES is a list of additional files.
  168. EXTENSIONS is a set of full-blown Guile packages (e.g., 'guile-json') that
  169. must be present in the search path."
  170. (let* ((modules (append extra-modules
  171. (closure modules
  172. (node-modules/recursive dependencies))))
  173. (module-files (map module->import modules))
  174. (source (file-mapping (string-append name "-source")
  175. (append module-files extra-files))))
  176. (node name modules source dependencies
  177. (compiled-modules name source
  178. (map car module-files)
  179. (map node-source dependencies)
  180. (map node-compiled dependencies)
  181. #:extensions extensions
  182. #:parallel? parallel?
  183. #:guile-for-build guile-for-build))))
  184. (define (file-imports directory sub-directory pred)
  185. "List all the files matching PRED under DIRECTORY/SUB-DIRECTORY. Return a
  186. list of file-name/file-like objects suitable as inputs to 'imported-files'."
  187. (map (lambda (file)
  188. (list (string-drop file (+ 1 (string-length directory)))
  189. (local-file file #:recursive? #t)))
  190. (find-files (string-append directory "/" sub-directory) pred)))
  191. (define (scheme-modules* directory sub-directory)
  192. "Return the list of module names found under SUB-DIRECTORY in DIRECTORY."
  193. (let ((prefix (string-length directory)))
  194. (map (lambda (file)
  195. (file-name->module-name (string-drop file prefix)))
  196. (scheme-files (string-append directory "/" sub-directory)))))
  197. (define* (sub-directory item sub-directory)
  198. "Return SUB-DIRECTORY within ITEM, which may be a file name or a file-like
  199. object."
  200. (match item
  201. ((? string?)
  202. ;; This is the optimal case: we return a new "source". Thus, a
  203. ;; derivation that depends on this sub-directory does not depend on ITEM
  204. ;; itself.
  205. (local-file (string-append item "/" sub-directory)
  206. #:recursive? #t))
  207. ;; TODO: Add 'local-file?' case.
  208. (_
  209. ;; In this case, anything that refers to the result also depends on ITEM,
  210. ;; which isn't great.
  211. (file-append item "/" sub-directory))))
  212. (define* (locale-data source domain
  213. #:optional (directory domain))
  214. "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
  215. DOMAIN, a gettext domain."
  216. (define gettext
  217. (module-ref (resolve-interface '(gnu packages gettext))
  218. 'gettext-minimal))
  219. (define build
  220. (with-imported-modules '((guix build utils))
  221. #~(begin
  222. (use-modules (guix build utils)
  223. (srfi srfi-26)
  224. (ice-9 match) (ice-9 ftw))
  225. (define po-directory
  226. #+(sub-directory source (string-append "po/" directory)))
  227. (define (compile language)
  228. (let ((gmo (string-append #$output "/" language "/LC_MESSAGES/"
  229. #$domain ".mo")))
  230. (mkdir-p (dirname gmo))
  231. (invoke #+(file-append gettext "/bin/msgfmt")
  232. "-c" "--statistics" "--verbose"
  233. "-o" gmo
  234. (string-append po-directory "/" language ".po"))))
  235. (define (linguas)
  236. ;; Return the list of languages. Note: don't read 'LINGUAS'
  237. ;; because it contains things like 'en@boldquot' that do not have
  238. ;; a corresponding .po file.
  239. (map (cut basename <> ".po")
  240. (scandir po-directory
  241. (cut string-suffix? ".po" <>))))
  242. (for-each compile (linguas)))))
  243. (computed-file (string-append "guix-locale-" domain)
  244. build))
  245. (define (info-manual source)
  246. "Return the Info manual built from SOURCE."
  247. (define texinfo
  248. (module-ref (resolve-interface '(gnu packages texinfo))
  249. 'texinfo))
  250. (define graphviz
  251. (module-ref (resolve-interface '(gnu packages graphviz))
  252. 'graphviz))
  253. (define documentation
  254. (sub-directory source "doc"))
  255. (define examples
  256. (sub-directory source "gnu/system/examples"))
  257. (define build
  258. (with-imported-modules '((guix build utils))
  259. #~(begin
  260. (use-modules (guix build utils))
  261. (mkdir #$output)
  262. ;; Create 'version.texi'.
  263. ;; XXX: Can we use a more meaningful version string yet one that
  264. ;; doesn't change at each commit?
  265. (call-with-output-file "version.texi"
  266. (lambda (port)
  267. (let ((version "0.0-git)"))
  268. (format port "
  269. @set UPDATED 1 January 1970
  270. @set UPDATED-MONTH January 1970
  271. @set EDITION ~a
  272. @set VERSION ~a\n" version version))))
  273. ;; Copy configuration templates that the manual includes.
  274. (for-each (lambda (template)
  275. (copy-file template
  276. (string-append
  277. "os-config-"
  278. (basename template ".tmpl")
  279. ".texi")))
  280. (find-files #$examples "\\.tmpl$"))
  281. ;; Build graphs.
  282. (mkdir-p (string-append #$output "/images"))
  283. (for-each (lambda (dot-file)
  284. (invoke #+(file-append graphviz "/bin/dot")
  285. "-Tpng" "-Gratio=.9" "-Gnodesep=.005"
  286. "-Granksep=.00005" "-Nfontsize=9"
  287. "-Nheight=.1" "-Nwidth=.1"
  288. "-o" (string-append #$output "/images/"
  289. (basename dot-file ".dot")
  290. ".png")
  291. dot-file))
  292. (find-files (string-append #$documentation "/images")
  293. "\\.dot$"))
  294. ;; Copy other PNGs.
  295. (for-each (lambda (png-file)
  296. (install-file png-file
  297. (string-append #$output "/images")))
  298. (find-files (string-append #$documentation "/images")
  299. "\\.png$"))
  300. ;; Finally build the manual. Copy it the Texinfo files to $PWD and
  301. ;; add a symlink to the 'images' directory so that 'makeinfo' can
  302. ;; see those images and produce image references in the Info output.
  303. (copy-recursively #$documentation "."
  304. #:log (%make-void-port "w"))
  305. (delete-file-recursively "images")
  306. (symlink (string-append #$output "/images") "images")
  307. (for-each (lambda (texi)
  308. (unless (string=? "guix.texi" texi)
  309. ;; Create 'version-LL.texi'.
  310. (let* ((base (basename texi ".texi"))
  311. (dot (string-index base #\.))
  312. (tag (string-drop base (+ 1 dot))))
  313. (symlink "version.texi"
  314. (string-append "version-" tag ".texi"))))
  315. (invoke #+(file-append texinfo "/bin/makeinfo")
  316. texi "-I" #$documentation
  317. "-I" "."
  318. "-o" (string-append #$output "/"
  319. (basename texi ".texi")
  320. ".info")))
  321. (cons "guix.texi"
  322. (find-files "." "^guix\\.[a-z]{2}\\.texi$"))))))
  323. (computed-file "guix-manual" build))
  324. (define* (guix-command modules #:optional compiled-modules
  325. #:key source (dependencies '())
  326. guile (guile-version (effective-version)))
  327. "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
  328. load path."
  329. (define source-directories
  330. (map (lambda (package)
  331. (file-append package "/share/guile/site/"
  332. guile-version))
  333. dependencies))
  334. (define object-directories
  335. (map (lambda (package)
  336. (file-append package "/lib/guile/"
  337. guile-version "/site-ccache"))
  338. dependencies))
  339. (program-file "guix-command"
  340. #~(begin
  341. (set! %load-path
  342. (append (filter file-exists? '#$source-directories)
  343. %load-path))
  344. (set! %load-compiled-path
  345. (append (filter file-exists? '#$object-directories)
  346. %load-compiled-path))
  347. (set! %load-path (cons #$modules %load-path))
  348. (set! %load-compiled-path
  349. (cons (or #$compiled-modules #$modules)
  350. %load-compiled-path))
  351. (let ((guix-main (module-ref (resolve-interface '(guix ui))
  352. 'guix-main)))
  353. #$(if source
  354. #~(begin
  355. (bindtextdomain "guix"
  356. #$(locale-data source "guix"))
  357. (bindtextdomain "guix-packages"
  358. #$(locale-data source
  359. "guix-packages"
  360. "packages")))
  361. #t)
  362. ;; XXX: It would be more convenient to change it to:
  363. ;; (exit (apply guix-main (command-line)))
  364. (apply guix-main (command-line))))
  365. #:guile guile))
  366. (define* (whole-package name modules dependencies
  367. #:key
  368. (guile-version (effective-version))
  369. compiled-modules
  370. info daemon guile
  371. (command (guix-command modules
  372. #:dependencies dependencies
  373. #:guile guile
  374. #:guile-version guile-version)))
  375. "Return the whole Guix package NAME that uses MODULES, a derivation of all
  376. the modules, and DEPENDENCIES, a list of packages depended on. COMMAND is the
  377. 'guix' program to use; INFO is the Info manual. When COMPILED-MODULES is
  378. true, it is linked as 'lib/guile/X.Y/site-ccache'; otherwise, .go files are
  379. assumed to be part of MODULES."
  380. (computed-file name
  381. (with-imported-modules '((guix build utils))
  382. #~(begin
  383. (use-modules (guix build utils))
  384. (mkdir-p (string-append #$output "/bin"))
  385. (symlink #$command
  386. (string-append #$output "/bin/guix"))
  387. (when #$daemon
  388. (symlink (string-append #$daemon "/bin/guix-daemon")
  389. (string-append #$output "/bin/guix-daemon")))
  390. (let ((modules (string-append #$output
  391. "/share/guile/site/"
  392. (effective-version)))
  393. (info #$info))
  394. (mkdir-p (dirname modules))
  395. (symlink #$modules modules)
  396. (when info
  397. (symlink #$info
  398. (string-append #$output
  399. "/share/info"))))
  400. ;; Object files.
  401. (when #$compiled-modules
  402. (let ((modules (string-append #$output "/lib/guile/"
  403. (effective-version)
  404. "/site-ccache")))
  405. (mkdir-p (dirname modules))
  406. (symlink #$compiled-modules modules)))))))
  407. (define* (compiled-guix source #:key (version %guix-version)
  408. (pull-version 1)
  409. (name (string-append "guix-" version))
  410. (guile-version (effective-version))
  411. (guile-for-build (guile-for-build guile-version))
  412. (libgcrypt (specification->package "libgcrypt"))
  413. (zlib (specification->package "zlib"))
  414. (gzip (specification->package "gzip"))
  415. (bzip2 (specification->package "bzip2"))
  416. (xz (specification->package "xz"))
  417. (guix (specification->package "guix")))
  418. "Return a file-like object that contains a compiled Guix."
  419. (define guile-json
  420. (package-for-guile guile-version
  421. "guile-json"
  422. "guile2.0-json"))
  423. (define guile-ssh
  424. (package-for-guile guile-version
  425. "guile-ssh"
  426. "guile2.0-ssh"))
  427. (define guile-git
  428. (package-for-guile guile-version
  429. "guile-git"
  430. "guile2.0-git"))
  431. (define guile-sqlite3
  432. (package-for-guile guile-version
  433. "guile-sqlite3"
  434. "guile2.0-sqlite3"))
  435. (define gnutls
  436. (package-for-guile guile-version
  437. "gnutls" "guile2.0-gnutls"))
  438. (define dependencies
  439. (match (append-map (lambda (package)
  440. (cons (list "x" package)
  441. (package-transitive-propagated-inputs package)))
  442. (list gnutls guile-git guile-json
  443. guile-ssh guile-sqlite3))
  444. (((labels packages _ ...) ...)
  445. packages)))
  446. (define *core-modules*
  447. (scheme-node "guix-core"
  448. '((guix)
  449. (guix monad-repl)
  450. (guix packages)
  451. (guix download)
  452. (guix discovery)
  453. (guix profiles)
  454. (guix build-system gnu)
  455. (guix build-system trivial)
  456. (guix build profiles)
  457. (guix build gnu-build-system))
  458. ;; Provide a dummy (guix config) with the default version
  459. ;; number, storedir, etc. This is so that "guix-core" is the
  460. ;; same across all installations and doesn't need to be
  461. ;; rebuilt when the version changes, which in turn means we
  462. ;; can have substitutes for it.
  463. #:extra-modules
  464. `(((guix config)
  465. => ,(make-config.scm #:libgcrypt
  466. (specification->package
  467. "libgcrypt"))))
  468. ;; (guix man-db) is needed at build-time by (guix profiles)
  469. ;; but we don't need to compile it; not compiling it allows
  470. ;; us to avoid an extra dependency on guile-gdbm-ffi.
  471. #:extra-files
  472. `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
  473. ("guix/store/schema.sql"
  474. ,(local-file "../guix/store/schema.sql")))
  475. #:guile-for-build guile-for-build))
  476. (define *extra-modules*
  477. (scheme-node "guix-extra"
  478. (filter-map (match-lambda
  479. (('guix 'scripts _ ..1) #f)
  480. (('guix 'man-db) #f)
  481. (name name))
  482. (scheme-modules* source "guix"))
  483. (list *core-modules*)
  484. #:extensions dependencies
  485. #:guile-for-build guile-for-build))
  486. (define *core-package-modules*
  487. (scheme-node "guix-packages-base"
  488. `((gnu packages)
  489. (gnu packages base))
  490. (list *core-modules* *extra-modules*)
  491. #:extensions dependencies
  492. ;; Add all the non-Scheme files here. We must do it here so
  493. ;; that 'search-patches' & co. can find them. Ideally we'd
  494. ;; keep them next to the .scm files that use them but it's
  495. ;; difficult to do (XXX).
  496. #:extra-files
  497. (file-imports source "gnu/packages"
  498. (lambda (file stat)
  499. (and (eq? 'regular (stat:type stat))
  500. (not (string-suffix? ".scm" file))
  501. (not (string-suffix? ".go" file))
  502. (not (string-prefix? ".#" file))
  503. (not (string-suffix? "~" file)))))
  504. #:guile-for-build guile-for-build))
  505. (define *package-modules*
  506. (scheme-node "guix-packages"
  507. (scheme-modules* source "gnu/packages")
  508. (list *core-modules* *extra-modules* *core-package-modules*)
  509. #:extensions dependencies
  510. #:guile-for-build guile-for-build))
  511. (define *system-modules*
  512. (scheme-node "guix-system"
  513. `((gnu system)
  514. (gnu services)
  515. ,@(scheme-modules* source "gnu/system")
  516. ,@(scheme-modules* source "gnu/services"))
  517. (list *core-package-modules* *package-modules*
  518. *extra-modules* *core-modules*)
  519. #:extensions dependencies
  520. #:extra-files
  521. (append (file-imports source "gnu/system/examples"
  522. (const #t))
  523. ;; Build-side code that we don't build. Some of
  524. ;; these depend on guile-rsvg, the Shepherd, etc.
  525. (file-imports source "gnu/build" (const #t)))
  526. #:guile-for-build
  527. guile-for-build))
  528. (define *cli-modules*
  529. (scheme-node "guix-cli"
  530. (scheme-modules* source "/guix/scripts")
  531. (list *core-modules* *extra-modules*
  532. *core-package-modules* *package-modules*
  533. *system-modules*)
  534. #:extensions dependencies
  535. #:guile-for-build guile-for-build))
  536. (define *config*
  537. (scheme-node "guix-config"
  538. '()
  539. #:extra-modules
  540. `(((guix config)
  541. => ,(make-config.scm #:libgcrypt libgcrypt
  542. #:zlib zlib
  543. #:gzip gzip
  544. #:bzip2 bzip2
  545. #:xz xz
  546. #:package-name
  547. %guix-package-name
  548. #:package-version
  549. version
  550. #:bug-report-address
  551. %guix-bug-report-address
  552. #:home-page-url
  553. %guix-home-page-url)))
  554. #:guile-for-build guile-for-build))
  555. (define (built-modules node-subset)
  556. (directory-union (string-append name "-modules")
  557. (append-map node-subset
  558. ;; Note: *CONFIG* comes first so that it
  559. ;; overrides the (guix config) module that
  560. ;; comes with *CORE-MODULES*.
  561. (list *config*
  562. *cli-modules*
  563. *system-modules*
  564. *package-modules*
  565. *core-package-modules*
  566. *extra-modules*
  567. *core-modules*))
  568. ;; Silently choose the first entry upon collision so that
  569. ;; we choose *CONFIG*.
  570. #:resolve-collision 'first
  571. ;; When we do (add-to-store "utils.scm"), "utils.scm" must
  572. ;; be a regular file, not a symlink. Thus, arrange so that
  573. ;; regular files appear as regular files in the final
  574. ;; output.
  575. #:copy? #t
  576. #:quiet? #t))
  577. ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
  578. ;; Version 1 is when we return the full package.
  579. (cond ((= 1 pull-version)
  580. ;; The whole package, with a standard file hierarchy.
  581. (let* ((modules (built-modules (compose list node-source)))
  582. (compiled (built-modules (compose list node-compiled)))
  583. (command (guix-command modules compiled
  584. #:source source
  585. #:dependencies dependencies
  586. #:guile guile-for-build
  587. #:guile-version guile-version)))
  588. (whole-package name modules dependencies
  589. #:compiled-modules compiled
  590. #:command command
  591. #:guile guile-for-build
  592. ;; Include 'guix-daemon'. XXX: Here we inject an
  593. ;; older snapshot of guix-daemon, but that's a good
  594. ;; enough approximation for now.
  595. #:daemon (module-ref (resolve-interface
  596. '(gnu packages
  597. package-management))
  598. 'guix-daemon)
  599. #:info (info-manual source)
  600. #:guile-version guile-version)))
  601. ((= 0 pull-version)
  602. ;; Legacy 'guix pull': return the .scm and .go files as one
  603. ;; directory.
  604. (built-modules (lambda (node)
  605. (list (node-source node)
  606. (node-compiled node)))))
  607. (else
  608. ;; Unsupported 'guix pull' version.
  609. #f)))
  610. ;;;
  611. ;;; Generating (guix config).
  612. ;;;
  613. (define %dependency-variables
  614. ;; (guix config) variables corresponding to dependencies.
  615. '(%libgcrypt %libz %xz %gzip %bzip2))
  616. (define %persona-variables
  617. ;; (guix config) variables that define Guix's persona.
  618. '(%guix-package-name
  619. %guix-version
  620. %guix-bug-report-address
  621. %guix-home-page-url))
  622. (define %config-variables
  623. ;; (guix config) variables corresponding to Guix configuration.
  624. (letrec-syntax ((variables (syntax-rules ()
  625. ((_)
  626. '())
  627. ((_ variable rest ...)
  628. (cons `(variable . ,variable)
  629. (variables rest ...))))))
  630. (variables %localstatedir %storedir %sysconfdir %system)))
  631. (define* (make-config.scm #:key libgcrypt zlib gzip xz bzip2
  632. (package-name "GNU Guix")
  633. (package-version "0")
  634. (bug-report-address "bug-guix@gnu.org")
  635. (home-page-url "https://gnu.org/s/guix"))
  636. ;; Hack so that Geiser is not confused.
  637. (define defmod 'define-module)
  638. (scheme-file "config.scm"
  639. #~(;; The following expressions get spliced.
  640. (#$defmod (guix config)
  641. #:export (%guix-package-name
  642. %guix-version
  643. %guix-bug-report-address
  644. %guix-home-page-url
  645. %store-directory
  646. %state-directory
  647. %store-database-directory
  648. %config-directory
  649. %libgcrypt
  650. %libz
  651. %gzip
  652. %bzip2
  653. %xz))
  654. #$@(map (match-lambda
  655. ((name . value)
  656. #~(define-public #$name #$value)))
  657. %config-variables)
  658. (define %store-directory
  659. (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
  660. %storedir))
  661. (define %state-directory
  662. ;; This must match `NIX_STATE_DIR' as defined in
  663. ;; `nix/local.mk'.
  664. (or (getenv "NIX_STATE_DIR")
  665. (string-append %localstatedir "/guix")))
  666. (define %store-database-directory
  667. (or (getenv "NIX_DB_DIR")
  668. (string-append %state-directory "/db")))
  669. (define %config-directory
  670. ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
  671. ;; defined in `nix/local.mk'.
  672. (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
  673. (string-append %sysconfdir "/guix")))
  674. (define %guix-package-name #$package-name)
  675. (define %guix-version #$package-version)
  676. (define %guix-bug-report-address #$bug-report-address)
  677. (define %guix-home-page-url #$home-page-url)
  678. (define %gzip
  679. #+(and gzip (file-append gzip "/bin/gzip")))
  680. (define %bzip2
  681. #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
  682. (define %xz
  683. #+(and xz (file-append xz "/bin/xz")))
  684. (define %libgcrypt
  685. #+(and libgcrypt
  686. (file-append libgcrypt "/lib/libgcrypt")))
  687. (define %libz
  688. #+(and zlib
  689. (file-append zlib "/lib/libz"))))
  690. ;; Guile 2.0 *requires* the 'define-module' to be at the
  691. ;; top-level or the 'toplevel-ref' in the resulting .go file are
  692. ;; made relative to a nonexistent anonymous module.
  693. #:splice? #t))
  694. ;;;
  695. ;;; Building.
  696. ;;;
  697. (define* (compiled-modules name module-tree module-files
  698. #:optional
  699. (dependencies '())
  700. (dependencies-compiled '())
  701. #:key
  702. (extensions '()) ;full-blown Guile packages
  703. parallel?
  704. guile-for-build)
  705. "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
  706. like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
  707. containing MODULE-FILES and possibly other files as well."
  708. ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
  709. ;; gexp).
  710. (define build
  711. (with-imported-modules (source-module-closure
  712. '((guix build compile)
  713. (guix build utils)))
  714. #~(begin
  715. (use-modules (srfi srfi-26)
  716. (ice-9 match)
  717. (ice-9 format)
  718. (ice-9 threads)
  719. (guix build compile)
  720. (guix build utils))
  721. (define (regular? file)
  722. (not (member file '("." ".."))))
  723. (define (report-load file total completed)
  724. (display #\cr)
  725. (format #t
  726. "loading...\t~5,1f% of ~d files" ;FIXME: i18n
  727. (* 100. (/ completed total)) total)
  728. (force-output))
  729. (define (report-compilation file total completed)
  730. (display #\cr)
  731. (format #t "compiling...\t~5,1f% of ~d files" ;FIXME: i18n
  732. (* 100. (/ completed total)) total)
  733. (force-output))
  734. (define (process-directory directory files output)
  735. ;; Hide compilation warnings.
  736. (parameterize ((current-warning-port (%make-void-port "w")))
  737. (compile-files directory #$output files
  738. #:workers (parallel-job-count)
  739. #:report-load report-load
  740. #:report-compilation report-compilation)))
  741. (setvbuf (current-output-port) _IONBF)
  742. (setvbuf (current-error-port) _IONBF)
  743. (set! %load-path (cons #+module-tree %load-path))
  744. (set! %load-path
  745. (append '#+dependencies
  746. (map (lambda (extension)
  747. (string-append extension "/share/guile/site/"
  748. (effective-version)))
  749. '#+extensions)
  750. %load-path))
  751. (set! %load-compiled-path
  752. (append '#+dependencies-compiled
  753. (map (lambda (extension)
  754. (string-append extension "/lib/guile/"
  755. (effective-version)
  756. "/site-ccache"))
  757. '#+extensions)
  758. %load-compiled-path))
  759. ;; Load the compiler modules upfront.
  760. (compile #f)
  761. (mkdir #$output)
  762. (chdir #+module-tree)
  763. (process-directory "." '#+module-files #$output)
  764. (newline))))
  765. (computed-file name build
  766. #:guile guile-for-build
  767. #:options
  768. `(#:local-build? #f ;allow substitutes
  769. ;; Don't annoy people about _IONBF deprecation.
  770. ;; Initialize 'terminal-width' in (system repl debug)
  771. ;; to a large-enough value to make backtrace more
  772. ;; verbose.
  773. #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
  774. ("COLUMNS" . "200")))))
  775. ;;;
  776. ;;; Building.
  777. ;;;
  778. (define (guile-for-build version)
  779. "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
  780. running Guile."
  781. (define canonical-package ;soft reference
  782. (module-ref (resolve-interface '(gnu packages base))
  783. 'canonical-package))
  784. (match version
  785. ("2.2.2"
  786. ;; Gross hack to avoid ABI incompatibilities (see
  787. ;; <https://bugs.gnu.org/29570>.)
  788. (module-ref (resolve-interface '(gnu packages guile))
  789. 'guile-2.2.2))
  790. ("2.2"
  791. ;; Use the latest version, which has fixes for
  792. ;; <https://bugs.gnu.org/30602> and VM stack-marking issues.
  793. (canonical-package (module-ref (resolve-interface '(gnu packages guile))
  794. 'guile-2.2.4)))
  795. ("2.0"
  796. (module-ref (resolve-interface '(gnu packages guile))
  797. 'guile-2.0))))
  798. (define* (guix-derivation source version
  799. #:optional (guile-version (effective-version))
  800. #:key (pull-version 0))
  801. "Return, as a monadic value, the derivation to build the Guix from SOURCE
  802. for GUILE-VERSION. Use VERSION as the version string. PULL-VERSION specifies
  803. the version of the 'guix pull' protocol. Return #f if this PULL-VERSION value
  804. is not supported."
  805. (define (shorten version)
  806. (if (and (string-every char-set:hex-digit version)
  807. (> (string-length version) 9))
  808. (string-take version 9) ;Git commit
  809. version))
  810. (define guile
  811. (guile-for-build guile-version))
  812. (mbegin %store-monad
  813. (set-guile-for-build guile)
  814. (let ((guix (compiled-guix source
  815. #:version version
  816. #:name (string-append "guix-"
  817. (shorten version))
  818. #:pull-version pull-version
  819. #:guile-version (match guile-version
  820. ("2.2.2" "2.2")
  821. (version version))
  822. #:guile-for-build guile)))
  823. (if guix
  824. (lower-object guix)
  825. (return #f)))))