Home Explore Blog Models CI



nixpkgs

3rd chunk of `doc/languages-frameworks/perl.section.md`
3e8b8b88b4b9208f8dc91105a60f93ff7d078c31e7481b760000000100000d8c
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: Perl Package Dependencies, Darwin Shebang Fixes, CPAN Generation, and Cross-Compilation
Summary
This chunk details various aspects of managing Perl packages within Nixpkgs. It explains how to declare dependencies using `buildInputs` (build-time only) and `propagatedBuildInputs` (runtime and build-time). It then addresses a specific issue on Darwin where excessively long shebang lines with `-Idir` flags can prevent Perl scripts from executing, offering the `shortenPerlShebang` function as a workaround to rewrite these flags to `use lib` statements on subsequent lines. The text also introduces `nix-generate-from-cpan`, a utility to automatically create Nix expressions for Perl packages by fetching module information from CPAN. Finally, it mentions experimental support for cross-compiling Perl modules, noting that while it often works out-of-the-box, some cases may require creating stubs for native modules during cross-compilation.