1 { config, pkgs, lib, ... }:
8 enable = mkEnableOption (lib.mdDoc "xl2tpd, the Layer 2 Tunnelling Protocol Daemon");
12 description = lib.mdDoc "The server-side IP address.";
13 default = "10.125.125.1";
16 clientIpRange = mkOption {
18 description = lib.mdDoc "The range from which client IPs are drawn.";
19 default = "10.125.125.2-11";
22 extraXl2tpOptions = mkOption {
24 description = lib.mdDoc "Adds extra lines to the xl2tpd configuration file.";
28 extraPppdOptions = mkOption {
30 description = lib.mdDoc "Adds extra lines to the pppd options file.";
40 config = mkIf config.services.xl2tpd.enable {
41 systemd.services.xl2tpd = let
42 cfg = config.services.xl2tpd;
44 # Config files from https://help.ubuntu.com/community/L2TPServer
45 xl2tpd-conf = pkgs.writeText "xl2tpd.conf" ''
50 local ip = ${cfg.serverIp}
51 ip range = ${cfg.clientIpRange}
52 pppoptfile = ${pppd-options}
56 ${cfg.extraXl2tpOptions}
59 pppd-options = pkgs.writeText "ppp-options-xl2tpd.conf" ''
81 ${cfg.extraPppdOptions}
84 xl2tpd-ppp-wrapped = pkgs.stdenv.mkDerivation {
85 name = "xl2tpd-ppp-wrapped";
86 phases = [ "installPhase" ];
87 nativeBuildInputs = with pkgs; [ makeWrapper ];
91 makeWrapper ${pkgs.ppp}/sbin/pppd $out/bin/pppd \
92 --set LD_PRELOAD "${pkgs.libredirect}/lib/libredirect.so" \
93 --set NIX_REDIRECTS "/etc/ppp=/etc/xl2tpd/ppp"
95 makeWrapper ${pkgs.xl2tpd}/bin/xl2tpd $out/bin/xl2tpd \
96 --set LD_PRELOAD "${pkgs.libredirect}/lib/libredirect.so" \
97 --set NIX_REDIRECTS "${pkgs.ppp}/sbin/pppd=$out/bin/pppd"
101 description = "xl2tpd server";
103 requires = [ "network-online.target" ];
104 wantedBy = [ "multi-user.target" ];
107 mkdir -p -m 700 /etc/xl2tpd
109 pushd /etc/xl2tpd > /dev/null
113 [ -f ppp/chap-secrets ] || cat > ppp/chap-secrets << EOF
114 # Secrets for authentication using CHAP
115 # client server secret IP addresses
116 #username xl2tpd password *
119 chown root:root ppp/chap-secrets
120 chmod 600 ppp/chap-secrets
122 # The documentation says this file should be present but doesn't explain why and things work even if not there:
123 [ -f l2tp-secrets ] || (echo -n "* * "; ${pkgs.apg}/bin/apg -n 1 -m 32 -x 32 -a 1 -M LCN) > l2tp-secrets
124 chown root:root l2tp-secrets
125 chmod 600 l2tp-secrets
130 chown root:root /run/xl2tpd
131 chmod 700 /run/xl2tpd
135 ExecStart = "${xl2tpd-ppp-wrapped}/bin/xl2tpd -D -c ${xl2tpd-conf} -s /etc/xl2tpd/l2tp-secrets -p /run/xl2tpd/pid -C /run/xl2tpd/control";
136 KillMode = "process";
137 Restart = "on-success";
139 PIDFile = "/run/xl2tpd/pid";