1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.connman;
5 configFile = pkgs.writeText "connman.conf" ''
6 [General]
7 NetworkInterfaceBlacklist=${lib.concatStringsSep "," cfg.networkInterfaceBlacklist}
8
9 ${cfg.extraConfig}
10 '';
11 enableIwd = cfg.wifi.backend == "iwd";
12in {
13 meta.maintainers = with lib.maintainers; [ AndersonTorres ];
14
15 imports = [
16 (lib.mkRenamedOptionModule [ "networking" "connman" ] [ "services" "connman" ])
17 ];
18
19 ###### interface
20
21 options = {
22 services.connman = {
23 enable = lib.mkOption {
24 type = lib.types.bool;
25 default = false;
26 description = ''
27 Whether to use ConnMan for managing your network connections.
28 '';
29 };
30
31 package = lib.mkOption {
32 type = lib.types.package;
33 description = "The connman package / build flavor";
34 default = pkgs.connman;
35 defaultText = lib.literalExpression "pkgs.connman";
36 example = lib.literalExpression "pkgs.connmanFull";
37 };
38
39 enableVPN = lib.mkOption {
40 type = lib.types.bool;
41 default = true;
42 description = ''
43 Whether to enable ConnMan VPN service.
44 '';
45 };
46
47 extraConfig = lib.mkOption {
48 type = lib.types.lines;
49 default = "";
50 description = ''
51 Configuration lines appended to the generated connman configuration file.
52 '';
53 };
54
55 networkInterfaceBlacklist = lib.mkOption {
56 type = with lib.types; listOf str;
57 default = [ "vmnet" "vboxnet" "virbr" "ifb" "ve" ];
58 description = ''
59 Default blacklisted interfaces, this includes NixOS containers interfaces (ve).
60 '';
61 };
62
63 wifi = {
64 backend = lib.mkOption {
65 type = lib.types.enum [ "wpa_supplicant" "iwd" ];
66 default = "wpa_supplicant";
67 description = ''
68 Specify the Wi-Fi backend used.
69 Currently supported are {option}`wpa_supplicant` or {option}`iwd`.
70 '';
71 };
72 };
73
74 extraFlags = lib.mkOption {
75 type = with lib.types; listOf str;
76 default = [ ];
77 example = [ "--nodnsproxy" ];
78 description = ''
79 Extra flags to pass to connmand
80 '';
81 };
82 };
83 };
84
85 ###### implementation
86
87 config = lib.mkIf cfg.enable {
88 assertions = [{
89 assertion = !config.networking.useDHCP;
90 message = "You can not use services.connman with networking.useDHCP";
91 }{
92 # TODO: connman seemingly can be used along network manager and
93 # connmanFull supports this - so this should be worked out somehow
94 assertion = !config.networking.networkmanager.enable;
95 message = "You can not use services.connman with networking.networkmanager";
96 }];
97
98 environment.systemPackages = [ cfg.package ];
99
100 systemd.services.connman = {
101 description = "Connection service";
102 wantedBy = [ "multi-user.target" ];
103 after = [ "syslog.target" ] ++ lib.optional enableIwd "iwd.service";
104 requires = lib.optional enableIwd "iwd.service";
105 serviceConfig = {
106 Type = "dbus";
107 BusName = "net.connman";
108 Restart = "on-failure";
109 ExecStart = toString ([
110 "${cfg.package}/sbin/connmand"
111 "--config=${configFile}"
112 "--nodaemon"
113 ] ++ lib.optional enableIwd "--wifi=iwd_agent"
114 ++ cfg.extraFlags);
115 StandardOutput = "null";
116 };
117 };
118
119 systemd.services.connman-vpn = lib.mkIf cfg.enableVPN {
120 description = "ConnMan VPN service";
121 wantedBy = [ "multi-user.target" ];
122 after = [ "syslog.target" ];
123 before = [ "connman.service" ];
124 serviceConfig = {
125 Type = "dbus";
126 BusName = "net.connman.vpn";
127 ExecStart = "${cfg.package}/sbin/connman-vpnd -n";
128 StandardOutput = "null";
129 };
130 };
131
132 systemd.services.net-connman-vpn = lib.mkIf cfg.enableVPN {
133 description = "D-BUS Service";
134 serviceConfig = {
135 Name = "net.connman.vpn";
136 before = [ "connman.service" ];
137 ExecStart = "${cfg.package}/sbin/connman-vpnd -n";
138 User = "root";
139 SystemdService = "connman-vpn.service";
140 };
141 };
142
143 networking = {
144 useDHCP = false;
145 wireless = {
146 enable = lib.mkIf (!enableIwd) true;
147 dbusControlled = true;
148 iwd = lib.mkIf enableIwd {
149 enable = true;
150 };
151 };
152 networkmanager.enable = false;
153 };
154 };
155}