build-self.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2016, 2017, 2018, 2019, 2020, 2021 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) (channel-metadata #f))
  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. ;; Also exclude (guix channels): it is autoloaded by (guix describe), but
  224. ;; only for peripheral functionality.
  225. (match-lambda
  226. (('guix 'config) #f)
  227. (('guix 'channels) #f)
  228. (('guix 'build 'download) #f) ;autoloaded by (guix download)
  229. (('guix _ ...) #t)
  230. (('gnu _ ...) #t)
  231. (_ #f)))
  232. (define fake-gcrypt-hash
  233. ;; Fake (gcrypt hash) module; see below.
  234. (scheme-file "hash.scm"
  235. #~(define-module (gcrypt hash)
  236. #:export (sha1 sha256))))
  237. (define fake-git
  238. (scheme-file "git.scm" #~(define-module (git))))
  239. (with-imported-modules `(((guix config)
  240. => ,(make-config.scm))
  241. ;; To avoid relying on 'with-extensions', which was
  242. ;; introduced in 0.15.0, provide a fake (gcrypt
  243. ;; hash) just so that we can build modules, and
  244. ;; adjust %LOAD-PATH later on.
  245. ((gcrypt hash) => ,fake-gcrypt-hash)
  246. ;; (guix git-download) depends on (git) but only
  247. ;; for peripheral functionality. Provide a dummy
  248. ;; (git) to placate it.
  249. ((git) => ,fake-git)
  250. ,@(source-module-closure `((guix store)
  251. (guix self)
  252. (guix derivations)
  253. (gnu packages bootstrap))
  254. (list source)
  255. #:select? select?))
  256. (gexp->script "compute-guix-derivation"
  257. #~(begin
  258. (use-modules (ice-9 match))
  259. (eval-when (expand load eval)
  260. ;; (gnu packages …) modules are going to be looked up
  261. ;; under SOURCE. (guix config) is looked up in FRONT.
  262. (match (command-line)
  263. ((_ source _ ...)
  264. (match %load-path
  265. ((front _ ...)
  266. (unless (string=? front source) ;already done?
  267. (set! %load-path
  268. (list source
  269. (string-append #$guile-gcrypt
  270. "/share/guile/site/"
  271. (effective-version))
  272. front)))))))
  273. ;; Only load Guile-Gcrypt, our own modules, or those
  274. ;; of Guile.
  275. (set! %load-compiled-path
  276. (cons (string-append #$guile-gcrypt "/lib/guile/"
  277. (effective-version)
  278. "/site-ccache")
  279. %load-compiled-path))
  280. ;; Disable position recording to save time and space
  281. ;; when loading the package modules.
  282. (read-disable 'positions))
  283. (use-modules (guix store)
  284. (guix self)
  285. (guix derivations)
  286. (srfi srfi-1))
  287. (match (command-line)
  288. ((_ source system version protocol-version
  289. build-output)
  290. ;; The current input port normally wraps a file
  291. ;; descriptor connected to the daemon, or it is
  292. ;; connected to /dev/null. In the former case, reuse
  293. ;; the connection such that we inherit build options
  294. ;; such as substitute URLs and so on; in the latter
  295. ;; case, attempt to open a new connection.
  296. (let* ((proto (string->number protocol-version))
  297. (store (if (integer? proto)
  298. (port->connection (duplicate-port
  299. (current-input-port)
  300. "w+0")
  301. #:version proto)
  302. (open-connection)))
  303. (sock (socket AF_UNIX SOCK_STREAM 0)))
  304. ;; Connect to BUILD-OUTPUT and send it the raw
  305. ;; build output.
  306. (connect sock AF_UNIX build-output)
  307. (display
  308. (and=>
  309. ;; Silence autoload warnings and the likes.
  310. (parameterize ((current-warning-port
  311. (%make-void-port "w"))
  312. (current-build-output-port sock))
  313. (run-with-store store
  314. (guix-derivation source version
  315. #$guile-version
  316. #:channel-metadata
  317. '#$channel-metadata
  318. #:pull-version
  319. #$pull-version)
  320. #:system system))
  321. derivation-file-name))))))
  322. #:module-path (list source))))
  323. (define (proxy input output)
  324. "Dump the contents of INPUT to OUTPUT until EOF is reached on INPUT.
  325. Display a spinner when nothing happens."
  326. (define spin
  327. (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
  328. (setvbuf input 'block 16384)
  329. (let loop ((spin spin))
  330. (match (select (list input) '() '() 1)
  331. ((() () ())
  332. (when (isatty? (current-error-port))
  333. (display (string-append "\b" (car spin))
  334. (current-error-port))
  335. (force-output (current-error-port)))
  336. (loop (cdr spin)))
  337. (((_) () ())
  338. ;; Read from INPUT as much as can be read without blocking.
  339. (let ((bv (get-bytevector-some input)))
  340. (unless (eof-object? bv)
  341. (put-bytevector output bv)
  342. (loop spin)))))))
  343. (define (call-with-clean-environment thunk)
  344. (let ((env (environ)))
  345. (dynamic-wind
  346. (lambda ()
  347. (environ '()))
  348. thunk
  349. (lambda ()
  350. (environ env)))))
  351. (define-syntax-rule (with-clean-environment exp ...)
  352. "Evaluate EXP in a context where zero environment variables are defined."
  353. (call-with-clean-environment (lambda () exp ...)))
  354. ;; The procedure below is our return value.
  355. (define* (build source
  356. #:key verbose?
  357. (version (date-version-string)) channel-metadata
  358. system
  359. (pull-version 0)
  360. ;; For the standalone Guix, default to Guile 3.0. For old
  361. ;; versions of 'guix pull' (pre-0.15.0), we have to use the
  362. ;; same Guile as the current one.
  363. (guile-version (if (> pull-version 0)
  364. "3.0"
  365. (effective-version)))
  366. #:allow-other-keys
  367. #:rest rest)
  368. "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
  369. files."
  370. ;; Build the build program and then use it as a trampoline to build from
  371. ;; SOURCE.
  372. (mlet %store-monad ((build (build-program source version guile-version
  373. #:channel-metadata channel-metadata
  374. #:pull-version pull-version))
  375. (system (if system (return system) (current-system)))
  376. (home -> (getenv "HOME"))
  377. ;; Note: Use the deprecated names here because the
  378. ;; caller might be Guix <= 0.16.0.
  379. (port ((store-lift nix-server-socket)))
  380. (major ((store-lift nix-server-major-version)))
  381. (minor ((store-lift nix-server-minor-version))))
  382. (mbegin %store-monad
  383. ;; Before 'with-build-handler' was implemented and used, we had to
  384. ;; explicitly call 'show-what-to-build*'.
  385. (munless (module-defined? (resolve-module '(guix store))
  386. 'with-build-handler)
  387. (show-what-to-build* (list build)))
  388. (built-derivations (list build))
  389. ;; Use the port beneath the current store as the stdin of BUILD. This
  390. ;; way, we know 'open-pipe*' will not close it on 'exec'. If PORT is
  391. ;; not a file port (e.g., it's an SSH channel), then the subprocess's
  392. ;; stdin will actually be /dev/null.
  393. (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
  394. (node (let ((file (string-append (or (getenv "TMPDIR") "/tmp")
  395. "/guix-build-output-"
  396. (number->string (getpid)))))
  397. (bind sock AF_UNIX file)
  398. (listen sock 1)
  399. file))
  400. (pipe (with-input-from-port port
  401. (lambda ()
  402. ;; Make sure BUILD is not influenced by
  403. ;; $GUILE_LOAD_PATH & co.
  404. (with-clean-environment
  405. (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
  406. (setenv "COLUMNS" "120") ;show wider backtraces
  407. (when home
  408. ;; Inherit HOME so that 'xdg-directory' works.
  409. (setenv "HOME" home))
  410. (open-pipe* OPEN_READ
  411. (derivation->output-path build)
  412. source system version
  413. (if (file-port? port)
  414. (number->string
  415. (logior major minor))
  416. "none")
  417. node))))))
  418. (format (current-error-port) "Computing Guix derivation for '~a'... "
  419. system)
  420. ;; Wait for a connection on SOCK and proxy build output so it can be
  421. ;; processed according to the settings currently in effect (build
  422. ;; traces, verbosity level, and so on).
  423. (match (accept sock)
  424. ((port . _)
  425. (close-port sock)
  426. (delete-file node)
  427. (proxy port (current-build-output-port))))
  428. ;; Now that the build output connection was closed, read the result, a
  429. ;; derivation file name, from PIPE.
  430. (let ((str (get-string-all pipe))
  431. (status (close-pipe pipe)))
  432. (match str
  433. ((? eof-object?)
  434. (error "build program failed" (list build status)))
  435. ((? derivation-path? drv)
  436. (mbegin %store-monad
  437. (return (newline (current-error-port)))
  438. ((store-lift add-temp-root) drv)
  439. (return (read-derivation-from-file drv))))
  440. ("#f"
  441. ;; Unsupported PULL-VERSION.
  442. (return #f))
  443. ((? string? str)
  444. (raise (condition
  445. (&message
  446. (message (format #f "You found a bug: the program '~a'
  447. failed to compute the derivation for Guix (version: ~s; system: ~s;
  448. host version: ~s; pull-version: ~s).
  449. Please report the COMPLETE output above by email to <~a>.~%"
  450. (derivation->output-path build)
  451. version system %guix-version pull-version
  452. %guix-bug-report-address))))))))))))
  453. ;; This file is loaded by 'guix pull'; return it the build procedure.
  454. build
  455. ;; Local Variables:
  456. ;; eval: (put 'with-load-path 'scheme-indent-function 1)
  457. ;; End:
  458. ;;; build-self.scm ends here