2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-06-10 21:45:24 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2013-06-10 21:45:24 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.memcached;
|
|
|
|
|
|
|
|
memcached = pkgs.memcached;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.memcached = {
|
2020-04-20 19:05:26 +01:00
|
|
|
enable = mkEnableOption "Memcached";
|
2013-06-10 21:45:24 +01:00
|
|
|
|
|
|
|
user = mkOption {
|
2019-12-29 22:56:11 +00:00
|
|
|
type = types.str;
|
2013-06-10 21:45:24 +01:00
|
|
|
default = "memcached";
|
|
|
|
description = "The user to run Memcached as";
|
|
|
|
};
|
|
|
|
|
2013-06-23 19:07:41 +01:00
|
|
|
listen = mkOption {
|
2019-12-29 22:56:11 +00:00
|
|
|
type = types.str;
|
2013-06-23 19:07:41 +01:00
|
|
|
default = "127.0.0.1";
|
2019-12-29 22:56:11 +00:00
|
|
|
description = "The IP address to bind to.";
|
2013-06-23 19:07:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2019-12-29 22:56:11 +00:00
|
|
|
type = types.port;
|
2013-06-23 19:07:41 +01:00
|
|
|
default = 11211;
|
2019-12-29 22:56:11 +00:00
|
|
|
description = "The port to bind to.";
|
2013-06-23 19:07:41 +01:00
|
|
|
};
|
|
|
|
|
2018-01-03 11:10:45 +00:00
|
|
|
enableUnixSocket = mkEnableOption "unix socket at /run/memcached/memcached.sock";
|
2013-06-23 19:07:41 +01:00
|
|
|
|
|
|
|
maxMemory = mkOption {
|
2019-12-29 22:56:11 +00:00
|
|
|
type = types.ints.unsigned;
|
2013-06-23 19:07:41 +01:00
|
|
|
default = 64;
|
|
|
|
description = "The maximum amount of memory to use for storage, in megabytes.";
|
|
|
|
};
|
|
|
|
|
|
|
|
maxConnections = mkOption {
|
2019-12-29 22:56:11 +00:00
|
|
|
type = types.ints.unsigned;
|
2013-06-23 19:07:41 +01:00
|
|
|
default = 1024;
|
2019-12-29 22:56:11 +00:00
|
|
|
description = "The maximum number of simultaneous connections.";
|
2013-06-23 19:07:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
2019-12-29 22:56:11 +00:00
|
|
|
type = types.listOf types.str;
|
2013-06-23 19:07:41 +01:00
|
|
|
default = [];
|
2019-12-29 22:56:11 +00:00
|
|
|
description = "A list of extra options that will be added as a suffix when running memcached.";
|
2013-06-23 19:07:41 +01:00
|
|
|
};
|
2013-06-10 21:45:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.memcached.enable {
|
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users = optionalAttrs (cfg.user == "memcached") {
|
|
|
|
memcached.description = "Memcached server user";
|
|
|
|
memcached.isSystemUser = true;
|
2021-08-08 13:00:00 +01:00
|
|
|
memcached.group = "memcached";
|
2018-01-03 11:10:45 +00:00
|
|
|
};
|
2021-08-08 13:00:00 +01:00
|
|
|
users.groups = optionalAttrs (cfg.user == "memcached") { memcached = {}; };
|
2013-06-10 21:45:24 +01:00
|
|
|
|
|
|
|
environment.systemPackages = [ memcached ];
|
|
|
|
|
2018-01-03 11:10:45 +00:00
|
|
|
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;
|
2019-06-10 08:50:04 +01:00
|
|
|
|
|
|
|
# Filesystem access
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectControlGroups = true;
|
2019-02-24 12:35:04 +00:00
|
|
|
RuntimeDirectory = "memcached";
|
2019-06-10 08:50:04 +01:00
|
|
|
# Caps
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
# Misc.
|
|
|
|
LockPersonality = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
2013-06-10 21:45:24 +01:00
|
|
|
};
|
2018-01-03 11:10:45 +00:00
|
|
|
};
|
2013-06-10 21:45:24 +01:00
|
|
|
};
|
2018-01-03 11:10:45 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule ["services" "memcached" "socket"] ''
|
2019-02-24 12:35:04 +00:00
|
|
|
This option was replaced by a fixed unix socket path at /run/memcached/memcached.sock enabled using services.memcached.enableUnixSocket.
|
2018-01-03 11:10:45 +00:00
|
|
|
'')
|
|
|
|
];
|
2013-06-10 21:45:24 +01:00
|
|
|
|
|
|
|
}
|