2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2021-10-06 13:57:05 +00:00
|
|
|
inherit (lib) escapeShellArgs mkEnableOption mkIf mkOption types;
|
2020-04-24 23:36:52 +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 = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "loki");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "loki";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
User under which the Loki service runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-03-30 22:05:00 +00:00
|
|
|
package = lib.mkPackageOptionMD pkgs "grafana-loki" { };
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "loki";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Group under which the Loki service runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/loki";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specify the directory for Loki.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configuration = mkOption {
|
2020-11-12 09:05:59 +00:00
|
|
|
type = (pkgs.formats.json {}).type;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = {};
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specify the configuration for Loki in Nix.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specify a configuration file that Loki should use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2021-10-06 13:57:05 +00:00
|
|
|
example = [ "--server.http-listen-port=3101" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
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 22:05:00 +00:00
|
|
|
environment.systemPackages = [ cfg.package ]; # logcli
|
2020-11-12 09:05:59 +00:00
|
|
|
|
2020-04-24 23:36:52 +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" ];
|
|
|
|
|
|
|
|
serviceConfig = let
|
|
|
|
conf = if cfg.configFile == null
|
|
|
|
then prettyJSON cfg.configuration
|
|
|
|
else cfg.configFile;
|
|
|
|
in
|
|
|
|
{
|
2023-03-30 22:05:00 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/loki --config.file=${conf} ${escapeShellArgs cfg.extraFlags}";
|
2020-04-24 23:36:52 +00:00
|
|
|
User = cfg.user;
|
|
|
|
Restart = "always";
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
WorkingDirectory = cfg.dataDir;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|