snix.scm 18 KB

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