Home Explore Blog Models CI



nixpkgs

2nd chunk of `doc/languages-frameworks/php.section.md`
0b31dcb7e2607b32d91e8e636c3728daddfad5d924564f910000000100000f6b
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 {
  extensions =
    { all, ... }:
    with all;
    [
      imagick
      opcache
    ];
  extraConfig = "memory_limit=256M";
}
```

#### Example setup for `phpfpm` {#ssec-php-user-guide-installing-with-extensions-phpfpm}

You can use the previous examples in a `phpfpm` pool called `foo` as
follows:

```nix
let
  myPhp = php.withExtensions (
    { all, ... }:
    with all;
    [
      imagick
      opcache
    ]
  );
in
{
  services.phpfpm.pools."foo".phpPackage = myPhp;
}
```

```nix
let
  myPhp = php.buildEnv {
    extensions =
      { all, ... }:
      with all;
      [
        imagick
        opcache
      ];
    extraConfig = "memory_limit=256M";
  };
in
{
  services.phpfpm.pools."foo".phpPackage = myPhp;
}
```

#### Example usage with `nix-shell` {#ssec-php-user-guide-installing-with-extensions-nix-shell}

This brings up a temporary environment that contains a PHP interpreter
with the extensions `imagick` and `opcache` enabled:

```sh
nix-shell -p 'php.withExtensions ({ all, ... }: with all; [ imagick opcache ])'
```

### 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
```

### Overriding PHP packages {#ssec-php-user-guide-overriding-packages}

`php-packages.nix` form a scope, allowing us to override the packages defined
within. For example, to apply a patch to a `mysqlnd` extension, you can
pass an overlay-style function to `php`’s `packageOverrides` argument:

```nix
php.override {
  packageOverrides = final: prev: {
    extensions = prev.extensions // {
      mysqlnd = prev.extensions.mysqlnd.overrideAttrs (attrs: {
        patches = attrs.patches or [ ] ++ [
          # ...
        ];
      });
    };
  };
}
```

### Building PHP projects {#ssec-building-php-projects}

With [Composer](https://getcomposer.org/), you can effectively build PHP
projects by streamlining dependency management. As the de-facto standard
dependency manager for PHP, Composer enables you to declare and manage the
libraries your project relies on, ensuring a more organized and efficient
development process.

Composer is not a package manager in the same sense as `Yum` or `Apt` are. Yes,
it deals with "packages" or libraries, but it manages them on a per-project
basis, installing them in a directory (e.g. `vendor`) inside your project. By
default, it does not install anything globally. This idea is not new and
Composer is strongly inspired by Node's `npm` and Ruby's `bundler`.

Currently, there is no other PHP tool that offers the same functionality as
Composer. Consequently, incorporating a helper in Nix to facilitate building
such applications is a logical choice.

In a Composer project, dependencies are defined in a `composer.json` file,
while their specific versions are locked in a `composer.lock` file. Some
Composer-based projects opt to include this `composer.lock` file in their source
code, while others choose not to.

In Nix, there are multiple approaches to building a Composer-based project.

One such method is the `php.buildComposerProject2` helper function, which serves

Title: PHP on Nix: Environment Customization, Package Overrides, and Composer Project Building
Summary
This section of the NixOS user guide provides further examples for configuring PHP environments. It shows how to apply custom PHP builds (with specific extensions and `php.ini` settings via `php.withExtensions` and `php.buildEnv`) within `phpfpm` pools and temporary `nix-shell` environments. It then details how to install PHP interactive tools, such as Composer, with a custom set of extensions. Advanced users can learn to override specific PHP packages or extensions using `php.override` and `packageOverrides`. Finally, the document introduces building PHP projects using Composer, explaining its role as a per-project dependency manager and noting that Nix offers helpers like `php.buildComposerProject2` for managing `composer.json` and `composer.lock`-based projects.