nixos/openvpn: add support for resolvconf

The update-resolve-conf script from the update-resolv-conf
package is very useful and should work in most of the common
cases, so this adds an option to enable it. The option is
disabled by default for backwards compatibility.

+19 -3
+19 -3
nixos/modules/services/networking/openvpn.nix
··· 29 29 done 30 30 31 31 ${cfg.up} 32 + ${optionalString cfg.updateResolvConf 33 + "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"} 32 34 ''; 33 35 34 36 downScript = '' 35 37 #! /bin/sh 36 38 export PATH=${path} 39 + ${optionalString cfg.updateResolvConf 40 + "${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"} 37 41 ${cfg.down} 38 42 ''; 39 43 40 44 configFile = pkgs.writeText "openvpn-config-${name}" 41 45 '' 42 46 errors-to-stderr 43 - ${optionalString (cfg.up != "" || cfg.down != "") "script-security 2"} 47 + ${optionalString (cfg.up != "" || cfg.down != "" || cfg.updateResolvConf) "script-security 2"} 44 48 ${cfg.config} 45 - ${optionalString (cfg.up != "") "up ${pkgs.writeScript "openvpn-${name}-up" upScript}"} 46 - ${optionalString (cfg.down != "") "down ${pkgs.writeScript "openvpn-${name}-down" downScript}"} 49 + ${optionalString (cfg.up != "" || cfg.updateResolvConf) 50 + "up ${pkgs.writeScript "openvpn-${name}-up" upScript}"} 51 + ${optionalString (cfg.down != "" || cfg.updateResolvConf) 52 + "down ${pkgs.writeScript "openvpn-${name}-down" downScript}"} 47 53 ''; 48 54 49 55 in { ··· 143 149 default = true; 144 150 type = types.bool; 145 151 description = "Whether this OpenVPN instance should be started automatically."; 152 + }; 153 + 154 + updateResolvConf = mkOption { 155 + default = false; 156 + type = types.bool; 157 + description = '' 158 + Use the script from the update-resolv-conf package to automatically 159 + update resolv.conf with the DNS information provided by openvpn. The 160 + script will be run after the "up" commands and before the "down" commands. 161 + ''; 146 162 }; 147 163 148 164 };