Perl: Difference between revisions
imported>Nix example using nix-shell in script |
imported>Nix create a "no /usr/bin/perl" section |
||
Line 30: | Line 30: | ||
<code>Can't locate DB_File.pm in @INC (you may need to install the DB_File module)</code>: run it with <code>nix-shell -p perl -p perlPackages.DBFile --run ./myscript.pl</code> | <code>Can't locate DB_File.pm in @INC (you may need to install the DB_File module)</code>: run it with <code>nix-shell -p perl -p perlPackages.DBFile --run ./myscript.pl</code> | ||
<code>./myscript.pl: bad interpreter: /usr/bin/perl: no such file or directory</ | === There is no /usr/bin/perl === | ||
By design, there is no <code>/usr/bin/perl</code> in Nix. So you may encounter messages like: | |||
<syntaxHighlight lang=shell> | |||
./myscript.pl: bad interpreter: /usr/bin/perl: no such file or directory | |||
</syntaxHighlight> | |||
Change the first line of the script to | |||
<syntaxHighlight lang=shell> | |||
#!/usr/bin/env -S perl | |||
</syntaxHighlight> | |||
or start it with <code>perl ./myscript.pl</code> | |||
==Adding something from CPAN to nixpkgs== | ==Adding something from CPAN to nixpkgs== |
Revision as of 14:24, 24 September 2021
Running a Perl script
Replacing #! with nix-shell
Perl scripts normally start something like this:
#!/usr/bin/env perl
In Nix, we often make isolated environments using nix-shell. You can do this in the #!
(shabang) section directly in the script too. Here is an example from the manual — a Perl script that specifies that it requires Perl and the HTML::TokeParser::Simple and LWP packages:
#! /usr/bin/env nix-shell
#! nix-shell -i perl -p perl perlPackages.HTMLTokeParserSimple perlPackages.LWP
use HTML::TokeParser::Simple;
# Fetch nixos.org and print all hrefs.
my $p = HTML::TokeParser::Simple->new(url => 'http://nixos.org/');
while (my $token = $p->get_tag("a")) {
my $href = $token->get_attr("href");
print "$href\n" if $href;
}
Invoking nix-shell on command-line
Can't locate DB_File.pm in @INC (you may need to install the DB_File module)
: run it with nix-shell -p perl -p perlPackages.DBFile --run ./myscript.pl
There is no /usr/bin/perl
By design, there is no /usr/bin/perl
in Nix. So you may encounter messages like:
./myscript.pl: bad interpreter: /usr/bin/perl: no such file or directory
Change the first line of the script to
#!/usr/bin/env -S perl
or start it with perl ./myscript.pl
Adding something from CPAN to nixpkgs
- Use the nix-generate-from-cpan.pl script (see nixpkgs/maintainers/scripts/) to generate something appropriate.
Example usage: nix-generate-from-cpan.pl Devel::REPL - After reviewing the result from the previous step and making appropriate modifications, add it to
pkgs/top-level/perl-packages.nix
. Note that some things usebuildPerlPackage
while some usebuildPerlModule
. Also note the mostly-followed naming convention as well as the mostly-followed alphabetical ordering. There are plenty of examples in perl-packages.nix — use the source, Luke! - Build and test.
Wrappers for installed programs
To make perl modules available to a program in your derivation:
- add
makeWrapper
tonativeBuildInputs
- add
postFixup = '' wrapProgram $out/bin/something \ --prefix PERL5LIB : "${with perlPackages; makePerlPath [ something ]}" '';