1 { config, lib, pkgs, ... }:
7 cfg = config.services.stunnel;
8 yesNo = val: if val then "yes" else "no";
10 verifyRequiredField = type: field: n: c: {
11 assertion = hasAttr field c;
12 message = "stunnel: \"${n}\" ${type} configuration - Field ${field} is required.";
15 verifyChainPathAssert = n: c: {
16 assertion = (c.verifyHostname or null) == null || (c.verifyChain || c.verifyPeer);
17 message = "stunnel: \"${n}\" client configuration - hostname verification " +
18 "is not possible without either verifyChain or verifyPeer enabled";
21 removeNulls = mapAttrs (_: filterAttrs (_: v: v != null));
23 if v == true then "yes"
24 else if v == false then "no"
25 else generators.mkValueStringDefault {} v;
29 mkKeyValue = k: v: "${k} = ${mkValueString v}";
45 description = "Whether to enable the stunnel TLS tunneling service.";
49 type = with types; nullOr str;
51 description = "The user under which stunnel runs.";
55 type = with types; nullOr str;
57 description = "The group under which stunnel runs.";
61 type = types.enum [ "emerg" "alert" "crit" "err" "warning" "notice" "info" "debug" ];
63 description = "Verbosity of stunnel output.";
69 description = "Enable FIPS 140-2 mode required for compliance.";
72 enableInsecureSSLv3 = mkOption {
75 description = "Enable support for the insecure SSLv3 protocol.";
81 Define the server configurations.
83 See "SERVICE-LEVEL OPTIONS" in {manpage}`stunnel(8)`.
85 type = with types; attrsOf (attrsOf (nullOr (oneOf [bool int str])));
90 cert = "/path/to/pem/file";
98 Define the client configurations.
100 By default, verifyChain and OCSPaia are enabled and a CAFile is provided from pkgs.cacert.
102 See "SERVICE-LEVEL OPTIONS" in {manpage}`stunnel(8)`.
104 type = with types; attrsOf (attrsOf (nullOr (oneOf [bool int str])));
109 CAFile = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
113 setCheckHostFromVerifyHostname = c:
114 # To preserve backward-compatibility with the old NixOS stunnel module
115 # definition, allow "verifyHostname" as an alias for "checkHost".
117 checkHost = c.checkHost or c.verifyHostname or null;
118 verifyHostname = null; # Not a real stunnel configuration setting
120 forceClient = c: c // { client = true; };
121 in mapAttrs (_: c: forceClient (setCheckHostFromVerifyHostname (applyDefaults c)));
125 accept = "0.0.0.0:8080";
126 connect = "nixos.org:443";
136 ###### implementation
138 config = mkIf cfg.enable {
140 assertions = concatLists [
142 assertion = (length (attrValues cfg.servers) != 0) || ((length (attrValues cfg.clients)) != 0);
143 message = "stunnel: At least one server- or client-configuration has to be present.";
146 (mapAttrsToList verifyChainPathAssert cfg.clients)
147 (mapAttrsToList (verifyRequiredField "client" "accept") cfg.clients)
148 (mapAttrsToList (verifyRequiredField "client" "connect") cfg.clients)
149 (mapAttrsToList (verifyRequiredField "server" "accept") cfg.servers)
150 (mapAttrsToList (verifyRequiredField "server" "cert") cfg.servers)
151 (mapAttrsToList (verifyRequiredField "server" "connect") cfg.servers)
154 environment.systemPackages = [ pkgs.stunnel ];
156 environment.etc."stunnel.cfg".text = ''
157 ${ optionalString (cfg.user != null) "setuid = ${cfg.user}" }
158 ${ optionalString (cfg.group != null) "setgid = ${cfg.group}" }
160 debug = ${cfg.logLevel}
162 ${ optionalString cfg.fipsMode "fips = yes" }
163 ${ optionalString cfg.enableInsecureSSLv3 "options = -NO_SSLv3" }
165 ; ----- SERVER CONFIGURATIONS -----
166 ${ generateConfig cfg.servers }
168 ; ----- CLIENT CONFIGURATIONS -----
169 ${ generateConfig cfg.clients }
172 systemd.services.stunnel = {
173 description = "stunnel TLS tunneling service";
174 after = [ "network.target" ];
175 wants = [ "network.target" ];
176 wantedBy = [ "multi-user.target" ];
177 restartTriggers = [ config.environment.etc."stunnel.cfg".source ];
179 ExecStart = "${pkgs.stunnel}/bin/stunnel ${config.environment.etc."stunnel.cfg".source}";
184 meta.maintainers = with maintainers; [