1 { config, lib, pkgs, ... }:
6 cfg = config.services.dex;
7 fixClient = client: if client ? secretFile then ((builtins.removeAttrs client [ "secretFile" ]) // { secret = client.secretFile; }) else client;
8 filteredSettings = mapAttrs (n: v: if n == "staticClients" then (builtins.map fixClient v) else v) cfg.settings;
9 secretFiles = flatten (builtins.map (c: optional (c ? secretFile) c.secretFile) (cfg.settings.staticClients or []));
11 settingsFormat = pkgs.formats.yaml {};
12 configFile = settingsFormat.generate "config.yaml" filteredSettings;
14 startPreScript = pkgs.writeShellScript "dex-start-pre"
15 (concatStringsSep "\n" (map (file: ''
16 replace-secret '${file}' '${file}' /run/dex/config.yaml
21 options.services.dex = {
22 enable = mkEnableOption "the OpenID Connect and OAuth2 identity provider";
24 environmentFile = mkOption {
25 type = types.nullOr types.path;
28 Environment file (see `systemd.exec(5)`
29 "EnvironmentFile=" section for the syntax) to define variables for dex.
30 This option can be used to safely include secret keys into the dex configuration.
35 type = settingsFormat.type;
37 example = literalExpression ''
40 issuer = "http://127.0.0.1:5556/dex";
43 config.host = "/var/run/postgres";
46 http = "127.0.0.1:5556";
48 enablePasswordDB = true;
53 redirectURIs = [ "https://example.com/callback" ];
54 secretFile = "/etc/dex/oidcclient"; # The content of `secretFile` will be written into to the config as `secret`.
60 The available options can be found in
61 [the example configuration](https://github.com/dexidp/dex/blob/v${pkgs.dex-oidc.version}/config.yaml.dist).
63 It's also possible to refer to environment variables (defined in [services.dex.environmentFile](#opt-services.dex.environmentFile))
64 using the syntax `$VARIABLE_NAME`.
69 config = mkIf cfg.enable {
70 systemd.services.dex = {
71 description = "dex identity provider";
72 wantedBy = [ "multi-user.target" ];
73 after = [ "networking.target" ] ++ (optional (cfg.settings.storage.type == "postgres") "postgresql.service");
74 path = with pkgs; [ replace-secret ];
76 ExecStart = "${pkgs.dex-oidc}/bin/dex serve /run/dex/config.yaml";
78 "${pkgs.coreutils}/bin/install -m 600 ${configFile} /run/dex/config.yaml"
82 RuntimeDirectory = "dex";
83 AmbientCapabilities = "CAP_NET_BIND_SERVICE";
91 "-/etc/ssl/certs/ca-certificates.crt"
93 BindPaths = optional (cfg.settings.storage.type == "postgres") "/var/run/postgresql";
94 CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
95 # ProtectClock= adds DeviceAllow=char-rtc r
98 LockPersonality = true;
99 MemoryDenyWriteExecute = true;
100 NoNewPrivileges = true;
101 PrivateDevices = true;
102 PrivateMounts = true;
103 # Port needs to be exposed to the host network
104 #PrivateNetwork = true;
110 ProtectHostname = true;
111 ProtectSystem = "strict";
112 ProtectControlGroups = true;
113 ProtectKernelLogs = true;
114 ProtectKernelModules = true;
115 ProtectKernelTunables = true;
116 ProtectProc = "invisible";
117 RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
118 RestrictNamespaces = true;
119 RestrictRealtime = true;
120 RestrictSUIDSGID = true;
121 SystemCallArchitectures = "native";
122 SystemCallFilter = [ "@system-service" "~@privileged @setuid @keyring" ];
124 } // optionalAttrs (cfg.environmentFile != null) {
125 EnvironmentFile = cfg.environmentFile;
130 # uses attributes of the linked package
131 meta.buildDocsInSandbox = false;