aboutsummaryrefslogtreecommitdiff
path: root/modules/services/options.nix
blob: e87ecb3501e662b0794a637c092076d7062b32b1 (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
{
  lib,
  config,
  ...
}: let
  inherit (lib) mkOption mkEnableOption;

  ip_addr = lib.types.strMatching "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$";
  ip_addr_cidr = lib.types.strMatching "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])/(3[0-2]|[12]?[0-9])$";
in {
  options = {
    collinux.services = {
      networking = {
        enable = mkEnableOption "wifi";

        iwd.enable = mkEnableOption "lightweight wifi daemon";
        networkmanager.enable = mkEnableOption "heavier wifi daemon";

        networkd = {
          enable = mkEnableOption "set static IP (systemd-networkd)";
          ssid = mkOption {type = lib.types.str;};
          pskFile = mkOption {type = lib.types.str;};

          static = lib.mkOption {
            type = lib.types.nullOr (lib.types.submodule {
              options = {
                ip = mkOption {type = ip_addr_cidr;};
                gateway = mkOption {type = ip_addr;};
              };
            });
            default = null;
          };
        };
        tailscale.enable = mkEnableOption "tailscale";
        sshd.enable = mkEnableOption "OpenSSH server";
      };
      audio = {
        enable = mkEnableOption "pipewire + wireplumber";
        pulse.enable = mkEnableOption "pipewire-pulse";
      };
      bluetooth = {
        enable = mkEnableOption "bluetooth";
        blueman.enable = mkEnableOption "graphical bluetooth manager";
        bluetuith.enable = mkEnableOption "terminal bluetooth manager";
      };

      selfhost = let
        selfhostOptions = {
          service_name,
          default_port ? null,
          ...
        }: {
          enable = mkEnableOption "";
          bind_host = mkOption {
            type = ip_addr;
            default =
              if config.collinux.services.networking.tailscale.enable
              then "100.69.160.89"
              else "0.0.0.0";
          };
          port = mkOption {
            type = lib.types.port;
            default = default_port;
          };

          root_url = mkOption {
            type = lib.types.str;
            default =
              if config.collinux.services.networking.tailscale.enable
              then "https://${service_name}.tail7cca06.ts.net"
              else null;
          };
        };
      in {
        adguard = selfhostOptions {
          service_name = "adguard";
          default_port = 8001;
        };
        forgejo = selfhostOptions {
          service_name = "forgejo";
          default_port = 8010;
        };
        caddy = {
          enable = mkEnableOption "caddy https server";
          envFile = mkOption {
            type = lib.types.str;
          };
        };
      };
    };
  };

  config = {
    assertions = [
      {
        assertion = with config.collinux.services.networking; (iwd.enable && !networkmanager.enable && !networkd.enable) || (!iwd.enable && networkmanager.enable && !networkd.enable) || (!iwd.enable && !networkmanager.enable && networkd.enable);
        message = "only one networking method (iwd, networkmanager, static) can be active";
      }
      {
        assertion = with config.collinux.services.networking; !(wireguard.enable && tailscale.enable);
        message = "only one vpn method (tailscale, wireguard) can be active";
      }
      {
        assertion = with config.collinux.services.networking; wireguard.enable && !networkd.enable;
        message = "wireguard configuration only supports networkd at the moment";
      }
      {
        assertion = with config.collinux.services.networking.wireguard; enable && (peersConfig |> (builtins.filter (m: m.role == "hub")) |> lib.count) == 1;
        message = "there must be exactly one hub in the wireguard configuration";
      }
    ];
  };
}