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

feat(redis): Support using Unix socket #353

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 40 additions & 3 deletions nix/services/redis.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ in

If port is set to `0`, redis will not listen on a TCP socket.
'';
apply = v:
lib.warnIf ((config.unixSocket != null) && (v != 0)) ''
`${name}` is listening on both the TCP port and Unix socket, set `port = 0;` to listen on only the Unix socket
''
v;
};

unixSocket = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The path to the socket to bind to.

If a relative path is used, it will be relative to `dataDir`.
'';
};

unixSocketPerm = lib.mkOption {
type = types.int;
default = 660;
description = "Change permissions for the socket";
example = 600;
};

extraConfig = lib.mkOption {
Expand All @@ -43,6 +65,8 @@ in
redisConfig = pkgs.writeText "redis.conf" ''
port ${toString config.port}
${lib.optionalString (config.bind != null) "bind ${config.bind}"}
${lib.optionalString (config.unixSocket != null) "unixsocket ${config.unixSocket}"}
${lib.optionalString (config.unixSocket != null) "unixsocketperm ${builtins.toString config.unixSocketPerm}"}
${config.extraConfig}
'';

Expand All @@ -65,9 +89,22 @@ in
{
command = startScript;

readiness_probe = {
exec.command = "${config.package}/bin/redis-cli -p ${toString config.port} ping";
};
readiness_probe =
let
# Transform `unixSocket` by prefixing `config.dataDir` if a relative path is used
transformedSocketPath =
if (config.unixSocket != null && (lib.hasPrefix "./" config.unixSocket)) then
"${config.dataDir}/${config.unixSocket}"
else
config.unixSocket;
in
{
exec.command =
if (transformedSocketPath != null && config.port == 0) then
"${config.package}/bin/redis-cli -s ${transformedSocketPath} ${toString config.port} ping"
else
"${config.package}/bin/redis-cli -p ${toString config.port} ping";
};
};
};
};
Expand Down
15 changes: 14 additions & 1 deletion nix/services/redis_test.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{ pkgs, config, ... }: {
services.redis."redis1".enable = true;

services.redis."redis2" = { config, ... }: {
enable = true;
port = 0;
unixSocket = "./redis.sock";
};

settings.processes.test =
let
cfg = config.services.redis."redis1";
Expand All @@ -9,10 +15,17 @@
command = pkgs.writeShellApplication {
runtimeInputs = [ cfg.package pkgs.gnugrep ];
text = ''
redis-cli ping | grep -q "PONG"
echo "Ping from redis1"
redis-cli ping | grep "PONG"

echo "Test connection to redis2 listening on Unix socket"
echo "Ping from redis2"

redis-cli -s ./data/redis2/redis.sock ping | grep "PONG"
'';
name = "redis-test";
};
depends_on."redis1".condition = "process_healthy";
depends_on."redis2".condition = "process_healthy";
};
}