Skip to content

Commit

Permalink
feat(redis): Support using Unix socket
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh committed Oct 9, 2024
1 parent ca67793 commit ed6ee99
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
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";
};
}

0 comments on commit ed6ee99

Please sign in to comment.