summary refs log tree commit diff
path: root/nixos/modules/services/databases/memcached.nix
blob: c6875af506d35f9c7fd343d82fa857f399de6d83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.memcached;

  memcached = pkgs.memcached;

in

{

  ###### interface

  options = {

    services.memcached = {

      enable = mkOption {
        default = false;
        description = "
          Whether to enable Memcached.
        ";
      };

      user = mkOption {
        default = "memcached";
        description = "The user to run Memcached as";
      };

      listen = mkOption {
        default = "127.0.0.1";
        description = "The IP address to bind to";
      };

      port = mkOption {
        default = 11211;
        description = "The port to bind to";
      };

      socket = mkOption {
        default = "";
        description = "Unix socket path to listen on. Setting this will disable network support";
        example = "/var/run/memcached";
      };

      maxMemory = mkOption {
        default = 64;
        description = "The maximum amount of memory to use for storage, in megabytes.";
      };

      maxConnections = mkOption {
        default = 1024;
        description = "The maximum number of simultaneous connections";
      };

      extraOptions = mkOption {
        default = [];
        description = "A list of extra options that will be added as a suffix when running memcached";
      };
    };

  };

  ###### implementation

  config = mkIf config.services.memcached.enable {

    users.extraUsers.memcached =
      { name = cfg.user;
        uid = config.ids.uids.memcached;
        description = "Memcached server user";
      };

    environment.systemPackages = [ memcached ];

    systemd.services.memcached =
      { description = "Memcached server";

        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];

        serviceConfig = {
          ExecStart =
            let
              networking = if cfg.socket != ""
                then "-s ${cfg.socket}"
                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;
        };
      };
  };

}