aboutsummaryrefslogtreecommitdiff
path: root/share/nixos-modules/iwd-ext.nix
blob: 09327a9b4dde5dfe0450488f3c4c7addc4465225 (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
{ config, lib, pkgs, ... }:
let
  inherit (lib) mkEnableOption mkIf mkOption types recursiveUpdate;

  cfg = config.networking.wireless.iwd-ext;
  ini = pkgs.formats.ini { };
  defaults = {
    # without UseDefaultInterface, sometimes wlan0 simply goes AWOL with 
    # NetworkManager
    # https://iwd.wiki.kernel.org/interface_lifecycle#interface_management_in_iwd
    General.UseDefaultInterface = with config.networking.networkmanager;
      (enable && (wifi.backend == "iwd"));
  };
  configFile = ini.generate "main.conf" (recursiveUpdate defaults cfg.settings);

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 ini.type;
      default = { };
      example = {
        HomeNetwork = { Security.PreSharedKey = "totallysecurepsk"; };
        "@NETWORK_2_SSID@" = { Security.PreSharedKey = "@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.
      '';
    };
  }; # 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.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 = {
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [ configFile ];
    };
  };
}