Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redis should listen on unix socket when port is 0 #1378

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/modules/services/redis.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ with lib;
let
cfg = config.services.redis;

REDIS_UNIX_SOCKET = "${config.env.DEVENV_RUNTIME}/redis.sock";

redisConfig = pkgs.writeText "redis.conf" ''
port ${toString cfg.port}
${optionalString (cfg.bind != null) "bind ${cfg.bind}"}
${optionalString (cfg.port == 0) "unixsocket ${REDIS_UNIX_SOCKET}"}
${optionalString (cfg.port == 0) "unixsocketperm 700"}
${cfg.extraConfig}
'';

Expand All @@ -20,6 +24,9 @@ let

exec ${cfg.package}/bin/redis-server ${redisConfig} --dir "$REDISDATA"
'';

tcpPing = "${cfg.package}/bin/redis-cli -p ${toString cfg.port} ping";
unixSocketPing = "${cfg.package}/bin/redis-cli -s ${REDIS_UNIX_SOCKET} ping";
in
{
imports = [
Expand Down Expand Up @@ -51,7 +58,7 @@ in
default = 6379;
description = ''
The TCP port to accept connections.
If port 0 is specified Redis, will not listen on a TCP socket.
If port 0 is specified Redis, will not listen on a TCP socket and a unix socket file will be found at $REDIS_UNIX_SOCKET.
'';
};

Expand All @@ -67,14 +74,15 @@ in
cfg.package
];

env.REDIS_UNIX_SOCKET = REDIS_UNIX_SOCKET;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should set this on non-zero port?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. It is only needed when port is zero.

I don't know what the right way to conditionally set an environment variable would be, would this work.

env.REDIS_UNIX_SOCKET = if cfg.port == 0 then REDIS_UNIX_SOCKET else null;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with optionalAttrs instead of setting it to null.

env.REDISDATA = config.env.DEVENV_STATE + "/redis";

processes.redis = {
exec = "${startScript}/bin/start-redis";

process-compose = {
readiness_probe = {
exec.command = "${cfg.package}/bin/redis-cli -p ${toString cfg.port} ping";
exec.command = if cfg.port == 0 then unixSocketPing else tcpPing;
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
Expand Down
4 changes: 4 additions & 0 deletions tests/redis-socket/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -ex

timeout 20 bash -c 'until redis-cli -s $REDIS_UNIX_SOCKET ping 2>/dev/null; do sleep 0.5; done'
8 changes: 8 additions & 0 deletions tests/redis-socket/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ ... }:

{
services.redis = {
enable = true;
port = 0;
};
}