]>
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"; | |
52 | keyfile = "${config.secrets.location}/redis/spiped_keyfile"; | |
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; | |
73 | paths = [ "${config.secrets.location}/redis/predixy.conf" ]; | |
74 | }; | |
75 | ||
76 | networking.firewall.allowedTCPPorts = [ 7617 16379 ]; | |
77 | secrets.keys = [ | |
78 | { | |
79 | dest = "redis/predixy.conf"; | |
80 | user = "redis"; | |
81 | group = "redis"; | |
82 | permissions = "0400"; | |
83 | text = '' | |
84 | Name Predixy | |
85 | Bind 127.0.0.1:7617 | |
86 | ClientTimeout 300 | |
87 | WorkerThreads 1 | |
88 | ||
89 | Authority { | |
ab8f306d | 90 | Auth "${config.myEnv.databases.redis.predixy.read}" { |
dded6699 IB |
91 | Mode read |
92 | } | |
93 | } | |
94 | ||
95 | StandaloneServerPool { | |
96 | Databases 16 | |
97 | RefreshMethod fixed | |
98 | Group shard001 { | |
ab8f306d | 99 | + ${config.myEnv.databases.redis.socket} |
dded6699 IB |
100 | } |
101 | } | |
102 | ''; | |
103 | } | |
104 | { | |
105 | dest = "redis/spiped_keyfile"; | |
106 | user = "spiped"; | |
107 | group = "spiped"; | |
108 | permissions = "0400"; | |
ab8f306d | 109 | text = config.myEnv.databases.redis.spiped_key; |
dded6699 IB |
110 | } |
111 | ]; | |
112 | ||
850adcf4 IB |
113 | systemd.slices.redis = { |
114 | description = "Redis slice"; | |
115 | }; | |
116 | ||
dded6699 IB |
117 | systemd.services.predixy = { |
118 | description = "Redis proxy"; | |
119 | wantedBy = [ "multi-user.target" ]; | |
120 | after = [ "redis.service" ]; | |
121 | ||
122 | serviceConfig = { | |
850adcf4 | 123 | Slice = "redis.slice"; |
dded6699 IB |
124 | User = "redis"; |
125 | Group = "redis"; | |
126 | SupplementaryGroups = "keys"; | |
127 | Type = "simple"; | |
128 | ||
129 | ExecStart = "${pkgs.predixy}/bin/predixy ${config.secrets.location}/redis/predixy.conf"; | |
130 | }; | |
131 | ||
132 | }; | |
182ae57f IB |
133 | }; |
134 | } | |
135 |