How to setup redis server configuration.nix?

Hi All –

New to Nixos and looking for some help on how to setup a redis server in my configuration.nix.

Both the repl and nixos search options show a wide number of options (https://search.nixos.org/options?channel=22.05&from=0&size=50&sort=relevance&type=packages&query=services.redis.servers) of the form:

services.redis.servers.<name>.<option

For example:

services.redis.servers.<name>.enable
services.redis.servers.<name>.user
services.redis.servers.<name>.port
...
...

All appear to have reasonable defaults, so I’ve added the following to my configuration.nix but no redis server seems to be running.

services.redis.servers."my-redis".enable=true;

Thoughts?

I’ve verified the redis server is install and can run it manually from the command-line as follows:

redis-server
      _.-``    `.  `_.  ''-._           Redis 7.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4117
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4117:M 21 Sep 2022 10:25:25.276 # Server initialized
4117:M 21 Sep 2022 10:25:25.276 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4117:M 21 Sep 2022 10:25:25.276 * Loading RDB produced by version 7.0.4
4117:M 21 Sep 2022 10:25:25.276 * RDB age 96 seconds
4117:M 21 Sep 2022 10:25:25.276 * RDB memory usage when created 0.85 Mb
4117:M 21 Sep 2022 10:25:25.276 * Done loading RDB, keys loaded: 0, keys expired: 0.
4117:M 21 Sep 2022 10:25:25.276 * DB loaded from disk: 0.000 seconds
4117:M 21 Sep 2022 10:25:25.276 * Ready to accept connections

And I can ping it with:

redli ping
PONG

So, its definitely installed and running, but why doesn’t my configuration.nix start the server automatically?

services.redis.servers.<name>.enable defaults to false, which contradicts a fair bit of other design in NixOS.

@aanderse Yes, I discovered that, however, my current configuration.nix contains:

  #-------------------------------------------------------------------------
  # Enable redis server
  #-------------------------------------------------------------------------
  services.redis.servers."talos-redis".enable=true;

Yet, the redis server – does not start.

Any ideas?

What does systemctl status redis-talon-redis.service say? What about journalctl -u redis-talon-redis.service?

@aanderse

systemctl status redis-talon-redis.service 
Unit redis-talon-redis.service could not be found.

and

journalctl -u redis-talon-redis.service
-- No entries --

Strangely, I can see messages during booting on the console stating “Starting redis server …”

and

journalctl -b

Sep 22 13:41:23 talos-devenv redis-server[1126]: Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.
Sep 22 13:41:23 talos-devenv redis-server[1126]: oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
Sep 22 13:41:23 talos-devenv redis-server[1126]: Redis version=7.0.4, bits=64, commit=00000000, modified=0, pid=1126, just started
Sep 22 13:41:23 talos-devenv redis-server[1126]: Configuration loaded
Sep 22 13:41:23 talos-devenv redis[1126]: monotonic clock: POSIX clock_gettime
Sep 22 13:41:23 talos-devenv redis[1126]: Running mode=standalone, port=0.
Sep 22 13:41:23 talos-devenv redis[1126]: Server initialized
Sep 22 13:41:23 talos-devenv redis[1126]: WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = >
Sep 22 13:41:23 talos-devenv redis[1126]: Loading RDB produced by version 7.0.4
Sep 22 13:41:23 talos-devenv redis[1126]: RDB age 113 seconds
Sep 22 13:41:23 talos-devenv redis[1126]: RDB memory usage when created 0.82 Mb
Sep 22 13:41:23 talos-devenv redis[1126]: Done loading RDB, keys loaded: 0, keys expired: 0.
Sep 22 13:41:23 talos-devenv redis[1126]: DB loaded from disk: 0.000 seconds
Sep 22 13:41:23 talos-devenv redis[1126]: The server is now ready to accept connections at /run/redis-talos-redis/redis.sock

It certainly appears to be running … and should be ‘ping-able’ on port 6379 – the default port, but alas I’, seeing ‘connection refused’.

2022/09/22 13:49:05 Dial dial tcp 127.0.0.1:6379: connect: connection refused

If I start the server manually, ‘redis-server’ , I can ping it ‘redli ping’ as expected.

@aanderse Solved redis server startup. Thanks for all your suggestions!

For what its worth, the problem appeared to be that both a ‘named’ server and ‘port’ are required.

  #-------------------------------------------------------------------------
  # Enable redis service
  #-------------------------------------------------------------------------
  services.redis.servers."talos".enable=true;
  services.redis.servers."talos".port=6379;

The Nixos redis options docs for “services.redis.servers..port” state if no port is supplied, the default port of 6379 will be used, however, that doesn’t seem to be the case.

services.redis.servers.<name>.port

Name

services.redis.servers.<name>.port

Description
The TCP port to accept connections. If port 0 is specified Redis will not listen on a TCP socket.
Type

16 bit unsigned integer; between 0 and 65535 (both inclusive)

Default

if name == "" then 6379 else 0

Declared in
nixos/modules/services/databases/redis.nix