How-To Test Nixpkg Perl Module Additions
I recently installed NixOS on my laptop.
So far, I'm very happy with how things work and can do most of my tasks as on any other Linux distribution. Some tasks, however, require some more reading and thinking.
Today I wanted to jot down a quick how-to on testing an addition to the Perl packages in the Nix Packages collection (Nixpkgs).
This how-to is based on:
- https://nixos.wiki/wiki/Perl#Adding_something_from_CPAN_to_nixpkgs
- https://dataswamp.org/~solene/2021-09-18-nix-cpan-pip.html#_Perl
- https://nixos.org/manual/nixpkgs/stable/#how-to-develop
- https://nixos.org/manual/nixpkgs/stable/#submitting-changes-making-patches
First, download nix-generate-from-cpan
by adding it to /etc/nixos/configuration.nix
and running sudo nixos-rebuild switch
.
I won't go into the details on the first step here, because this is a task you should already be familiar with after having spent the first few hours configuring your NixOS installation.
The next steps involve patching the <nixpkgs>
“collection” (which is part of the default nixpkgs
“channel”) .
git clone --depth=1 https://github.com/NixOS/nixpkgs nixpkgs.git
cd nixpkgs.git
Then you create the patch for the pkgs/top-level/perl-packages.nix
file as follows:
nix-generate-from-cpan Cal::DAV
attribute name: CalDAV
module: Cal::DAV
version: 0.6
package: Cal-DAV-0.6.tar.gz (Cal-DAV-0.6, CalDAV)
path: authors/id/S/SI/SIMONW
downloaded to: /home/andi/.cpanplus/authors/id/S/SI/SIMONW/Cal-DAV-0.6.tar.gz
sha-256: e436ecb61db9a2fa3ba4b2111149dfd31bdf7aa6acbc12cc49584e50db63e79b
unpacked to: /home/andi/.cpanplus/5.36.0/build/CM_4_0IbZK/Cal-DAV-0.6
runtime deps: DataICal HTTPDAV LWP
build deps:
description: A CalDAV client
license: perl_5
RSS feed: https://metacpan.org/feed/distribution/Cal-DAV
===
CalDAV = buildPerlPackage {
pname = "Cal-DAV";
version = "0.6";
src = fetchurl {
url = "mirror://cpan/authors/id/S/SI/SIMONW/Cal-DAV-0.6.tar.gz";
sha256 = "e436ecb61db9a2fa3ba4b2111149dfd31bdf7aa6acbc12cc49584e50db63e79b";
};
propagatedBuildInputs = [ DataICal HTTPDAV LWP ];
meta = {
description = "A CalDAV client";
license = with lib.licenses; [ artistic1 gpl1Plus ];
};
};
Add the expression in alphabetical order to pkgs/top-level/perl-packages.nix. Only copy/paste the “Nix expression” after the ===
characters into the file.
To test the patch, spin-up a nix-shell
which includes the new Perl module. Create the default.nix
expression file in any working directory:
cat <<EOF > default.nix
with (import <nixpkgs> {});
let
CalDAV = perlPackages.buildPerlPackage {
pname = "Cal-DAV";
version = "0.6";
src = fetchurl {
url = "mirror://cpan/authors/id/S/SI/SIMONW/Cal-DAV-0.6.tar.gz";
sha256 = "e436ecb61db9a2fa3ba4b2111149dfd31bdf7aa6acbc12cc49584e50db63e79b";
};
propagatedBuildInputs = [ perlPackages.DataICal perlPackages.HTTPDAV perlPackages.LWP ];
meta = {
description = "A CalDAV client";
license = with lib.licenses; [ artistic1 gpl1Plus ];
};
};
in
mkShell {
buildInputs = [ CalDAV perl ];
# putting perl here is only required when not using NixOS, this tell you want Nix perl binary
}
EOF
The expression includes the result of the previous nix-generate-from-cpan Cal::DAV
with slight modifications.
Notice that propagatedBuildInputs
needs to refer to already installed packages prefixed by perlPackages
.
More information on how to develop with nix-shell.
Solènes article nicely shows how the let
expression can also include an entirely new block of code to define a variable. In this example, the let expression uses the buildPerlPackage
function, which is a “builder” for building Perl packages (as the name indicates).
With that said, you can launch the development shell to test any Perl script with the newly added Perl library as follows:
nix-shell
If you don't want to create a “descriptive” shell expression, you can also launch the shell more in an ad-hoc fashion using the following command:
nix-shell -p perlPackages.CalDAV perl -I nixpkgs=~/Downloads/nixpkgs.git
See also the example for Perl in the nix-shell
Manual.
If you are happy with how the module works in the nix-shell
, you can add it to your system/profile (using nix-env
or nixos-rebuild
) and even contribute it to the upstream <nixpkgs>
repository.
This post mostly reminds myself of all the new terminology I learned the past few days. Let me know on Mastodon if the short how-to here was useful (or if it needs correction).