Skip to content

Commit 82a382e

Browse files
authored
nixos/keycloak: add realmFiles option (#273833)
2 parents 5e666ef + cd10f9a commit 82a382e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

nixos/doc/manual/release-notes/rl-2505.section.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@
614614

615615
- `services.soft-serve` now restarts upon config change.
616616

617+
- `services.keycloak` now provides a `realmFiles` option that allows to import realms during startup. See https://www.keycloak.org/server/importExport
618+
617619
- `bind.cacheNetworks` now only controls access for recursive queries, where it previously controlled access for all queries.
618620

619621
- [`services.mongodb.enableAuth`](#opt-services.mongodb.enableAuth) now uses the newer [mongosh](https://github.com/mongodb-js/mongosh) shell instead of the legacy shell to configure the initial superuser. You can configure the mongosh package to use through the [`services.mongodb.mongoshPackage`](#opt-services.mongodb.mongoshPackage) option.

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ in
9090
enum
9191
package
9292
port
93+
listOf
9394
;
9495

9596
assertStringPath =
@@ -288,6 +289,25 @@ in
288289
'';
289290
};
290291

292+
realmFiles = mkOption {
293+
type = listOf path;
294+
example = lib.literalExpression ''
295+
[
296+
./some/realm.json
297+
./another/realm.json
298+
]
299+
'';
300+
default = [ ];
301+
description = ''
302+
Realm files that the server is going to import during startup.
303+
If a realm already exists in the server, the import operation is
304+
skipped. Importing the master realm is not supported. All files are
305+
expected to be in `json` format. See the
306+
[documentation](https://www.keycloak.org/server/importExport) for
307+
further information.
308+
'';
309+
};
310+
291311
settings = mkOption {
292312
type = lib.types.submodule {
293313
freeformType = attrsOf (
@@ -644,6 +664,24 @@ in
644664
'';
645665
};
646666

667+
systemd.tmpfiles.settings."10-keycloak" =
668+
let
669+
mkTarget =
670+
file:
671+
let
672+
baseName = builtins.baseNameOf file;
673+
name = if lib.hasSuffix ".json" baseName then baseName else "${baseName}.json";
674+
in
675+
"/run/keycloak/data/import/${name}";
676+
settingsList = map (f: {
677+
name = mkTarget f;
678+
value = {
679+
"L+".argument = "${f}";
680+
};
681+
}) cfg.realmFiles;
682+
in
683+
builtins.listToAttrs settingsList;
684+
647685
systemd.services.keycloak =
648686
let
649687
databaseServices =
@@ -725,7 +763,7 @@ in
725763
cp $CREDENTIALS_DIRECTORY/ssl_{cert,key} /run/keycloak/ssl/
726764
''
727765
+ ''
728-
kc.sh --verbose start --optimized
766+
kc.sh --verbose start --optimized ${lib.optionalString (cfg.realmFiles != [ ]) "--import-realm"}
729767
'';
730768
};
731769

0 commit comments

Comments
 (0)