services-flake/nix/redis.nix
Shivaraj B H 819ab20e8e
refactor: writeShellScriptBin -> writeShellApplication (#155)
Provides us with following benefits:
- use `runtimeInputs` instead of manually exporting `PATH`
- get shellcheck by default

---------

Co-authored-by: Sridhar Ratnakumar <3998+srid@users.noreply.github.com>
2024-03-19 01:47:25 +05:30

95 lines
2.5 KiB
Nix

# Based on https://github.com/cachix/devenv/blob/main/src/modules/services/redis.nix
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
in
{
options = {
enable = lib.mkEnableOption name;
package = lib.mkPackageOption pkgs "redis" { };
dataDir = lib.mkOption {
type = types.str;
default = "./data/${name}";
description = "The redis data directory";
};
bind = lib.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 = lib.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 = lib.mkOption {
type = types.lines;
default = "";
description = "Additional text to be appended to `redis.conf`.";
};
outputs.settings = lib.mkOption {
type = types.deferredModule;
internal = true;
readOnly = true;
default = {
processes = {
"${name}" =
let
redisConfig = pkgs.writeText "redis.conf" ''
port ${toString config.port}
${lib.optionalString (config.bind != null) "bind ${config.bind}"}
${config.extraConfig}
'';
startScript = pkgs.writeShellApplication {
name = "start-redis";
runtimeInputs = [ pkgs.coreutils config.package ];
text = ''
set -euo pipefail
export REDISDATA=${config.dataDir}
if [[ ! -d "$REDISDATA" ]]; then
mkdir -p "$REDISDATA"
fi
exec redis-server ${redisConfig} --dir "$REDISDATA"
'';
};
in
{
command = startScript;
readiness_probe = {
exec.command = "${config.package}/bin/redis-cli -p ${toString config.port} ping";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
namespace = name;
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability.restart = "on_failure";
};
};
};
};
};
}