aboutsummaryrefslogtreecommitdiff
path: root/share/nixos-modules/iwd-ext.nix
blob: be9e8e2090dd5a40edef692a4121267964035d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib)
    mkEnableOption
    mkIf
    mkOption
    types
    recursiveUpdate
    literalExpression
    optionalAttrs
    ;

  inherit (pkgs)
    concatTextFile
    writeText
    ;

  inherit (lib.strings)
    concatStringsSep
    ;

  inherit (lib.attrsets)
    mapAttrs'
    ;

  cfg = config.networking.wireless.iwd-ext;

  ini = pkgs.formats.ini {
    listToValue = l: concatStringsSep "," (map toString l);
  };

  defaults =
    with config.networking.networkmanager;
    optionalAttrs (enable && (wifi.backend == "iwd")) {
      # without DefaultInterface, sometimes wlan0 simply goes AWOL with NetworkManager
      # https://iwd.wiki.kernel.org/interface_lifecycle#interface_management_in_iwd
      DriverQuirks.DefaultInterface = "?*";
    };
  configFile = ini.generate "main.conf" (recursiveUpdate defaults cfg.settings);

  networkTemplates = mapAttrs' (
    k: v:
    let
      name = "${k}.${v.networkType}";
      generatedConfig = ini.generate "${name}" v.config;
      extraLinesFile = writeText "${name}-extra-lines" v.extraConfigLines;
    in
    {
      inherit name;
      value = concatTextFile {
        name = "${k}-network";
        files = [
          generatedConfig
          extraLinesFile
        ];
        destination = "/var/lib/iwd/${name}";
      };
    }
  ) cfg.networks;

  networkTemplateDir = pkgs.symlinkJoin {
    name = "iwd-network-templates";
    paths = builtins.attrValues networkTemplates;
  };

  networkSubmodule = types.submodule {
    options = {
      networkType = mkOption {
        type = types.enum [
          "open"
          "psk"
          "8021x"
        ];
        default = "psk";
        defaultText = literalExpression "psk";
        description = ''
          Network type according to security method. See iwd.network (5)
        '';
      };
      config = mkOption { type = ini.type; };
      extraConfigLines = mkOption {
        type = types.lines;
        default = "";
        description = "Extra lines to append to the network config verbatim.";
      };
    };
  };
in
{
  options.networking.wireless.iwd-ext = {
    enable = mkEnableOption (lib.mdDoc "iwd");

    package = mkOption {
      type = types.package;
      default = pkgs.iwd;
      defaultText = lib.literalExpression "pkgs.iwd";
      description = lib.mdDoc ''
        The iwd package to use
      '';
    };

    settings = mkOption {
      type = ini.type;
      default = { };

      example = {
        Settings.AutoConnect = true;

        Network = {
          EnableIPv6 = true;
          RoutePriorityOffset = 300;
        };
      };

      description = lib.mdDoc ''
        Options passed to iwd.
        See [here](https://iwd.wiki.kernel.org/networkconfigurationsettings) for
        supported options.
      '';
    };

    networks = mkOption {
      type = types.attrsOf networkSubmodule;
      example = {
        HomeNetwork = {
          networkType = "psk";
          config.Security.Passphrase = "totallysecurepsk";
        };
        SomeOtherNetworkSSID = {
          networkType = "psk";
          config.Security.Passphrase = "@NETWORK_2_PSK@";
        };
      };

      description = lib.mdDoc ''
        AttrSet of type SSID - iwd network INI config.
        See [here](https://iwd.wiki.kernel.org/networkconfigurationsettings) for
        supported options.
      '';
    };

    secretsEnvFile = mkOption {
      type = types.path;
      example = "/run/secrets/network-secrets.env";
      description = lib.mdDoc ''
        dotenv-formatted file containing secrets that get injected into
        cfg.networks settings.
      '';
    };
  }; # end of options.networking.wireless.iwd-ext

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = !config.networking.wireless.enable;
        message = ''
          Only one wireless daemon is allowed at the time:
          networking.wireless.enable and networking.wireless.iwd-ext.enable are
          mutually exclusive.
        '';
      }
      {
        assertion = !config.networking.wireless.iwd.enable;
        message = ''
          Only one wireless daemon is allowed at the time:
          networking.wireless.iwd.enable and networking.wireless.iwd-ext.enable are
          mutually exclusive.
        '';
      }
    ];

    environment.etc."iwd/${configFile.name}".source = configFile;

    environment.systemPackages = [ cfg.package ]; # for iwctl
    services.dbus.packages = [ cfg.package ];
    systemd.packages = [ cfg.package ];

    systemd.network.links."80-iwd" = {
      matchConfig.Type = "wlan";
      linkConfig.NamePolicy = "keep kernel";
    };

    systemd.services.iwd = {
      path = [ config.networking.resolvconf.package ];
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [ configFile ];
      serviceConfig.ReadWritePaths = "-/etc/resolv.conf";
    };

    systemd.services."prepopulate-iwd-netowks" = {
      enable = true;
      enableStrictShellChecks = true;
      description = "Pre-populates network configs for iwd";
      before = [ "iwd.service" ];
      wantedBy = [ "iwd.service" ];
      after = [ "sops-install-secrets.service" ];

      serviceConfig = {
        EnvironmentFile = "${cfg.secretsEnvFile}";
        StateDirectory = "iwd";
        StateDirectoryMode = 0700;
        Umask = 077;
      };

      path = [ pkgs.envsubst ];
      script = ''
        set -xeu

        rm -f "$STATE_DIRECTORY/*.{open,psk,8021x}"

        for f in ${networkTemplateDir}/var/lib/iwd/*; do
          NETWORK_PATH="$STATE_DIRECTORY/$(basename "$f")"
          cat "$f" |
            sed -E 's/@@/\x00/g; s/@([A-Za-z_][A-Za-z0-9_]*)@/$\{\1}/g; s/\x00/@/g' |
            envsubst > "$NETWORK_PATH"

          chmod go-rwx "$NETWORK_PATH"
        done
      '';
    };
  };
}