summary refs log tree commit diff
path: root/doc/language-support.xml
blob: f5e89df57fcce28537b1b307f35d3a778d76dfa4 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<chapter xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xml:id="chap-language-support">

<title>Support for specific programming languages</title>

<para>The <link linkend="chap-stdenv">standard build
environment</link> makes it easy to build typical Autotools-based
packages with very little code.  Any other kind of package can be
accomodated by overriding the appropriate phases of
<literal>stdenv</literal>.  However, there are specialised functions
in Nixpkgs to easily build packages for other programming languages,
such as Perl or Haskell.  These are described in this chapter.</para>


<section xml:id="ssec-language-perl"><title>Perl</title>

<para>Nixpkgs provides a function <varname>buildPerlPackage</varname>,
a generic package builder function for any Perl package that has a
standard <varname>Makefile.PL</varname>.  It’s implemented in <link
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/perl-modules/generic"><filename>pkgs/development/perl-modules/generic</filename></link>.</para>

<para>Perl packages from CPAN are defined in <link
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/perl-packages.nix"><filename>pkgs/top-level/perl-packages.nix</filename></link>,
rather than <filename>pkgs/all-packages.nix</filename>.  Most Perl
packages are so straight-forward to build that they are defined here
directly, rather than having a separate function for each package
called from <filename>perl-packages.nix</filename>.  However, more
complicated packages should be put in a separate file, typically in
<filename>pkgs/development/perl-modules</filename>.  Here is an
example of the former:

<programlisting>
ClassC3 = buildPerlPackage rec {
  name = "Class-C3-0.21";
  src = fetchurl {
    url = "mirror://cpan/authors/id/F/FL/FLORA/${name}.tar.gz";
    sha256 = "1bl8z095y4js66pwxnm7s853pi9czala4sqc743fdlnk27kq94gz";
  };
};
</programlisting>

Note the use of <literal>mirror://cpan/</literal>, and the
<literal>${name}</literal> in the URL definition to ensure that the
name attribute is consistent with the source that we’re actually
downloading.  Perl packages are made available in
<filename>all-packages.nix</filename> through the variable
<varname>perlPackages</varname>.  For instance, if you have a package
that needs <varname>ClassC3</varname>, you would typically write

<programlisting>
foo = import ../path/to/foo.nix {
  inherit stdenv fetchurl ...;
  inherit (perlPackages) ClassC3;
};
</programlisting>

in <filename>all-packages.nix</filename>.  You can test building a
Perl package as follows:

<screen>
$ nix-build -A perlPackages.ClassC3
</screen>

<varname>buildPerlPackage</varname> adds <literal>perl-</literal> to
the start of the name attribute, so the package above is actually
called <literal>perl-Class-C3-0.21</literal>.  So to install it, you
can say:

<screen>
$ nix-env -i perl-Class-C3
</screen>

(Of course you can also install using the attribute name:
<literal>nix-env -i -A perlPackages.ClassC3</literal>.)</para>

<para>So what does <varname>buildPerlPackage</varname> do?  It does
the following:

<orderedlist>

  <listitem><para>In the configure phase, it calls <literal>perl
  Makefile.PL</literal> to generate a Makefile.  You can set the
  variable <varname>makeMakerFlags</varname> to pass flags to
  <filename>Makefile.PL</filename></para></listitem>

  <listitem><para>It adds the contents of the <envar>PERL5LIB</envar>
  environment variable to <literal>#! .../bin/perl</literal> line of
  Perl scripts as <literal>-I<replaceable>dir</replaceable></literal>
  flags.  This ensures that a script can find its
  dependencies.</para></listitem>

  <listitem><para>In the fixup phase, it writes the propagated build
  inputs (<varname>propagatedBuildInputs</varname>) to the file
  <filename>$out/nix-support/propagated-user-env-packages</filename>.
  <command>nix-env</command> recursively installs all packages listed
  in this file when you install a package that has it.  This ensures
  that a Perl package can find its dependencies.</para></listitem>

</orderedlist>

</para>

<para><varname>buildPerlPackage</varname> is built on top of
<varname>stdenv</varname>, so everything can be customised in the
usual way.  For instance, the <literal>BerkeleyDB</literal> module has
a <varname>preConfigure</varname> hook to generate a configuration
file used by <filename>Makefile.PL</filename>:

<programlisting>
{buildPerlPackage, fetchurl, db}:

buildPerlPackage rec {
  name = "BerkeleyDB-0.36";

  src = fetchurl {
    url = "mirror://cpan/authors/id/P/PM/PMQS/${name}.tar.gz";
    sha256 = "07xf50riarb60l1h6m2dqmql8q5dij619712fsgw7ach04d8g3z1";
  };

  preConfigure = ''
    echo "LIB = ${db}/lib" > config.in
    echo "INCLUDE = ${db}/include" >> config.in
  '';
}
</programlisting>

</para>

<para>Dependencies on other Perl packages can be specified in the
<varname>buildInputs</varname> and
<varname>propagatedBuildInputs</varname> attributes.  If something is
exclusively a build-time dependency, use
<varname>buildInputs</varname>; if it’s (also) a runtime dependency,
use <varname>propagatedBuildInputs</varname>.  For instance, this
builds a Perl module that has runtime dependencies on a bunch of other
modules:

