2021-10-01 09:20:50 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.dex;
|
|
|
|
fixClient = client: if client ? secretFile then ((builtins.removeAttrs client [ "secretFile" ]) // { secret = client.secretFile; }) else client;
|
|
|
|
filteredSettings = mapAttrs (n: v: if n == "staticClients" then (builtins.map fixClient v) else v) cfg.settings;
|
2023-07-15 17:15:38 +00:00
|
|
|
secretFiles = flatten (builtins.map (c: optional (c ? secretFile) c.secretFile) (cfg.settings.staticClients or []));
|
2021-10-01 09:20:50 +00:00
|
|
|
|
|
|
|
settingsFormat = pkgs.formats.yaml {};
|
|
|
|
configFile = settingsFormat.generate "config.yaml" filteredSettings;
|
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
startPreScript = pkgs.writeShellScript "dex-start-pre"
|
|
|
|
(concatStringsSep "\n" (map (file: ''
|
|
|
|
replace-secret '${file}' '${file}' /run/dex/config.yaml
|
|
|
|
'')
|
|
|
|
secretFiles));
|
2021-10-01 09:20:50 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.dex = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "the OpenID Connect and OAuth2 identity provider");
|
2021-10-01 09:20:50 +00:00
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
environmentFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Environment file (see `systemd.exec(5)`
|
2022-08-21 13:32:41 +00:00
|
|
|
"EnvironmentFile=" section for the syntax) to define variables for dex.
|
|
|
|
This option can be used to safely include secret keys into the dex configuration.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-10-01 09:20:50 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = settingsFormat.type;
|
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2021-10-01 09:20:50 +00:00
|
|
|
{
|
|
|
|
# External url
|
|
|
|
issuer = "http://127.0.0.1:5556/dex";
|
|
|
|
storage = {
|
|
|
|
type = "postgres";
|
|
|
|
config.host = "/var/run/postgres";
|
|
|
|
};
|
|
|
|
web = {
|
|
|
|
http = "127.0.0.1:5556";
|
|
|
|
};
|
|
|
|
enablePasswordDB = true;
|
|
|
|
staticClients = [
|
|
|
|
{
|
|
|
|
id = "oidcclient";
|
|
|
|
name = "Client";
|
|
|
|
redirectURIs = [ "https://example.com/callback" ];
|
|
|
|
secretFile = "/etc/dex/oidcclient"; # The content of `secretFile` will be written into to the config as `secret`.
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-01 09:20:50 +00:00
|
|
|
The available options can be found in
|
2022-10-30 15:09:59 +00:00
|
|
|
[the example configuration](https://github.com/dexidp/dex/blob/v${pkgs.dex-oidc.version}/config.yaml.dist).
|
2022-08-21 13:32:41 +00:00
|
|
|
|
|
|
|
It's also possible to refer to environment variables (defined in [services.dex.environmentFile](#opt-services.dex.environmentFile))
|
|
|
|
using the syntax `$VARIABLE_NAME`.
|
2021-10-01 09:20:50 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.dex = {
|
|
|
|
description = "dex identity provider";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "networking.target" ] ++ (optional (cfg.settings.storage.type == "postgres") "postgresql.service");
|
2022-08-21 13:32:41 +00:00
|
|
|
path = with pkgs; [ replace-secret ];
|
2021-10-01 09:20:50 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.dex-oidc}/bin/dex serve /run/dex/config.yaml";
|
|
|
|
ExecStartPre = [
|
|
|
|
"${pkgs.coreutils}/bin/install -m 600 ${configFile} /run/dex/config.yaml"
|
|
|
|
"+${startPreScript}"
|
|
|
|
];
|
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
RuntimeDirectory = "dex";
|
2021-10-01 09:20:50 +00:00
|
|
|
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
|
|
|
|
BindReadOnlyPaths = [
|
|
|
|
"/nix/store"
|
2022-12-28 21:21:41 +00:00
|
|
|
"-/etc/dex"
|
2021-10-01 09:20:50 +00:00
|
|
|
"-/etc/hosts"
|
|
|
|
"-/etc/localtime"
|
2022-12-28 21:21:41 +00:00
|
|
|
"-/etc/nsswitch.conf"
|
|
|
|
"-/etc/resolv.conf"
|
|
|
|
"-/etc/ssl/certs/ca-certificates.crt"
|
2021-10-01 09:20:50 +00:00
|
|
|
];
|
|
|
|
BindPaths = optional (cfg.settings.storage.type == "postgres") "/var/run/postgresql";
|
|
|
|
CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
|
|
|
|
# ProtectClock= adds DeviceAllow=char-rtc r
|
|
|
|
DeviceAllow = "";
|
|
|
|
DynamicUser = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
# Port needs to be exposed to the host network
|
|
|
|
#PrivateNetwork = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
2023-11-16 04:20:00 +00:00
|
|
|
ProtectSystem = "strict";
|
2021-10-01 09:20:50 +00:00
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
2022-10-30 15:09:59 +00:00
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged @setuid @keyring" ];
|
2023-11-16 04:20:00 +00:00
|
|
|
UMask = "0066";
|
2022-08-21 13:32:41 +00:00
|
|
|
} // optionalAttrs (cfg.environmentFile != null) {
|
|
|
|
EnvironmentFile = cfg.environmentFile;
|
2021-10-01 09:20:50 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2022-01-07 04:07:37 +00:00
|
|
|
|
|
|
|
# uses attributes of the linked package
|
|
|
|
meta.buildDocsInSandbox = false;
|
2021-10-01 09:20:50 +00:00
|
|
|
}
|