Php memcached install on NIXOS

I’m trying to install memcache for php7.4 right now, unfortunately I can’t get it to work. In the configuration.nix I added the following:

environment.systemPackages = with pkgs; [
     apacheHttpdPackages.php
     libmemcached
     memcached
     php74Extensions.memcached     
     .
     .
   ];
.
.
# Main services
  services.memcached.enable = true;

.
.

systemctl status memcached
● memcached.service - Memcached server
     Loaded: loaded (/etc/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-02-06 18:42:12 CET; 11min ago
   Main PID: 54477 (memcached)
         IP: 0B in, 0B out
         IO: 668.0K read, 0B written
      Tasks: 10 (limit: 19124)
     Memory: 1.7M
        CPU: 109ms
     CGroup: /system.slice/memcached.service
             └─54477 /nix/store/jhajypc59h374cdv7fn11w42dfxkvgkm-memcached-1.6.9/bin/memcached -l 127.0.0.1 -p 11211 -m 64 -c 1024
ps aux | grep memcached
54477 memcache  0:00 /nix/store/jhajypc59h374cdv7fn11w42dfxkvgkm-memcached-1.6.9/bin/memcached -l 127.0.0.1 -p 11211 -m 64 -c 1024
56736 root      0:00 grep memcached

But it still doesn’t show up in php.info. What have I forgotten?

Thanks Bavra

PHP doesn’t know about memcached’s existence.

You need to either specify it in the PHP configuration or use

(apacheHttpdPackages.php.withExtensions(ex: with ex; [ memcached ]))

which will do it for you

(and remove php74Extensions.memcached)

Thank you for your feedback. But how can I set this where? I have not found anything in the “NixOS Search - Loading...”. And under my “services.httpd.phpOptions” a simple “extension=memcached.so;” brought nothing.

And I can’t do anything with the notation at the moment:

(apacheHttpdPackages.php.withExtensions(ex: with ex; [ memcached ]))".

Should this be in the “environment.systemPackages”? Maybe I just have a knot in my head there. Would appreciate any help at least?

Thnx Bavra

You should add this to your configuration:

services.httpd.phpPackage = pkgs.php74.withExtensions (ex: [ ex.memcached ]);
services.httpd.phpOptions = ''
  ; whatever else you want here, but not extensions... they are taken care of by the above 'withExtensions' part
''

The above configuration can also be written like this:

services.httpd.phpPackage = pkgs.php74.buildEnv {
  extensions = { enabled, all }: enabled ++ (with all; [ memcached ]);
  extraConfig = ''
    ; whatever else you want here, but not extensions... they are taken care of by the above 
  '';
};

Let me know if you have any issues.

First of all, thank you very much for your feedback. Unfortunately, I get the following error with the configuration.nix addition:

building Nix…
building the system configuration…
error: attribute ‘memcached’ missing, at /etc/nixos/configuration.nix:256:64
(use ‘–show-trace’ to show detailed location information)

Here’s what it says in my configuration.nix:
.
.
.
environment.systemPackages = with pkgs; [
apacheHttpdPackages.php
#php74Extensions.memcached
memcached
imagick
discord
.
.
.

Main services

services.memcached.enable = true;
services.postgresql.enable = true;
.
.
.
services.httpd.phpPackage = pkgs.php74.withExtensions (ex: [ ex.memcached ]);
#services.httpd.hostName = “localhost”;

services.httpd.phpOptions =
‘’
display_errors = On
display_startup_errors = On
post_max_size = 200M
upload_max_filesize = 200M
max_execution_time = 6000
max_input_time = 3000
mbstring.http_input = pass
mbstring.http_output = pass
mbstring.internal_encoding = pass
memory_limit = 8G;
allow_url_include = On;
opcache.enable=1;
opcache.memory_consumption=128;
opcache_revalidate_freq = 240;
opcache.max_accelerated_files=4000;
extension=memcached.so;
‘’;
.
.
.

What have I done wrong? Because it also affects imagick.

Thnx Bavra

Hi @bavramor :wave:

I created a file ~/php.nix on my system to simulate yours:

{ config, pkgs, lib, ... }:
let
  custom-php = pkgs.php74.buildEnv {
    extensions = { enabled, all }: enabled ++ (with all; [ imagick memcached ]);
    extraConfig = ''
      display_errors = On
      display_startup_errors = On
      post_max_size = 200M
      upload_max_filesize = 200M
      max_execution_time = 6000
      max_input_time = 3000
      mbstring.http_input = pass
      mbstring.http_output = pass
      mbstring.internal_encoding = pass
      memory_limit = 8G;
      allow_url_include = On;
      opcache.enable=1;
      opcache.memory_consumption=128;
      opcache_revalidate_freq = 240;
      opcache.max_accelerated_files=4000;
      ; do NOT put any 'extension' blocks in here, including 'extension=memcached.so;'
    '';
  };
in
{
  environment.systemPackages = with pkgs; [
    # do NOT put apacheHttpdPackages.php, php74Extensions.memcached, memcached, or imagick here... instead:
    custom-php

    discord
  ];

  services.httpd.enable = true;
  services.httpd.adminAddr = "admin@example.com";
  services.httpd.enablePHP = true;
  services.httpd.phpPackage = custom-php;
  # do NOT set services.httpd.phpOptions because that is already taken care of by our `custom-php` package

  nixpkgs.config.allowUnfree = true;
}

I hope this helps you.

I ran this system as a NixOS container - hopefully the following output helps you:

~> sudo nixos-container create php --config-file ~/php.nix
~> sudo nixos-container root-login php

[root@php:~]# php --version
PHP 7.4.21 (cli) (built: Jun 29 2021 15:17:27) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies

[root@php:~]# php -m | grep memcached
memcached

[root@php:~]# php -m | grep imagick
imagick

Let me know if any details are lacking. I’m always happy to help.

So that worked now, I put the part in configuration.nix now and memcached and imagick is running. Now I have to deal with the nix-shell. I haven’t really dealt with that yet.

What still doesn’t work is the command “php -m | grep memcached”. It does not produce any output. However, php.info says something different.

Thanks a lot for your help, once again. I am also sure that you can help me a few more times. ;0)

Thank you!

Bavra