From cfcd8678e5e8ce0c9b2bdc5eacf2d5c1ec403d49 Mon Sep 17 00:00:00 2001 From: kraftnix Date: Fri, 25 Jul 2025 00:38:14 +0200 Subject: [PATCH] nixos/nextcloud: add secrets option Uses existing `nix_read_secret` and LoadCredential to read contents of a file into an entry in `config.php` --- nixos/modules/services/web-apps/nextcloud.nix | 24 ++++++++++++++++++- nixos/tests/nextcloud/basic.nix | 6 +++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/web-apps/nextcloud.nix b/nixos/modules/services/web-apps/nextcloud.nix index be0b87eb7642..e05c8955cafe 100644 --- a/nixos/modules/services/web-apps/nextcloud.nix +++ b/nixos/modules/services/web-apps/nextcloud.nix @@ -120,7 +120,8 @@ let ++ (lib.optional ( cfg.config.objectstore.s3.sseCKeyFile != null ) "s3_sse_c_key:${cfg.config.objectstore.s3.sseCKeyFile}") - ++ (lib.optional (cfg.secretFile != null) "secret_file:${cfg.secretFile}"); + ++ (lib.optional (cfg.secretFile != null) "secret_file:${cfg.secretFile}") + ++ (lib.mapAttrsToList (credential: file: "${credential}:${file}") cfg.secrets); requiresRuntimeSystemdCredentials = (lib.length runtimeSystemdCredentials) != 0; @@ -296,6 +297,9 @@ let ) "'dbtableprefix' => '${toString c.dbtableprefix}',"} ${lib.optionalString (c.dbpassFile != null) "'dbpassword' => nix_read_secret('dbpass'),"} 'dbtype' => '${c.dbtype}', + ${lib.concatStringsSep "\n" ( + lib.mapAttrsToList (name: credential: "'${name}' => nix_read_secret('${name}'),") cfg.secrets + )} ${objectstoreConfig} ]; @@ -390,6 +394,24 @@ in ''; example = "/mnt/nextcloud-file"; }; + secrets = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.pathWith { + inStore = false; + absolute = true; + } + ); + default = { }; + description = '' + Secret files to read into entries in `config.php`. + This uses `nix_read_secret` and LoadCredential to read the contents of the file into the entry in `config.php`. + ''; + example = lib.literalExpression '' + { + oidc_login_client_secret = "/run/secrets/nextcloud_oidc_secret"; + } + ''; + }; extraApps = lib.mkOption { type = lib.types.attrsOf lib.types.package; default = { }; diff --git a/nixos/tests/nextcloud/basic.nix b/nixos/tests/nextcloud/basic.nix index 276465716617..6062e198269c 100644 --- a/nixos/tests/nextcloud/basic.nix +++ b/nixos/tests/nextcloud/basic.nix @@ -63,8 +63,11 @@ runTest ( }; phpExtraExtensions = all: [ all.bz2 ]; nginx.enableFastcgiRequestBuffering = true; + secrets.mysecret = "/etc/nextcloud/mysecretfile"; }; + environment.etc."nextcloud/mysecretfile".text = "foobar"; + specialisation.withoutMagick.configuration = { services.nextcloud.enableImagemagick = false; }; @@ -116,6 +119,9 @@ runTest ( client_hash = client.succeed("nix-hash testfile.bin").strip() nextcloud_hash = nextcloud.succeed("nix-hash /var/lib/nextcloud-data/data/root/files/testfile.bin").strip() t.assertEqual(client_hash, nextcloud_hash) + + with subtest("secrets"): + assert "foobar" == nextcloud.succeed("nextcloud-occ config:system:get mysecret").strip() ''; } )