You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

448 lines
17 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2010, 2011, 2012, 2013 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 (guix import snix)
  19. #:use-module (sxml ssax)
  20. #:use-module (ice-9 popen)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 rdelim)
  23. #:use-module (ice-9 format)
  24. #:use-module (ice-9 regex)
  25. #:use-module (ice-9 vlist)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-9)
  28. #:use-module (srfi srfi-11)
  29. #:use-module (srfi srfi-26)
  30. #:use-module (srfi srfi-37)
  31. #:use-module (system foreign)
  32. #:use-module (rnrs bytevectors)
  33. #:use-module (guix utils)
  34. #:use-module (guix import utils)
  35. #:use-module (guix base32)
  36. #:use-module (guix config)
  37. #:use-module (guix gnu-maintenance)
  38. #:export (open-nixpkgs
  39. xml->snix
  40. nixpkgs->guix-package))
  41. ;;; Commentary:
  42. ;;;
  43. ;;; Converting Nix code to s-expressions, and then to Guix `package'
  44. ;;; declarations, using the XML output of `nix-instantiate'.
  45. ;;;
  46. ;;; Code:
  47. ;;;
  48. ;;; SNix.
  49. ;;;
  50. ;; Nix object types visible in the XML output of `nix-instantiate' and
  51. ;; mapping to S-expressions (we map to sexps, not records, so that we
  52. ;; can do pattern matching):
  53. ;;
  54. ;; at (at varpat attrspat)
  55. ;; attr (attribute loc name value)
  56. ;; attrs (attribute-set attributes)
  57. ;; attrspat (attribute-set-pattern patterns)
  58. ;; bool #f|#t
  59. ;; derivation (derivation drv-path out-path attributes)
  60. ;; ellipsis '...
  61. ;; expr (snix loc body ...)
  62. ;; function (function loc at|attrspat|varpat)
  63. ;; int int
  64. ;; list list
  65. ;; null 'null
  66. ;; path string
  67. ;; string string
  68. ;; unevaluated 'unevaluated
  69. ;; varpat (varpat name)
  70. ;;
  71. ;; Initially ATTRIBUTES in `derivation' and `attribute-set' was a promise;
  72. ;; however, handling `repeated' nodes makes it impossible to do anything
  73. ;; lazily because the whole SXML tree has to be traversed to maintain the
  74. ;; list of known derivations.
  75. (define (xml-element->snix elem attributes body derivations)
  76. "Return an SNix element corresponding to XML element ELEM."
  77. (define (loc)
  78. (location (assq-ref attributes 'path)
  79. (assq-ref attributes 'line)
  80. (assq-ref attributes 'column)))
  81. (case elem
  82. ((at)
  83. (values `(at ,(car body) ,(cadr body)) derivations))
  84. ((attr)
  85. (let ((name (assq-ref attributes 'name)))
  86. (cond ((null? body)
  87. (values `(attribute-pattern ,name) derivations))
  88. ((and (pair? body) (null? (cdr body)))
  89. (values `(attribute ,(loc) ,name ,(car body))
  90. derivations))
  91. (else
  92. (error "invalid attribute body" name (loc) body)))))
  93. ((attrs)
  94. (values `(attribute-set ,(reverse body)) derivations))
  95. ((attrspat)
  96. (values `(attribute-set-pattern ,body) derivations))
  97. ((bool)
  98. (values (string-ci=? "true" (assq-ref attributes 'value))
  99. derivations))
  100. ((derivation)
  101. (let ((drv-path (assq-ref attributes 'drvPath))
  102. (out-path (assq-ref attributes 'outPath)))
  103. (if (equal? body '(repeated))
  104. (let ((body (vhash-assoc drv-path derivations)))
  105. (if (pair? body)
  106. (values `(derivation ,drv-path ,out-path ,(cdr body))
  107. derivations)
  108. ;; DRV-PATH hasn't been encountered yet but may be later
  109. ;; (see <http://article.gmane.org/gmane.linux.distributions.nixos/5946>.)
  110. ;; Return an `unresolved' node.
  111. (values `(unresolved
  112. ,(lambda (derivations)
  113. (let ((body (vhash-assoc drv-path derivations)))
  114. (if (pair? body)
  115. `(derivation ,drv-path ,out-path
  116. ,(cdr body))
  117. (error "no previous occurrence of derivation"
  118. drv-path)))))
  119. derivations)))
  120. (values `(derivation ,drv-path ,out-path ,body)
  121. (vhash-cons drv-path body derivations)))))
  122. ((ellipsis)
  123. (values '... derivations))
  124. ((expr)
  125. (values `(snix ,(loc) ,@body) derivations))
  126. ((function)
  127. (values `(function ,(loc) ,body) derivations))
  128. ((int)
  129. (values (string->number (assq-ref attributes 'value))
  130. derivations))
  131. ((list)
  132. (values body derivations))
  133. ((null)
  134. (values 'null derivations))
  135. ((path)
  136. (values (assq-ref attributes 'value) derivations))
  137. ((repeated)
  138. (values 'repeated derivations))
  139. ((string)
  140. (values (assq-ref attributes 'value) derivations))
  141. ((unevaluated)
  142. (values 'unevaluated derivations))
  143. ((varpat)
  144. (values `(varpat ,(assq-ref attributes 'name)) derivations))
  145. (else (error "unhandled Nix XML element" elem))))
  146. (define (resolve snix derivations)
  147. "Return a new SNix tree where `unresolved' nodes from SNIX have been
  148. replaced by the result of their application to DERIVATIONS, a vhash."
  149. (let loop ((node snix)
  150. (seen vlist-null))
  151. (if (vhash-assq node seen)
  152. (values node seen)
  153. (match node
  154. (('unresolved proc)
  155. (let ((n (proc derivations)))
  156. (values n seen)))
  157. ((tag body ...)
  158. (let ((body+seen (fold (lambda (n body+seen)
  159. (call-with-values
  160. (lambda ()
  161. (loop n (cdr body+seen)))
  162. (lambda (n* seen)
  163. (cons (cons n* (car body+seen))
  164. (vhash-consq n #t seen)))))
  165. (cons '() (vhash-consq node #t seen))
  166. body)))
  167. (values (cons tag (reverse (car body+seen)))
  168. (vhash-consq node #t (cdr body+seen)))))
  169. (anything
  170. (values anything seen))))))
  171. (define xml->snix
  172. (let ((parse
  173. (ssax:make-parser NEW-LEVEL-SEED
  174. (lambda (elem-gi attributes namespaces expected-content
  175. seed)
  176. (cons '() (cdr seed)))
  177. FINISH-ELEMENT
  178. (lambda (elem-gi attributes namespaces parent-seed
  179. seed)
  180. (let ((snix (car seed))
  181. (derivations (cdr seed)))
  182. (let-values (((snix derivations)
  183. (xml-element->snix elem-gi
  184. attributes
  185. snix
  186. derivations)))
  187. (cons (cons snix (car parent-seed))
  188. derivations))))
  189. CHAR-DATA-HANDLER
  190. (lambda (string1 string2 seed)
  191. ;; Discard inter-node strings, which are blanks.
  192. seed))))
  193. (lambda (port)
  194. "Return the SNix represention of TREE, an SXML tree as returned by
  195. parsing the XML output of `nix-instantiate' on Nixpkgs."
  196. (match (parse port (cons '() vlist-null))
  197. (((snix) . derivations)
  198. (resolve snix derivations))))))
  199. (define (attribute-value attribute)
  200. "Return the value of ATTRIBUTE."
  201. (match attribute
  202. (('attribute _ _ value) value)))
  203. (define (derivation-source derivation)
  204. "Return the \"src\" attribute of DERIVATION or #f if not found."
  205. (match derivation
  206. (('derivation _ _ (attributes ...))
  207. (find-attribute-by-name "src" attributes))))
  208. (define (derivation-output-path derivation)
  209. "Return the output path of DERIVATION."
  210. (match derivation
  211. (('derivation _ out-path _)
  212. out-path)
  213. (_ #f)))
  214. (define (source-output-path src)
  215. "Return the output path of SRC, the \"src\" attribute of a derivation."
  216. (derivation-output-path (attribute-value src)))
  217. (define (source-urls src)
  218. "Return the URLs of SRC, the \"src\" attribute of a derivation."
  219. (match src
  220. (('attribute _ _ ('derivation _ _ (attributes ...)))
  221. (match (find-attribute-by-name "urls" attributes)
  222. (('attribute _ _ value)
  223. value)))
  224. (_ #f)))
  225. (define (source-sha256 src)
  226. "Return the sha256 of SRC, the \"src\" attribute of a derivation, as a
  227. bytevector."
  228. (match src
  229. (('attribute _ _ ('derivation _ _ (attributes ...)))
  230. (match (find-attribute-by-name "outputHash" attributes)
  231. (('attribute _ _ value)
  232. (match value
  233. ((= string-length 52)
  234. (nix-base32-string->bytevector value))
  235. ((= string-length 64)
  236. (base16-string->bytevector value))
  237. (_
  238. (error "unsupported hash format" value))))))
  239. (_ #f)))
  240. (define (derivation-source-output-path derivation)
  241. "Return the output path of the \"src\" attribute of DERIVATION or #f
  242. if DERIVATION lacks an \"src\" attribute."
  243. (and=> (derivation-source derivation) source-output-path))
  244. (define* (open-nixpkgs nixpkgs #:optional attribute)
  245. "Return an input pipe to the XML representation of Nixpkgs. When
  246. ATTRIBUTE is true, only that attribute is considered."
  247. (with-fluids ((%default-port-encoding "UTF-8"))
  248. (let ((cross-system (format #f "{
  249. config = \"i686-guix-linux-gnu\";
  250. libc = \"glibc\";
  251. arch = \"guix\";
  252. withTLS = true;
  253. float = \"hard\";
  254. openssl.system = \"linux-generic32\";
  255. platform = (import ~a/pkgs/top-level/platforms.nix).sheevaplug;
  256. }" nixpkgs)))
  257. (apply open-pipe* OPEN_READ
  258. %nix-instantiate "--strict" "--eval-only" "--xml"
  259. ;; Pass a dummy `crossSystem' argument so that `buildInputs' and
  260. ;; `nativeBuildInputs' are not coalesced.
  261. ;; XXX: This is hacky and has other problems.
  262. ;"--arg" "crossSystem" cross-system
  263. `(,@(if attribute
  264. `("-A" ,attribute)
  265. '())
  266. ,nixpkgs)))))
  267. (define (pipe-failed? pipe)
  268. "Close pipe and return its status if it failed."
  269. (let ((status (close-pipe pipe)))
  270. (if (or (status:term-sig status)
  271. (not (= (status:exit-val status) 0)))
  272. status
  273. #f)))
  274. (define (find-attribute-by-name name attributes)
  275. "Return attribute NAME in ATTRIBUTES, an attribute set or list of SNix
  276. attributes, or #f if NAME cannot be found."
  277. (find (lambda (a)
  278. (match a
  279. (('attribute _ (? (cut string=? <> name)) _)
  280. a)
  281. (_ #f)))
  282. (match attributes
  283. (('attribute-set (attributes ...))
  284. attributes)
  285. (_
  286. attributes))))
  287. (define (package-source-output-path package)
  288. "Return the output path of the \"src\" derivation of PACKAGE."
  289. (derivation-source-output-path (attribute-value package)))
  290. ;;;
  291. ;;; Conversion of "Nix expressions" to "Guix expressions".
  292. ;;;
  293. (define (snix-derivation->guix-package derivation)
  294. "Return the `package' s-expression corresponding to SNix DERIVATION, a
  295. Nixpkgs `stdenv.mkDerivation'-style derivation, and the original source
  296. location of DERIVATION."
  297. (match derivation
  298. (('derivation _ _ (attributes ...))
  299. (let*-values (((full-name loc)
  300. (match (find-attribute-by-name "name" attributes)
  301. (('attribute loc _ value)
  302. (values value loc))
  303. (_
  304. (values #f #f))))
  305. ((name version)
  306. (package-name->name+version full-name)))
  307. (define (convert-inputs type)
  308. ;; Convert the derivation's input from a list of SNix derivations to
  309. ;; a list of name/variable pairs.
  310. (match (and=> (find-attribute-by-name type attributes)
  311. attribute-value)
  312. (#f
  313. '())
  314. ((inputs ...)
  315. ;; Inputs can be either derivations or the null value.
  316. (filter-map (match-lambda
  317. (('derivation _ _ (attributes ...))
  318. (let* ((full-name
  319. (attribute-value
  320. (find-attribute-by-name "name" attributes)))
  321. (name (package-name->name+version full-name)))
  322. (list name
  323. (list 'unquote (string->symbol name)))))
  324. ('null #f))
  325. inputs))))
  326. (define (maybe-inputs guix-name inputs)
  327. (match inputs
  328. (()
  329. '())
  330. ((inputs ...)
  331. (list (list guix-name
  332. (list 'quasiquote inputs))))))
  333. (define (pretty-uri uri version)
  334. (if version
  335. (match (factorize-uri uri version)
  336. ((items ...)
  337. `(string-append ,@items))
  338. (x x))
  339. uri))
  340. (define (license-variable license)
  341. ;; Return the name of the (guix licenses) variable for LICENSE.
  342. (match license
  343. ("GPLv2+" 'gpl2+)
  344. ("GPLv3+" 'gpl3+)
  345. ("LGPLv2+" 'lgpl2.1+)
  346. ("LGPLv2.1+" 'lgpl2.1+)
  347. ("LGPLv3+" 'lgpl3+)
  348. (_ license)))
  349. (let* ((source (find-attribute-by-name "src" attributes))
  350. (urls (source-urls source))
  351. (sha256 (source-sha256 source))
  352. (meta (and=> (find-attribute-by-name "meta" attributes)
  353. attribute-value)))
  354. (values
  355. `(package
  356. (name ,name)
  357. (version ,version)
  358. (source (origin
  359. (method url-fetch)
  360. (uri ,(pretty-uri (car urls) version))
  361. (sha256
  362. (base32
  363. ,(bytevector->nix-base32-string sha256)))))
  364. (build-system gnu-build-system)
  365. ;; When doing a native Nixpkgs build, `buildInputs' is empty and
  366. ;; everything is in `nativeBuildInputs'. So we can't distinguish
  367. ;; between both, here.
  368. ;;
  369. ;; Note that `nativeBuildInputs' was renamed from
  370. ;; `buildNativeInputs' in Nixpkgs sometime around March 2013.
  371. ,@(maybe-inputs 'inputs
  372. (convert-inputs "nativeBuildInputs"))
  373. ,@(maybe-inputs 'propagated-inputs
  374. (convert-inputs "propagatedNativeBuildInputs"))
  375. (home-page ,(and=> (find-attribute-by-name "homepage" meta)
  376. attribute-value))
  377. (synopsis
  378. ;; For GNU packages, prefer the official synopsis.
  379. ,(or (false-if-exception
  380. (and=> (find (lambda (gnu-package)
  381. (equal? (gnu-package-name gnu-package)
  382. name))
  383. (official-gnu-packages))
  384. gnu-package-doc-summary))
  385. (and=> (find-attribute-by-name "description" meta)
  386. attribute-value)))
  387. (description
  388. ;; Likewise, prefer the official description of GNU packages.
  389. ,(or (false-if-exception
  390. (and=> (find (lambda (gnu-package)
  391. (equal? (gnu-package-name gnu-package)
  392. name))
  393. (official-gnu-packages))
  394. gnu-package-doc-description))
  395. (and=> (find-attribute-by-name "longDescription" meta)
  396. attribute-value)))
  397. (license ,(and=> (find-attribute-by-name "license" meta)
  398. (compose license-variable attribute-value))))
  399. loc))))))
  400. (define (nixpkgs->guix-package nixpkgs attribute)
  401. "Evaluate ATTRIBUTE in NIXPKGS, the file name of a Nixpkgs checkout,
  402. and return the `package' s-expression corresponding to that package."
  403. (let ((port (open-nixpkgs nixpkgs attribute)))
  404. (match (xml->snix port)
  405. (('snix loc (and drv ('derivation _ ...)))
  406. (and (not (pipe-failed? port))
  407. (snix-derivation->guix-package drv)))
  408. (_
  409. (not (pipe-failed? port))))))
  410. ;;; snix.scm ends here