@@ -0,0 +1,133 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.pppd;
7+in
8+{
9+ meta = {
10+ maintainers = with maintainers; [ danderson ];
11+ };
12+
13+ options = {
14+ services.pppd = {
15+ enable = mkEnableOption "pppd";
16+
17+ package = mkOption {
18+ default = pkgs.ppp;
19+ defaultText = "pkgs.ppp";
20+ type = types.package;
21+ description = "pppd package to use.";
22+ };
23+
24+ peers = mkOption {
25+ default = {};
26+ type = types.attrsOf (types.submodule (
27+ { name, ... }:
28+ {
29+ options = {
30+ name = mkOption {
31+ type = types.str;
32+ default = name;
33+ example = "dialup";
34+ description = "Name of the PPP peer.";
35+ };
36+
37+ enable = mkOption {
38+ type = types.bool;
39+ default = true;
40+ example = false;
41+ description = "Whether to enable this PPP peer.";
42+ };
43+
44+ autostart = mkOption {
45+ type = types.bool;
46+ default = true;
47+ example = false;
48+ description = "Whether the PPP session is automatically started at boot time.";
49+ };
50+
51+ config = mkOption {
52+ type = types.lines;
53+ default = "";
54+ description = "pppd configuration for this peer, see the pppd(8) man page.";
55+ };
56+ };
57+ }));
58+ };
59+ };
60+ };
61+
62+ config = let
63+ enabledConfigs = filter (f: f.enable) (attrValues cfg.peers);
64+
65+ mkEtc = peerCfg: {
66+ "ppp/peers/${peerCfg.name}".text = peerCfg.config;
67+ };
68+
69+ mkSystemd = peerCfg: {
70+ "pppd-${peerCfg.name}" = {
71+ restartTriggers = [ config.environment.etc."ppp/peers/${peerCfg.name}".source ];
72+ before = [ "network.target" ];
73+ wants = [ "network.target" ];
74+ after = [ "network-pre.target" ];
75+ environment = {
76+ # pppd likes to write directly into /var/run. This is rude
77+ # on a modern system, so we use libredirect to transparently
78+ # move those files into /run/pppd.
79+ LD_PRELOAD = "${pkgs.libredirect}/lib/libredirect.so";
80+ NIX_REDIRECTS = "/var/run=/run/pppd";
81+ };
82+ serviceConfig = {
83+ ExecStart = "${getBin cfg.package}/sbin/pppd call ${peerCfg.name} nodetach nolog";
84+ Restart = "always";
85+ RestartSec = 5;
86+
87+ AmbientCapabilities = "CAP_SYS_TTY_CONFIG CAP_NET_ADMIN CAP_NET_RAW CAP_SYS_ADMIN";
88+ CapabilityBoundingSet = "CAP_SYS_TTY_CONFIG CAP_NET_ADMIN CAP_NET_RAW CAP_SYS_ADMIN";
89+ KeyringMode = "private";
90+ LockPersonality = true;
91+ MemoryDenyWriteExecute = true;
92+ NoNewPrivileges = true;
93+ PrivateMounts = true;
94+ PrivateTmp = true;
95+ ProtectControlGroups = true;
96+ ProtectHome = true;
97+ ProtectHostname = true;
98+ ProtectKernelModules = true;
99+ # pppd can be configured to tweak kernel settings.
100+ ProtectKernelTunables = false;
101+ ProtectSystem = "strict";
102+ RemoveIPC = true;
103+ RestrictAddressFamilies = "AF_PACKET AF_UNIX AF_PPPOX AF_ATMPVC AF_ATMSVC AF_INET AF_INET6 AF_IPX";
104+ RestrictNamespaces = true;
105+ RestrictRealtime = true;
106+ RestrictSUIDSGID = true;
107+ SecureBits = "no-setuid-fixup-locked noroot-locked";
108+ SystemCallFilter = "@system-service";
109+ SystemCallArchitectures = "native";
110+
111+ # All pppd instances on a system must share a runtime
112+ # directory in order for PPP multilink to work correctly. So
113+ # we give all instances the same /run/pppd directory to store
114+ # things in.
115+ #
116+ # For the same reason, we can't set PrivateUsers=true, because
117+ # all instances need to run as the same user to access the
118+ # multilink database.
119+ RuntimeDirectory = "pppd";
120+ RuntimeDirectoryPreserve = true;
121+ };
122+ wantedBy = mkIf peerCfg.autostart [ "multi-user.target" ];
123+ };
124+ };
125+
126+ etcFiles = map mkEtc enabledConfigs;
127+ systemdConfigs = map mkSystemd enabledConfigs;
128+
129+ in mkIf cfg.enable {
130+ environment.etc = mkMerge etcFiles;
131+ systemd.services = mkMerge systemdConfigs;
132+ };
133+}