summaryrefslogtreecommitdiff
path: root/config/keycloak.nix
blob: 7327bfb31852ce71f53fe6fbba8453f5231bc959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.services.keycloak;

  defaultConfig = "${pkgs.keycloak}/standalone/configuration";

  keycloakConfig = pkgs.runCommand "keycloak-config" {} ''
    mkdir $out
    cp ${defaultConfig}/application-roles.properties $out/
    cp ${defaultConfig}/application-users.properties $out/
    cp ${defaultConfig}/mgmt-groups.properties $out/
    cp ${defaultConfig}/mgmt-users.properties $out/
    cp ${defaultConfig}/standalone.xml $out/
    {
      grep -v FILE ${defaultConfig}/logging.properties
      echo "logger.handlers=CONSOLE"
      echo "handler.CONSOLE.level=ALL"
    } > $out/logging.properties
  '';

in {
  options = {
    services.keycloak = {
      enable = mkEnableOption "Keycloak Identity and Access Management Server";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.keycloak = {
      description = "Keycloak Identity and Access Management Server";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      preStart = ''
        mkdir -p /var/lib/keycloak/logs
        mkdir -p /var/lib/keycloak/config
        cp ${keycloakConfig}/*.properties /var/lib/keycloak/config
      '';
      serviceConfig = {
        ExecStart = "${pkgs.keycloak}/bin/standalone.sh -Djboss.server.base.dir=/var/lib/keycloak -Djboss.server.config.dir=/var/lib/keycloak/config --read-only-server-config=${keycloakConfig}/standalone.xml";
      };
    };
  };
}