build-self.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2016, 2017, 2018, 2019 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 (build-self)
  19. #:use-module (gnu)
  20. #:use-module (guix)
  21. #:use-module (guix ui)
  22. #:use-module (guix config)
  23. #:use-module (guix modules)
  24. #:use-module (guix build-system gnu)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-19)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (srfi srfi-35)
  29. #:use-module (rnrs io ports)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 popen)
  32. #:export (build))
  33. ;;; Commentary:
  34. ;;;
  35. ;;; When loaded, this module returns a monadic procedure of at least one
  36. ;;; argument: the source tree to build. It returns a derivation that
  37. ;;; builds it.
  38. ;;;
  39. ;;; This file uses modules provided by the already-installed Guix. Those
  40. ;;; modules may be arbitrarily old compared to the version we want to
  41. ;;; build. Because of that, it must rely on the smallest set of features
  42. ;;; that are likely to be provided by the (guix) and (gnu) modules, and by
  43. ;;; Guile itself, forever and ever.
  44. ;;;
  45. ;;; Code:
  46. ;;;
  47. ;;; Generating (guix config).
  48. ;;;
  49. ;;; This is copied from (guix self) because we cannot assume (guix self) is
  50. ;;; available at this point.
  51. ;;;
  52. (define %dependency-variables
  53. ;; (guix config) variables corresponding to dependencies.
  54. '(%libgcrypt %libz %xz %gzip %bzip2))
  55. (define %persona-variables
  56. ;; (guix config) variables that define Guix's persona.
  57. '(%guix-package-name
  58. %guix-version
  59. %guix-bug-report-address
  60. %guix-home-page-url))
  61. (define %config-variables
  62. ;; (guix config) variables corresponding to Guix configuration.
  63. (letrec-syntax ((variables (syntax-rules ()
  64. ((_)
  65. '())
  66. ((_ variable rest ...)
  67. (cons `(variable . ,variable)
  68. (variables rest ...))))))
  69. (variables %localstatedir %storedir %sysconfdir %system)))
  70. (define* (make-config.scm #:key zlib gzip xz bzip2
  71. (package-name "GNU Guix")
  72. (package-version "0")
  73. (bug-report-address "bug-guix@gnu.org")
  74. (home-page-url "https://gnu.org/s/guix"))
  75. ;; Hack so that Geiser is not confused.
  76. (define defmod 'define-module)
  77. (scheme-file "config.scm"
  78. #~(begin
  79. (#$defmod (guix config)
  80. #:export (%guix-package-name
  81. %guix-version
  82. %guix-bug-report-address
  83. %guix-home-page-url
  84. %store-directory
  85. %state-directory
  86. %store-database-directory
  87. %config-directory
  88. %libz
  89. %gzip
  90. %bzip2
  91. %xz))
  92. ;; XXX: Work around <http://bugs.gnu.org/15602>.
  93. (eval-when (expand load eval)
  94. #$@(map (match-lambda
  95. ((name . value)
  96. #~(define-public #$name #$value)))
  97. %config-variables)
  98. (define %store-directory
  99. (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
  100. %storedir))
  101. (define %state-directory
  102. ;; This must match `NIX_STATE_DIR' as defined in
  103. ;; `nix/local.mk'.
  104. (or (getenv "GUIX_STATE_DIRECTORY")
  105. (string-append %localstatedir "/guix")))
  106. (define %store-database-directory
  107. (or (getenv "GUIX_DATABASE_DIRECTORY")
  108. (string-append %state-directory "/db")))
  109. (define %config-directory
  110. ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
  111. ;; defined in `nix/local.mk'.
  112. (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
  113. (string-append %sysconfdir "/guix")))
  114. (define %guix-package-name #$package-name)
  115. (define %guix-version #$package-version)
  116. (define %guix-bug-report-address #$bug-report-address)
  117. (define %guix-home-page-url #$home-page-url)
  118. (define %gzip
  119. #+(and gzip (file-append gzip "/bin/gzip")))
  120. (define %bzip2
  121. #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
  122. (define %xz
  123. #+(and xz (file-append xz "/bin/xz")))
  124. (define %libz
  125. #+(and zlib
  126. (file-append zlib "/lib/libz")))))))
  127. ;;;
  128. ;;; 'gexp->script'.
  129. ;;;
  130. ;;; This is our own variant of 'gexp->script' with an extra #:module-path
  131. ;;; parameter, which was unavailable in (guix gexp) until commit
  132. ;;; 1ae16033f34cebe802023922436883867010850f (March 2018.)
  133. ;;;
  134. (define (load-path-expression modules path)
  135. "Return as a monadic value a gexp that sets '%load-path' and
  136. '%load-compiled-path' to point to MODULES, a list of module names. MODULES
  137. are searched for in PATH."
  138. (mlet %store-monad ((modules (imported-modules modules
  139. #:module-path path))
  140. (compiled (compiled-modules modules
  141. #:module-path path)))
  142. (return (gexp (eval-when (expand load eval)
  143. (set! %load-path
  144. (cons (ungexp modules) %load-path))
  145. (set! %load-compiled-path
  146. (cons (ungexp compiled)
  147. %load-compiled-path)))))))
  148. (define* (gexp->script name exp
  149. #:key (guile (default-guile))
  150. (module-path %load-path))
  151. "Return an executable script NAME that runs EXP using GUILE, with EXP's
  152. imported modules in its search path."
  153. (mlet %store-monad ((set-load-path
  154. (load-path-expression (gexp-modules exp)
  155. module-path)))
  156. (gexp->derivation name
  157. (gexp
  158. (call-with-output-file (ungexp output)
  159. (lambda (port)
  160. ;; Note: that makes a long shebang. When the store
  161. ;; is /gnu/store, that fits within the 128-byte
  162. ;; limit imposed by Linux, but that may go beyond
  163. ;; when running tests.
  164. (format port
  165. "#!~a/bin/guile --no-auto-compile~%!#~%"
  166. (ungexp guile))
  167. (write '(ungexp set-load-path) port)
  168. (write '(ungexp exp) port)
  169. (chmod port #o555))))
  170. #:module-path module-path)))
  171. (define (date-version-string)
  172. "Return the current date and hour in UTC timezone, for use as a poor
  173. person's version identifier."
  174. ;; XXX: Replace with a Git commit id.
  175. (date->string (current-date 0) "~Y~m~d.~H"))
  176. (define guile-gcrypt
  177. ;; The host Guix may or may not have 'guile-gcrypt', which was introduced in
  178. ;; August 2018. If it has it, it's at least version 0.1.0, which is good
  179. ;; enough. If it doesn't, specify our own package because the target Guix
  180. ;; requires it.
  181. (match (find-best-packages-by-name "guile-gcrypt" #f)
  182. (()
  183. (package
  184. (name "guile-gcrypt")
  185. (version "0.1.0")
  186. (home-page "https://notabug.org/cwebber/guile-gcrypt")
  187. (source (origin
  188. (method url-fetch)
  189. (uri (string-append home-page "/archive/v" version ".tar.gz"))
  190. (sha256
  191. (base32
  192. "1gir7ifknbmbvjlql5j6wzk7bkb5lnmq80q59ngz43hhpclrk5k3"))
  193. (file-name (string-append name "-" version ".tar.gz"))))
  194. (build-system gnu-build-system)
  195. (arguments
  196. ;; The 'bootstrap' phase appeared in 'core-updates', which was merged
  197. ;; into 'master' ca. June 2018.
  198. '(#:phases (modify-phases %standard-phases
  199. (delete 'bootstrap)
  200. (add-before 'configure 'bootstrap
  201. (lambda _
  202. (unless (zero? (system* "autoreconf" "-vfi"))
  203. (error "autoreconf failed"))
  204. #t)))))
  205. (native-inputs
  206. `(("pkg-config" ,(specification->package "pkg-config"))
  207. ("autoconf" ,(specification->package "autoconf"))
  208. ("automake" ,(specification->package "automake"))
  209. ("texinfo" ,(specification->package "texinfo"))))
  210. (inputs
  211. `(("guile" ,(specification->package "guile"))
  212. ("libgcrypt" ,(specification->package "libgcrypt"))))
  213. (synopsis "Cryptography library for Guile using Libgcrypt")
  214. (description
  215. "Guile-Gcrypt provides a Guile 2.x interface to a subset of the
  216. GNU Libgcrypt crytographic library. It provides modules for cryptographic
  217. hash functions, message authentication codes (MAC), public-key cryptography,
  218. strong randomness, and more. It is implemented using the foreign function
  219. interface (FFI) of Guile.")
  220. (license #f))) ;license:gpl3+
  221. ((package . _)
  222. package)))
  223. (define* (build-program source version
  224. #:optional (guile-version (effective-version))
  225. #:key (pull-version 0))
  226. "Return a program that computes the derivation to build Guix from SOURCE."
  227. (define select?
  228. ;; Select every module but (guix config) and non-Guix modules.
  229. (match-lambda
  230. (('guix 'config) #f)
  231. (('guix _ ...) #t)
  232. (('gnu _ ...) #t)
  233. (_ #f)))
  234. (define fake-gcrypt-hash
  235. ;; Fake (gcrypt hash) module; see below.
  236. (scheme-file "hash.scm"
  237. #~(define-module (gcrypt hash)
  238. #:export (sha1 sha256))))
  239. (define fake-git
  240. (scheme-file "git.scm" #~(define-module (git))))
  241. (with-imported-modules `(((guix config)
  242. => ,(make-config.scm))
  243. ;; To avoid relying on 'with-extensions', which was
  244. ;; introduced in 0.15.0, provide a fake (gcrypt
  245. ;; hash) just so that we can build modules, and
  246. ;; adjust %LOAD-PATH later on.
  247. ((gcrypt hash) => ,fake-gcrypt-hash)
  248. ;; (guix git-download) depends on (git) but only
  249. ;; for peripheral functionality. Provide a dummy
  250. ;; (git) to placate it.
  251. ((git) => ,fake-git)
  252. ,@(source-module-closure `((guix store)
  253. (guix self)
  254. (guix derivations)
  255. (gnu packages bootstrap))
  256. (list source)
  257. #:select? select?))
  258. (gexp->script "compute-guix-derivation"
  259. #~(begin
  260. (use-modules (ice-9 match))
  261. (eval-when (expand load eval)
  262. ;; (gnu packages …) modules are going to be looked up
  263. ;; under SOURCE. (guix config) is looked up in FRONT.
  264. (match (command-line)
  265. ((_ source _ ...)
  266. (match %load-path
  267. ((front _ ...)
  268. (unless (string=? front source) ;already done?
  269. (set! %load-path
  270. (list source
  271. (string-append #$guile-gcrypt
  272. "/share/guile/site/"
  273. (effective-version))
  274. front)))))))
  275. ;; Only load Guile-Gcrypt, our own modules, or those
  276. ;; of Guile.
  277. (set! %load-compiled-path
  278. (cons (string-append #$guile-gcrypt "/lib/guile/"
  279. (effective-version)
  280. "/site-ccache")
  281. %load-compiled-path)))
  282. (use-modules (guix store)
  283. (guix self)
  284. (guix derivations)
  285. (srfi srfi-1))
  286. (define (spin system)
  287. (define spin
  288. (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
  289. (format (current-error-port)
  290. "Computing Guix derivation for '~a'... "
  291. system)
  292. (when (isatty? (current-error-port))
  293. (let loop ((spin spin))
  294. (display (string-append "\b" (car spin))
  295. (current-error-port))
  296. (force-output (current-error-port))
  297. (sleep 1)
  298. (loop (cdr spin)))))
  299. (match (command-line)
  300. ((_ source system version protocol-version)
  301. ;; The current input port normally wraps a file
  302. ;; descriptor connected to the daemon, or it is
  303. ;; connected to /dev/null. In the former case, reuse
  304. ;; the connection such that we inherit build options
  305. ;; such as substitute URLs and so on; in the latter
  306. ;; case, attempt to open a new connection.
  307. (let* ((proto (string->number protocol-version))
  308. (store (if (integer? proto)
  309. (port->connection (duplicate-port
  310. (current-input-port)
  311. "w+0")
  312. #:version proto)
  313. (open-connection))))
  314. (call-with-new-thread
  315. (lambda ()
  316. (spin system)))
  317. (display
  318. (and=>
  319. (run-with-store store
  320. (guix-derivation source version
  321. #$guile-version
  322. #:pull-version
  323. #$pull-version)
  324. #:system system)
  325. derivation-file-name))))))
  326. #:module-path (list source))))
  327. (define (call-with-clean-environment thunk)
  328. (let ((env (environ)))
  329. (dynamic-wind
  330. (lambda ()
  331. (environ '()))
  332. thunk
  333. (lambda ()
  334. (environ env)))))
  335. (define-syntax-rule (with-clean-environment exp ...)
  336. "Evaluate EXP in a context where zero environment variables are defined."
  337. (call-with-clean-environment (lambda () exp ...)))
  338. ;; The procedure below is our return value.
  339. (define* (build source
  340. #:key verbose? (version (date-version-string)) system
  341. (pull-version 0)
  342. ;; For the standalone Guix, default to Guile 2.2. For old
  343. ;; versions of 'guix pull' (pre-0.15.0), we have to use the
  344. ;; same Guile as the current one.
  345. (guile-version (if (> pull-version 0)
  346. "2.2"
  347. (effective-version)))
  348. #:allow-other-keys
  349. #:rest rest)
  350. "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
  351. files."
  352. ;; Build the build program and then use it as a trampoline to build from
  353. ;; SOURCE.
  354. (mlet %store-monad ((build (build-program source version guile-version
  355. #:pull-version pull-version))
  356. (system (if system (return system) (current-system)))
  357. (port ((store-lift nix-server-socket)))
  358. (major ((store-lift nix-server-major-version)))
  359. (minor ((store-lift nix-server-minor-version))))
  360. (mbegin %store-monad
  361. (show-what-to-build* (list build))
  362. (built-derivations (list build))
  363. ;; Use the port beneath the current store as the stdin of BUILD. This
  364. ;; way, we know 'open-pipe*' will not close it on 'exec'. If PORT is
  365. ;; not a file port (e.g., it's an SSH channel), then the subprocess's
  366. ;; stdin will actually be /dev/null.
  367. (let* ((pipe (with-input-from-port port
  368. (lambda ()
  369. ;; Make sure BUILD is not influenced by
  370. ;; $GUILE_LOAD_PATH & co.
  371. (with-clean-environment
  372. (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
  373. (open-pipe* OPEN_READ
  374. (derivation->output-path build)
  375. source system version
  376. (if (file-port? port)
  377. (number->string
  378. (logior major minor))
  379. "none"))))))
  380. (str (get-string-all pipe))
  381. (status (close-pipe pipe)))
  382. (match str
  383. ((? eof-object?)
  384. (error "build program failed" (list build status)))
  385. ((? derivation-path? drv)
  386. (mbegin %store-monad
  387. (return (newline (current-error-port)))
  388. ((store-lift add-temp-root) drv)
  389. (return (read-derivation-from-file drv))))
  390. ("#f"
  391. ;; Unsupported PULL-VERSION.
  392. (return #f))
  393. ((? string? str)
  394. (raise (condition
  395. (&message
  396. (message (format #f "You found a bug: the program '~a'
  397. failed to compute the derivation for Guix (version: ~s; system: ~s;
  398. host version: ~s; pull-version: ~s).
  399. Please report it by email to <~a>.~%"
  400. (derivation->output-path build)
  401. version system %guix-version pull-version
  402. %guix-bug-report-address)))))))))))
  403. ;; This file is loaded by 'guix pull'; return it the build procedure.
  404. build
  405. ;; Local Variables:
  406. ;; eval: (put 'with-load-path 'scheme-indent-function 1)
  407. ;; End:
  408. ;;; build-self.scm ends here