Nix and older versions of PHP

Yesterday, I wrote about using Nix to create shells for different versions of PHP.

The current stable version of Nix has PHP 8.4.8, 8.3.22, 8.2.28 and 8.1.32, but what if you need an older version?

If it has been in nixpkgs previously, it can still be accessed.

Nix allows you to create different channels to install different versions of the nixpkgs repository and access different sets of packages.

With flakes, different versions of nixpkgs can be used as inputs to add multiple releases, such as stable and unstable, or older releases or even a specific Git commit.

For example, if I needed to use PHP 7.4 - which is not available in the current release - I could use this flake.nix file:

{
inputs = {
nixpkgs-php74.url = "github:nixos/nixpkgs/81b77fd3847a";
};

outputs = { nixpkgs-php74, ... }:
let
system = "x86_64-linux";
pkgs = import nixpkgs-php74 { inherit system; };
in {
devShells.${system}.default = pkgs.mkShell {
packages = [
pkgs.php74
];
};
};
}

Pinning nixpkgs to that commit hash gives me access to php74, which I can access using nix develop.

This approach works with any package in nixpkgs - not just PHP.

Or, if it wasn't available in an older version of nixpkgs, I can write a custom derivation and add it myself.