]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/databases/redis.nix
WIP upgrade
[perso/Immae/Config/Nix.git] / modules / private / databases / redis.nix
CommitLineData
ab8f306d 1{ lib, config, pkgs, ... }:
182ae57f
IB
2let
3 cfg = config.myServices.databases.redis;
4in {
5 options.myServices.databases.redis = {
6 enable = lib.mkOption {
8415083e 7 default = false;
182ae57f
IB
8 example = true;
9 description = "Whether to enable redis database";
10 type = lib.types.bool;
11 };
12 socketsDir = lib.mkOption {
13 type = lib.types.path;
14 default = "/run/redis";
15 description = ''
16 The directory where Redis puts sockets.
17 '';
18 };
19 # Output variables
182ae57f
IB
20 sockets = lib.mkOption {
21 type = lib.types.attrsOf lib.types.path;
22 default = {
23 redis = "${cfg.socketsDir}/redis.sock";
24 };
25 readOnly = true;
26 description = ''
27 Redis sockets
28 '';
29 };
30 };
31
32 config = lib.mkIf cfg.enable {
33 users.users.redis.uid = config.ids.uids.redis;
34 users.groups.redis.gid = config.ids.gids.redis;
e34b3079 35 services.redis.servers."" = {
182ae57f
IB
36 enable = true;
37 bind = "127.0.0.1";
38 unixSocket = cfg.sockets.redis;
e34b3079
IB
39 unixSocketPerm = 777;
40 maxclients = 1024;
182ae57f 41 };
850adcf4 42 systemd.services.redis.serviceConfig.Slice = "redis.slice";
e34b3079 43 systemd.services.redis.serviceConfig.RuntimeDirectoryMode = lib.mkForce "0755";
dded6699
IB
44
45 services.spiped = {
46 enable = true;
47 config.redis = {
48 decrypt = true;
49 source = "0.0.0.0:16379";
50 target = "/run/redis/redis.sock";
da30ae4f 51 keyfile = config.secrets.fullPaths."redis/spiped_keyfile";
dded6699
IB
52 };
53 };
54 systemd.services.spiped_redis = {
55 description = "Secure pipe 'redis'";
56 after = [ "network.target" ];
57 wantedBy = [ "multi-user.target" ];
58
59 serviceConfig = {
850adcf4
IB
60 Slice = "redis.slice";
61 Restart = "always";
62 User = "spiped";
dded6699
IB
63 PermissionsStartOnly = true;
64 SupplementaryGroups = "keys";
65 };
66
67 script = "exec ${pkgs.spiped}/bin/spiped -F `cat /etc/spiped/redis.spec`";
68 };
69
70 services.filesWatcher.predixy = {
71 restart = true;
da30ae4f 72 paths = [ config.secrets.fullPaths."redis/predixy.conf" ];
dded6699
IB
73 };
74
75 networking.firewall.allowedTCPPorts = [ 7617 16379 ];
4c4652aa
IB
76 secrets.keys = {
77 "redis/predixy.conf" = {
dded6699
IB
78 user = "redis";
79 group = "redis";
80 permissions = "0400";
81 text = ''
82 Name Predixy
83 Bind 127.0.0.1:7617
84 ClientTimeout 300
85 WorkerThreads 1
86
87 Authority {
ab8f306d 88 Auth "${config.myEnv.databases.redis.predixy.read}" {
dded6699
IB
89 Mode read
90 }
91 }
92
93 StandaloneServerPool {
94 Databases 16
95 RefreshMethod fixed
96 Group shard001 {
ab8f306d 97 + ${config.myEnv.databases.redis.socket}
dded6699
IB
98 }
99 }
100 '';
4c4652aa
IB
101 };
102 "redis/spiped_keyfile" = {
dded6699
IB
103 user = "spiped";
104 group = "spiped";
105 permissions = "0400";
ab8f306d 106 text = config.myEnv.databases.redis.spiped_key;
4c4652aa
IB
107 };
108 };
dded6699 109
850adcf4
IB
110 systemd.slices.redis = {
111 description = "Redis slice";
112 };
113
dded6699
IB
114 systemd.services.predixy = {
115 description = "Redis proxy";
116 wantedBy = [ "multi-user.target" ];
117 after = [ "redis.service" ];
118
119 serviceConfig = {
850adcf4 120 Slice = "redis.slice";
dded6699
IB
121 User = "redis";
122 Group = "redis";
123 SupplementaryGroups = "keys";
124 Type = "simple";
125
da30ae4f 126 ExecStart = "${pkgs.predixy}/bin/predixy ${config.secrets.fullPaths."redis/predixy.conf"}";
dded6699
IB
127 };
128
129 };
182ae57f
IB
130 };
131}
132