]>
Commit | Line | Data |
---|---|---|
ab8f306d | 1 | { lib, config, pkgs, ... }: |
182ae57f IB |
2 | let |
3 | cfg = config.myServices.databases.redis; | |
4 | in { | |
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; | |
35 | services.redis = rec { | |
36 | enable = true; | |
37 | bind = "127.0.0.1"; | |
38 | unixSocket = cfg.sockets.redis; | |
39 | extraConfig = '' | |
40 | unixsocketperm 777 | |
41 | maxclients 1024 | |
42 | ''; | |
43 | }; | |
850adcf4 | 44 | systemd.services.redis.serviceConfig.Slice = "redis.slice"; |
dded6699 IB |
45 | |
46 | services.spiped = { | |
47 | enable = true; | |
48 | config.redis = { | |
49 | decrypt = true; | |
50 | source = "0.0.0.0:16379"; | |
51 | target = "/run/redis/redis.sock"; | |
da30ae4f | 52 | keyfile = config.secrets.fullPaths."redis/spiped_keyfile"; |
dded6699 IB |
53 | }; |
54 | }; | |
55 | systemd.services.spiped_redis = { | |
56 | description = "Secure pipe 'redis'"; | |
57 | after = [ "network.target" ]; | |
58 | wantedBy = [ "multi-user.target" ]; | |
59 | ||
60 | serviceConfig = { | |
850adcf4 IB |
61 | Slice = "redis.slice"; |
62 | Restart = "always"; | |
63 | User = "spiped"; | |
dded6699 IB |
64 | PermissionsStartOnly = true; |
65 | SupplementaryGroups = "keys"; | |
66 | }; | |
67 | ||
68 | script = "exec ${pkgs.spiped}/bin/spiped -F `cat /etc/spiped/redis.spec`"; | |
69 | }; | |
70 | ||
71 | services.filesWatcher.predixy = { | |
72 | restart = true; | |
da30ae4f | 73 | paths = [ config.secrets.fullPaths."redis/predixy.conf" ]; |
dded6699 IB |
74 | }; |
75 | ||
76 | networking.firewall.allowedTCPPorts = [ 7617 16379 ]; | |
4c4652aa IB |
77 | secrets.keys = { |
78 | "redis/predixy.conf" = { | |
dded6699 IB |
79 | user = "redis"; |
80 | group = "redis"; | |
81 | permissions = "0400"; | |
82 | text = '' | |
83 | Name Predixy | |
84 | Bind 127.0.0.1:7617 | |
85 | ClientTimeout 300 | |
86 | WorkerThreads 1 | |
87 | ||
88 | Authority { | |
ab8f306d | 89 | Auth "${config.myEnv.databases.redis.predixy.read}" { |
dded6699 IB |
90 | Mode read |
91 | } | |
92 | } | |
93 | ||
94 | StandaloneServerPool { | |
95 | Databases 16 | |
96 | RefreshMethod fixed | |
97 | Group shard001 { | |
ab8f306d | 98 | + ${config.myEnv.databases.redis.socket} |
dded6699 IB |
99 | } |
100 | } | |
101 | ''; | |
4c4652aa IB |
102 | }; |
103 | "redis/spiped_keyfile" = { | |
dded6699 IB |
104 | user = "spiped"; |
105 | group = "spiped"; | |
106 | permissions = "0400"; | |
ab8f306d | 107 | text = config.myEnv.databases.redis.spiped_key; |
4c4652aa IB |
108 | }; |
109 | }; | |
dded6699 | 110 | |
850adcf4 IB |
111 | systemd.slices.redis = { |
112 | description = "Redis slice"; | |
113 | }; | |
114 | ||
dded6699 IB |
115 | systemd.services.predixy = { |
116 | description = "Redis proxy"; | |
117 | wantedBy = [ "multi-user.target" ]; | |
118 | after = [ "redis.service" ]; | |
119 | ||
120 | serviceConfig = { | |
850adcf4 | 121 | Slice = "redis.slice"; |
dded6699 IB |
122 | User = "redis"; |
123 | Group = "redis"; | |
124 | SupplementaryGroups = "keys"; | |
125 | Type = "simple"; | |
126 | ||
da30ae4f | 127 | ExecStart = "${pkgs.predixy}/bin/predixy ${config.secrets.fullPaths."redis/predixy.conf"}"; |
dded6699 IB |
128 | }; |
129 | ||
130 | }; | |
182ae57f IB |
131 | }; |
132 | } | |
133 |