summary refs log tree commit diff
path: root/nixos/modules/services/networking/shorewall.nix
blob: 0f94d414fcf748faa4845e5e27e817a4b21787e5 (plain) (blame)
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
{ config, lib, pkgs, ... }:
let
  types = lib.types;
  cfg = config.services.shorewall;
in {
  options = {
    services.shorewall = {
      enable = lib.mkOption {
        type        = types.bool;
        default     = false;
        description = ''
          Whether to enable Shorewall IPv4 Firewall.
          <warning>
            <para>
            Enabling this service WILL disable the existing NixOS
            firewall! Default firewall rules provided by packages are not
            considered at the moment.
            </para>
          </warning>
        '';
      };
      package = lib.mkOption {
        type        = types.package;
        default     = pkgs.shorewall;
        defaultText = "pkgs.shorewall";
        description = "The shorewall package to use.";
      };
      configs = lib.mkOption {
        type        = types.attrsOf types.str;
        default     = {};
        description = ''
          This option defines the Shorewall configs.
          The attribute name defines the name of the config,
          and the attribute value defines the content of the config.
        '';
        apply = lib.mapAttrs (name: text: pkgs.writeText "${name}" text);
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.firewall.enable = false;
    systemd.services.shorewall = {
      description     = "Shorewall IPv4 Firewall";
      after           = [ "ipset.target" ];
      before          = [ "network-pre.target" ];
      wants           = [ "network-pre.target" ];
      wantedBy        = [ "multi-user.target" ];
      reloadIfChanged = true;
      restartTriggers = lib.attrValues cfg.configs;
      serviceConfig = {
        Type            = "oneshot";
        RemainAfterExit = "yes";
        ExecStart       = "${cfg.package}/bin/shorewall start";
        ExecReload      = "${cfg.package}/bin/shorewall reload";
        ExecStop        = "${cfg.package}/bin/shorewall stop";
      };
      preStart = ''
        install -D -d -m 750 /var/lib/shorewall
        install -D -d -m 755 /var/lock/subsys
        touch                /var/log/shorewall.log
        chown 750            /var/log/shorewall.log
      '';
    };
    environment = {
      etc = lib.mapAttrsToList
              (name: file:
                { source = file;
                  target = "shorewall/${name}";
                })
              cfg.configs;
      systemPackages = [ cfg.package ];
    };
  };
}