nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at master 156 lines 6.8 kB view raw view rendered
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 37{ 38 ClassC3 = buildPerlPackage rec { 39 pname = "Class-C3"; 40 version = "0.21"; 41 src = fetchurl { 42 url = "mirror://cpan/authors/id/F/FL/FLORA/Class-C3-${version}.tar.gz"; 43 hash = "sha256-/5GE5xHT0uYGOQxroqj6LMU7CtKn2s6vMVoSXxL4iK4="; 44 }; 45 }; 46} 47``` 48 49Note the use of `mirror://cpan/`, and the `pname` and `version` in the URL definition to ensure that the `pname` 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 50 51```nix 52{ 53 foo = import ../path/to/foo.nix { 54 inherit 55 stdenv 56 fetchurl # ... 57 ; 58 inherit (perlPackages) ClassC3; 59 }; 60} 61``` 62 63in `all-packages.nix`. You can test building a Perl package as follows: 64 65```ShellSession 66$ nix-build -A perlPackages.ClassC3 67``` 68 69To install it with `nix-env` instead: `nix-env -f. -iA perlPackages.ClassC3`. 70 71So what does `buildPerlPackage` do? It does the following: 72 731. 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` 742. It adds the contents of the `PERL5LIB` environment variable to a use lib statement at the start of Perl scripts. This ensures that a script can find its dependencies. 753. 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. 76 77`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`: 78 79```nix 80{ 81 buildPerlPackage, 82 fetchurl, 83 db, 84}: 85 86buildPerlPackage rec { 87 pname = "BerkeleyDB"; 88 version = "0.36"; 89 90 src = fetchurl { 91 url = "mirror://cpan/authors/id/P/PM/PMQS/BerkeleyDB-${version}.tar.gz"; 92 hash = "sha256-4Y+HGgGQqcOfdiKcFIyMrWBEccVNVAMDBWZlFTMorh8="; 93 }; 94 95 preConfigure = '' 96 echo "LIB = ${db.out}/lib" > config.in 97 echo "INCLUDE = ${db.dev}/include" >> config.in 98 ''; 99} 100``` 101 102Dependencies 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: 103 104```nix 105{ 106 ClassC3Componentised = buildPerlPackage rec { 107 pname = "Class-C3-Componentised"; 108 version = "1.0004"; 109 src = fetchurl { 110 url = "mirror://cpan/authors/id/A/AS/ASH/Class-C3-Componentised-${version}.tar.gz"; 111 hash = "sha256-ASO9rV/FzJYZ0BH572Fxm2ZrFLMZLFATJng1NuU4FHc="; 112 }; 113 propagatedBuildInputs = [ 114 ClassC3 115 ClassInspector 116 TestException 117 MROCompat 118 ]; 119 }; 120} 121``` 122 123### Generation from CPAN {#ssec-generation-from-CPAN} 124 125Nix 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: 126 127```ShellSession 128$ nix-env -f "<nixpkgs>" -iA nix-generate-from-cpan 129``` 130 131Substitute `<nixpkgs>` by the path of a nixpkgs clone to use the latest version. 132 133This 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: 134 135```ShellSession 136$ nix-generate-from-cpan XML::Simple 137 XMLSimple = buildPerlPackage rec { 138 pname = "XML-Simple"; 139 version = "2.22"; 140 src = fetchurl { 141 url = "mirror://cpan/authors/id/G/GR/GRANTM/XML-Simple-2.22.tar.gz"; 142 hash = "sha256-uUUO8i6pZErl1q2ghtxDAPoQW+BQogMOvU79KMGY60k="; 143 }; 144 propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ]; 145 meta = { 146 description = "API for simple XML files"; 147 license = with lib.licenses; [ artistic1 gpl1Plus ]; 148 }; 149 }; 150``` 151 152The output can be pasted into `pkgs/top-level/perl-packages.nix` or wherever else you need it. 153 154### Cross-compiling modules {#ssec-perl-cross-compilation} 155 156Nixpkgs 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.