Home Explore Blog CI



nixpkgs

3rd chunk of `doc/languages-frameworks/perl.section.md`
43566d59fd3e07ba3dbaf8d73bd5693e94ae43e997d813010000000100000d8c
Dependencies 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:

```nix
{
  ClassC3Componentised = buildPerlPackage rec {
    pname = "Class-C3-Componentised";
    version = "1.0004";
    src = fetchurl {
      url = "mirror://cpan/authors/id/A/AS/ASH/Class-C3-Componentised-${version}.tar.gz";
      hash = "sha256-ASO9rV/FzJYZ0BH572Fxm2ZrFLMZLFATJng1NuU4FHc=";
    };
    propagatedBuildInputs = [
      ClassC3
      ClassInspector
      TestException
      MROCompat
    ];
  };
}
```

On 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:

```nix
{
  lib,
  stdenv,
  buildPerlPackage,
  fetchurl,
  shortenPerlShebang,
}:

{
  ImageExifTool = buildPerlPackage {
    pname = "Image-ExifTool";
    version = "12.50";

    src = fetchurl {
      url = "https://exiftool.org/Image-ExifTool-${version}.tar.gz";
      hash = "sha256-vOhB/FwQMC8PPvdnjDvxRpU6jAZcC6GMQfc0AH4uwKg=";
    };

    nativeBuildInputs = lib.optional stdenv.hostPlatform.isDarwin shortenPerlShebang;
    postInstall = lib.optionalString stdenv.hostPlatform.isDarwin ''
      shortenPerlShebang $out/bin/exiftool
    '';
  };
}
```

This 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.

### Generation from CPAN {#ssec-generation-from-CPAN}

Nix 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:

```ShellSession
$ nix-env -f "<nixpkgs>" -iA nix-generate-from-cpan
```

Substitute `<nixpkgs>` by the path of a nixpkgs clone to use the latest version.

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:

```ShellSession
$ nix-generate-from-cpan XML::Simple
  XMLSimple = buildPerlPackage rec {
    pname = "XML-Simple";
    version = "2.22";
    src = fetchurl {
      url = "mirror://cpan/authors/id/G/GR/GRANTM/XML-Simple-2.22.tar.gz";
      hash = "sha256-uUUO8i6pZErl1q2ghtxDAPoQW+BQogMOvU79KMGY60k=";
    };
    propagatedBuildInputs = [ XMLNamespaceSupport XMLSAX XMLSAXExpat ];
    meta = {
      description = "API for simple XML files";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
```

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

### Cross-compiling modules {#ssec-perl-cross-compilation}

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 `lib/perl5/site_perl/cross_perl/${perl.version}`. See the `postInstall` for `DBI` for an example.

Title: Addressing Darwin Shebang Issues, Generating Packages from CPAN, and Cross-Compiling Modules
Summary
This section continues discussing Perl package building, focusing on resolving shebang line limitations on Darwin using `shortenPerlShebang`. It then introduces `nix-generate-from-cpan`, a tool to automatically generate Nix expressions for Perl packages from CPAN, providing an example usage. Finally, it touches on cross-compiling Perl modules in Nixpkgs, mentioning the need for stubs for native modules during the Makefile.PL phase and referencing the DBI package as an example.