nixpkgs-suyu/nixos/modules/services/databases/dragonflydb.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
4.4 KiB
Nix
Raw Normal View History

2022-06-05 18:41:31 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.dragonflydb;
dragonflydb = pkgs.dragonflydb;
settings =
{
port = cfg.port;
dir = "/var/lib/dragonflydb";
keys_output_limit = cfg.keysOutputLimit;
} //
(lib.optionalAttrs (cfg.bind != null) { bind = cfg.bind; }) //
(lib.optionalAttrs (cfg.requirePass != null) { requirepass = cfg.requirePass; }) //
(lib.optionalAttrs (cfg.maxMemory != null) { maxmemory = cfg.maxMemory; }) //
(lib.optionalAttrs (cfg.memcachePort != null) { memcache_port = cfg.memcachePort; }) //
(lib.optionalAttrs (cfg.dbNum != null) { dbnum = cfg.dbNum; }) //
(lib.optionalAttrs (cfg.cacheMode != null) { cache_mode = cfg.cacheMode; });
in
{
###### interface
options = {
services.dragonflydb = {
enable = mkEnableOption "DragonflyDB";
user = mkOption {
type = types.str;
default = "dragonfly";
description = lib.mdDoc "The user to run DragonflyDB as";
2022-06-05 18:41:31 +02:00
};
port = mkOption {
type = types.port;
default = 6379;
description = lib.mdDoc "The TCP port to accept connections.";
2022-06-05 18:41:31 +02:00
};
bind = mkOption {
type = with types; nullOr str;
default = "127.0.0.1";
description = lib.mdDoc ''
2022-06-05 18:41:31 +02:00
The IP interface to bind to.
`null` means "all interfaces".
2022-06-05 18:41:31 +02:00
'';
};
requirePass = mkOption {
type = with types; nullOr str;
default = null;
description = lib.mdDoc "Password for database";
2022-06-05 18:41:31 +02:00
example = "letmein!";
};
maxMemory = mkOption {
type = with types; nullOr ints.unsigned;
default = null;
description = lib.mdDoc ''
2022-06-05 18:41:31 +02:00
The maximum amount of memory to use for storage (in bytes).
`null` means this will be automatically set.
2022-06-05 18:41:31 +02:00
'';
};
memcachePort = mkOption {
type = with types; nullOr port;
default = null;
description = lib.mdDoc ''
2022-06-05 18:41:31 +02:00
To enable memcached compatible API on this port.
`null` means disabled.
2022-06-05 18:41:31 +02:00
'';
};
keysOutputLimit = mkOption {
type = types.ints.unsigned;
default = 8192;
description = lib.mdDoc ''
2022-06-05 18:41:31 +02:00
Maximum number of returned keys in keys command.
`keys` is a dangerous command.
2022-06-05 18:41:31 +02:00
We truncate its result to avoid blowup in memory when fetching too many keys.
'';
};
dbNum = mkOption {
type = with types; nullOr ints.unsigned;
default = null;
description = lib.mdDoc "Maximum number of supported databases for `select`";
2022-06-05 18:41:31 +02:00
};
cacheMode = mkOption {
type = with types; nullOr bool;
default = null;
description = lib.mdDoc ''
2022-06-05 18:41:31 +02:00
Once this mode is on, Dragonfly will evict items least likely to be stumbled
upon in the future but only when it is near maxmemory limit.
'';
};
};
};
###### implementation
config = mkIf config.services.dragonflydb.enable {
users.users = optionalAttrs (cfg.user == "dragonfly") {
dragonfly.description = "DragonflyDB server user";
dragonfly.isSystemUser = true;
dragonfly.group = "dragonfly";
};
users.groups = optionalAttrs (cfg.user == "dragonfly") { dragonfly = { }; };
environment.systemPackages = [ dragonflydb ];
systemd.services.dragonflydb = {
description = "DragonflyDB server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = "${dragonflydb}/bin/dragonfly --alsologtostderr ${builtins.concatStringsSep " " (attrsets.mapAttrsToList (n: v: "--${n} ${strings.escapeShellArg v}") settings)}";
User = cfg.user;
# Filesystem access
ReadWritePaths = [ settings.dir ];
StateDirectory = "dragonflydb";
StateDirectoryMode = "0700";
# Process Properties
LimitMEMLOCK = "infinity";
# Caps
CapabilityBoundingSet = "";
NoNewPrivileges = true;
# Sandboxing
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
LockPersonality = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
RestrictRealtime = true;
PrivateMounts = true;
MemoryDenyWriteExecute = true;
};
};
};
}