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.
 
 
 
 
 
 

930 lines
38 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. ("guile-gcrypt" (ref '(gnu packages gnupg) 'guile-gcrypt))
  81. ("gnutls" (ref '(gnu packages tls) 'gnutls))
  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* (sub-directory item sub-directory)
  192. "Return SUB-DIRECTORY within ITEM, which may be a file name or a file-like
  193. object."
  194. (match item
  195. ((? string?)
  196. ;; This is the optimal case: we return a new "source". Thus, a
  197. ;; derivation that depends on this sub-directory does not depend on ITEM
  198. ;; itself.
  199. (local-file (string-append item "/" sub-directory)
  200. #:recursive? #t))
  201. ;; TODO: Add 'local-file?' case.
  202. (_
  203. ;; In this case, anything that refers to the result also depends on ITEM,
  204. ;; which isn't great.
  205. (file-append item "/" sub-directory))))
  206. (define* (locale-data source domain
  207. #:optional (directory domain))
  208. "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
  209. DOMAIN, a gettext domain."
  210. (define gettext
  211. (module-ref (resolve-interface '(gnu packages gettext))
  212. 'gettext-minimal))
  213. (define build
  214. (with-imported-modules '((guix build utils))
  215. #~(begin
  216. (use-modules (guix build utils)
  217. (srfi srfi-26)
  218. (ice-9 match) (ice-9 ftw))
  219. (define po-directory
  220. #+(sub-directory source (string-append "po/" directory)))
  221. (define (compile language)
  222. (let ((gmo (string-append #$output "/" language "/LC_MESSAGES/"
  223. #$domain ".mo")))
  224. (mkdir-p (dirname gmo))
  225. (invoke #+(file-append gettext "/bin/msgfmt")
  226. "-c" "--statistics" "--verbose"
  227. "-o" gmo
  228. (string-append po-directory "/" language ".po"))))
  229. (define (linguas)
  230. ;; Return the list of languages. Note: don't read 'LINGUAS'
  231. ;; because it contains things like 'en@boldquot' that do not have
  232. ;; a corresponding .po file.
  233. (map (cut basename <> ".po")
  234. (scandir po-directory
  235. (cut string-suffix? ".po" <>))))
  236. (for-each compile (linguas)))))
  237. (computed-file (string-append "guix-locale-" domain)
  238. build))
  239. (define (info-manual source)
  240. "Return the Info manual built from SOURCE."
  241. (define texinfo
  242. (module-ref (resolve-interface '(gnu packages texinfo))
  243. 'texinfo))
  244. (define graphviz
  245. (module-ref (resolve-interface '(gnu packages graphviz))
  246. 'graphviz))
  247. (define documentation
  248. (sub-directory source "doc"))
  249. (define examples
  250. (sub-directory source "gnu/system/examples"))
  251. (define build
  252. (with-imported-modules '((guix build utils))
  253. #~(begin
  254. (use-modules (guix build utils))
  255. (mkdir #$output)
  256. ;; Create 'version.texi'.
  257. ;; XXX: Can we use a more meaningful version string yet one that
  258. ;; doesn't change at each commit?
  259. (call-with-output-file "version.texi"
  260. (lambda (port)
  261. (let ((version "0.0-git)"))
  262. (format port "
  263. @set UPDATED 1 January 1970
  264. @set UPDATED-MONTH January 1970
  265. @set EDITION ~a
  266. @set VERSION ~a\n" version version))))
  267. ;; Copy configuration templates that the manual includes.
  268. (for-each (lambda (template)
  269. (copy-file template
  270. (string-append
  271. "os-config-"
  272. (basename template ".tmpl")
  273. ".texi")))
  274. (find-files #$examples "\\.tmpl$"))
  275. ;; Build graphs.
  276. (mkdir-p (string-append #$output "/images"))
  277. (for-each (lambda (dot-file)
  278. (invoke #+(file-append graphviz "/bin/dot")
  279. "-Tpng" "-Gratio=.9" "-Gnodesep=.005"
  280. "-Granksep=.00005" "-Nfontsize=9"
  281. "-Nheight=.1" "-Nwidth=.1"
  282. "-o" (string-append #$output "/images/"
  283. (basename dot-file ".dot")
  284. ".png")
  285. dot-file))
  286. (find-files (string-append #$documentation "/images")
  287. "\\.dot$"))
  288. ;; Copy other PNGs.
  289. (for-each (lambda (png-file)
  290. (install-file png-file
  291. (string-append #$output "/images")))
  292. (find-files (string-append #$documentation "/images")
  293. "\\.png$"))
  294. ;; Finally build the manual. Copy it the Texinfo files to $PWD and
  295. ;; add a symlink to the 'images' directory so that 'makeinfo' can
  296. ;; see those images and produce image references in the Info output.
  297. (copy-recursively #$documentation "."
  298. #:log (%make-void-port "w"))
  299. (delete-file-recursively "images")
  300. (symlink (string-append #$output "/images") "images")
  301. (for-each (lambda (texi)
  302. (unless (string=? "guix.texi" texi)
  303. ;; Create 'version-LL.texi'.
  304. (let* ((base (basename texi ".texi"))
  305. (dot (string-index base #\.))
  306. (tag (string-drop base (+ 1 dot))))
  307. (symlink "version.texi"
  308. (string-append "version-" tag ".texi"))))
  309. (invoke #+(file-append texinfo "/bin/makeinfo")
  310. texi "-I" #$documentation
  311. "-I" "."
  312. "-o" (string-append #$output "/"
  313. (basename texi ".texi")
  314. ".info")))
  315. (cons "guix.texi"
  316. (find-files "." "^guix\\.[a-z]{2}\\.texi$"))))))
  317. (computed-file "guix-manual" build))
  318. (define* (guix-command modules #:optional compiled-modules
  319. #:key source (dependencies '())
  320. guile (guile-version (effective-version)))
  321. "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
  322. load path."
  323. (define source-directories
  324. (map (lambda (package)
  325. (file-append package "/share/guile/site/"
  326. guile-version))
  327. dependencies))
  328. (define object-directories
  329. (map (lambda (package)
  330. (file-append package "/lib/guile/"
  331. guile-version "/site-ccache"))
  332. dependencies))
  333. (program-file "guix-command"
  334. #~(begin
  335. (set! %load-path
  336. (append (filter file-exists? '#$source-directories)
  337. %load-path))
  338. (set! %load-compiled-path
  339. (append (filter file-exists? '#$object-directories)
  340. %load-compiled-path))
  341. (set! %load-path (cons #$modules %load-path))
  342. (set! %load-compiled-path
  343. (cons (or #$compiled-modules #$modules)
  344. %load-compiled-path))
  345. (let ((guix-main (module-ref (resolve-interface '(guix ui))
  346. 'guix-main)))
  347. #$(if source
  348. #~(begin
  349. (bindtextdomain "guix"
  350. #$(locale-data source "guix"))
  351. (bindtextdomain "guix-packages"
  352. #$(locale-data source
  353. "guix-packages"
  354. "packages")))
  355. #t)
  356. ;; XXX: It would be more convenient to change it to:
  357. ;; (exit (apply guix-main (command-line)))
  358. (apply guix-main (command-line))))
  359. #:guile guile))
  360. (define* (whole-package name modules dependencies
  361. #:key
  362. (guile-version (effective-version))
  363. compiled-modules
  364. info daemon guile
  365. (command (guix-command modules
  366. #:dependencies dependencies
  367. #:guile guile
  368. #:guile-version guile-version)))
  369. "Return the whole Guix package NAME that uses MODULES, a derivation of all
  370. the modules, and DEPENDENCIES, a list of packages depended on. COMMAND is the
  371. 'guix' program to use; INFO is the Info manual. When COMPILED-MODULES is
  372. true, it is linked as 'lib/guile/X.Y/site-ccache'; otherwise, .go files are
  373. assumed to be part of MODULES."
  374. (computed-file name
  375. (with-imported-modules '((guix build utils))
  376. #~(begin
  377. (use-modules (guix build utils))
  378. (mkdir-p (string-append #$output "/bin"))
  379. (symlink #$command
  380. (string-append #$output "/bin/guix"))
  381. (when #$daemon
  382. (symlink (string-append #$daemon "/bin/guix-daemon")
  383. (string-append #$output "/bin/guix-daemon")))
  384. (let ((modules (string-append #$output
  385. "/share/guile/site/"
  386. (effective-version)))
  387. (info #$info))
  388. (mkdir-p (dirname modules))
  389. (symlink #$modules modules)
  390. (when info
  391. (symlink #$info
  392. (string-append #$output
  393. "/share/info"))))
  394. ;; Object files.
  395. (when #$compiled-modules
  396. (let ((modules (string-append #$output "/lib/guile/"
  397. (effective-version)
  398. "/site-ccache")))
  399. (mkdir-p (dirname modules))
  400. (symlink #$compiled-modules modules)))))))
  401. (define* (compiled-guix source #:key (version %guix-version)
  402. (pull-version 1)
  403. (name (string-append "guix-" version))
  404. (guile-version (effective-version))
  405. (guile-for-build (guile-for-build guile-version))
  406. (zlib (specification->package "zlib"))
  407. (gzip (specification->package "gzip"))
  408. (bzip2 (specification->package "bzip2"))
  409. (xz (specification->package "xz"))
  410. (guix (specification->package "guix")))
  411. "Return a file-like object that contains a compiled Guix."
  412. (define guile-json
  413. (package-for-guile guile-version
  414. "guile-json"
  415. "guile2.0-json"))
  416. (define guile-ssh
  417. (package-for-guile guile-version
  418. "guile-ssh"
  419. "guile2.0-ssh"))
  420. (define guile-git
  421. (package-for-guile guile-version
  422. "guile-git"
  423. "guile2.0-git"))
  424. (define guile-sqlite3
  425. (package-for-guile guile-version
  426. "guile-sqlite3"
  427. "guile2.0-sqlite3"))
  428. (define guile-gcrypt
  429. (package-for-guile guile-version
  430. "guile-gcrypt"))
  431. (define gnutls
  432. (package-for-guile guile-version
  433. "gnutls" "guile2.0-gnutls"))
  434. (define dependencies
  435. (match (append-map (lambda (package)
  436. (cons (list "x" package)
  437. (package-transitive-propagated-inputs package)))
  438. (list guile-gcrypt gnutls guile-git guile-json
  439. guile-ssh guile-sqlite3))
  440. (((labels packages _ ...) ...)
  441. packages)))
  442. (define *core-modules*
  443. (scheme-node "guix-core"
  444. '((guix)
  445. (guix monad-repl)
  446. (guix packages)
  447. (guix download)
  448. (guix discovery)
  449. (guix profiles)
  450. (guix build-system gnu)
  451. (guix build-system trivial)
  452. (guix build profiles)
  453. (guix build gnu-build-system))
  454. ;; Provide a dummy (guix config) with the default version
  455. ;; number, storedir, etc. This is so that "guix-core" is the
  456. ;; same across all installations and doesn't need to be
  457. ;; rebuilt when the version changes, which in turn means we
  458. ;; can have substitutes for it.
  459. #:extra-modules
  460. `(((guix config) => ,(make-config.scm)))
  461. ;; (guix man-db) is needed at build-time by (guix profiles)
  462. ;; but we don't need to compile it; not compiling it allows
  463. ;; us to avoid an extra dependency on guile-gdbm-ffi.
  464. #:extra-files
  465. `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
  466. ("guix/store/schema.sql"
  467. ,(local-file "../guix/store/schema.sql")))
  468. #:extensions (list guile-gcrypt)
  469. #:guile-for-build guile-for-build))
  470. (define *extra-modules*
  471. (scheme-node "guix-extra"
  472. (filter-map (match-lambda
  473. (('guix 'scripts _ ..1) #f)
  474. (('guix 'man-db) #f)
  475. (name name))
  476. (scheme-modules* source "guix"))
  477. (list *core-modules*)
  478. #:extensions dependencies
  479. #:guile-for-build guile-for-build))
  480. (define *core-package-modules*
  481. (scheme-node "guix-packages-base"
  482. `((gnu packages)
  483. (gnu packages base))
  484. (list *core-modules* *extra-modules*)
  485. #:extensions dependencies
  486. ;; Add all the non-Scheme files here. We must do it here so
  487. ;; that 'search-patches' & co. can find them. Ideally we'd
  488. ;; keep them next to the .scm files that use them but it's
  489. ;; difficult to do (XXX).
  490. #:extra-files
  491. (file-imports source "gnu/packages"
  492. (lambda (file stat)
  493. (and (eq? 'regular (stat:type stat))
  494. (not (string-suffix? ".scm" file))
  495. (not (string-suffix? ".go" file))
  496. (not (string-prefix? ".#" file))
  497. (not (string-suffix? "~" file)))))
  498. #:guile-for-build guile-for-build))
  499. (define *package-modules*
  500. (scheme-node "guix-packages"
  501. (scheme-modules* source "gnu/packages")
  502. (list *core-modules* *extra-modules* *core-package-modules*)
  503. #:extensions dependencies
  504. #:guile-for-build guile-for-build))
  505. (define *system-modules*
  506. (scheme-node "guix-system"
  507. `((gnu system)
  508. (gnu services)
  509. ,@(scheme-modules* source "gnu/system")
  510. ,@(scheme-modules* source "gnu/services"))
  511. (list *core-package-modules* *package-modules*
  512. *extra-modules* *core-modules*)
  513. #:extensions dependencies
  514. #:extra-files
  515. (append (file-imports source "gnu/system/examples"
  516. (const #t))
  517. ;; Build-side code that we don't build. Some of
  518. ;; these depend on guile-rsvg, the Shepherd, etc.
  519. (file-imports source "gnu/build" (const #t)))
  520. #:guile-for-build
  521. guile-for-build))
  522. (define *cli-modules*
  523. (scheme-node "guix-cli"
  524. (scheme-modules* source "/guix/scripts")
  525. (list *core-modules* *extra-modules*
  526. *core-package-modules* *package-modules*
  527. *system-modules*)
  528. #:extensions dependencies
  529. #:guile-for-build guile-for-build))
  530. (define *config*
  531. (scheme-node "guix-config"
  532. '()
  533. #:extra-modules
  534. `(((guix config)
  535. => ,(make-config.scm #:zlib zlib
  536. #:gzip gzip
  537. #:bzip2 bzip2
  538. #:xz xz
  539. #:package-name
  540. %guix-package-name
  541. #:package-version
  542. version
  543. #:bug-report-address
  544. %guix-bug-report-address
  545. #:home-page-url
  546. %guix-home-page-url)))
  547. #:guile-for-build guile-for-build))
  548. (define (built-modules node-subset)
  549. (directory-union (string-append name "-modules")
  550. (append-map node-subset
  551. ;; Note: *CONFIG* comes first so that it
  552. ;; overrides the (guix config) module that
  553. ;; comes with *CORE-MODULES*.
  554. (list *config*
  555. *cli-modules*
  556. *system-modules*
  557. *package-modules*
  558. *core-package-modules*
  559. *extra-modules*
  560. *core-modules*))
  561. ;; Silently choose the first entry upon collision so that
  562. ;; we choose *CONFIG*.
  563. #:resolve-collision 'first
  564. ;; When we do (add-to-store "utils.scm"), "utils.scm" must
  565. ;; be a regular file, not a symlink. Thus, arrange so that
  566. ;; regular files appear as regular files in the final
  567. ;; output.
  568. #:copy? #t
  569. #:quiet? #t))
  570. ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
  571. ;; Version 1 is when we return the full package.
  572. (cond ((= 1 pull-version)
  573. ;; The whole package, with a standard file hierarchy.
  574. (let* ((modules (built-modules (compose list node-source)))
  575. (compiled (built-modules (compose list node-compiled)))
  576. (command (guix-command modules compiled
  577. #:source source
  578. #:dependencies dependencies
  579. #:guile guile-for-build
  580. #:guile-version guile-version)))
  581. (whole-package name modules dependencies
  582. #:compiled-modules compiled
  583. #:command command
  584. #:guile guile-for-build
  585. ;; Include 'guix-daemon'. XXX: Here we inject an
  586. ;; older snapshot of guix-daemon, but that's a good
  587. ;; enough approximation for now.
  588. #:daemon (module-ref (resolve-interface
  589. '(gnu packages
  590. package-management))
  591. 'guix-daemon)
  592. #:info (info-manual source)
  593. #:guile-version guile-version)))
  594. ((= 0 pull-version)
  595. ;; Legacy 'guix pull': return the .scm and .go files as one
  596. ;; directory.
  597. (built-modules (lambda (node)
  598. (list (node-source node)
  599. (node-compiled node)))))
  600. (else
  601. ;; Unsupported 'guix pull' version.
  602. #f)))
  603. ;;;
  604. ;;; Generating (guix config).
  605. ;;;
  606. (define %dependency-variables
  607. ;; (guix config) variables corresponding to dependencies.
  608. '(%libz %xz %gzip %bzip2))
  609. (define %persona-variables
  610. ;; (guix config) variables that define Guix's persona.
  611. '(%guix-package-name
  612. %guix-version
  613. %guix-bug-report-address
  614. %guix-home-page-url))
  615. (define %config-variables
  616. ;; (guix config) variables corresponding to Guix configuration.
  617. (letrec-syntax ((variables (syntax-rules ()
  618. ((_)
  619. '())
  620. ((_ variable rest ...)
  621. (cons `(variable . ,variable)
  622. (variables rest ...))))))
  623. (variables %localstatedir %storedir %sysconfdir %system)))
  624. (define* (make-config.scm #:key zlib gzip xz bzip2
  625. (package-name "GNU Guix")
  626. (package-version "0")
  627. (bug-report-address "bug-guix@gnu.org")
  628. (home-page-url "https://gnu.org/s/guix"))
  629. ;; Hack so that Geiser is not confused.
  630. (define defmod 'define-module)
  631. (scheme-file "config.scm"
  632. #~(;; The following expressions get spliced.
  633. (#$defmod (guix config)
  634. #:export (%guix-package-name
  635. %guix-version
  636. %guix-bug-report-address
  637. %guix-home-page-url
  638. %store-directory
  639. %state-directory
  640. %store-database-directory
  641. %config-directory
  642. %libz
  643. %gzip
  644. %bzip2
  645. %xz))
  646. #$@(map (match-lambda
  647. ((name . value)
  648. #~(define-public #$name #$value)))
  649. %config-variables)
  650. (define %store-directory
  651. (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
  652. %storedir))
  653. (define %state-directory
  654. ;; This must match `NIX_STATE_DIR' as defined in
  655. ;; `nix/local.mk'.
  656. (or (getenv "NIX_STATE_DIR")
  657. (string-append %localstatedir "/guix")))
  658. (define %store-database-directory
  659. (or (getenv "NIX_DB_DIR")
  660. (string-append %state-directory "/db")))
  661. (define %config-directory
  662. ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
  663. ;; defined in `nix/local.mk'.
  664. (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
  665. (string-append %sysconfdir "/guix")))
  666. (define %guix-package-name #$package-name)
  667. (define %guix-version #$package-version)
  668. (define %guix-bug-report-address #$bug-report-address)
  669. (define %guix-home-page-url #$home-page-url)
  670. (define %gzip
  671. #+(and gzip (file-append gzip "/bin/gzip")))
  672. (define %bzip2
  673. #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
  674. (define %xz
  675. #+(and xz (file-append xz "/bin/xz")))
  676. (define %libz
  677. #+(and zlib
  678. (file-append zlib "/lib/libz"))))
  679. ;; Guile 2.0 *requires* the 'define-module' to be at the
  680. ;; top-level or the 'toplevel-ref' in the resulting .go file are
  681. ;; made relative to a nonexistent anonymous module.
  682. #:splice? #t))
  683. ;;;
  684. ;;; Building.
  685. ;;;
  686. (define* (compiled-modules name module-tree module-files
  687. #:optional
  688. (dependencies '())
  689. (dependencies-compiled '())
  690. #:key
  691. (extensions '()) ;full-blown Guile packages
  692. parallel?
  693. guile-for-build)
  694. "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
  695. like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
  696. containing MODULE-FILES and possibly other files as well."
  697. ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
  698. ;; gexp).
  699. (define build
  700. (with-imported-modules (source-module-closure
  701. '((guix build compile)
  702. (guix build utils)))
  703. #~(begin
  704. (use-modules (srfi srfi-26)
  705. (ice-9 match)
  706. (ice-9 format)
  707. (ice-9 threads)
  708. (guix build compile)
  709. (guix build utils))
  710. (define (regular? file)
  711. (not (member file '("." ".."))))
  712. (define (report-load file total completed)
  713. (display #\cr)
  714. (format #t
  715. "loading...\t~5,1f% of ~d files" ;FIXME: i18n
  716. (* 100. (/ completed total)) total)
  717. (force-output))
  718. (define (report-compilation file total completed)
  719. (display #\cr)
  720. (format #t "compiling...\t~5,1f% of ~d files" ;FIXME: i18n
  721. (* 100. (/ completed total)) total)
  722. (force-output))
  723. (define (process-directory directory files output)
  724. ;; Hide compilation warnings.
  725. (parameterize ((current-warning-port (%make-void-port "w")))
  726. (compile-files directory #$output files
  727. #:workers (parallel-job-count)
  728. #:report-load report-load
  729. #:report-compilation report-compilation)))
  730. (setvbuf (current-output-port) _IONBF)
  731. (setvbuf (current-error-port) _IONBF)
  732. (set! %load-path (cons #+module-tree %load-path))
  733. (set! %load-path
  734. (append '#+dependencies
  735. (map (lambda (extension)
  736. (string-append extension "/share/guile/site/"
  737. (effective-version)))
  738. '#+extensions)
  739. %load-path))
  740. (set! %load-compiled-path
  741. (append '#+dependencies-compiled
  742. (map (lambda (extension)
  743. (string-append extension "/lib/guile/"
  744. (effective-version)
  745. "/site-ccache"))
  746. '#+extensions)
  747. %load-compiled-path))
  748. ;; Load the compiler modules upfront.
  749. (compile #f)
  750. (mkdir #$output)
  751. (chdir #+module-tree)
  752. (process-directory "." '#+module-files #$output)
  753. (newline))))
  754. (computed-file name build
  755. #:guile guile-for-build
  756. #:options
  757. `(#:local-build? #f ;allow substitutes
  758. ;; Don't annoy people about _IONBF deprecation.
  759. ;; Initialize 'terminal-width' in (system repl debug)
  760. ;; to a large-enough value to make backtrace more
  761. ;; verbose.
  762. #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
  763. ("COLUMNS" . "200")))))
  764. ;;;
  765. ;;; Building.
  766. ;;;
  767. (define (guile-for-build version)
  768. "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
  769. running Guile."
  770. (define canonical-package ;soft reference
  771. (module-ref (resolve-interface '(gnu packages base))
  772. 'canonical-package))
  773. (match version
  774. ("2.2.2"
  775. ;; Gross hack to avoid ABI incompatibilities (see
  776. ;; <https://bugs.gnu.org/29570>.)
  777. (module-ref (resolve-interface '(gnu packages guile))
  778. 'guile-2.2.2))
  779. ("2.2"
  780. ;; Use the latest version, which has fixes for
  781. ;; <https://bugs.gnu.org/30602> and VM stack-marking issues.
  782. (canonical-package (module-ref (resolve-interface '(gnu packages guile))
  783. 'guile-2.2.4)))
  784. ("2.0"
  785. (module-ref (resolve-interface '(gnu packages guile))
  786. 'guile-2.0))))
  787. (define* (guix-derivation source version
  788. #:optional (guile-version (effective-version))
  789. #:key (pull-version 0))
  790. "Return, as a monadic value, the derivation to build the Guix from SOURCE
  791. for GUILE-VERSION. Use VERSION as the version string. PULL-VERSION specifies
  792. the version of the 'guix pull' protocol. Return #f if this PULL-VERSION value
  793. is not supported."
  794. (define (shorten version)
  795. (if (and (string-every char-set:hex-digit version)
  796. (> (string-length version) 9))
  797. (string-take version 9) ;Git commit
  798. version))
  799. (define guile
  800. (guile-for-build guile-version))
  801. (mbegin %store-monad
  802. (set-guile-for-build guile)
  803. (let ((guix (compiled-guix source
  804. #:version version
  805. #:name (string-append "guix-"
  806. (shorten version))
  807. #:pull-version pull-version
  808. #:guile-version (match guile-version
  809. ("2.2.2" "2.2")
  810. (version version))
  811. #:guile-for-build guile)))
  812. (if guix
  813. (lower-object guix)
  814. (return #f)))))