| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 |
|
| 33 | (define-module (build-self)
|
| 34 | #:use-module (gnu)
|
| 35 | #:use-module (guix)
|
| 36 | #:use-module (guix ui)
|
| 37 | #:use-module (guix config)
|
| 38 | #:use-module (guix modules)
|
| 39 | #:use-module ((guix self) #:select (make-config.scm))
|
| 40 | #:use-module (srfi srfi-1)
|
| 41 | #:use-module (srfi srfi-19)
|
| 42 | #:use-module (srfi srfi-34)
|
| 43 | #:use-module (srfi srfi-35)
|
| 44 | #:use-module (rnrs io ports)
|
| 45 | #:use-module (ice-9 match)
|
| 46 | #:use-module (ice-9 popen)
|
| 47 | #:export (build))
|
| 48 |
|
| 49 | (define (date-version-string)
|
| 50 | "Return the current date and hour in UTC timezone, for use as a poor
|
| 51 | person's version identifier."
|
| 52 | |
| 53 | (date->string (current-date 0) "~Y~m~d.~H"))
|
| 54 |
|
| 55 | (define guile-gcrypt
|
| 56 | |
| 57 | (match (find-best-packages-by-name "guile-gcrypt" #f)
|
| 58 | ((package . _)
|
| 59 | package)))
|
| 60 |
|
| 61 | (define* (build-program source version
|
| 62 | #:optional (guile-version (effective-version))
|
| 63 | #:key (pull-version 0) (channel-metadata #f)
|
| 64 | built-in-builders)
|
| 65 | "Return a program that computes the derivation to build Guix from SOURCE.
|
| 66 | If BUILT-IN-BUILDERS is provided, it should be a list of
|
| 67 | strings and this will be used instead of the builtin builders provided by the
|
| 68 | build daemon, from within the generated build program."
|
| 69 | (define select?
|
| 70 | |
| 71 | |
| 72 | |
| 73 | (match-lambda
|
| 74 | (('guix 'config) #f)
|
| 75 | (('guix 'channels) #f)
|
| 76 | (('guix 'build 'download) #f) |
| 77 | (('guix _ ...) #t)
|
| 78 | (('gnu _ ...) #t)
|
| 79 | (_ #f)))
|
| 80 |
|
| 81 | (define fake-gcrypt-hash
|
| 82 | |
| 83 | (scheme-file "hash.scm"
|
| 84 | #~(define-module (gcrypt hash)
|
| 85 | #:export (sha1 sha256))))
|
| 86 |
|
| 87 | (define fake-git
|
| 88 | (scheme-file "git.scm" #~(define-module (git))))
|
| 89 |
|
| 90 | (with-imported-modules `(((guix config)
|
| 91 | => ,(make-config.scm))
|
| 92 |
|
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | ((gcrypt hash) => ,fake-gcrypt-hash)
|
| 98 |
|
| 99 | |
| 100 | |
| 101 | |
| 102 | ((git) => ,fake-git)
|
| 103 |
|
| 104 | ,@(source-module-closure `((guix store)
|
| 105 | (guix self)
|
| 106 | (guix derivations)
|
| 107 | (gnu packages bootstrap))
|
| 108 | (list source)
|
| 109 | #:select? select?))
|
| 110 | (gexp->script "compute-guix-derivation"
|
| 111 | #~(begin
|
| 112 | (use-modules (ice-9 match))
|
| 113 |
|
| 114 | |
| 115 | (fluid-set! %file-port-name-canonicalization #f)
|
| 116 |
|
| 117 | (eval-when (expand load eval)
|
| 118 | |
| 119 | |
| 120 | (match (command-line)
|
| 121 | ((_ source _ ...)
|
| 122 | (match %load-path
|
| 123 | ((front _ ...)
|
| 124 | (unless (string=? front source) |
| 125 | (set! %load-path
|
| 126 | (list source
|
| 127 | (string-append #$guile-gcrypt
|
| 128 | "/share/guile/site/"
|
| 129 | (effective-version))
|
| 130 | front)))))))
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | (set! %load-compiled-path
|
| 135 | (cons (string-append #$guile-gcrypt "/lib/guile/"
|
| 136 | (effective-version)
|
| 137 | "/site-ccache")
|
| 138 | %load-compiled-path))
|
| 139 |
|
| 140 | |
| 141 | |
| 142 | (read-disable 'positions))
|
| 143 |
|
| 144 | (use-modules (guix store)
|
| 145 | (guix self)
|
| 146 | (guix derivations)
|
| 147 | (srfi srfi-1))
|
| 148 |
|
| 149 | (match (command-line)
|
| 150 | ((_ source system version protocol-version
|
| 151 | build-output)
|
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | (let* ((proto (string->number protocol-version))
|
| 159 | (store (if (integer? proto)
|
| 160 | (port->connection
|
| 161 | (duplicate-port
|
| 162 | (current-input-port)
|
| 163 | "w+0")
|
| 164 | #:version proto
|
| 165 | #:built-in-builders
|
| 166 | '#$built-in-builders)
|
| 167 | (open-connection
|
| 168 | #:built-in-builders
|
| 169 | '#$built-in-builders)))
|
| 170 | (sock (socket AF_UNIX SOCK_STREAM 0)))
|
| 171 | |
| 172 | |
| 173 | (connect sock AF_UNIX build-output)
|
| 174 |
|
| 175 | (when (integer? proto)
|
| 176 | |
| 177 | |
| 178 | (setvbuf (store-connection-socket store)
|
| 179 | 'block
|
| 180 | %default-store-connection-buffer-size))
|
| 181 |
|
| 182 | (display
|
| 183 | (and=>
|
| 184 | |
| 185 | (parameterize ((current-warning-port
|
| 186 | (%make-void-port "w"))
|
| 187 | (current-build-output-port sock))
|
| 188 | (run-with-store store
|
| 189 | (guix-derivation source version
|
| 190 | #$guile-version
|
| 191 | #:channel-metadata
|
| 192 | '#$channel-metadata
|
| 193 | #:pull-version
|
| 194 | #$pull-version)
|
| 195 | #:system system))
|
| 196 | derivation-file-name))))))
|
| 197 | #:module-path (list source))))
|
| 198 |
|
| 199 | (define (proxy input output)
|
| 200 | "Dump the contents of INPUT to OUTPUT until EOF is reached on INPUT.
|
| 201 | Display a spinner when nothing happens."
|
| 202 | (define spin
|
| 203 | (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
|
| 204 |
|
| 205 | (setvbuf input 'block 16384)
|
| 206 | (let loop ((spin spin))
|
| 207 | (match (select (list input) '() '() 1)
|
| 208 | ((() () ())
|
| 209 | (when (isatty? (current-error-port))
|
| 210 | (display (string-append "\b" (car spin))
|
| 211 | (current-error-port))
|
| 212 | (force-output (current-error-port)))
|
| 213 | (loop (cdr spin)))
|
| 214 | (((_) () ())
|
| 215 | |
| 216 | (let ((bv (get-bytevector-some input)))
|
| 217 | (unless (eof-object? bv)
|
| 218 | (put-bytevector output bv)
|
| 219 | (loop spin)))))))
|
| 220 |
|
| 221 | (define (call-with-clean-environment thunk)
|
| 222 | (let ((env (environ)))
|
| 223 | (dynamic-wind
|
| 224 | (lambda ()
|
| 225 | (environ '()))
|
| 226 | thunk
|
| 227 | (lambda ()
|
| 228 | (environ env)))))
|
| 229 |
|
| 230 | (define-syntax-rule (with-clean-environment exp ...)
|
| 231 | "Evaluate EXP in a context where zero environment variables are defined."
|
| 232 | (call-with-clean-environment (lambda () exp ...)))
|
| 233 |
|
| 234 | |
| 235 | (define* (build source
|
| 236 | #:key verbose?
|
| 237 | (version (date-version-string)) channel-metadata
|
| 238 | system
|
| 239 | (pull-version 0)
|
| 240 |
|
| 241 | |
| 242 | |
| 243 | |
| 244 | (guile-version (if (> pull-version 0)
|
| 245 | "3.0"
|
| 246 | (effective-version)))
|
| 247 | built-in-builders
|
| 248 | #:allow-other-keys
|
| 249 | #:rest rest)
|
| 250 | "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
|
| 251 | files."
|
| 252 | |
| 253 | (fluid-set! %file-port-name-canonicalization #f)
|
| 254 |
|
| 255 | |
| 256 | |
| 257 | (mlet %store-monad ((build (build-program source version guile-version
|
| 258 | #:channel-metadata channel-metadata
|
| 259 | #:pull-version pull-version
|
| 260 | #:built-in-builders
|
| 261 | built-in-builders))
|
| 262 | (system (if system (return system) (current-system)))
|
| 263 | (home -> (getenv "HOME"))
|
| 264 |
|
| 265 | |
| 266 | |
| 267 | (port ((store-lift nix-server-socket)))
|
| 268 | (major ((store-lift nix-server-major-version)))
|
| 269 | (minor ((store-lift nix-server-minor-version))))
|
| 270 | (mbegin %store-monad
|
| 271 | |
| 272 | |
| 273 | (munless (module-defined? (resolve-module '(guix store))
|
| 274 | 'with-build-handler)
|
| 275 | (show-what-to-build* (list build)))
|
| 276 | (built-derivations (list build))
|
| 277 |
|
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | (let* ((sock (socket AF_UNIX SOCK_STREAM 0))
|
| 283 | (node (let ((file (string-append (or (getenv "TMPDIR") "/tmp")
|
| 284 | "/guix-build-output-"
|
| 285 | (number->string (getpid)))))
|
| 286 | (bind sock AF_UNIX file)
|
| 287 | (listen sock 1)
|
| 288 | file))
|
| 289 | (pipe (with-input-from-port port
|
| 290 | (lambda ()
|
| 291 | |
| 292 | |
| 293 | (with-clean-environment
|
| 294 | (setenv "GUILE_WARN_DEPRECATED" "no") |
| 295 | (setenv "COLUMNS" "120") |
| 296 | (when home
|
| 297 | |
| 298 | (setenv "HOME" home))
|
| 299 | (open-pipe* OPEN_READ
|
| 300 | (derivation->output-path build)
|
| 301 | source system version
|
| 302 | (if (file-port? port)
|
| 303 | (number->string
|
| 304 | (logior major minor))
|
| 305 | "none")
|
| 306 | node))))))
|
| 307 | (format (current-error-port) "Computing Guix derivation for '~a'... "
|
| 308 | system)
|
| 309 |
|
| 310 | |
| 311 | |
| 312 | |
| 313 | (match (accept sock)
|
| 314 | ((port . _)
|
| 315 | (close-port sock)
|
| 316 | (delete-file node)
|
| 317 | (proxy port (current-build-output-port))))
|
| 318 |
|
| 319 | |
| 320 | |
| 321 | (let ((str (get-string-all pipe))
|
| 322 | (status (close-pipe pipe)))
|
| 323 | (match str
|
| 324 | ((? eof-object?)
|
| 325 | (error "build program failed" (list build status)))
|
| 326 | ((? derivation-path? drv)
|
| 327 | (mbegin %store-monad
|
| 328 | (return (newline (current-error-port)))
|
| 329 | ((store-lift add-temp-root) drv)
|
| 330 | (return (read-derivation-from-file drv))))
|
| 331 | ("#f"
|
| 332 | |
| 333 | (return #f))
|
| 334 | ((? string? str)
|
| 335 | (raise (condition
|
| 336 | (&message
|
| 337 | (message (format #f "You found a bug: the program '~a'
|
| 338 | failed to compute the derivation for Guix (version: ~s; system: ~s;
|
| 339 | host version: ~s; pull-version: ~s).
|
| 340 | Please report the COMPLETE output above to <~a>.~%"
|
| 341 | (derivation->output-path build)
|
| 342 | version system %guix-version pull-version
|
| 343 | %guix-bug-report-address))))))))))))
|
| 344 |
|
| 345 | |
| 346 | build
|
| 347 |
|
| 348 | |
| 349 | |
| 350 | |
| 351 |
|
| 352 | |