Add redis service (#8)

This commit is contained in:
Shivaraj B H 2023-07-10 20:44:26 +05:30 committed by GitHub
parent 9a9fc54749
commit beaaca9f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 1 deletions

View File

@ -17,7 +17,7 @@ TODO
- [x] Hello World
- [x] PostgreSQL
- [ ] MySQL
- [ ] Redis
- [x] Redis
- [ ] ...
## Contributing

View File

@ -2,5 +2,6 @@
imports = [
./hello.nix
./postgres.nix
./redis.nix
];
}

98
nix/redis.nix Normal file
View File

@ -0,0 +1,98 @@
# Based on https://github.com/cachix/devenv/blob/main/src/modules/services/redis.nix
{ pkgs, lib, config, ... }:
with lib;
{
options.services.redis = lib.mkOption {
description = ''
Enable redis server
'';
default = { };
type = lib.types.submodule ({ config, ... }: {
options = {
enable = lib.mkEnableOption "redis";
name = lib.mkOption {
type = lib.types.str;
default = "redis";
description = "Unique process name";
};
package = lib.mkPackageOption pkgs "redis" { };
dataDir = lib.mkOption {
type = lib.types.str;
default = "./data/${config.name}";
description = "The redis data directory";
};
bind = mkOption {
type = types.nullOr types.str;
default = "127.0.0.1";
description = ''
The IP interface to bind to.
`null` means "all interfaces".
'';
example = "127.0.0.1";
};
port = mkOption {
type = types.port;
default = 6379;
description = ''
The TCP port to accept connections.
If port is set to `0`, redis will not listen on a TCP socket.
'';
};
extraConfig = mkOption {
type = types.lines;
default = "";
description = "Additional text to be appended to `redis.conf`.";
};
};
});
};
config = let cfg = config.services.redis; in lib.mkIf cfg.enable {
settings.processes.${cfg.name} =
let
redisConfig = pkgs.writeText "redis.conf" ''
port ${toString cfg.port}
${optionalString (cfg.bind != null) "bind ${cfg.bind}"}
${cfg.extraConfig}
'';
startScript = pkgs.writeShellScriptBin "start-redis" ''
set -euo pipefail
export REDISDATA=${cfg.dataDir}
if [[ ! -d "$REDISDATA" ]]; then
mkdir -p "$REDISDATA"
fi
exec ${cfg.package}/bin/redis-server ${redisConfig} --dir "$REDISDATA"
'';
in
{
command = "${startScript}/bin/start-redis";
readiness_probe = {
exec.command = "${cfg.package}/bin/redis-cli -p ${toString cfg.port} ping";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability.restart = "on_failure";
};
};
}

10
nix/redis_test.nix Normal file
View File

@ -0,0 +1,10 @@
{ config, ... }: {
services.redis.enable = true;
testScript = ''
process_compose.wait_until(lambda procs:
# TODO: Check for 'is_ready' instead of 'status'
procs["redis"]["status"] == "Running"
)
machine.succeed("${config.services.redis.package}/bin/redis-cli ping | grep -q 'PONG'")
'';
}

View File

@ -20,6 +20,12 @@
../nix/postgres_test.nix
];
};
redis = {
imports = [
inputs.services-flake.processComposeModules.default
../nix/redis_test.nix
];
};
};
};
};