How to: PHP extension with Nix

Learn how to build and package a PHP extension using Nix, specifically for the dbase extension that isn't available in nixpkgs. This guide shows you how to create a custom Nix derivation to include PHP extensions not found in the standard repository.

How to: PHP extension with Nix
1 mins

Was working on porting most of our docker images at work to building them using nix [check this for more info] and found one of our older images in need of some love.

As it happens it contained ext-dbase, a php extension that has no package in nixpkgs. Dbase is such an old and odd little thing, it is a sort of Excel file combined with some sqlite abilities. Something that came from another era totally.

Since it is that old I see no reason for me to add it to nixpkgs so how would I go to build it just for me?

Surprinsingly, Nix loves php. So it is pretty straight forward if the darn thing is in PECL. I wanted to share this in case some other bloke looking for a dbase fix stumbles upon this somehow:

php.nix
{ pkgs ? import <nixpkgs> { }, php}:
with pkgs;
let
dbase = php.buildPecl
rec {
pname = "dbase";
version = "7.1.1";
src = fetchFromGitHub {
owner = "php";
repo = "pecl-database-dbase";
rev = "ba33dfe16f13c2093d19f93deb316390151aa729";
sha256 = "djtRnx5rFGkRzs/Jai8m5zCBkJtHk1xZmJ0fra7EXeY=";
};
buildInputs = [ libuv ];
meta = with lib; {
description = "Interface to dbase for php";
license = licenses.php301;
homepage = "https://github.com/php/pecl-database-dbase";
maintainers = teams.php.members;
platforms = platforms.linux;
};
};
in
php.buildEnv {
extensions = ({ enabled, all }: enabled ++ (with all; [
dbase
]));
}

Then, instead of the php package you normally use, use the above to create a derivation based on the php version you want with your extension enabled:

php82WithDbase = import ./php.nix { php = pkgs.php82; };

That’s it! Very simple.