From 84f70eefd1c4f90e892164afa39931a9fc5ba8db Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Sun, 9 Jan 2022 20:58:25 +0300 Subject: [PATCH] keycloak service: add themes support Custom themes can be packaged and then added using `themes` config attribute. --- nixos/modules/services/web-apps/keycloak.nix | 47 +++++++++++++++++++- nixos/modules/services/web-apps/keycloak.xml | 11 +++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/web-apps/keycloak.nix b/nixos/modules/services/web-apps/keycloak.nix index d4177c77bce3..39e5ab970b94 100644 --- a/nixos/modules/services/web-apps/keycloak.nix +++ b/nixos/modules/services/web-apps/keycloak.nix @@ -229,6 +229,20 @@ in ''; }; + themes = lib.mkOption { + type = lib.types.attrsOf lib.types.package; + default = {}; + description = '' + Additional theme packages for Keycloak. Each theme is linked into + subdirectory with a corresponding attribute name. + + Theme packages consist of several subdirectories which provide + different theme types: for example, account, + login etc. After adding a theme to this option you + can select it by its name in Keycloak administration console. + ''; + }; + extraConfig = lib.mkOption { type = lib.types.attrsOf lib.types.anything; default = { }; @@ -289,16 +303,45 @@ in ${pkgs.jre}/bin/keytool -importcert -trustcacerts -alias MySQLCACert -file ${cfg.database.caCert} -keystore $out -storepass notsosecretpassword -noprompt ''; + # Both theme and theme type directories need to be actual directories in one hierarchy to pass Keycloak checks. + themesBundle = pkgs.runCommand "keycloak-themes" {} '' + linkTheme() { + theme="$1" + name="$2" + + mkdir "$out/$name" + for typeDir in "$theme"/*; do + if [ -d "$typeDir" ]; then + type="$(basename "$typeDir")" + mkdir "$out/$name/$type" + for file in "$typeDir"/*; do + ln -sn "$file" "$out/$name/$type/$(basename "$file")" + done + fi + done + } + + mkdir -p "$out" + for theme in ${cfg.package}/themes/*; do + if [ -d "$theme" ]; then + linkTheme "$theme" "$(basename "$theme")" + fi + done + + ${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: theme: "linkTheme ${theme} ${lib.escapeShellArg name}") cfg.themes)} + ''; + keycloakConfig' = builtins.foldl' lib.recursiveUpdate { "interface=public".inet-address = cfg.bindAddress; "socket-binding-group=standard-sockets"."socket-binding=http".port = cfg.httpPort; - "subsystem=keycloak-server"."spi=hostname" = { - "provider=default" = { + "subsystem=keycloak-server" = { + "spi=hostname"."provider=default" = { enabled = true; properties = { inherit (cfg) frontendUrl forceBackendUrlToFrontendUrl; }; }; + "theme=defaults".dir = toString themesBundle; }; "subsystem=datasources"."data-source=KeycloakDS" = { max-pool-size = "20"; diff --git a/nixos/modules/services/web-apps/keycloak.xml b/nixos/modules/services/web-apps/keycloak.xml index 7ba656c20f16..8c3e35a051bc 100644 --- a/nixos/modules/services/web-apps/keycloak.xml +++ b/nixos/modules/services/web-apps/keycloak.xml @@ -131,6 +131,17 @@ +
+ Themes + + You can package custom themes and make them visible to Keycloak via + + option. See the + Themes section of the Keycloak Server Development Guide + and respective NixOS option description for more information. + +
+
Additional configuration