Perl: Difference between revisions
imported>Nix add link to Perl section of Nixpkgs manual |
No edit summary |
||
| (8 intermediate revisions by 6 users not shown) | |||
| Line 28: | Line 28: | ||
=== Invoking nix-shell on command-line === | === Invoking nix-shell on command-line === | ||
< | If you run a perl script and encounter a dependency error like this: | ||
<syntaxHighlight lang=shell> | |||
Can't locate DB_File.pm in @INC (you may need to install the DB_File module) | |||
</syntaxHighlight > | |||
... use <code>nix-shell</code> to create a shell environment which includes the dependency. Here we searched NixOS packages and found an existing perl package which suits, [https://search.nixos.org/packages?channel=unstable&from=0&size=30&sort=relevance&type=packages&query=dbfile like so]. | |||
<syntaxHighlight lang=shell> | |||
nix-shell -p perl perl534Packages.DBFile --run ./myscript.pl | |||
</syntaxHighlight > | |||
=== There is no /usr/bin/perl === | === There is no /usr/bin/perl === | ||
| Line 45: | Line 55: | ||
==Adding something from CPAN to nixpkgs== | ==Adding something from CPAN to nixpkgs== | ||
# Enter a <tt>nix-shell</tt> that provides the necessary dependencies: <syntaxHighlight lang=shell>nix-shell -p perl perlPackages.CPANPLUS perlPackages.GetoptLongDescriptive perlPackages.LogLog4perl perlPackages.Readonly</syntaxHighlight>. | |||
# Use the <tt>nix-generate-from-cpan.pl</tt> script (see <tt>nixpkgs/maintainers/scripts/</tt>) to generate something appropriate.<br/>Example usage: <syntaxHighlight lang=shell>nix-generate-from-cpan.pl Devel::REPL</syntaxHighlight> | |||
# After reviewing the result from the previous step and making appropriate modifications, add it to <code>pkgs/top-level/perl-packages.nix</code>. Note that some things use <code>buildPerlPackage</code> while some use <code>buildPerlModule</code>. Also note the mostly-followed naming convention as well as the mostly-followed alphabetical ordering. There are plenty of examples in <tt>perl-packages.nix</tt> — use the source, Luke! | |||
# Build and test. | |||
==Adding something without a Makefile.PL== | |||
The perlPackages.buildPerlPackage assumes a Makefile.PL to exist as part of the configurePhase. | |||
If no such file is present in the project you are building, it is easiest to add a Makefile.PL into the source yourself. | |||
The example below does so by adding it using the postPatch hook: | |||
<syntaxHighlight lang="nix"> | |||
postPatch = '' | |||
cat <<EOF > Makefile.PL | |||
use v5.10; | |||
use ExtUtils::MakeMaker; | |||
WriteMakefile( | |||
NAME => 'RevBank', | |||
VERSION_FROM => 'revbank', | |||
ABSTRACT_FROM => 'lib/RevBank.pod', | |||
EXE_FILES => [ 'revbank' ], | |||
); | |||
EOF | |||
''; | |||
</syntaxhighlight> | |||
The documentation for ExtUtils::MakeMaker can be found [https://perldoc.perl.org/ExtUtils::MakeMaker here]. | |||
==Wrappers for installed programs== | ==Wrappers for installed programs== | ||
To make perl modules available to a program in your derivation: | To make perl modules available to a program in your derivation: | ||
# Add <code>makeWrapper</code> to <code>nativeBuildInputs</code> | |||
# Add <syntaxhighlight lang="nix"> | |||
postFixup = '' | postFixup = '' | ||
wrapProgram $out/bin/something \ | wrapProgram $out/bin/something \ | ||
| Line 59: | Line 93: | ||
''; | ''; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Also keep in mind that <code>makePerlPath</code> would not resolve transitive dependencies of Perl packages. Hence if you want to just reference top-level packages, then use <code>makeFullPerlPath</code> which would recursively resolve dependency graph for you. | |||
== See also == | == See also == | ||
* [https://nixos.org/manual/nixpkgs/ | * [https://nixos.org/manual/nixpkgs/stable/#sec-language-perl Nixpkgs Manual - Perl section] | ||
* [https://blog.stigok.com/2020/04/ | * [https://blog.stigok.com/2020/04/16/building-a-custom-perl-package-for-nixos.html Overriding an existing Perl package in NixOS] | ||
[[Category:Languages]] | [[Category:Languages]] | ||
[[Category:Perl]] | |||