1 { config, lib, pkgs, ... }:
7 cfg = config.services.memcached;
9 memcached = pkgs.memcached;
19 services.memcached = {
20 enable = mkEnableOption "Memcached";
24 default = "memcached";
25 description = "The user to run Memcached as";
30 default = "127.0.0.1";
31 description = "The IP address to bind to.";
37 description = "The port to bind to.";
40 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";
42 maxMemory = mkOption {
43 type = types.ints.unsigned;
45 description = "The maximum amount of memory to use for storage, in megabytes.";
48 maxConnections = mkOption {
49 type = types.ints.unsigned;
51 description = "The maximum number of simultaneous connections.";
54 extraOptions = mkOption {
55 type = types.listOf types.str;
57 description = "A list of extra options that will be added as a suffix when running memcached.";
65 config = mkIf config.services.memcached.enable {
67 users.users = optionalAttrs (cfg.user == "memcached") {
68 memcached.description = "Memcached server user";
69 memcached.isSystemUser = true;
70 memcached.group = "memcached";
72 users.groups = optionalAttrs (cfg.user == "memcached") { memcached = {}; };
74 environment.systemPackages = [ memcached ];
76 systemd.services.memcached = {
77 description = "Memcached server";
79 wantedBy = [ "multi-user.target" ];
80 after = [ "network.target" ];
85 networking = if cfg.enableUnixSocket
86 then "-s /run/memcached/memcached.sock"
87 else "-l ${cfg.listen} -p ${toString cfg.port}";
88 in "${memcached}/bin/memcached ${networking} -m ${toString cfg.maxMemory} -c ${toString cfg.maxConnections} ${concatStringsSep " " cfg.extraOptions}";
93 ProtectSystem = "strict";
96 PrivateDevices = true;
97 ProtectKernelTunables = true;
98 ProtectKernelModules = true;
99 ProtectControlGroups = true;
100 RuntimeDirectory = "memcached";
102 CapabilityBoundingSet = "";
103 NoNewPrivileges = true;
105 LockPersonality = true;
106 RestrictRealtime = true;
107 PrivateMounts = true;
108 MemoryDenyWriteExecute = true;
113 (mkRemovedOptionModule ["services" "memcached" "socket"] ''
114 This option was replaced by a fixed unix socket path at /run/memcached/memcached.sock enabled using services.memcached.enableUnixSocket.