summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/netplan.nix
blob: a4ab0f901f5527547b9c99c1ab57d5997f974bce (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
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.networking.netplan;
  networkdEnabled = (config.networking.useNetworkd || config.systemd.network.enable);
  networkmanagerEnabled = config.networking.networkmanager.enable;
in
{
  options = {
    networking.netplan = {
      enable = lib.mkEnableOption "Whether to enable the netplan-configure service at boot";
      package = lib.mkPackageOption pkgs "netplan" { };
      configFiles = lib.mkOption {
        type = lib.types.attrsOf lib.types.str;
        default = { };
        example = ''
          {
            "10-example.yaml" = \'\'
               ---
               network:
                 version: 2
                 dummy-devices:
                   example:
                     addresses: ["fd00::feed:cafe/32"]
               \'\'
          }
        '';
        description = "Netplan YAML configuration files to write under /etc/netplan/";
      };
    };
  };

  config = lib.mkIf cfg.enable {
    warnings = lib.optionals (!(networkdEnabled || networkmanagerEnabled)) [
      "You enabled the netplan-configure service, but you haven't enabled a backend for it. It's likely you want to enable either networkd or NetworkManager."
    ];
    environment = {
      systemPackages = [ cfg.package ];
      etc = builtins.listToAttrs (
        builtins.map (key: {
          name = "netplan/" + key;
          value = {
            text = cfg.configFiles."${key}";
            user = "root";
            group = "root";
            mode = "0600";
          };
        }) (pkgs.lib.attrNames cfg.configFiles)
      );
    };
    systemd = {
      packages = [ cfg.package ];
      services.netplan-configure.wantedBy = [ "sysinit.target" ];
    };
  };
}