Old PHP versions in devenv

I maintain a project that still uses PHP 7.4 (long story) so I need to build out my local environment with that version. Sourcing a Nix package for PHP 7.4 isn’t an issue; the nix-phps repository maintains PHP 5.6-PHP 8.4. The tricky part was adding that package to my local devenv.sh environment. Devenv supports PHP, but by default the PHP versions are limited to what’s in the devenv-nixpkgs repository, which starts at PHP 8.0.

I went down a few blind alleys, followed some outdated documentation, and generally overthought this. Devenv has awareness of older PHP versions; they’re simply not enabled by default. The first thing you need to do is enable the nix-phps repository. You can do that with this command:

1
devenv inputs add phps github:fossar/nix-phps --follows nixpkgs

That adds the following block to your devenv.yaml file:

1
2
3
4
5
phps:
url: github:fossar/nix-phps
inputs:
nixpkgs:
follows: nixpkgs

I thought that I need to muck around with inputs and imports, but I didn’t, because the devenv PHP support can interpret a request for an older version and will look for the nix-phps repository if you’ve enabled it. This is what a working PHP language block looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
languages.php = {
enable = true;
extensions = [
"yaml"
];
version = "7.4";
package = pkgs.php.buildEnv {
extraConfig = ''
sendmail_path = ${config.services.mailpit.package}/bin/mailpit sendmail
smtp_port = 1025
'';
};
};

Declaring the version is enough; I don’t need to specify the package itself. I also don’t need to declare the extension within the buildEnv argument because I did it in languages.php.extensions.

And that’s it:

1
2
3
4
(nix:devenv-shell-env) (devenv) $ php -v
PHP 7.4.33 (cli) (built: Oct 31 2022 10:36:16) ( NTS )
(nix:devenv-shell-env) (devenv) $ php -m | grep yaml
yaml