summaryrefslogtreecommitdiffstats
path: root/nixos/modules/programs/openvpn3.nix
blob: e45475ab3c37f98f0f1f888922bfa28e29eedec3 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
  config,
  lib,
  pkgs,
  ...
}:

let
  json = pkgs.formats.json { };
  cfg = config.programs.openvpn3;

  inherit (lib)
    mkEnableOption
    mkPackageOption
    mkOption
    literalExpression
    max
    options
    lists
    ;
  inherit (lib.types)
    bool
    submodule
    ints
    attrsOf
    ;
in
{
  options.programs.openvpn3 = {
    enable = mkEnableOption "the openvpn3 client";
    package = mkPackageOption pkgs "openvpn3" { };
    netcfg = mkOption {
      description = "Network configuration";
      default = { };
      type = submodule {
        options = {
          settings = mkOption {
            description = "Options stored in {file}`/etc/openvpn3/netcfg.json` configuration file";
            default = { };
            type = submodule {
              freeformType = attrsOf json.type;
              options = {
                systemd_resolved = mkOption {
                  type = bool;
                  description = "Whether to use systemd-resolved integration";
                  default = config.services.resolved.enable;
                  defaultText = literalExpression "config.services.resolved.enable";
                  example = false;
                };
              };
            };
          };
        };
      };
    };
    log-service = mkOption {
      description = "Log service configuration";
      default = { };
      type = submodule {
        options = {
          settings = mkOption {
            description = "Options stored in {file}`/etc/openvpn3/log-service.json` configuration file";
            default = { };
            type = submodule {
              freeformType = attrsOf json.type;
              options = {
                journald = mkOption {
                  description = "Use systemd-journald";
                  type = bool;
                  default = true;
                  example = false;
                };
                log_dbus_details = mkOption {
                  description = "Add D-Bus details in log file/syslog";
                  type = bool;
                  default = true;
                  example = false;
                };
                log_level = mkOption {
                  description = "How verbose should the logging be";
                  type = (ints.between 0 7) // {
                    merge = _loc: defs: lists.foldl max 0 (options.getValues defs);
                  };
                  default = 3;
                  example = 6;
                };
                timestamp = mkOption {
                  description = "Add timestamp log file";
                  type = bool;
                  default = false;
                  example = true;
                };
              };
            };
          };
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    services.dbus.packages = [ cfg.package ];

    users.users.openvpn = {
      isSystemUser = true;
      uid = config.ids.uids.openvpn;
      group = "openvpn";
    };

    users.groups.openvpn = {
      gid = config.ids.gids.openvpn;
    };

    environment = {
      systemPackages = [ cfg.package ];
      etc = {
        "openvpn3/netcfg.json".source = json.generate "netcfg.json" cfg.netcfg.settings;
        "openvpn3/log-service.json".source = json.generate "log-service.json" cfg.log-service.settings;
      };
    };

    systemd = {
      packages = [ cfg.package ];
      tmpfiles.rules = [
        "d /etc/openvpn3/configs 0750 openvpn openvpn - -"
      ];
    };
  };

  meta.maintainers = with lib.maintainers; [
    progrm_jarvis
  ];
}