2020-05-01 08:15:47 +01:00
|
|
|
# PHP {#sec-php}
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-05-01 08:15:47 +01:00
|
|
|
## User Guide {#ssec-php-user-guide}
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-05-01 08:15:47 +01:00
|
|
|
### Overview {#ssec-php-user-guide-overview}
|
2020-03-22 14:33:48 +00:00
|
|
|
|
|
|
|
Several versions of PHP are available on Nix, each of which having a
|
|
|
|
wide variety of extensions and libraries available.
|
|
|
|
|
2020-04-29 11:46:26 +01:00
|
|
|
The different versions of PHP that nixpkgs provides are located under
|
|
|
|
attributes named based on major and minor version number; e.g.,
|
|
|
|
`php74` is PHP 7.4.
|
2020-03-22 14:33:48 +00:00
|
|
|
|
|
|
|
Only versions of PHP that are supported by upstream for the entirety
|
|
|
|
of a given NixOS release will be included in that release of
|
|
|
|
NixOS. See [PHP Supported
|
|
|
|
Versions](https://www.php.net/supported-versions.php).
|
|
|
|
|
2020-04-29 11:46:26 +01:00
|
|
|
The attribute `php` refers to the version of PHP considered most
|
|
|
|
stable and thoroughly tested in nixpkgs for any given release of
|
|
|
|
NixOS - not necessarily the latest major release from upstream.
|
|
|
|
|
|
|
|
All available PHP attributes are wrappers around their respective
|
|
|
|
binary PHP package and provide commonly used extensions this way. The
|
|
|
|
real PHP 7.4 package, i.e. the unwrapped one, is available as
|
|
|
|
`php74.unwrapped`; see the next section for more details.
|
|
|
|
|
2020-04-03 17:56:12 +01:00
|
|
|
Interactive tools built on PHP are put in `php.packages`; composer is
|
|
|
|
for example available at `php.packages.composer`.
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-04-03 17:56:12 +01:00
|
|
|
Most extensions that come with PHP, as well as some popular
|
|
|
|
third-party ones, are available in `php.extensions`; for example, the
|
|
|
|
opcache extension shipped with PHP is available at
|
|
|
|
`php.extensions.opcache` and the third-party ImageMagick extension at
|
|
|
|
`php.extensions.imagick`.
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-05-01 08:15:47 +01:00
|
|
|
### Installing PHP with extensions {#ssec-php-user-guide-installing-with-extensions}
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-04-03 17:56:12 +01:00
|
|
|
A PHP package with specific extensions enabled can be built using
|
|
|
|
`php.withExtensions`. This is a function which accepts an anonymous
|
2020-04-12 22:31:56 +01:00
|
|
|
function as its only argument; the function should accept two named
|
|
|
|
parameters: `enabled` - a list of currently enabled extensions and
|
|
|
|
`all` - the set of all extensions, and return a list of wanted
|
|
|
|
extensions. For example, a PHP package with all default extensions and
|
|
|
|
ImageMagick enabled:
|
2020-04-03 17:56:12 +01:00
|
|
|
|
|
|
|
```nix
|
2020-04-12 22:31:56 +01:00
|
|
|
php.withExtensions ({ enabled, all }:
|
|
|
|
enabled ++ [ all.imagick ])
|
2020-04-03 17:56:12 +01:00
|
|
|
```
|
|
|
|
|
2020-04-12 22:31:56 +01:00
|
|
|
To exclude some, but not all, of the default extensions, you can
|
|
|
|
filter the `enabled` list like this:
|
2020-04-05 14:56:28 +01:00
|
|
|
|
2020-04-12 22:31:56 +01:00
|
|
|
```nix
|
|
|
|
php.withExtensions ({ enabled, all }:
|
|
|
|
(lib.filter (e: e != php.extensions.opcache) enabled)
|
|
|
|
++ [ all.imagick ])
|
|
|
|
```
|
|
|
|
|
|
|
|
To build your list of extensions from the ground up, you can simply
|
|
|
|
ignore `enabled`:
|
2020-04-05 14:56:28 +01:00
|
|
|
|
|
|
|
```nix
|
2020-05-01 08:33:04 +01:00
|
|
|
php.withExtensions ({ all, ... }: with all; [ imagick opcache ])
|
2020-04-05 14:56:28 +01:00
|
|
|
```
|
|
|
|
|
2020-04-24 20:28:33 +01:00
|
|
|
`php.withExtensions` provides extensions by wrapping a minimal php
|
|
|
|
base package, providing a `php.ini` file listing all extensions to be
|
2020-04-29 11:46:26 +01:00
|
|
|
loaded. You can access this package through the `php.unwrapped`
|
2020-04-24 20:28:33 +01:00
|
|
|
attribute; useful if you, for example, need access to the `dev`
|
|
|
|
output. The generated `php.ini` file can be accessed through the
|
|
|
|
`php.phpIni` attribute.
|
|
|
|
|
2020-04-03 17:56:12 +01:00
|
|
|
If you want a PHP build with extra configuration in the `php.ini`
|
|
|
|
file, you can use `php.buildEnv`. This function takes two named and
|
|
|
|
optional parameters: `extensions` and `extraConfig`. `extensions`
|
|
|
|
takes an extension specification equivalent to that of
|
|
|
|
`php.withExtensions`, `extraConfig` a string of additional `php.ini`
|
|
|
|
configuration parameters. For example, a PHP package with the opcache
|
|
|
|
and ImageMagick extensions enabled, and `memory_limit` set to `256M`:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
php.buildEnv {
|
2020-04-12 22:31:56 +01:00
|
|
|
extensions = { all, ... }: with all; [ imagick opcache ];
|
2020-04-03 17:56:12 +01:00
|
|
|
extraConfig = "memory_limit=256M";
|
|
|
|
}
|
|
|
|
```
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-05-01 08:15:47 +01:00
|
|
|
#### Example setup for `phpfpm` {#ssec-php-user-guide-installing-with-extensions-phpfpm}
|
2020-03-22 14:33:48 +00:00
|
|
|
|
2020-04-03 17:56:12 +01:00
|
|
|
You can use the previous examples in a `phpfpm` pool called `foo` as
|
|
|
|
follows:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
let
|
2020-05-01 08:33:04 +01:00
|
|
|
myPhp = php.withExtensions ({ all, ... }: with all; [ imagick opcache ]);
|
2020-04-03 17:56:12 +01:00
|
|
|
in {
|
|
|
|
services.phpfpm.pools."foo".phpPackage = myPhp;
|
|
|
|
};
|
|
|
|
```
|
2020-03-22 14:33:48 +00:00
|
|
|
|
|
|
|
```nix
|
|
|
|
let
|
2020-04-03 17:56:12 +01:00
|
|
|
myPhp = php.buildEnv {
|
2020-04-12 22:31:56 +01:00
|
|
|
extensions = { all, ... }: with all; [ imagick opcache ];
|
2020-04-03 17:56:12 +01:00
|
|
|
extraConfig = "memory_limit=256M";
|
|
|
|
};
|
2020-03-22 14:33:48 +00:00
|
|
|
in {
|
|
|
|
services.phpfpm.pools."foo".phpPackage = myPhp;
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2020-05-01 08:15:47 +01:00
|
|
|
#### Example usage with `nix-shell` {#ssec-php-user-guide-installing-with-extensions-nix-shell}
|
2020-03-22 14:33:48 +00:00
|
|
|
|
|
|
|
This brings up a temporary environment that contains a PHP interpreter
|
2020-04-12 22:31:56 +01:00
|
|
|
with the extensions `imagick` and `opcache` enabled:
|
2020-03-22 14:33:48 +00:00
|
|
|
|
|
|
|
```sh
|
2020-04-12 22:31:56 +01:00
|
|
|
nix-shell -p 'php.withExtensions ({ all, ... }: with all; [ imagick opcache ])'
|
2020-03-22 14:33:48 +00:00
|
|
|
```
|
2020-05-01 08:33:04 +01:00
|
|
|
|
|
|
|
### Installing PHP packages with extensions {#ssec-php-user-guide-installing-packages-with-extensions}
|
|
|
|
|
|
|
|
All interactive tools use the PHP package you get them from, so all
|
|
|
|
packages at `php.packages.*` use the `php` package with its default
|
|
|
|
extensions. Sometimes this default set of extensions isn't enough and
|
|
|
|
you may want to extend it. A common case of this is the `composer`
|
|
|
|
package: a project may depend on certain extensions and `composer`
|
|
|
|
won't work with that project unless those extensions are loaded.
|
|
|
|
|
|
|
|
Example of building `composer` with additional extensions:
|
|
|
|
```nix
|
|
|
|
(php.withExtensions ({ all, enabled }:
|
|
|
|
enabled ++ (with all; [ imagick redis ]))
|
|
|
|
).packages.composer
|
|
|
|
```
|