Skip to content

Commit 7986289

Browse files
committedJul 21, 2024
nixos/keycloak: add realmFiles option
Add an option to import Keycloak realms during startup from exported realm files.
1 parent ecc9d53 commit 7986289

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
 

‎nixos/doc/manual/release-notes/rl-2411.section.md

+2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@
244244

245245
- `nixosTests` now provide a working IPv6 setup for VLAN 1 by default.
246246

247+
- The `keycloak` module provides now a `realmFiles` options that allows to import realms during startup.
248+
247249
- To facilitate dependency injection, the `imgui` package now builds a static archive using vcpkg' CMake rules.
248250
The derivation now installs "impl" headers selectively instead of by a wildcard.
249251
Use `imgui.src` if you just want to access the unpacked sources.

‎nixos/modules/services/web-apps/keycloak.nix

+30-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ in
8282
path
8383
enum
8484
package
85-
port;
85+
port
86+
listOf;
8687

8788
assertStringPath = optionName: value:
8889
if isPath value then
@@ -272,6 +273,25 @@ in
272273
'';
273274
};
274275

276+
realmFiles = mkOption {
277+
type = listOf path;
278+
example = lib.literalExpression ''
279+
[
280+
./some/realm.json
281+
./another/realm.json
282+
]
283+
'';
284+
default = [];
285+
description = ''
286+
Set of realm files that the server is going to try to import
287+
during startup. If a realm already exists in the server, the import
288+
operation is skipped. Importing the master realm is not supported.
289+
All files are expected to be in `json` format. See the
290+
[documentation](https://www.keycloak.org/server/importExport) for
291+
further information.
292+
'';
293+
};
294+
275295
settings = mkOption {
276296
type = lib.types.submodule {
277297
freeformType = attrsOf (nullOr (oneOf [ str int bool (attrsOf path) ]));
@@ -620,6 +640,12 @@ in
620640
replace-secret ${hashString "sha256" file} $CREDENTIALS_DIRECTORY/${baseNameOf file} /run/keycloak/conf/keycloak.conf
621641
'';
622642
secretReplacements = lib.concatMapStrings mkSecretReplacement secretPaths;
643+
installRealmFile = file:
644+
let
645+
baseName = builtins.baseNameOf file;
646+
target = if lib.hasSuffix ".json" baseName then baseName else "${baseName}.json";
647+
in
648+
"install -D -m 0600 ${file} /run/keycloak/data/import/${target}";
623649
in
624650
{
625651
after = databaseServices;
@@ -671,10 +697,12 @@ in
671697
'' + optionalString (cfg.sslCertificate != null && cfg.sslCertificateKey != null) ''
672698
mkdir -p /run/keycloak/ssl
673699
cp $CREDENTIALS_DIRECTORY/ssl_{cert,key} /run/keycloak/ssl/
700+
'' + ''
701+
${concatStringsSep "\n" (map installRealmFile cfg.realmFiles)}
674702
'' + ''
675703
export KEYCLOAK_ADMIN=admin
676704
export KEYCLOAK_ADMIN_PASSWORD=${escapeShellArg cfg.initialAdminPassword}
677-
kc.sh --verbose start --optimized
705+
kc.sh --verbose start --optimized ${lib.optionalString (cfg.realmFiles != []) "--import-realm"}
678706
'';
679707
};
680708

0 commit comments

Comments
 (0)
Failed to load comments.