~aasg/nixexprs

ref: 880638840c289a9d561d636725e68b924bc4726c nixexprs/modules/services/networking/monitoring/bird-lg.nix -rw-r--r-- 5.4 KiB
88063884 — Aluísio Augusto Silva Gonçalves bird-lg: Fix import in config loader script 2 months ago
                                                                                
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{ config, lib, pkgs, ... }:
let
  inherit (lib) concatStringsSep mkDefault mkEnableOption mkIf mkOption types;
  settingsFormat = pkgs.formats.json { };

  cfg = config.services.bird-lg;
in
{

  options = {
    services.bird-lg.server = {
      enable = mkEnableOption "BIRD looking glass server";

      logToSyslog = mkOption {
        description = "Whether to log to journald via syslog instead of writing to stderr.";
        type = types.bool;
        default = true;
      };

      appSettings = mkOption {
        description = "Configuration for bird-lg's server.";
        type = settingsFormat.type;
        default = { };
      };

      gunicornSettings = mkOption {
        description = "Configuration for the Gunicorn instance running bird-lg's server.";
        type = settingsFormat.type;
        default = { };
      };

      extraConfigFiles = mkOption {
        description = "Extra JSON files containing configuration, for example secrets.";
        type = types.listOf types.path;
        default = [ ];
      };
    };

    services.bird-lg.client = {
      enable = mkEnableOption "BIRD looking glass client proxy";

      logToSyslog = mkOption {
        description = "Whether to log to journald via syslog instead of writing to stderr.";
        type = types.bool;
        default = true;
      };

      appSettings = mkOption {
        description = "Configuration for bird-lg's client proxy.";
        type = settingsFormat.type;
        default = { };
      };

      gunicornSettings = mkOption {
        description = "Configuration for the Gunicorn instance running bird-lg's client proxy.";
        type = settingsFormat.type;
        default = { };
      };

      extraConfigFiles = mkOption {
        description = "Extra JSON files containing configuration, for example secrets.";
        type = types.listOf types.path;
        default = [ ];
      };
    };
  };

  config = {

    ################
    # Server setup #
    ################

    services.bird-lg.server.appSettings = {
      DEBUG = mkDefault true;
      LOG_LEVEL = mkDefault "WARNING";
      PROXY = mkDefault { };
      PROXY_TIMEOUT = mkDefault {
        bird = 10;
        traceroute = 60;
      };
      UNIFIED_DAEMON = mkDefault true;
    };

    services.bird-lg.server.gunicornSettings = mkIf cfg.server.logToSyslog {
      errorlog = mkDefault "/dev/null";
      syslog = mkDefault true;
      syslog_addr = mkDefault "unix:///dev/log";
    };

    systemd.services.bird-lg-server = mkIf cfg.server.enable {
      description = "BIRD looking glass web server";
      requires = [ "network-online.target" ];
      after = [ "bird.service" "bird6.service" "bird2.service" "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      environment = {
        BIRD_LG_CONFIG = ./bird-lg-config.py;
        BIRD_LG_CONFIG_FILES = concatStringsSep ":" ([
          (settingsFormat.generate "bird-lg-gunicorn.json" cfg.server.gunicornSettings)
          (settingsFormat.generate "bird-lg.json" cfg.server.appSettings)
        ] ++ cfg.server.extraConfigFiles);
        BIRD_LG_SYSLOG = toString cfg.server.logToSyslog;
      };
      serviceConfig = {
        Type = "simple";
        ExecStart = "${pkgs.bird-lg}/bin/bird-lg-webservice --config=\${BIRD_LG_CONFIG}";
        Restart = "on-failure";

        DynamicUser = true;
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        PrivateDevices = true;
        PrivateTmp = true;
        DevicePolicy = "closed";
        MemoryDenyWriteExecute = true;
      };
    };

    ######################
    # Client proxy setup #
    ######################

    services.bird-lg.client.appSettings = {
      DEBUG = mkDefault false;
      LOG_LEVEL = mkDefault "WARNING";
      BIRD_SOCKET = mkDefault "/run/bird.ctl";
      BIRD6_SOCKET = mkDefault "/run/bird6.ctl";
    };

    services.bird-lg.client.gunicornSettings = mkIf cfg.client.logToSyslog {
      errorlog = mkDefault "/dev/null";
      syslog = mkDefault true;
      syslog_addr = mkDefault "unix:///dev/log#dgram";
    };

    systemd.services.bird-lg-client = mkIf cfg.client.enable {
      description = "BIRD looking glass client proxy";
      requires = [ "network-online.target" ];
      after = [ "bird.service" "bird6.service" "bird2.service" "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      environment = {
        BIRD_LG_CONFIG = ./bird-lg-config.py;
        BIRD_LG_CONFIG_FILES = concatStringsSep ":" ([
          (settingsFormat.generate "bird-lgproxy-gunicorn.json" cfg.client.gunicornSettings)
          (settingsFormat.generate "bird-lgproxy.json" cfg.client.appSettings)
        ] ++ cfg.client.extraConfigFiles);
        BIRD_LG_SYSLOG = toString cfg.client.logToSyslog;
      };
      serviceConfig = {
        Type = "simple";
        ExecStart = "${pkgs.bird-lg}/bin/bird-lg-proxy --config=\${BIRD_LG_CONFIG}";
        Restart = "on-failure";

        DynamicUser = true;
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        PrivateDevices = true;
        PrivateTmp = true;
        DevicePolicy = "closed";
      };
    };

  };

}