@@ -0,0 +1,155 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+
7+ cfg = config.services.aerospike;
8+
9+ aerospikeConf = pkgs.writeText "aerospike.conf" ''
10+ # This stanza must come first.
11+ service {
12+ user aerospike
13+ group aerospike
14+ paxos-single-replica-limit 1 # Number of nodes where the replica count is automatically reduced to 1.
15+ proto-fd-max 15000
16+ work-directory ${cfg.workDir}
17+ }
18+ logging {
19+ console {
20+ context any info
21+ }
22+ }
23+ mod-lua {
24+ system-path ${cfg.package}/share/udf/lua
25+ user-path ${cfg.workDir}/udf/lua
26+ }
27+ network {
28+ ${cfg.networkConfig}
29+ }
30+ ${cfg.extraConfig}
31+ '';
32+
33+in
34+
35+{
36+
37+ ###### interface
38+
39+ options = {
40+
41+ services.aerospike = {
42+ enable = mkEnableOption "Aerospike server";
43+
44+ package = mkOption {
45+ default = pkgs.aerospike;
46+ type = types.package;
47+ description = "Which Aerospike derivation to use";
48+ };
49+
50+ workDir = mkOption {
51+ type = types.str;
52+ default = "/var/lib/aerospike";
53+ description = "Location where Aerospike stores its files";
54+ };
55+
56+ networkConfig = mkOption {
57+ type = types.lines;
58+ default = ''
59+ service {
60+ address any
61+ port 3000
62+ }
63+
64+ heartbeat {
65+ address any
66+ mode mesh
67+ port 3002
68+ interval 150
69+ timeout 10
70+ }
71+
72+ fabric {
73+ address any
74+ port 3001
75+ }
76+
77+ info {
78+ address any
79+ port 3003
80+ }
81+ '';
82+ description = "network section of configuration file";
83+ };
84+
85+ extraConfig = mkOption {
86+ type = types.lines;
87+ default = "";
88+ example = ''
89+ namespace test {
90+ replication-factor 2
91+ memory-size 4G
92+ default-ttl 30d
93+ storage-engine memory
94+ }
95+ '';
96+ description = "Extra configuration";
97+ };
98+ };
99+
100+ };
101+
102+
103+ ###### implementation
104+
105+ config = mkIf config.services.aerospike.enable {
106+
107+ users.users.aerospike = {
108+ name = "aerospike";
109+ group = "aerospike";
110+ uid = config.ids.uids.aerospike;
111+ description = "Aerospike server user";
112+ };
113+ users.groups.aerospike.gid = config.ids.gids.aerospike;
114+
115+ systemd.services.aerospike = rec {
116+ description = "Aerospike server";
117+
118+ wantedBy = [ "multi-user.target" ];
119+ after = [ "network.target" ];
120+
121+ serviceConfig = {
122+ ExecStart = "${cfg.package}/bin/asd --fgdaemon --config-file ${aerospikeConf}";
123+ User = "aerospike";
124+ Group = "aerospike";
125+ LimitNOFILE = 100000;
126+ PermissionsStartOnly = true;
127+ };
128+
129+ preStart = ''
130+ if [ $(echo "$(${pkgs.procps}/bin/sysctl -n kernel.shmall) < 4294967296" | ${pkgs.bc}/bin/bc) == "1" ]; then
131+ echo "kernel.shmall too low, setting to 4G pages"
132+ ${pkgs.procps}/bin/sysctl -w kernel.shmall=4294967296
133+ fi
134+ if [ $(echo "$(${pkgs.procps}/bin/sysctl -n kernel.shmmax) < 1073741824" | ${pkgs.bc}/bin/bc) == "1" ]; then
135+ echo "kernel.shmmax too low, setting to 1GB"
136+ ${pkgs.procps}/bin/sysctl -w kernel.shmmax=1073741824
137+ fi
138+ if [ $(echo "$(cat /proc/sys/net/core/rmem_max) < 15728640" | ${pkgs.bc}/bin/bc) == "1" ]; then
139+ echo "increasing socket buffer limit (/proc/sys/net/core/rmem_max): $(cat /proc/sys/net/core/rmem_max) -> 15728640"
140+ echo 15728640 > /proc/sys/net/core/rmem_max
141+ fi
142+ if [ $(echo "$(cat /proc/sys/net/core/wmem_max) < 5242880" | ${pkgs.bc}/bin/bc) == "1" ]; then
143+ echo "increasing socket buffer limit (/proc/sys/net/core/wmem_max): $(cat /proc/sys/net/core/wmem_max) -> 5242880"
144+ echo 5242880 > /proc/sys/net/core/wmem_max
145+ fi
146+ install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}"
147+ install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}/smd"
148+ install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}/udf"
149+ install -d -m0700 -o ${serviceConfig.User} -g ${serviceConfig.Group} "${cfg.workDir}/udf/lua"
150+ '';
151+ };
152+
153+ };
154+
155+}