OpenVPN: Difference between revisions
imported>HLandau Created page with " === VPN Client === Auto-starting openvpn on Nixos can easily be done by enabling it in the configuration nix. Just place the configs where you want them to have and set it up..." |
imported>HLandau No edit summary |
||
Line 1: | Line 1: | ||
=VPN Client= | |||
OpenVPN can be configured for automatic startup by enabling it in <tt>/etc/nixos/configuration.nix</tt>: | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
services.openvpn.servers = { | { | ||
... | |||
services.openvpn.servers = { | |||
officeVPN = { config = '' config /root/nixos/openvpn/officeVPN.conf ''; }; | officeVPN = { config = '' config /root/nixos/openvpn/officeVPN.conf ''; }; | ||
homeVPN = { config = '' config /root/nixos/openvpn/homeVPN.conf ''; }; | homeVPN = { config = '' config /root/nixos/openvpn/homeVPN.conf ''; }; | ||
serverVPN = { config = '' config /root/nixos/openvpn/serverVPN.conf ''; }; | serverVPN = { config = '' config /root/nixos/openvpn/serverVPN.conf ''; }; | ||
}; | }; | ||
... | |||
} | |||
</syntaxHighlight> | </syntaxHighlight> | ||
You will need to create the referenced configuration files. The above example will start three VPN instances; more can be added. | |||
Ensure you use absolute paths for any files such as certificates and keys referenced from the configuration files. | |||
==Mounting filesystems via a VPN== | |||
If you mount filesystems through the VPN, the filesystem will not be unmounted properly because the VPN connection will be shut down prior to unmounting the filesystem. However, newer systemd versions allow you to set mount options to unmount the mount before closing the VPN connection via the mount option <tt>x-systemd.requires=openvpn-<em>vpnname</em>.service</tt>. | |||
Example mount configurations: | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
fileSystems."/mnt/office" = { | { | ||
... | |||
fileSystems."/mnt/office" = { | |||
device = "//10.8.0.x/Share"; | device = "//10.8.0.x/Share"; | ||
fsType = "cifs"; | fsType = "cifs"; | ||
options = [ "noauto" "user" "uid=1000" "gid=100" "username=xxx" "password=xxx" "iocharset=utf8" "x-systemd.requires=openvpn-officeVPN.service" ]; | options = [ "noauto" "user" "uid=1000" "gid=100" "username=xxx" "password=xxx" "iocharset=utf8" | ||
}; | "x-systemd.requires=openvpn-officeVPN.service" ]; | ||
fileSystems."/mnt/home" = { | }; | ||
fileSystems."/mnt/home" = { | |||
device = "//10.9.0.x/Share"; | device = "//10.9.0.x/Share"; | ||
fsType = "cifs"; | fsType = "cifs"; | ||
options = [ "noauto" "user" "uid=1000" "gid=100" "username=xxx" "password=xxx" "iocharset=utf8" "x-systemd.requires=openvpn-homeVPN.service" ]; | options = [ "noauto" "user" "uid=1000" "gid=100" "username=xxx" "password=xxx" "iocharset=utf8" | ||
}; | "x-systemd.requires=openvpn-homeVPN.service" ]; | ||
}; | |||
... | |||
} | |||
</syntaxHighlight> | </syntaxHighlight> | ||
If you want to run OpenVPN clients in NixOS declarative containers, you will need to set the {{nixos:option|enableTun}} container option. | |||
If you want to run OpenVPN clients in | |||
==== Simple one-client VPN | =VPN Server= | ||
==Simple one-client VPN gateway server== | |||
The following is an example of a VPN server configuration which supports a single known client. | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> |