guix: node-build-system: Use guile-json instead of a custom parser.
[jackhill/guix/guix.git] / guix / build-system / node.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Jelle Licht <jlicht@fsfe.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
19 (define-module (guix build-system node)
20 #:use-module (guix store)
21 #:use-module (guix build union)
22 #:use-module (guix utils)
23 #:use-module (guix packages)
24 #:use-module (guix derivations)
25 #:use-module (guix search-paths)
26 #:use-module (guix build-system)
27 #:use-module (guix build-system gnu)
28 #:use-module (ice-9 match)
29 #:use-module (json parser)
30 #:export (npm-meta-uri
31 %node-build-system-modules
32 node-build
33 node-build-system))
34
35 (define (npm-meta-uri name)
36 "Return a URI string for the metadata of node module NAME found in the npm
37 registry."
38 (string-append "https://registry.npmjs.org/" name))
39
40 (define %node-build-system-modules
41 ;; Build-side modules imported by default.
42 `((guix build node-build-system)
43 (guix build union)
44 (json parser)
45 ,@%gnu-build-system-modules)) ;; TODO: Might be not needed
46
47 (define (default-node)
48 "Return the default Node package."
49 ;; Lazily resolve the binding to avoid a circular dependency.
50 (let ((node (resolve-interface '(gnu packages node))))
51 (module-ref node 'node)))
52
53 (define* (lower name
54 #:key source inputs native-inputs outputs system target
55 (node (default-node))
56 #:allow-other-keys
57 #:rest arguments)
58 "Return a bag for NAME."
59 (define private-keywords
60 '(#:source #:target #:node #:inputs #:native-inputs))
61
62 (and (not target) ;XXX: no cross-compilation
63 (bag
64 (name name)
65 (system system)
66 (host-inputs `(,@(if source
67 `(("source" ,source))
68 '())
69 ,@inputs
70
71 ;; Keep the standard inputs of 'gnu-build-system'.
72 ,@(standard-packages)))
73 (build-inputs `(("node" ,node)
74 ,@native-inputs))
75 (outputs outputs)
76 (build node-build)
77 (arguments (strip-keyword-arguments private-keywords arguments)))))
78
79 (define* (node-build store name inputs
80 #:key
81 (npm-flags ''())
82 (tests? #t)
83 (phases '(@ (guix build node-build-system)
84 %standard-phases))
85 (outputs '("out"))
86 (search-paths '())
87 (system (%current-system))
88 (guile #f)
89 (imported-modules %node-build-system-modules)
90 (modules '((guix build node-build-system)
91 (guix build union)
92 (guix build utils)
93 (json parser))))
94 "Build SOURCE using NODE and INPUTS."
95 (define builder
96 `(begin
97 (use-modules ,@modules)
98 (node-build #:name ,name
99 #:source ,(match (assoc-ref inputs "source")
100 (((? derivation? source))
101 (derivation->output-path source))
102 ((source)
103 source)
104 (source
105 source))
106 #:system ,system
107 #:npm-flags ,npm-flags
108 #:tests? ,tests?
109 #:phases ,phases
110 #:outputs %outputs
111 #:search-paths ',(map search-path-specification->sexp
112 search-paths)
113 #:inputs %build-inputs)))
114
115 (define guile-for-build
116 (match guile
117 ((? package?)
118 (package-derivation store guile system #:graft? #f))
119 (#f
120 (let* ((distro (resolve-interface '(gnu packages commencement)))
121 (guile (module-ref distro 'guile-final)))
122 (package-derivation store guile system #:graft? #f)))))
123
124 (build-expression->derivation store name builder
125 #:inputs inputs
126 #:system system
127 #:modules imported-modules
128 #:outputs outputs
129 #:guile-for-build guile-for-build))
130
131 (define node-build-system
132 (build-system
133 (name 'node)
134 (description "The standard Node build system")
135 (lower lower)))