1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (build-self)
22 #:use-module (guix config)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-19)
25 #:use-module (ice-9 match)
30 ;;; When loaded, this module returns a monadic procedure of at least one
31 ;;; argument: the source tree to build. It returns a derivation that
34 ;;; This file uses modules provided by the already-installed Guix. Those
35 ;;; modules may be arbitrarily old compared to the version we want to
36 ;;; build. Because of that, it must rely on the smallest set of features
37 ;;; that are likely to be provided by the (guix) and (gnu) modules, and by
38 ;;; Guile itself, forever and ever.
43 ;; The dependencies. Don't refer explicitly to the variables because they
44 ;; could be renamed or shuffled around in modules over time. Conversely,
45 ;; 'find-best-packages-by-name' is expected to always have the same semantics.
48 (first (find-best-packages-by-name "libgcrypt" #f)))
51 (first (find-best-packages-by-name "zlib" #f)))
54 (first (find-best-packages-by-name "gzip" #f)))
57 (first (find-best-packages-by-name "bzip2" #f)))
60 (first (find-best-packages-by-name "xz" #f)))
62 (define (false-if-wrong-guile package)
63 "Return #f if PACKAGE depends on the \"wrong\" major version of Guile (e.g.,
64 2.0 instead of 2.2), otherwise return PACKAGE."
65 (let ((guile (any (match-lambda
66 ((label (? package? dep) _ ...)
67 (and (string=? (package-name dep) "guile")
69 (package-direct-inputs package))))
71 (string-prefix? (effective-version)
72 (package-version guile)))
75 (define (package-for-current-guile . names)
76 "Return the package with one of the given NAMES that depends on the current
77 Guile major version (2.0 or 2.2), or #f if none of the packages matches."
78 (let loop ((names names))
83 (match (find-best-packages-by-name name #f)
87 (or (false-if-wrong-guile first)
91 (package-for-current-guile "guile-json"
96 (package-for-current-guile "guile-ssh"
101 ;; The actual build procedure.
103 (define (top-source-directory)
104 "Return the name of the top-level directory of this source tree."
105 (and=> (assoc-ref (current-source-location) 'filename)
107 (string-append (dirname file) "/.."))))
110 (define (date-version-string)
111 "Return the current date and hour in UTC timezone, for use as a poor
112 person's version identifier."
113 ;; XXX: Replace with a Git commit id.
114 (date->string (current-date 0) "~Y~m~d.~H"))
116 (define (guile-for-build)
117 "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
119 (package->derivation (cond-expand
122 (specification->package "guile@2.2")))
125 (specification->package "guile@2.0"))))))
127 ;; The procedure below is our return value.
128 (define* (build source
129 #:key verbose? (version (date-version-string))
132 "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
134 ;; The '%xxxdir' variables were added to (guix config) in July 2016 so we
135 ;; cannot assume that they are defined. Try to guess their value when
136 ;; they're undefined (XXX: we get an incorrect guess when environment
137 ;; variables such as 'NIX_STATE_DIR' are defined!).
139 (if (defined? '%storedir) %storedir %store-directory))
140 (define localstatedir
141 (if (defined? '%localstatedir) %localstatedir (dirname %state-directory)))
143 (if (defined? '%sysconfdir) %sysconfdir (dirname %config-directory)))
145 (if (defined? '%sbindir) %sbindir (dirname %guix-register-program)))
149 (use-modules (guix build pull))
151 (let ((json (string-append #$guile-json "/share/guile/site/"
152 #$(effective-version))))
155 (string-append #$guile-ssh "/share/guile/site/"
156 #$(effective-version))
158 (set! %load-compiled-path
160 (string-append #$guile-ssh "/lib/guile/"
161 #$(effective-version)
163 %load-compiled-path)))
165 ;; XXX: The 'guile-ssh' package prior to Guix commit 92b7258 was
166 ;; broken: libguile-ssh could not be found. Work around that.
167 ;; FIXME: We want Guile-SSH 0.10.2 or later anyway.
168 #$(if (string-prefix? "0.9." (package-version guile-ssh))
169 #~(setenv "LTDL_LIBRARY_PATH" (string-append #$guile-ssh "/lib"))
172 (build-guix #$output #$source
175 #:storedir #$storedir
176 #:localstatedir #$localstatedir
177 #:sysconfdir #$sysconfdir
180 #:package-name #$%guix-package-name
181 #:package-version #$version
182 #:bug-report-address #$%guix-bug-report-address
183 #:home-page-url #$%guix-home-page-url
185 #:libgcrypt #$libgcrypt
191 ;; XXX: This is not perfect, enabling VERBOSE? means
192 ;; building a different derivation.
193 #:debug-port (if #$verbose?
195 (%make-void-port "w")))))
197 (mlet %store-monad ((guile (guile-for-build)))
198 (gexp->derivation "guix-latest" builder
199 #:modules '((guix build pull)
202 ;; Closure of (guix modules).
207 ;; Arrange so that our own (guix build …) modules are
209 #:module-path (list (top-source-directory))
211 #:guile-for-build guile)))
213 ;; This file is loaded by 'guix pull'; return it the build procedure.
217 ;; eval: (put 'with-load-path 'scheme-indent-function 1)
220 ;;; build-self.scm ends here