summaryrefslogtreecommitdiff
path: root/modules/nixos/personal/networking/default.nix
blob: eec4195e359261abddd4473d86d20e72398205ce (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
{
  config,
  lib,
  pkgs,
  options,
  ...
}: let
  cfg = config.personal.networking;
  mkFirewallEnableOption = name:
    lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Whether to open ports for ${name}.";
    };
in {
  imports = [./wifi.nix];

  options.personal.networking = {
    enable = lib.mkEnableOption "networking";
    bluetooth.enable = lib.mkEnableOption "bluetooth";
    networkmanager.enable = lib.mkEnableOption "NetworkManager";
    ssh.enable = lib.mkEnableOption "SSH server";
    firewall = {
      syncthing = mkFirewallEnableOption "Syncthing";
      kdeconnect = mkFirewallEnableOption "KDE Connect";
      http = mkFirewallEnableOption "HTTP and HTTPS (incoming)";
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages =
      lib.optional cfg.networkmanager.enable pkgs.networkmanager;
    networking = {
      networkmanager = lib.mkIf cfg.networkmanager.enable {
        enable = true;
        unmanaged = ["interface-name:ve-*"];
      };
      firewall = {
        enable = true;
        allowedTCPPorts =
          lib.optional cfg.firewall.syncthing 22000
          ++ lib.optionals cfg.firewall.http [80 443];
        allowedUDPPorts = lib.optionals cfg.firewall.syncthing [22000 21027];
        allowedTCPPortRanges = lib.optional cfg.firewall.kdeconnect {
          from = 1714;
          to = 1764;
        };
        allowedUDPPortRanges = lib.optional cfg.firewall.kdeconnect {
          from = 1714;
          to = 1764;
        };
      };
    };
    services = lib.mkIf cfg.ssh.enable {
      openssh =
        {
          enable = true;
          extraConfig = ''
            AcceptEnv PS1
          '';
        }
        // (
          if options.services.openssh ? settings
          then {
            settings = {
              PermitRootLogin = "no";
              PasswordAuthentication = false;
            };
          }
          else {
            permitRootLogin = "no";
            passwordAuthentication = false;
          }
        );
      fail2ban.enable = true;
    };
    hardware.bluetooth.enable = cfg.bluetooth.enable;
  };
}