summary refs log tree commit diff
path: root/nixos/modules/services/databases/aerospike.nix
blob: 8109762aea788dd0dabfe277631a26bbf4c5f9aa (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.aerospike;

  aerospikeConf = pkgs.writeText "aerospike.conf" ''
    # This stanza must come first.
    service {
      user aerospike
      group aerospike
      paxos-single-replica-limit 1 # Number of nodes where the replica count is automatically reduced to 1.
      proto-fd-max 15000
      work-directory ${cfg.workDir}
    }
    logging {
      console {
        context any info
      }
    }
    mod-lua {
      system-path ${cfg.package}/share/udf/lua
      user-path ${cfg.workDir}/udf/lua
    }
    network {
      ${cfg.networkConfig}
    }
    ${cfg.extraConfig}
  '';

in

{

  ###### interface

  options = {

    services.aerospike = {
      enable = mkEnableOption "Aerospike server";

      package = mkOption {
        default = pkgs.aerospike;
        defaultText = literalExpression "pkgs.aerospike";
        type = types.package;
        description = "Which Aerospike derivation to use";
      };

      workDir = mkOption {
        type = types.str;
        default = "/var/lib/aerospike";
        description = "Location where Aerospike stores its files";
      };

      networkConfig = mkOption {
        type = types.lines;
        default = ''
          service {
            address any
            port 3000
          }

          heartbeat {
            address any
            mode mesh
            port 3002
            interval 150
            timeout 10
          }

          fabric {
            address any
            port 3001
          }

          info {
            address any
            port 3003
          }
        '';
        description = "network section of configuration file";
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        example = ''
          namespace test {
            replication-factor 2
            memory-size 4G
            default-ttl 30d
            storage-engine memory
          }
        '';
        description = "Extra configuration";
      };
    };

  };


  ###### implementation

  config = mkIf config.services.aerospike.enable {

    users.users.aerospike = {
      name = "aerospike";
      group = "aerospike";
      uid = config.ids.uids.aerospike;
      description = "Aerospike server user";
    };
    users.groups.aerospike.gid = config.ids.gids.aerospike;

    systemd.services.aerospike = rec {
      description = "Aerospike server";

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

      serviceConfig = {
        ExecStart = "${cfg.package}/bin/asd --fgdaemon --config-file ${aerospikeConf}";
        User = "aerospike";
        Group = "aerospike";
        LimitNOFILE = 100000;
        PermissionsStartOnly = true;
      };

      preStart = ''
        if [ $(echo "$(${pkgs.procps}/bin/sysctl -n kernel.shmall) < 4294967296" | ${pkgs.bc}/bin/bc) == "1"  ]; then
          echo "kernel.shmall too low, setting to 4G pages"
          ${pkgs.procps}/bin/sysctl -w kernel.shmall=4294967296
        fi
        if [ $(echo "$(${pkgs.procps}/bin/sysctl -n kernel.shmmax) < 1073741824" | ${pkgs.bc}/bin/bc) == "1"  ]; then
          echo "kernel.shmmax too low, setting to 1GB"
          ${pkgs.procps}/bin/sysctl -w kernel.shmmax=1073741824
        fi
        if [ $(echo "$(cat /proc/sys/net/core/rmem_max) < 15728640" | ${pkgs.bc}/bin/bc) == "1" ]; then
          echo "increasing socket buffer limit (/proc/sys/net/core/rmem_max): $(cat /proc/sys/net/core/rmem_max) -> 15728640"
          echo 15728640 > /proc/sys/net/core/rmem_max
        fi
        if [ $(echo "$(cat /proc/sys/net/core/wmem_max) <  5242880" | ${pkgs.bc}/bin/bc) == "1"  ]; then
          echo "increasing socket buffer limit (/proc/sys/net/core/wmem_max): $(cat /proc/sys/net/core/wmem_max) -> 5242880"
          echo  5242880 > /proc/sys/net/core/wmem_max
        fi
        install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}"
        install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}/smd"
        install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}/udf"
        install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}/udf/lua"
      '';
    };

  };

}