Skip to content

Commit 23468ad

Browse files
nixos/nextcloud: use LoadCredential to read services.nextcloud.secretFile
This patch adds support for using systemd's LoadCredential feature to read in a json file at a path defined in the services.nextcloud.secretFile option. This is a follow up to 2ce1e84.
1 parent 866d0db commit 23468ad

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

nixos/modules/services/web-apps/nextcloud.nix

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ let
116116
++ (lib.optional (cfg.config.objectstore.s3.enable) "s3_secret:${cfg.config.objectstore.s3.secretFile}")
117117
++ (lib.optional (
118118
cfg.config.objectstore.s3.sseCKeyFile != null
119-
) "s3_sse_c_key:${cfg.config.objectstore.s3.sseCKeyFile}");
119+
) "s3_sse_c_key:${cfg.config.objectstore.s3.sseCKeyFile}")
120+
++ (lib.optional (cfg.secretFile != null) "secret_file:${cfg.secretFile}");
120121

121122
requiresRuntimeSystemdCredentials = (lib.length runtimeSystemdCredentials) != 0;
122123

@@ -195,7 +196,6 @@ let
195196
overrideConfig =
196197
let
197198
c = cfg.config;
198-
requiresReadSecretFunction = c.dbpassFile != null || c.objectstore.s3.enable;
199199
objectstoreConfig =
200200
let
201201
s3 = c.objectstore.s3;
@@ -232,7 +232,7 @@ let
232232
in
233233
pkgs.writeText "nextcloud-config.php" ''
234234
<?php
235-
${optionalString requiresReadSecretFunction ''
235+
${optionalString requiresRuntimeSystemdCredentials ''
236236
function nix_read_secret($credential_name) {
237237
$credentials_directory = getenv("CREDENTIALS_DIRECTORY");
238238
if (!$credentials_directory) {
@@ -253,7 +253,19 @@ let
253253
}
254254
255255
return trim(file_get_contents($credential_path));
256-
}''}
256+
}
257+
258+
function nix_read_secret_and_decode_json_file($credential_name) {
259+
$decoded = json_decode(nix_read_secret($credential_name), true);
260+
261+
if (json_last_error() !== JSON_ERROR_NONE) {
262+
error_log(sprintf("Cannot decode %s, because: %s", $file, json_last_error_msg()));
263+
exit(1);
264+
}
265+
266+
return $decoded;
267+
}
268+
''}
257269
function nix_decode_json_file($file, $error) {
258270
if (!file_exists($file)) {
259271
throw new \RuntimeException(sprintf($error, $file));
@@ -287,10 +299,7 @@ let
287299
));
288300
289301
${optionalString (cfg.secretFile != null) ''
290-
$CONFIG = array_replace_recursive($CONFIG, nix_decode_json_file(
291-
"${cfg.secretFile}",
292-
"Cannot start Nextcloud, secrets file %s set by NixOS doesn't exist!"
293-
));
302+
$CONFIG = array_replace_recursive($CONFIG, nix_read_secret_and_decode_json_file('secret_file'));
294303
''}
295304
'';
296305
in

nixos/tests/nextcloud/with-declarative-redis-and-secrets.nix

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ runTest (
8484
# This file is meant to contain secret options which should
8585
# not go into the nix store. Here it is just used to set the
8686
# redis password.
87-
environment.etc."nextcloud-secrets.json".text = ''
88-
{
89-
"redis": {
90-
"password": "secret"
91-
}
92-
}
93-
'';
87+
environment.etc."nextcloud-secrets.json" = {
88+
mode = "0600";
89+
text = builtins.toJSON {
90+
redis.password = "secret";
91+
};
92+
};
9493
};
9594
};
9695

0 commit comments

Comments
 (0)