git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #86589 from raboof/document-running-perl-scripts-from-the-shell

doc: add section on running ad-hoc Perl programs to the nixpkgs manual

commit3829979d8d29c9f24d52768a0fe485cfb3257a2b authorJörg Thalheim <Mic92@users.noreply.github.com> dateFri 17 Jul 2020, 10:20 (6 years ago) parentfd726a96093372
1 file changed+176−142
doc/languages-frameworks/perl.xml+176−142
doc/languages-frameworks/perl.xml+176−142
@@ -3,159 +3,193 @@
33 xml:id="sec-language-perl">
44 <title>Perl</title>
55
6− <para>
7− 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
8−xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/perl-modules/generic"><filename>pkgs/development/perl-modules/generic</filename></link>.
9− </para>
10−
11− <para>
12− Perl packages from CPAN are defined in <link
13−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:
14−<programlisting>
15−ClassC3 = buildPerlPackage rec {
16− name = "Class-C3-0.21";
17− src = fetchurl {
18− url = "mirror://cpan/authors/id/F/FL/FLORA/${name}.tar.gz";
19− sha256 = "1bl8z095y4js66pwxnm7s853pi9czala4sqc743fdlnk27kq94gz";
20− };
21−};
22−</programlisting>
23− 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
24−<programlisting>
25−foo = import ../path/to/foo.nix {
26− inherit stdenv fetchurl ...;
27− inherit (perlPackages) ClassC3;
28−};
29−</programlisting>
30− in <filename>all-packages.nix</filename>. You can test building a Perl package as follows:
31−<screen>
32−<prompt>$ </prompt>nix-build -A perlPackages.ClassC3
33−</screen>
34− <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:
35−<screen>
36−<prompt>$ </prompt>nix-env -i perl-Class-C3
37−</screen>
38− (Of course you can also install using the attribute name: <literal>nix-env -i -A perlPackages.ClassC3</literal>.)
39− </para>
40−
41− <para>
42− So what does <varname>buildPerlPackage</varname> do? It does the following:
43− <orderedlist>
44− <listitem>
45− <para>
46− 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>
47− </para>
48− </listitem>
49− <listitem>
50− <para>
51− 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. (This can cause this shebang line to become too long for Darwin to handle; see the note below.)
52− </para>
53− </listitem>
54− <listitem>
55− <para>
56− 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.
57− </para>
58− </listitem>
59− </orderedlist>
60− </para>
61−
62− <para>
63− <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>:
64−<programlisting>
65−{ buildPerlPackage, fetchurl, db }:
66−
67−buildPerlPackage rec {
68− name = "BerkeleyDB-0.36";
69−
70− src = fetchurl {
71− url = "mirror://cpan/authors/id/P/PM/PMQS/${name}.tar.gz";
72− sha256 = "07xf50riarb60l1h6m2dqmql8q5dij619712fsgw7ach04d8g3z1";
73− };
74−
75− preConfigure = ''
76− echo "LIB = ${db.out}/lib" > config.in
77− echo "INCLUDE = ${db.dev}/include" >> config.in
78− '';
79−}
80−</programlisting>
81− </para>
82−
83− <para>
84− 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:
85−<programlisting>
86−ClassC3Componentised = buildPerlPackage rec {
87− name = "Class-C3-Componentised-1.0004";
88− src = fetchurl {
89− url = "mirror://cpan/authors/id/A/AS/ASH/${name}.tar.gz";
90− sha256 = "0xql73jkcdbq4q9m0b0rnca6nrlvf5hyzy8is0crdk65bynvs8q1";
91− };
92− propagatedBuildInputs = [
93− ClassC3 ClassInspector TestException MROCompat
94− ];
95−};
96−</programlisting>
97− </para>
98−
99− <para>
100− On Darwin, if a script has too many <literal>-I<replaceable>dir</replaceable></literal> flags in its first line (its “shebang line”), it will not run. This can be worked around by calling the <literal>shortenPerlShebang</literal> function from the <literal>postInstall</literal> phase:
101−<programlisting>
102−{ stdenv, buildPerlPackage, fetchurl, shortenPerlShebang }:
103−
104−ImageExifTool = buildPerlPackage {
105− pname = "Image-ExifTool";
106− version = "11.50";
107−
108− src = fetchurl {
109− url = "https://www.sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.50.tar.gz";
110− sha256 = "0d8v48y94z8maxkmw1rv7v9m0jg2dc8xbp581njb6yhr7abwqdv3";
111− };
112−
113− buildInputs = stdenv.lib.optional stdenv.isDarwin shortenPerlShebang;
114− postInstall = stdenv.lib.optional stdenv.isDarwin ''
115− shortenPerlShebang $out/bin/exiftool
116− '';
117−};
118−</programlisting>
119− This will remove the <literal>-I</literal> flags from the shebang line, rewrite them in the <literal>use lib</literal> form, and put them on the next line instead. This function can be given any number of Perl scripts as arguments; it will modify them in-place.
120− </para>
121−
122− <section xml:id="ssec-generation-from-CPAN">
123− <title>Generation from CPAN</title>
6+ <section xml:id="ssec-perl-running">
7+ <title>Running perl programs on the shell</title>
1248
1259 <para>
126− 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:
10+ When executing a Perl script, it is possible you get an error such as <literal>./myscript.pl: bad interpreter: /usr/bin/perl: no such file or directory</literal>. This happens when the script expects Perl to be installed at <filename>/usr/bin/perl</filename>, which is not the case when using Perl from nixpkgs. You can fix the script by changing the first line to:
11+ <programlisting>
12+ #!/usr/bin/env perl
13+ </programlisting>
14+ to take the Perl installation from the <literal>PATH<literal> environment variable, or invoke Perl directly with:
15+ <screen>
16+ <prompt>$ </prompt>perl ./myscript.pl
17+ </screen>
12718 </para>
12819
129−<screen>
130−<prompt>$ </prompt>nix-env -i nix-generate-from-cpan
131−</screen>
20+ <para>
21+ When the script is using a Perl library that is not installed globally, you might get an error such as <literal>Can't locate DB_File.pm in @INC (you may need to install the DB_File module)</literal>. In that case, you can use <command>nix-shell</command> to start an ad-hoc shell with that library installed, for instance:
22+ <screen>
23+ <prompt>$ </prompt>nix-shell -p perl perlPackages.DBFile --run ./myscript.pl
24+ </screen>
25+ </para>
13226
13327 <para>
134− 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:
135−<screen>
136−<prompt>$ </prompt>nix-generate-from-cpan XML::Simple
137− XMLSimple = buildPerlPackage rec {
138− name = "XML-Simple-2.22";
139− src = fetchurl {
140− url = "mirror://cpan/authors/id/G/GR/GRANTM/${name}.tar.gz";
141− sha256 = "b9450ef22ea9644ae5d6ada086dc4300fa105be050a2030ebd4efd28c198eb49";
142− };
143− propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ];
144− meta = {
145− description = "An API for simple XML files";
146− license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
147− };
148− };
149−</screen>
150− The output can be pasted into <filename>pkgs/top-level/perl-packages.nix</filename> or wherever else you need it.
28+ If you are always using the script in places where <command>nix-shell</command> is available, you can embed the <command>nix-shell</command> invocation in the shebang like this:
29+ <programlisting>
30+ #!/usr/bin/env nix-shell
31+ #! nix-shell -i perl -p perl perlPackages.DBFile
32+ </programlisting>
15133 </para>
15234 </section>
15335
154− <section xml:id="ssec-perl-cross-compilation">
155− <title>Cross-compiling modules</title>
36+ <section xml:id="ssec-perl-packaging">
37+ <title>Packaging Perl programs</title>
38+
39+ <para>
40+ 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
41+ xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/perl-modules/generic"><filename>pkgs/development/perl-modules/generic</filename></link>.
42+ </para>
43+
44+ <para>
45+ Perl packages from CPAN are defined in <link
46+ 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:
47+ <programlisting>
48+ ClassC3 = buildPerlPackage rec {
49+ name = "Class-C3-0.21";
50+ src = fetchurl {
51+ url = "mirror://cpan/authors/id/F/FL/FLORA/${name}.tar.gz";
52+ sha256 = "1bl8z095y4js66pwxnm7s853pi9czala4sqc743fdlnk27kq94gz";
53+ };
54+ };
55+ </programlisting>
56+ 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
57+ <programlisting>
58+ foo = import ../path/to/foo.nix {
59+ inherit stdenv fetchurl ...;
60+ inherit (perlPackages) ClassC3;
61+ };
62+ </programlisting>
63+ in <filename>all-packages.nix</filename>. You can test building a Perl package as follows:
64+ <screen>
65+ <prompt>$ </prompt>nix-build -A perlPackages.ClassC3
66+ </screen>
67+ <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:
68+ <screen>
69+ <prompt>$ </prompt>nix-env -i perl-Class-C3
70+ </screen>
71+ (Of course you can also install using the attribute name: <literal>nix-env -i -A perlPackages.ClassC3</literal>.)
72+ </para>
73+
74+ <para>
75+ So what does <varname>buildPerlPackage</varname> do? It does the following:
76+ <orderedlist>
77+ <listitem>
78+ <para>
79+ 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>
80+ </para>
81+ </listitem>
82+ <listitem>
83+ <para>
84+ 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. (This can cause this shebang line to become too long for Darwin to handle; see the note below.)
85+ </para>
86+ </listitem>
87+ <listitem>
88+ <para>
89+ 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.
90+ </para>
91+ </listitem>
92+ </orderedlist>
93+ </para>
94+
95+ <para>
96+ <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>:
97+ <programlisting>
98+ { buildPerlPackage, fetchurl, db }:
99+
100+ buildPerlPackage rec {
101+ name = "BerkeleyDB-0.36";
102+
103+ src = fetchurl {
104+ url = "mirror://cpan/authors/id/P/PM/PMQS/${name}.tar.gz";
105+ sha256 = "07xf50riarb60l1h6m2dqmql8q5dij619712fsgw7ach04d8g3z1";
106+ };
107+
108+ preConfigure = ''
109+ echo "LIB = ${db.out}/lib" > config.in
110+ echo "INCLUDE = ${db.dev}/include" >> config.in
111+ '';
112+ }
113+ </programlisting>
114+ </para>
156115
157116 <para>
158− Nixpkgs has experimental support for cross-compiling Perl modules. In many cases, it will just work out of the box, even for modules with native extensions. Sometimes, however, the Makefile.PL for a module may (indirectly) import a native module. In that case, you will need to make a stub for that module that will satisfy the Makefile.PL and install it into <filename>lib/perl5/site_perl/cross_perl/${perl.version}</filename>. See the <varname>postInstall</varname> for <varname>DBI</varname> for an example.
117+ 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:
118+ <programlisting>
119+ ClassC3Componentised = buildPerlPackage rec {
120+ name = "Class-C3-Componentised-1.0004";
121+ src = fetchurl {
122+ url = "mirror://cpan/authors/id/A/AS/ASH/${name}.tar.gz";
123+ sha256 = "0xql73jkcdbq4q9m0b0rnca6nrlvf5hyzy8is0crdk65bynvs8q1";
124+ };
125+ propagatedBuildInputs = [
126+ ClassC3 ClassInspector TestException MROCompat
127+ ];
128+ };
129+ </programlisting>
159130 </para>
131+
132+ <para>
133+ On Darwin, if a script has too many <literal>-I<replaceable>dir</replaceable></literal> flags in its first line (its “shebang line”), it will not run. This can be worked around by calling the <literal>shortenPerlShebang</literal> function from the <literal>postInstall</literal> phase:
134+ <programlisting>
135+ { stdenv, buildPerlPackage, fetchurl, shortenPerlShebang }:
136+
137+ ImageExifTool = buildPerlPackage {
138+ pname = "Image-ExifTool";
139+ version = "11.50";
140+
141+ src = fetchurl {
142+ url = "https://www.sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.50.tar.gz";
143+ sha256 = "0d8v48y94z8maxkmw1rv7v9m0jg2dc8xbp581njb6yhr7abwqdv3";
144+ };
145+
146+ buildInputs = stdenv.lib.optional stdenv.isDarwin shortenPerlShebang;
147+ postInstall = stdenv.lib.optional stdenv.isDarwin ''
148+ shortenPerlShebang $out/bin/exiftool
149+ '';
150+ };
151+ </programlisting>
152+ This will remove the <literal>-I</literal> flags from the shebang line, rewrite them in the <literal>use lib</literal> form, and put them on the next line instead. This function can be given any number of Perl scripts as arguments; it will modify them in-place.
153+ </para>
154+
155+ <section xml:id="ssec-generation-from-CPAN">
156+ <title>Generation from CPAN</title>
157+
158+ <para>
159+ 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:
160+ </para>
161+
162+ <screen>
163+ <prompt>$ </prompt>nix-env -i nix-generate-from-cpan
164+ </screen>
165+
166+ <para>
167+ 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:
168+ <screen>
169+ <prompt>$ </prompt>nix-generate-from-cpan XML::Simple
170+ XMLSimple = buildPerlPackage rec {
171+ name = "XML-Simple-2.22";
172+ src = fetchurl {
173+ url = "mirror://cpan/authors/id/G/GR/GRANTM/${name}.tar.gz";
174+ sha256 = "b9450ef22ea9644ae5d6ada086dc4300fa105be050a2030ebd4efd28c198eb49";
175+ };
176+ propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ];
177+ meta = {
178+ description = "An API for simple XML files";
179+ license = with stdenv.lib.licenses; [ artistic1 gpl1Plus ];
180+ };
181+ };
182+ </screen>
183+ The output can be pasted into <filename>pkgs/top-level/perl-packages.nix</filename> or wherever else you need it.
184+ </para>
185+ </section>
186+
187+ <section xml:id="ssec-perl-cross-compilation">
188+ <title>Cross-compiling modules</title>
189+
190+ <para>
191+ Nixpkgs has experimental support for cross-compiling Perl modules. In many cases, it will just work out of the box, even for modules with native extensions. Sometimes, however, the Makefile.PL for a module may (indirectly) import a native module. In that case, you will need to make a stub for that module that will satisfy the Makefile.PL and install it into <filename>lib/perl5/site_perl/cross_perl/${perl.version}</filename>. See the <varname>postInstall</varname> for <varname>DBI</varname> for an example.
192+ </para>
193+ </section>
160194 </section>
161195</section>