···
cfg = config.virtualisation.vswitch;
+
options.virtualisation.vswitch = {
+
Whether to enable Open vSwitch. A configuration daemon (ovs-server)
default = pkgs.openvswitch;
Open vSwitch package to use.
+
Whether to start racoon service for openvswitch.
+
config = mkIf cfg.enable (let
# Where the communication sockets live
runDir = "/var/run/openvswitch";
···
# Where the config database live (can't be in nix-store)
stateDir = "/var/db/openvswitch";
+
# The path to the an initialized version of the database
db = pkgs.stdenv.mkDerivation {
···
buildInputs = with pkgs; [
+
installPhase = "mkdir -p $out";
+
environment.systemPackages = [ cfg.package pkgs.ipsecTools ];
boot.kernelModules = [ "tun" "openvswitch" ];
···
restartTriggers = [ db cfg.package ];
# Create the config database
mkdir -p /var/db/openvswitch
···
chmod -R +w /var/db/openvswitch
+
${cfg.package}/bin/ovsdb-server \
+
--remote=punix:${runDir}/db.sock \
+
--private-key=db:Open_vSwitch,SSL,private_key \
+
--certificate=db:Open_vSwitch,SSL,certificate \
+
--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \
+
--unixctl=ovsdb.ctl.sock \
+
--pidfile=/var/run/openvswitch/ovsdb.pid \
+
/var/db/openvswitch/conf.db
+
PIDFile = "/var/run/openvswitch/ovsdb.pid";
${cfg.package}/bin/ovs-vsctl --timeout 3 --retry --no-wait init
systemd.services.vswitchd = {
···
bindsTo = [ "ovsdb.service" ];
after = [ "ovsdb.service" ];
+
${cfg.package}/bin/ovs-vswitchd \
+
--pidfile=/var/run/openvswitch/ovs-vswitchd.pid \
+
PIDFile = "/var/run/openvswitch/ovs-vswitchd.pid";
+
services.racoon.enable = true;
+
services.racoon.configPath = "${runDir}/ipsec/etc/racoon/racoon.conf";
+
networking.firewall.extraCommands = ''
+
iptables -I INPUT -t mangle -p esp -j MARK --set-mark 1/1
+
iptables -I INPUT -t mangle -p udp --dport 4500 -j MARK --set-mark 1/1
+
systemd.services.ovs-monitor-ipsec = {
+
description = "Open_vSwitch Ipsec Daemon";
+
wantedBy = [ "multi-user.target" ];
+
requires = [ "racoon.service" ];
+
after = [ "vswitchd.service" ];
+
environment.UNIXCTLPATH = "/tmp/ovsdb.ctl.sock";
+
${cfg.package}/bin/ovs-monitor-ipsec \
+
--root-prefix ${runDir}/ipsec \
+
--pidfile /var/run/openvswitch/ovs-monitor-ipsec.pid \
+
unix:/var/run/openvswitch/db.sock
+
PIDFile = "/var/run/openvswitch/ovs-monitor-ipsec.pid";
+
rm -r ${runDir}/ipsec/etc/racoon/certs || true
+
mkdir -p ${runDir}/ipsec/{etc/racoon,etc/init.d/,usr/sbin/}
+
ln -fs ${pkgs.ipsecTools}/bin/setkey ${runDir}/ipsec/usr/sbin/setkey
+
ln -fs ${pkgs.writeScript "racoon-restart" ''
+
/var/run/current-system/sw/bin/systemctl $1 racoon
+
''} ${runDir}/ipsec/etc/init.d/racoon