1<section xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xml:id="sec-language-perl">
4
5<title>Perl</title>
6
7<para>Nixpkgs provides a function <varname>buildPerlPackage</varname>,
8a generic package builder function for any Perl package that has a
9standard <varname>Makefile.PL</varname>. It’s implemented in <link
10xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/perl-modules/generic"><filename>pkgs/development/perl-modules/generic</filename></link>.</para>
11
12<para>Perl packages from CPAN are defined in <link
13xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/perl-packages.nix"><filename>pkgs/top-level/perl-packages.nix</filename></link>,
14rather than <filename>pkgs/all-packages.nix</filename>. Most Perl
15packages are so straight-forward to build that they are defined here
16directly, rather than having a separate function for each package
17called from <filename>perl-packages.nix</filename>. However, more
18complicated packages should be put in a separate file, typically in
19<filename>pkgs/development/perl-modules</filename>. Here is an
20example of the former:
21
22<programlisting>
23ClassC3 = buildPerlPackage rec {
24 name = "Class-C3-0.21";
25 src = fetchurl {
26 url = "mirror://cpan/authors/id/F/FL/FLORA/${name}.tar.gz";
27 sha256 = "1bl8z095y4js66pwxnm7s853pi9czala4sqc743fdlnk27kq94gz";
28 };
29};
30</programlisting>
31
32Note the use of <literal>mirror://cpan/</literal>, and the
33<literal>${name}</literal> in the URL definition to ensure that the
34name attribute is consistent with the source that we’re actually
35downloading. Perl packages are made available in
36<filename>all-packages.nix</filename> through the variable
37<varname>perlPackages</varname>. For instance, if you have a package
38that needs <varname>ClassC3</varname>, you would typically write
39
40<programlisting>
41foo = import ../path/to/foo.nix {
42 inherit stdenv fetchurl ...;
43 inherit (perlPackages) ClassC3;
44};
45</programlisting>
46
47in <filename>all-packages.nix</filename>. You can test building a
48Perl package as follows:
49
50<screen>
51$ nix-build -A perlPackages.ClassC3
52</screen>
53
54<varname>buildPerlPackage</varname> adds <literal>perl-</literal> to
55the start of the name attribute, so the package above is actually
56called <literal>perl-Class-C3-0.21</literal>. So to install it, you
57can say:
58
59<screen>
60$ nix-env -i perl-Class-C3
61</screen>
62
63(Of course you can also install using the attribute name:
64<literal>nix-env -i -A perlPackages.ClassC3</literal>.)</para>
65
66<para>So what does <varname>buildPerlPackage</varname> do? It does
67the following:
68
69<orderedlist>
70
71 <listitem><para>In the configure phase, it calls <literal>perl
72 Makefile.PL</literal> to generate a Makefile. You can set the
73 variable <varname>makeMakerFlags</varname> to pass flags to
74 <filename>Makefile.PL</filename></para></listitem>
75
76 <listitem><para>It adds the contents of the <envar>PERL5LIB</envar>
77 environment variable to <literal>#! .../bin/perl</literal> line of
78 Perl scripts as <literal>-I<replaceable>dir</replaceable></literal>
79 flags. This ensures that a script can find its
80 dependencies.</para></listitem>
81
82 <listitem><para>In the fixup phase, it writes the propagated build
83 inputs (<varname>propagatedBuildInputs</varname>) to the file
84 <filename>$out/nix-support/propagated-user-env-packages</filename>.
85 <command>nix-env</command> recursively installs all packages listed
86 in this file when you install a package that has it. This ensures
87 that a Perl package can find its dependencies.</para></listitem>
88
89</orderedlist>
90
91</para>
92
93<para><varname>buildPerlPackage</varname> is built on top of
94<varname>stdenv</varname>, so everything can be customised in the
95usual way. For instance, the <literal>BerkeleyDB</literal> module has
96a <varname>preConfigure</varname> hook to generate a configuration
97file used by <filename>Makefile.PL</filename>:
98
99<programlisting>
100{ buildPerlPackage, fetchurl, db }:
101
102buildPerlPackage rec {
103 name = "BerkeleyDB-0.36";
104
105 src = fetchurl {
106 url = "mirror://cpan/authors/id/P/PM/PMQS/${name}.tar.gz";
107 sha256 = "07xf50riarb60l1h6m2dqmql8q5dij619712fsgw7ach04d8g3z1";
108 };
109
110 preConfigure = ''
111 echo "LIB = ${db}/lib" > config.in
112 echo "INCLUDE = ${db}/include" >> config.in
113 '';
114}
115</programlisting>
116
117</para>
118
119<para>Dependencies on other Perl packages can be specified in the
120<varname>buildInputs</varname> and
121<varname>propagatedBuildInputs</varname> attributes. If something is
122exclusively a build-time dependency, use
123<varname>buildInputs</varname>; if it’s (also) a runtime dependency,
124use <varname>propagatedBuildInputs</varname>. For instance, this
125builds a Perl module that has runtime dependencies on a bunch of other
126modules:
127
128<programlisting>
129ClassC3Componentised = buildPerlPackage rec {
130 name = "Class-C3-Componentised-1.0004";
131 src = fetchurl {
132 url = "mirror://cpan/authors/id/A/AS/ASH/${name}.tar.gz";
133 sha256 = "0xql73jkcdbq4q9m0b0rnca6nrlvf5hyzy8is0crdk65bynvs8q1";
134 };
135 propagatedBuildInputs = [
136 ClassC3 ClassInspector TestException MROCompat
137 ];
138};
139</programlisting>
140
141</para>
142
143<section xml:id="ssec-generation-from-CPAN"><title>Generation from CPAN</title>
144
145<para>Nix expressions for Perl packages can be generated (almost)
146automatically from CPAN. This is done by the program
147<command>nix-generate-from-cpan</command>, which can be installed
148as follows:</para>
149
150<screen>
151$ nix-env -i nix-generate-from-cpan
152</screen>
153
154<para>This program takes a Perl module name, looks it up on CPAN,
155fetches and unpacks the corresponding package, and prints a Nix
156expression on standard output. For example:
157
158<screen>
159$ nix-generate-from-cpan XML::Simple
160 XMLSimple = buildPerlPackage rec {
161 name = "XML-Simple-2.22";
162 src = fetchurl {
163 url = "mirror://cpan/authors/id/G/GR/GRANTM/${name}.tar.gz";
164 sha256 = "b9450ef22ea9644ae5d6ada086dc4300fa105be050a2030ebd4efd28c198eb49";
165 };
166 propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ];
167 meta = {
168 description = "An API for simple XML files";
169 license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
170 };
171 };
172</screen>
173
174The output can be pasted into
175<filename>pkgs/top-level/perl-packages.nix</filename> or wherever else
176you need it.</para>
177
178</section>
179
180</section>
181