<programlisting>
ClassC3Componentised = buildPerlPackage rec {
  name = "Class-C3-Componentised-1.0004";
  src = fetchurl {
    url = "mirror://cpan/authors/id/A/AS/ASH/${name}.tar.gz";
    sha256 = "0xql73jkcdbq4q9m0b0rnca6nrlvf5hyzy8is0crdk65bynvs8q1";
  };
  propagatedBuildInputs = [
    ClassC3 ClassInspector TestException MROCompat
  ];
};
</programlisting>

</para>

<section><title>Generation from CPAN</title>

<para>Nix expressions for Perl packages can be generated (almost)
automatically from CPAN.  This is done by the program
<command>nix-generate-from-cpan</command>, which can be installed
as follows:</para>

<screen>
$ nix-env -i nix-generate-from-cpan
</screen>

<para>This program takes a Perl module name, looks it up on CPAN,
fetches and unpacks the corresponding package, and prints a Nix
expression on standard output.  For example:

<screen>
$ nix-generate-from-cpan XML::Simple
  XMLSimple = buildPerlPackage {
    name = "XML-Simple-2.20";
    src = fetchurl {
      url = mirror://cpan/authors/id/G/GR/GRANTM/XML-Simple-2.20.tar.gz;
      sha256 = "5cff13d0802792da1eb45895ce1be461903d98ec97c9c953bc8406af7294434a";
    };
    propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ];
    meta = {
      description = "Easily read/write XML (esp config files)";
      license = "perl";
    };
  };
</screen>

The output can be pasted into
<filename>pkgs/top-level/perl-packages.nix</filename> or wherever else
you need it.</para>

</section>

</section>


<section><title>Python</title>

<para>
  Python packages that
  use <link xlink:href="http://pypi.python.org/pypi/setuptools/"><literal>setuptools</literal></link>,
  which many Python packages do nowadays, can be built very simply using
  the <varname>buildPythonPackage</varname> function.  This function is
  implemented
  in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix"><filename>pkgs/development/python-modules/generic/default.nix</filename></link>
  and works similarly to <varname>buildPerlPackage</varname>. (See
  <xref linkend="ssec-language-perl"/> for details.)
</para>

<para>
  Python packages that use <varname>buildPythonPackage</varname> are
  defined
  in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/python-packages.nix"><filename>pkgs/top-level/python-packages.nix</filename></link>.
  Most of them are simple.  For example:

  <programlisting>
twisted = buildPythonPackage {
  name = "twisted-8.1.0";

  src = fetchurl {
    url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
    sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
  };

  propagatedBuildInputs = [ pkgs.ZopeInterface ];

  meta = {
    homepage = http://twistedmatrix.com/;
    description = "Twisted, an event-driven networking engine written in Python";
    license = "MIT";
  };
};
  </programlisting>
</para>

</section>


<section xml:id="ssec-language-java"><title>Java</title>

<para>Ant-based Java packages are typically built from source as follows:

<programlisting>
stdenv.mkDerivation {
  name = "...";
  src = fetchurl { ... };

  buildInputs = [ jdk ant ];

  buildPhase = "ant";
}
</programlisting>

Note that <varname>jdk</varname> is an alias for the OpenJDK.</para>

<para>JAR files that are intended to be used by other packages should
be installed in <filename>$out/share/java</filename>.  The OpenJDK has
a stdenv setup hook that adds any JARs in the
<filename>share/java</filename> directories of the build inputs to the
<envar>CLASSPATH</envar> environment variable.  For instance, if the
package <literal>libfoo</literal> installs a JAR named
<filename>foo.jar</filename> in its <filename>share/java</filename>
directory, and another package declares the attribute

<programlisting>
buildInputs = [ jdk libfoo ];
</programlisting>

then <envar>CLASSPATH</envar> will be set to
<filename>/nix/store/...-libfoo/share/java/foo.jar</filename>.</para>

<para>Private JARs
should be installed in a location like
<filename>$out/share/<replaceable>package-name</replaceable></filename>.</para>

<para>If your Java package provides a program, you need to generate a
wrapper script to run it using the OpenJRE.  You can use
<literal>makeWrapper</literal> for this:

<programlisting>
buildInputs = [ makeWrapper ];

installPhase =
  ''
    mkdir -p $out/bin
    makeWrapper ${jre}/bin/java $out/bin/foo \
      --add-flags "-cp $out/share/java/foo.jar org.foo.Main"
  '';
</programlisting>

Note the use of <literal>jre</literal>, which is the part of the
OpenJDK package that contains the Java Runtime Environment.  By using
<literal>${jre}/bin/java</literal> instead of
<literal>${jdk}/bin/java</literal>, you prevent your package from
depending on the JDK at runtime.</para>

<para>It is possible to use a different Java compiler than
<command>javac</command> from the OpenJDK.  For instance, to use the
Eclipse Java Compiler:

<programlisting>
buildInputs = [ jre ant ecj ];
</programlisting>

(Note that here you don’t need the full JDK as an input, but just the
JRE.)  The ECJ has a stdenv setup hook that sets some environment
variables to cause Ant to use ECJ, but this doesn’t work with all Ant
files.  Similarly, you can use the GNU Java Compiler:

<programlisting>
buildInputs = [ gcj ant ];
</programlisting>

Here, Ant will automatically use <command>gij</command> (the GNU Java
Runtime) instead of the OpenJRE.</para>

</section>


<!--
<section><title>Haskell</title>

<para>TODO</para>

</section>


<section><title>TeX / LaTeX</title>

<para>* Special support for building TeX documents</para>

</section>
-->


</chapter>