build-self.scm 20 KB

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