1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.memcached;
8
9 memcached = pkgs.memcached;
10
11in
12
13{
14
15 ###### interface
16
17 options = {
18
19 services.memcached = {
20
21 enable = mkOption {
22 default = false;
23 description = "
24 Whether to enable Memcached.
25 ";
26 };
27
28 user = mkOption {
29 default = "memcached";
30 description = "The user to run Memcached as";
31 };
32
33 listen = mkOption {
34 default = "127.0.0.1";
35 description = "The IP address to bind to";
36 };
37
38 port = mkOption {
39 default = 11211;
40 description = "The port to bind to";
41 };
42
43 socket = mkOption {
44 default = "";
45 description = "Unix socket path to listen on. Setting this will disable network support";
46 example = "/var/run/memcached";
47 };
48
49 maxMemory = mkOption {
50 default = 64;
51 description = "The maximum amount of memory to use for storage, in megabytes.";
52 };
53
54 maxConnections = mkOption {
55 default = 1024;
56 description = "The maximum number of simultaneous connections";
57 };
58
59 extraOptions = mkOption {
60 default = [];
61 description = "A list of extra options that will be added as a suffix when running memcached";
62 };
63 };
64
65 };
66
67 ###### implementation
68
69 config = mkIf config.services.memcached.enable {
70
71 users.extraUsers.memcached =
72 { name = cfg.user;
73 uid = config.ids.uids.memcached;
74 description = "Memcached server user";
75 };
76
77 environment.systemPackages = [ memcached ];
78
79 systemd.services.memcached =
80 { description = "Memcached server";
81
82 wantedBy = [ "multi-user.target" ];
83 after = [ "network.target" ];
84
85 serviceConfig = {
86 ExecStart =
87 let
88 networking = if cfg.socket != ""
89 then "-s ${cfg.socket}"
90 else "-l ${cfg.listen} -p ${toString cfg.port}";
91 in "${memcached}/bin/memcached ${networking} -m ${toString cfg.maxMemory} -c ${toString cfg.maxConnections} ${concatStringsSep " " cfg.extraOptions}";
92
93 User = cfg.user;
94 };
95 };
96 };
97
98}