1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix import snix)
20 #:use-module (sxml ssax)
21 #:use-module (ice-9 popen)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 rdelim)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 regex)
26 #:use-module (ice-9 vlist)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-9)
29 #:use-module (srfi srfi-11)
30 #:use-module (srfi srfi-26)
31 #:use-module (srfi srfi-37)
32 #:use-module (system foreign)
33 #:use-module (rnrs bytevectors)
35 ;; Use the 'package-name->name+version' procedure that works with
36 ;; hyphen-separate name/version, not the one that works with '@'-separated
37 ;; name/version. Subtle!
38 #:use-module ((guix utils) #:hide (package-name->name+version))
39 #:use-module ((guix build utils) #:select (package-name->name+version))
41 #:use-module (guix import utils)
42 #:use-module (guix base16)
43 #:use-module (guix base32)
44 #:use-module (guix config)
45 #:use-module (guix gnu-maintenance)
46 #:export (open-nixpkgs
48 nixpkgs->guix-package))
52 ;;; Converting Nix code to s-expressions, and then to Guix `package'
53 ;;; declarations, using the XML output of `nix-instantiate'.
62 ;; Nix object types visible in the XML output of `nix-instantiate' and
63 ;; mapping to S-expressions (we map to sexps, not records, so that we
64 ;; can do pattern matching):
66 ;; at (at varpat attrspat)
67 ;; attr (attribute loc name value)
68 ;; attrs (attribute-set attributes)
69 ;; attrspat (attribute-set-pattern patterns)
71 ;; derivation (derivation drv-path out-path attributes)
73 ;; expr (snix loc body ...)
74 ;; function (function loc at|attrspat|varpat)
80 ;; unevaluated 'unevaluated
81 ;; varpat (varpat name)
83 ;; Initially ATTRIBUTES in `derivation' and `attribute-set' was a promise;
84 ;; however, handling `repeated' nodes makes it impossible to do anything
85 ;; lazily because the whole SXML tree has to be traversed to maintain the
86 ;; list of known derivations.
88 (define (xml-element->snix elem attributes body derivations)
89 "Return an SNix element corresponding to XML element ELEM."
92 (location (assq-ref attributes 'path)
93 (assq-ref attributes 'line)
94 (assq-ref attributes 'column)))
98 (values `(at ,(car body) ,(cadr body)) derivations))
100 (let ((name (assq-ref attributes 'name)))
102 (values `(attribute-pattern ,name) derivations))
103 ((and (pair? body) (null? (cdr body)))
104 (values `(attribute ,(loc) ,name ,(car body))
107 (error "invalid attribute body" name (loc) body)))))
109 (values `(attribute-set ,(reverse body)) derivations))
111 (values `(attribute-set-pattern ,body) derivations))
113 (values (string-ci=? "true" (assq-ref attributes 'value))
116 (let ((drv-path (assq-ref attributes 'drvPath))
117 (out-path (assq-ref attributes 'outPath)))
118 (if (equal? body '(repeated))
119 (let ((body (vhash-assoc drv-path derivations)))
121 (values `(derivation ,drv-path ,out-path ,(cdr body))
124 ;; DRV-PATH hasn't been encountered yet but may be later
125 ;; (see <http://article.gmane.org/gmane.linux.distributions.nixos/5946>.)
126 ;; Return an `unresolved' node.
128 ,(lambda (derivations)
129 (let ((body (vhash-assoc drv-path derivations)))
131 `(derivation ,drv-path ,out-path
133 (error "no previous occurrence of derivation"
136 (values `(derivation ,drv-path ,out-path ,body)
137 (vhash-cons drv-path body derivations)))))
139 (values '... derivations))
141 (values `(snix ,(loc) ,@body) derivations))
143 (values `(function ,(loc) ,body) derivations))
145 (values (string->number (assq-ref attributes 'value))
148 (values body derivations))
150 (values 'null derivations))
152 (values (assq-ref attributes 'value) derivations))
154 (values 'repeated derivations))
156 (values (assq-ref attributes 'value) derivations))
158 (values 'unevaluated derivations))
160 (values `(varpat ,(assq-ref attributes 'name)) derivations))
161 (else (error "unhandled Nix XML element" elem))))
163 (define (resolve snix derivations)
164 "Return a new SNix tree where `unresolved' nodes from SNIX have been
165 replaced by the result of their application to DERIVATIONS, a vhash."
166 (let loop ((node snix)
168 (if (vhash-assq node seen)
172 (let ((n (proc derivations)))
175 (let ((body+seen (fold (lambda (n body+seen)
178 (loop n (cdr body+seen)))
180 (cons (cons n* (car body+seen))
181 (vhash-consq n #t seen)))))
182 (cons '() (vhash-consq node #t seen))
184 (values (cons tag (reverse (car body+seen)))
185 (vhash-consq node #t (cdr body+seen)))))
187 (values anything seen))))))
191 (ssax:make-parser NEW-LEVEL-SEED
192 (lambda (elem-gi attributes namespaces expected-content
194 (cons '() (cdr seed)))
197 (lambda (elem-gi attributes namespaces parent-seed
199 (let ((snix (car seed))
200 (derivations (cdr seed)))
201 (let-values (((snix derivations)
202 (xml-element->snix elem-gi
206 (cons (cons snix (car parent-seed))
210 (lambda (string1 string2 seed)
211 ;; Discard inter-node strings, which are blanks.
214 "Return the SNix represention of TREE, an SXML tree as returned by
215 parsing the XML output of `nix-instantiate' on Nixpkgs."
216 (match (parse port (cons '() vlist-null))
217 (((snix) . derivations)
218 (resolve snix derivations))))))
220 (define (attribute-value attribute)
221 "Return the value of ATTRIBUTE."
223 (('attribute _ _ value) value)))
225 (define (derivation-source derivation)
226 "Return the \"src\" attribute of DERIVATION or #f if not found."
228 (('derivation _ _ (attributes ...))
229 (find-attribute-by-name "src" attributes))))
231 (define (derivation-output-path derivation)
232 "Return the output path of DERIVATION."
234 (('derivation _ out-path _)
238 (define (source-output-path src)
239 "Return the output path of SRC, the \"src\" attribute of a derivation."
240 (derivation-output-path (attribute-value src)))
242 (define (source-urls src)
243 "Return the URLs of SRC, the \"src\" attribute of a derivation."
245 (('attribute _ _ ('derivation _ _ (attributes ...)))
246 (match (find-attribute-by-name "urls" attributes)
247 (('attribute _ _ value)
251 (define (source-sha256 src)
252 "Return the sha256 of SRC, the \"src\" attribute of a derivation, as a
255 (('attribute _ _ ('derivation _ _ (attributes ...)))
256 (match (find-attribute-by-name "outputHash" attributes)
257 (('attribute _ _ value)
259 ((= string-length 52)
260 (nix-base32-string->bytevector value))
261 ((= string-length 64)
262 (base16-string->bytevector value))
264 (error "unsupported hash format" value))))))
267 (define (derivation-source-output-path derivation)
268 "Return the output path of the \"src\" attribute of DERIVATION or #f
269 if DERIVATION lacks an \"src\" attribute."
270 (and=> (derivation-source derivation) source-output-path))
272 (define* (open-nixpkgs nixpkgs #:optional attribute)
273 "Return an input pipe to the XML representation of Nixpkgs. When
274 ATTRIBUTE is true, only that attribute is considered."
275 (with-fluids ((%default-port-encoding "UTF-8"))
276 (let ((cross-system (format #f "{
277 config = \"i686-guix-linux-gnu\";
282 openssl.system = \"linux-generic32\";
283 platform = (import ~a/pkgs/top-level/platforms.nix).sheevaplug;
285 (apply open-pipe* OPEN_READ
286 "nix-instantiate" "--strict" "--eval-only" "--xml"
288 ;; Pass a dummy `crossSystem' argument so that `buildInputs' and
289 ;; `nativeBuildInputs' are not coalesced.
290 ;; XXX: This is hacky and has other problems.
291 ;"--arg" "crossSystem" cross-system
298 (define (pipe-failed? pipe)
299 "Close pipe and return its status if it failed."
300 (let ((status (close-pipe pipe)))
301 (if (or (status:term-sig status)
302 (not (= (status:exit-val status) 0)))
306 (define (find-attribute-by-name name attributes)
307 "Return attribute NAME in ATTRIBUTES, an attribute set or list of SNix
308 attributes, or #f if NAME cannot be found."
311 (('attribute _ (? (cut string=? <> name)) _)
315 (('attribute-set (attributes ...))
320 (define (license-variable license)
321 "Return the name of the (guix licenses) variable for LICENSE."
325 ("LGPLv2+" 'lgpl2.1+)
326 ("LGPLv2.1+" 'lgpl2.1+)
328 (('attribute-set _ ...)
329 ;; At some point in 2013, Nixpkgs switched to attribute sets to represent
330 ;; licenses. These are listed in lib/licenses.nix.
331 (match (and=> (find-attribute-by-name "shortName" license)
333 ("agpl3Plus" 'agpl3+)
336 ("lgpl2Plus" 'lgpl2.0+)
337 ("lgpl21Plus" 'lgpl2.1+)
338 ("lgpl3Plus" 'lgpl3+)
343 (define (package-source-output-path package)
344 "Return the output path of the \"src\" derivation of PACKAGE."
345 (derivation-source-output-path (attribute-value package)))
349 ;;; Conversion of "Nix expressions" to "Guix expressions".
352 (define (snix-derivation->guix-package derivation)
353 "Return the `package' s-expression corresponding to SNix DERIVATION, a
354 Nixpkgs `stdenv.mkDerivation'-style derivation, and the original source
355 location of DERIVATION."
357 (('derivation _ _ (attributes ...))
358 (let*-values (((full-name loc)
359 (match (find-attribute-by-name "name" attributes)
360 (('attribute loc _ value)
365 (package-name->name+version full-name)))
366 (define (convert-inputs type)
367 ;; Convert the derivation's input from a list of SNix derivations to
368 ;; a list of name/variable pairs.
369 (match (and=> (find-attribute-by-name type attributes)
374 ;; Inputs can be either derivations or the null value.
375 (filter-map (match-lambda
376 (('derivation _ _ (attributes ...))
379 (find-attribute-by-name "name" attributes)))
380 (name (package-name->name+version full-name)))
382 (list 'unquote (string->symbol name)))))
386 (define (maybe-inputs guix-name inputs)
391 (list (list guix-name
392 (list 'quasiquote inputs))))))
394 (define (pretty-uri uri version)
396 (match (factorize-uri uri version)
398 `(string-append ,@items))
402 (let* ((source (find-attribute-by-name "src" attributes))
403 (urls (source-urls source))
404 (sha256 (source-sha256 source))
405 (meta (and=> (find-attribute-by-name "meta" attributes)
413 (uri ,(pretty-uri (car urls) version))
416 ,(bytevector->nix-base32-string sha256)))))
417 (build-system gnu-build-system)
419 ;; When doing a native Nixpkgs build, `buildInputs' is empty and
420 ;; everything is in `nativeBuildInputs'. So we can't distinguish
421 ;; between both, here.
423 ;; Note that `nativeBuildInputs' was renamed from
424 ;; `buildNativeInputs' in Nixpkgs sometime around March 2013.
425 ,@(maybe-inputs 'inputs
426 (convert-inputs "nativeBuildInputs"))
427 ,@(maybe-inputs 'propagated-inputs
428 (convert-inputs "propagatedNativeBuildInputs"))
430 (home-page ,(and=> (find-attribute-by-name "homepage" meta)
433 ;; For GNU packages, prefer the official synopsis.
434 ,(or (false-if-exception
435 (and=> (find (lambda (gnu-package)
436 (equal? (gnu-package-name gnu-package)
438 (official-gnu-packages))
439 gnu-package-doc-summary))
440 (and=> (find-attribute-by-name "description" meta)
443 ;; Likewise, prefer the official description of GNU packages.
444 ,(or (false-if-exception
445 (and=> (find (lambda (gnu-package)
446 (equal? (gnu-package-name gnu-package)
448 (official-gnu-packages))
449 gnu-package-doc-description))
450 (and=> (find-attribute-by-name "longDescription" meta)
452 (license ,(and=> (find-attribute-by-name "license" meta)
453 (compose license-variable attribute-value))))
456 (define (nixpkgs->guix-package nixpkgs attribute)
457 "Evaluate ATTRIBUTE in NIXPKGS, the file name of a Nixpkgs checkout,
458 and return the `package' s-expression corresponding to that package."
459 (let ((port (open-nixpkgs nixpkgs attribute)))
460 (match (xml->snix port)
461 (('snix loc (and drv ('derivation _ ...)))
462 (and (not (pipe-failed? port))
463 (snix-derivation->guix-package drv)))
465 (not (pipe-failed? port))))))
467 ;;; snix.scm ends here