1# Perl {#sec-language-perl} 2 3## Running perl programs on the shell {#ssec-perl-running} 4 5When executing a Perl script, it is possible you get an error such as `./myscript.pl: bad interpreter: /usr/bin/perl: no such file or directory`. This happens when the script expects Perl to be installed at `/usr/bin/perl`, which is not the case when using Perl from nixpkgs. You can fix the script by changing the first line to: 6 7```perl 8#!/usr/bin/env perl 9``` 10 11to take the Perl installation from the `PATH` environment variable, or invoke Perl directly with: 12 13```ShellSession 14$ perl ./myscript.pl 15``` 16 17When the script is using a Perl library that is not installed globally, you might get an error such as `Can't locate DB_File.pm in @INC (you may need to install the DB_File module)`. In that case, you can use `nix-shell` to start an ad-hoc shell with that library installed, for instance: 18 19```ShellSession 20$ nix-shell -p perl perlPackages.DBFile --run ./myscript.pl 21``` 22 23If you are always using the script in places where `nix-shell` is available, you can embed the `nix-shell` invocation in the shebang like this: 24 25```perl 26#!/usr/bin/env nix-shell 27#! nix-shell -i perl -p perl perlPackages.DBFile 28``` 29 30## Packaging Perl programs {#ssec-perl-packaging} 31 32Nixpkgs provides a function `buildPerlPackage`, a generic package builder function for any Perl package that has a standard `Makefile.PL`. It’s implemented in [pkgs/development/perl-modules/generic](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/perl-modules/generic). 33 34Perl packages from CPAN are defined in [pkgs/top-level/perl-packages.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/perl-packages.nix) rather than `pkgs/all-packages.nix`. 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 `perl-packages.nix`. However, more complicated packages should be put in a separate file, typically in `pkgs/development/perl-modules`. Here is an example of the former: 35 36```nix 37ClassC3 = buildPerlPackage rec { 38 name = "Class-C3-0.21"; 39 src = fetchurl { 40 url = "mirror://cpan/authors/id/F/FL/FLORA/${name}.tar.gz"; 41 sha256 = "1bl8z095y4js66pwxnm7s853pi9czala4sqc743fdlnk27kq94gz"; 42 }; 43}; 44``` 45 46Note the use of `mirror://cpan/`, and the `${name}` 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 `all-packages.nix` through the variable `perlPackages`. For instance, if you have a package that needs `ClassC3`, you would typically write 47 48```nix 49foo = import ../path/to/foo.nix { 50 inherit stdenv fetchurl ...; 51 inherit (perlPackages) ClassC3; 52}; 53``` 54 55in `all-packages.nix`. You can test building a Perl package as follows: 56 57```ShellSession 58$ nix-build -A perlPackages.ClassC3 59``` 60 61`buildPerlPackage` adds `perl-` to the start of the name attribute, so the package above is actually called `perl-Class-C3-0.21`. So to install it, you can say: 62 63```ShellSession 64$ nix-env -i perl-Class-C3 65``` 66 67(Of course you can also install using the attribute name: `nix-env -i -A perlPackages.ClassC3`.) 68 69So what does `buildPerlPackage` do? It does the following: 70 711. In the configure phase, it calls `perl Makefile.PL` to generate a Makefile. You can set the variable `makeMakerFlags` to pass flags to `Makefile.PL` 722. It adds the contents of the `PERL5LIB` environment variable to `#! .../bin/perl` line of Perl scripts as `-Idir` 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.) 733. In the fixup phase, it writes the propagated build inputs (`propagatedBuildInputs`) to the file `$out/nix-support/propagated-user-env-packages`. `nix-env` 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. 74 75`buildPerlPackage` is built on top of `stdenv`, so everything can be customised in the usual way. For instance, the `BerkeleyDB` module has a `preConfigure` hook to generate a configuration file used by `Makefile.PL`: 76 77```nix 78{ buildPerlPackage, fetchurl, db }: 79 80buildPerlPackage rec { 81 name = "BerkeleyDB-0.36"; 82 83 src = fetchurl { 84 url = "mirror://cpan/authors/id/P/PM/PMQS/${name}.tar.gz"; 85 sha256 = "07xf50riarb60l1h6m2dqmql8q5dij619712fsgw7ach04d8g3z1"; 86 }; 87 88 preConfigure = '' 89 echo "LIB = ${db.out}/lib" > config.in 90 echo "INCLUDE = ${db.dev}/include" >> config.in 91 ''; 92} 93``` 94 95Dependencies on other Perl packages can be specified in the `buildInputs` and `propagatedBuildInputs` attributes. If something is exclusively a build-time dependency, use `buildInputs`; if it’s (also) a runtime dependency, use `propagatedBuildInputs`. For instance, this builds a Perl module that has runtime dependencies on a bunch of other modules: 96 97```nix 98ClassC3Componentised = buildPerlPackage rec { 99 name = "Class-C3-Componentised-1.0004"; 100 src = fetchurl { 101 url = "mirror://cpan/authors/id/A/AS/ASH/${name}.tar.gz"; 102 sha256 = "0xql73jkcdbq4q9m0b0rnca6nrlvf5hyzy8is0crdk65bynvs8q1"; 103 }; 104 propagatedBuildInputs = [ 105 ClassC3 ClassInspector TestException MROCompat 106 ]; 107}; 108``` 109 110On Darwin, if a script has too many `-Idir` flags in its first line (its “shebang line”), it will not run. This can be worked around by calling the `shortenPerlShebang` function from the `postInstall` phase: 111 112```nix 113{ lib, stdenv, buildPerlPackage, fetchurl, shortenPerlShebang }: 114 115ImageExifTool = buildPerlPackage { 116 pname = "Image-ExifTool"; 117 version = "11.50"; 118 119 src = fetchurl { 120 url = "https://www.sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.50.tar.gz"; 121 sha256 = "0d8v48y94z8maxkmw1rv7v9m0jg2dc8xbp581njb6yhr7abwqdv3"; 122 }; 123 124 buildInputs = lib.optional stdenv.isDarwin shortenPerlShebang; 125 postInstall = lib.optionalString stdenv.isDarwin '' 126 shortenPerlShebang $out/bin/exiftool 127 ''; 128}; 129``` 130 131This will remove the `-I` flags from the shebang line, rewrite them in the `use lib` 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. 132 133### Generation from CPAN {#ssec-generation-from-CPAN} 134 135Nix expressions for Perl packages can be generated (almost) automatically from CPAN. This is done by the program `nix-generate-from-cpan`, which can be installed as follows: 136 137```ShellSession 138$ nix-env -i nix-generate-from-cpan 139``` 140 141This 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: 142 143```ShellSession 144$ nix-generate-from-cpan XML::Simple 145 XMLSimple = buildPerlPackage rec { 146 name = "XML-Simple-2.22"; 147 src = fetchurl { 148 url = "mirror://cpan/authors/id/G/GR/GRANTM/${name}.tar.gz"; 149 sha256 = "b9450ef22ea9644ae5d6ada086dc4300fa105be050a2030ebd4efd28c198eb49"; 150 }; 151 propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ]; 152 meta = { 153 description = "An API for simple XML files"; 154 license = with lib.licenses; [ artistic1 gpl1Plus ]; 155 }; 156 }; 157``` 158 159The output can be pasted into `pkgs/top-level/perl-packages.nix` or wherever else you need it. 160 161### Cross-compiling modules {#ssec-perl-cross-compilation} 162 163Nixpkgs 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 `lib/perl5/site_perl/cross_perl/${perl.version}`. See the `postInstall` for `DBI` for an example.