1 { config, lib, pkgs, ... }:
3 cfg = config.services.dragonflydb;
4 dragonflydb = pkgs.dragonflydb;
9 dir = "/var/lib/dragonflydb";
10 keys_output_limit = cfg.keysOutputLimit;
12 (lib.optionalAttrs (cfg.bind != null) { bind = cfg.bind; }) //
13 (lib.optionalAttrs (cfg.requirePass != null) { requirepass = cfg.requirePass; }) //
14 (lib.optionalAttrs (cfg.maxMemory != null) { maxmemory = cfg.maxMemory; }) //
15 (lib.optionalAttrs (cfg.memcachePort != null) { memcache_port = cfg.memcachePort; }) //
16 (lib.optionalAttrs (cfg.dbNum != null) { dbnum = cfg.dbNum; }) //
17 (lib.optionalAttrs (cfg.cacheMode != null) { cache_mode = cfg.cacheMode; });
24 services.dragonflydb = {
25 enable = lib.mkEnableOption "DragonflyDB";
29 default = "dragonfly";
30 description = "The user to run DragonflyDB as";
34 type = lib.types.port;
36 description = "The TCP port to accept connections.";
40 type = with lib.types; nullOr str;
41 default = "127.0.0.1";
43 The IP interface to bind to.
44 `null` means "all interfaces".
48 requirePass = lib.mkOption {
49 type = with lib.types; nullOr str;
51 description = "Password for database";
55 maxMemory = lib.mkOption {
56 type = with lib.types; nullOr ints.unsigned;
59 The maximum amount of memory to use for storage (in bytes).
60 `null` means this will be automatically set.
64 memcachePort = lib.mkOption {
65 type = with lib.types; nullOr port;
68 To enable memcached compatible API on this port.
69 `null` means disabled.
73 keysOutputLimit = lib.mkOption {
74 type = lib.types.ints.unsigned;
77 Maximum number of returned keys in keys command.
78 `keys` is a dangerous command.
79 We truncate its result to avoid blowup in memory when fetching too many keys.
83 dbNum = lib.mkOption {
84 type = with lib.types; nullOr ints.unsigned;
86 description = "Maximum number of supported databases for `select`";
89 cacheMode = lib.mkOption {
90 type = with lib.types; nullOr bool;
93 Once this mode is on, Dragonfly will evict items least likely to be stumbled
94 upon in the future but only when it is near maxmemory limit.
100 ###### implementation
102 config = lib.mkIf config.services.dragonflydb.enable {
104 users.users = lib.optionalAttrs (cfg.user == "dragonfly") {
105 dragonfly.description = "DragonflyDB server user";
106 dragonfly.isSystemUser = true;
107 dragonfly.group = "dragonfly";
109 users.groups = lib.optionalAttrs (cfg.user == "dragonfly") { dragonfly = { }; };
111 environment.systemPackages = [ dragonflydb ];
113 systemd.services.dragonflydb = {
114 description = "DragonflyDB server";
116 wantedBy = [ "multi-user.target" ];
117 after = [ "network.target" ];
120 ExecStart = "${dragonflydb}/bin/dragonfly --alsologtostderr ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "--${n} ${lib.escapeShellArg v}") settings)}";
125 ReadWritePaths = [ settings.dir ];
126 StateDirectory = "dragonflydb";
127 StateDirectoryMode = "0700";
129 LimitMEMLOCK = "infinity";
131 CapabilityBoundingSet = "";
132 NoNewPrivileges = true;
134 ProtectSystem = "strict";
137 PrivateDevices = true;
138 ProtectKernelTunables = true;
139 ProtectKernelModules = true;
140 ProtectControlGroups = true;
141 LockPersonality = true;
142 RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
143 RestrictRealtime = true;
144 PrivateMounts = true;
145 MemoryDenyWriteExecute = true;