summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/ntopng.nix
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2022-02-12 15:45:57 +0300
committerNikolay Amiantov <ab@fmap.me>2022-02-19 14:03:33 +0300
commit41f4d999ad6ea5233e0852daea33d3c375b5a5ee (patch)
tree77c66d2fde6e8196c710396d1c2d795579c0f9e4 /nixos/modules/services/networking/ntopng.nix
parent6a91c56637e58e3dd4b7c2ff05998169e5c43f37 (diff)
nixos/ntopng: update user and redis configuration
New ntopng version supports running as specified user. Create a separate user for ntopng with a separate Redis instance. Separate instance is only used for new `system.stateVersion`s to avoid breaking existing setups. To configure that we add two new options, `redis.address` and `redis.createInstance`. They can also be used to specify your own Redis address.
Diffstat (limited to 'nixos/modules/services/networking/ntopng.nix')
-rw-r--r--nixos/modules/services/networking/ntopng.nix55
1 files changed, 47 insertions, 8 deletions
diff --git a/nixos/modules/services/networking/ntopng.nix b/nixos/modules/services/networking/ntopng.nix
index 77a004e8ab3a..2c45d418a0db 100644
--- a/nixos/modules/services/networking/ntopng.nix
+++ b/nixos/modules/services/networking/ntopng.nix
@@ -6,7 +6,13 @@ let
cfg = config.services.ntopng;
opt = options.services.ntopng;
- redisCfg = config.services.redis;
+
+ createRedis = cfg.redis.createInstance != null;
+ redisService =
+ if cfg.redis.createInstance == "" then
+ "redis.service"
+ else
+ "redis-${cfg.redis.createInstance}.service";
configFile = if cfg.configText != "" then
pkgs.writeText "ntopng.conf" ''
@@ -16,7 +22,9 @@ let
pkgs.writeText "ntopng.conf" ''
${concatStringsSep " " (map (e: "--interface=" + e) cfg.interfaces)}
--http-port=${toString cfg.http-port}
- --redis=localhost:${toString redisCfg.port}
+ --redis=${cfg.redis.address}
+ --data-dir=/var/lib/ntopng
+ --user=ntopng
${cfg.extraConfig}
'';
@@ -64,6 +72,24 @@ in
'';
};
+ redis.address = mkOption {
+ type = types.str;
+ example = literalExpression "config.services.redis.ntopng.unixSocket";
+ description = ''
+ Redis address - may be a Unix socket or a network host and port.
+ '';
+ };
+
+ redis.createInstance = mkOption {
+ type = types.nullOr types.str;
+ default = if versionAtLeast config.system.stateVersion "22.05" then "ntopng" else "";
+ description = ''
+ Local Redis instance name. Set to <literal>null</literal> to disable
+ local Redis instance. Defaults to <literal>""</literal> for
+ <literal>system.stateVersion</literal> older than 22.05.
+ '';
+ };
+
configText = mkOption {
default = "";
example = ''
@@ -95,23 +121,36 @@ in
config = mkIf cfg.enable {
# ntopng uses redis for data storage
- services.redis.enable = true;
+ services.ntopng.redis.address =
+ mkIf createRedis config.services.redis.servers.${cfg.redis.createInstance}.unixSocket;
+
+ services.redis.servers = mkIf createRedis {
+ ${cfg.redis.createInstance} = {
+ enable = true;
+ user = mkIf (cfg.redis.createInstance == "ntopng") "ntopng";
+ };
+ };
# nice to have manual page and ntopng command in PATH
environment.systemPackages = [ pkgs.ntopng ];
+ systemd.tmpfiles.rules = [ "d /var/lib/ntopng 0700 ntopng ntopng -" ];
+
systemd.services.ntopng = {
description = "Ntopng Network Monitor";
- requires = [ "redis.service" ];
- after = [ "network.target" "redis.service" ];
+ requires = optional createRedis redisService;
+ after = [ "network.target" ] ++ optional createRedis redisService;
wantedBy = [ "multi-user.target" ];
- preStart = "mkdir -p /var/lib/ntopng/";
serviceConfig.ExecStart = "${pkgs.ntopng}/bin/ntopng ${configFile}";
unitConfig.Documentation = "man:ntopng(8)";
};
- # ntopng drops priveleges to user "nobody" and that user is already defined
- # in users-groups.nix.
+ users.extraUsers.ntopng = {
+ group = "ntopng";
+ isSystemUser = true;
+ };
+
+ users.extraGroups.ntopng = { };
};
}