2019-06-27 16:10:41 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2021-10-03 16:06:03 +00:00
|
|
|
inherit (lib) escapeShellArgs mkEnableOption mkIf mkOption types;
|
2019-06-27 16:10:41 +00:00
|
|
|
|
|
|
|
cfg = config.services.loki;
|
|
|
|
|
|
|
|
prettyJSON = conf:
|
|
|
|
pkgs.runCommand "loki-config.json" { } ''
|
|
|
|
echo '${builtins.toJSON conf}' | ${pkgs.jq}/bin/jq 'del(._module)' > $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.services.loki = {
|
|
|
|
enable = mkEnableOption "loki";
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "loki";
|
|
|
|
description = ''
|
|
|
|
User under which the Loki service runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-11-30 18:03:14 +00:00
|
|
|
package = lib.mkPackageOption pkgs "grafana-loki" { };
|
2023-03-30 12:35:53 +00:00
|
|
|
|
2019-06-27 16:10:41 +00:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "loki";
|
|
|
|
description = ''
|
|
|
|
Group under which the Loki service runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/loki";
|
|
|
|
description = ''
|
|
|
|
Specify the directory for Loki.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configuration = mkOption {
|
2020-11-10 09:29:18 +00:00
|
|
|
type = (pkgs.formats.json {}).type;
|
2019-06-27 16:10:41 +00:00
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Specify the configuration for Loki in Nix.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Specify a configuration file that Loki should use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2021-10-03 16:06:03 +00:00
|
|
|
example = [ "--server.http-listen-port=3101" ];
|
2019-06-27 16:10:41 +00:00
|
|
|
description = ''
|
|
|
|
Specify a list of additional command line flags,
|
|
|
|
which get escaped and are then passed to Loki.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [{
|
|
|
|
assertion = (
|
|
|
|
(cfg.configuration == {} -> cfg.configFile != null) &&
|
|
|
|
(cfg.configFile != null -> cfg.configuration == {})
|
|
|
|
);
|
|
|
|
message = ''
|
|
|
|
Please specify either
|
|
|
|
'services.loki.configuration' or
|
|
|
|
'services.loki.configFile'.
|
|
|
|
'';
|
|
|
|
}];
|
|
|
|
|
2023-03-30 12:35:53 +00:00
|
|
|
environment.systemPackages = [ cfg.package ]; # logcli
|
2020-11-10 09:15:54 +00:00
|
|
|
|
2019-06-27 16:10:41 +00:00
|
|
|
users.groups.${cfg.group} = { };
|
|
|
|
users.users.${cfg.user} = {
|
|
|
|
description = "Loki Service User";
|
|
|
|
group = cfg.group;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.loki = {
|
|
|
|
description = "Loki Service Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-05-28 15:02:04 +00:00
|
|
|
after = [ "network.target" ];
|
2019-06-27 16:10:41 +00:00
|
|
|
|
|
|
|
serviceConfig = let
|
|
|
|
conf = if cfg.configFile == null
|
2024-05-19 02:47:36 +00:00
|
|
|
then
|
|
|
|
# Config validation may fail when using extraFlags = [ "-config.expand-env=true" ].
|
|
|
|
# To work around this, we simply skip it when extraFlags is not empty.
|
|
|
|
if cfg.extraFlags == []
|
|
|
|
then validateConfig (prettyJSON cfg.configuration)
|
|
|
|
else prettyJSON cfg.configuration
|
2019-06-27 16:10:41 +00:00
|
|
|
else cfg.configFile;
|
2024-05-15 17:03:48 +00:00
|
|
|
validateConfig = file:
|
|
|
|
pkgs.runCommand "validate-loki-conf" {
|
2024-05-16 20:11:19 +00:00
|
|
|
nativeBuildInputs = [ cfg.package ];
|
2024-05-15 17:03:48 +00:00
|
|
|
} ''
|
|
|
|
loki -verify-config -config.file "${file}"
|
|
|
|
ln -s "${file}" "$out"
|
|
|
|
'';
|
2019-06-27 16:10:41 +00:00
|
|
|
in
|
|
|
|
{
|
2024-05-19 02:47:36 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/loki --config.file=${conf} ${escapeShellArgs cfg.extraFlags}";
|
2019-06-27 16:10:41 +00:00
|
|
|
User = cfg.user;
|
|
|
|
Restart = "always";
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectSystem = "full";
|
2019-08-17 10:08:51 +00:00
|
|
|
DevicePolicy = "closed";
|
2019-06-27 16:10:41 +00:00
|
|
|
NoNewPrivileges = true;
|
|
|
|
WorkingDirectory = cfg.dataDir;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|