build-self.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 _ ...) #t)
  229. (('gnu _ ...) #t)
  230. (_ #f)))
  231. (define fake-gcrypt-hash
  232. ;; Fake (gcrypt hash) module; see below.
  233. (scheme-file "hash.scm"
  234. #~(define-module (gcrypt hash)
  235. #:export (sha1 sha256))))
  236. (define fake-git
  237. (scheme-file "git.scm" #~(define-module (git))))
  238. (with-imported-modules `(((guix config)
  239. => ,(make-config.scm))
  240. ;; To avoid relying on 'with-extensions', which was
  241. ;; introduced in 0.15.0, provide a fake (gcrypt
  242. ;; hash) just so that we can build modules, and
  243. ;; adjust %LOAD-PATH later on.
  244. ((gcrypt hash) => ,fake-gcrypt-hash)
  245. ;; (guix git-download) depends on (git) but only
  246. ;; for peripheral functionality. Provide a dummy
  247. ;; (git) to placate it.
  248. ((git) => ,fake-git)
  249. ,@(source-module-closure `((guix store)
  250. (guix self)
  251. (guix derivations)
  252. (gnu packages bootstrap))
  253. (list source)
  254. #:select? select?))
  255. (gexp->script "compute-guix-derivation"
  256. #~(begin
  257. (use-modules (ice-9 match)
  258. (ice-9 threads))
  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. (define (spin system)
  288. (define spin
  289. (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
  290. (format (current-error-port)
  291. "Computing Guix derivation for '~a'... "
  292. system)
  293. (when (isatty? (current-error-port))
  294. (let loop ((spin spin))
  295. (display (string-append "\b" (car spin))
  296. (current-error-port))
  297. (force-output (current-error-port))
  298. (sleep 1)
  299. (loop (cdr spin)))))
  300. (match (command-line)
  301. ((_ source system version protocol-version)
  302. ;; The current input port normally wraps a file
  303. ;; descriptor connected to the daemon, or it is
  304. ;; connected to /dev/null. In the former case, reuse
  305. ;; the connection such that we inherit build options
  306. ;; such as substitute URLs and so on; in the latter
  307. ;; case, attempt to open a new connection.
  308. (let* ((proto (string->number protocol-version))
  309. (store (if (integer? proto)
  310. (port->connection (duplicate-port
  311. (current-input-port)
  312. "w+0")
  313. #:version proto)
  314. (open-connection))))
  315. (call-with-new-thread
  316. (lambda ()
  317. (spin system)))
  318. (display
  319. (and=>
  320. (run-with-store store
  321. (guix-derivation source version
  322. #$guile-version
  323. #:channel-metadata
  324. '#$channel-metadata
  325. #:pull-version
  326. #$pull-version)
  327. #:system system)
  328. derivation-file-name))))))
  329. #:module-path (list source))))
  330. (define (call-with-clean-environment thunk)
  331. (let ((env (environ)))
  332. (dynamic-wind
  333. (lambda ()
  334. (environ '()))
  335. thunk
  336. (lambda ()
  337. (environ env)))))
  338. (define-syntax-rule (with-clean-environment exp ...)
  339. "Evaluate EXP in a context where zero environment variables are defined."
  340. (call-with-clean-environment (lambda () exp ...)))
  341. ;; The procedure below is our return value.
  342. (define* (build source
  343. #:key verbose?
  344. (version (date-version-string)) channel-metadata
  345. system
  346. (pull-version 0)
  347. ;; For the standalone Guix, default to Guile 3.0. For old
  348. ;; versions of 'guix pull' (pre-0.15.0), we have to use the
  349. ;; same Guile as the current one.
  350. (guile-version (if (> pull-version 0)
  351. "3.0"
  352. (effective-version)))
  353. #:allow-other-keys
  354. #:rest rest)
  355. "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
  356. files."
  357. ;; Build the build program and then use it as a trampoline to build from
  358. ;; SOURCE.
  359. (mlet %store-monad ((build (build-program source version guile-version
  360. #:channel-metadata channel-metadata
  361. #:pull-version pull-version))
  362. (system (if system (return system) (current-system)))
  363. (home -> (getenv "HOME"))
  364. ;; Note: Use the deprecated names here because the
  365. ;; caller might be Guix <= 0.16.0.
  366. (port ((store-lift nix-server-socket)))
  367. (major ((store-lift nix-server-major-version)))
  368. (minor ((store-lift nix-server-minor-version))))
  369. (mbegin %store-monad
  370. ;; Before 'with-build-handler' was implemented and used, we had to
  371. ;; explicitly call 'show-what-to-build*'.
  372. (munless (module-defined? (resolve-module '(guix store))
  373. 'with-build-handler)
  374. (show-what-to-build* (list build)))
  375. (built-derivations (list build))
  376. ;; Use the port beneath the current store as the stdin of BUILD. This
  377. ;; way, we know 'open-pipe*' will not close it on 'exec'. If PORT is
  378. ;; not a file port (e.g., it's an SSH channel), then the subprocess's
  379. ;; stdin will actually be /dev/null.
  380. (let* ((pipe (with-input-from-port port
  381. (lambda ()
  382. ;; Make sure BUILD is not influenced by
  383. ;; $GUILE_LOAD_PATH & co.
  384. (with-clean-environment
  385. (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
  386. (setenv "COLUMNS" "120") ;show wider backtraces
  387. (when home
  388. ;; Inherit HOME so that 'xdg-directory' works.
  389. (setenv "HOME" home))
  390. (open-pipe* OPEN_READ
  391. (derivation->output-path build)
  392. source system version
  393. (if (file-port? port)
  394. (number->string
  395. (logior major minor))
  396. "none"))))))
  397. (str (get-string-all pipe))
  398. (status (close-pipe pipe)))
  399. (match str
  400. ((? eof-object?)
  401. (error "build program failed" (list build status)))
  402. ((? derivation-path? drv)
  403. (mbegin %store-monad
  404. (return (newline (current-error-port)))
  405. ((store-lift add-temp-root) drv)
  406. (return (read-derivation-from-file drv))))
  407. ("#f"
  408. ;; Unsupported PULL-VERSION.
  409. (return #f))
  410. ((? string? str)
  411. (raise (condition
  412. (&message
  413. (message (format #f "You found a bug: the program '~a'
  414. failed to compute the derivation for Guix (version: ~s; system: ~s;
  415. host version: ~s; pull-version: ~s).
  416. Please report it by email to <~a>.~%"
  417. (derivation->output-path build)
  418. version system %guix-version pull-version
  419. %guix-bug-report-address)))))))))))
  420. ;; This file is loaded by 'guix pull'; return it the build procedure.
  421. build
  422. ;; Local Variables:
  423. ;; eval: (put 'with-load-path 'scheme-indent-function 1)
  424. ;; End:
  425. ;;; build-self.scm ends here