git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #80032 from Mic92/redis

nixos/redis: add requirePassFile option

commit7448211021e9e2dd754c72a63e8c64b84687f674 authorJörg Thalheim <Mic92@users.noreply.github.com> dateMon 17 Feb 2020, 21:28 (7 years ago) parent99927029cfe5a7
1 file changed+33−14
nixos/modules/services/databases/redis.nix+33−14
nixos/modules/services/databases/redis.nix+33−14
@@ -150,10 +150,20 @@ in
150150 requirePass = mkOption {
151151 type = with types; nullOr str;
152152 default = null;
153− description = "Password for database (STORED PLAIN TEXT, WORLD-READABLE IN NIX STORE)";
153+ description = ''
154+ Password for database (STORED PLAIN TEXT, WORLD-READABLE IN NIX STORE).
155+ Use requirePassFile to store it outside of the nix store in a dedicated file.
156+ '';
154157 example = "letmein!";
155158 };
156159
160+ requirePassFile = mkOption {
161+ type = with types; nullOr path;
162+ default = null;
163+ description = "File with password for the database.";
164+ example = "/run/keys/redis-password";
165+ };
166+
157167 appendOnly = mkOption {
158168 type = types.bool;
159169 default = false;
@@ -192,6 +202,10 @@ in
192202 ###### implementation
193203
194204 config = mkIf config.services.redis.enable {
205+ assertions = [{
206+ assertion = cfg.requirePass != null -> cfg.requirePassFile == null;
207+ message = "You can only set one services.redis.requirePass or services.redis.requirePassFile";
208+ }];
195209 boot.kernel.sysctl = (mkMerge [
196210 { "vm.nr_hugepages" = "0"; }
197211 ( mkIf cfg.vmOverCommit { "vm.overcommit_memory" = "1"; } )
@@ -208,21 +222,26 @@ in
208222
209223 environment.systemPackages = [ cfg.package ];
210224
211− systemd.services.redis =
212− { description = "Redis Server";
225+ systemd.services.redis = {
226+ description = "Redis Server";
213227
214− wantedBy = [ "multi-user.target" ];
215− after = [ "network.target" ];
228+ wantedBy = [ "multi-user.target" ];
229+ after = [ "network.target" ];
216230
217− serviceConfig = {
218− ExecStart = "${cfg.package}/bin/redis-server ${redisConfig}";
219− RuntimeDirectory = "redis";
220− StateDirectory = "redis";
221− Type = "notify";
222− User = "redis";
223− };
224− };
231+ preStart = ''
232+ install -m 600 ${redisConfig} /run/redis/redis.conf
233+ '' + optionalString (cfg.requirePassFile != null) ''
234+ password=$(cat ${escapeShellArg cfg.requirePassFile})
235+ echo "requirePass $password" >> /run/redis/redis.conf
236+ '';
225237
238+ serviceConfig = {
239+ ExecStart = "${cfg.package}/bin/redis-server /run/redis/redis.conf";
240+ RuntimeDirectory = "redis";
241+ StateDirectory = "redis";
242+ Type = "notify";
243+ User = "redis";
244+ };
245+ };
226246 };
227−
228247}