2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.memcached;
|
|
|
|
|
|
|
|
memcached = pkgs.memcached;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.memcached = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "Memcached";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
user = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "memcached";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The user to run Memcached as";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
listen = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "127.0.0.1";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The IP address to bind to.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.port;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 11211;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The port to bind to.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
enableUnixSocket = mkEnableOption "Unix Domain Socket at /run/memcached/memcached.sock instead of listening on an IP address and port. The `listen` and `port` options are ignored";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
maxMemory = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.ints.unsigned;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 64;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The maximum amount of memory to use for storage, in megabytes.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
maxConnections = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.ints.unsigned;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 1024;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The maximum number of simultaneous connections.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.listOf types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "A list of extra options that will be added as a suffix when running memcached.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.memcached.enable {
|
|
|
|
|
|
|
|
users.users = optionalAttrs (cfg.user == "memcached") {
|
|
|
|
memcached.description = "Memcached server user";
|
|
|
|
memcached.isSystemUser = true;
|
2021-09-18 10:52:07 +00:00
|
|
|
memcached.group = "memcached";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2021-09-18 10:52:07 +00:00
|
|
|
users.groups = optionalAttrs (cfg.user == "memcached") { memcached = {}; };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
environment.systemPackages = [ memcached ];
|
|
|
|
|
|
|
|
systemd.services.memcached = {
|
|
|
|
description = "Memcached server";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart =
|
|
|
|
let
|
|
|
|
networking = if cfg.enableUnixSocket
|
|
|
|
then "-s /run/memcached/memcached.sock"
|
|
|
|
else "-l ${cfg.listen} -p ${toString cfg.port}";
|
|
|
|
in "${memcached}/bin/memcached ${networking} -m ${toString cfg.maxMemory} -c ${toString cfg.maxConnections} ${concatStringsSep " " cfg.extraOptions}";
|
|
|
|
|
|
|
|
User = cfg.user;
|
|
|
|
|
|
|
|
# Filesystem access
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
RuntimeDirectory = "memcached";
|
|
|
|
# Caps
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
# Misc.
|
|
|
|
LockPersonality = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule ["services" "memcached" "socket"] ''
|
|
|
|
This option was replaced by a fixed unix socket path at /run/memcached/memcached.sock enabled using services.memcached.enableUnixSocket.
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|