1 { config, lib, pkgs, ... }:
4 inherit (lib) escapeShellArgs mkEnableOption mkIf mkOption types;
6 cfg = config.services.loki;
9 pkgs.runCommand "loki-config.json" { } ''
10 echo '${builtins.toJSON conf}' | ${pkgs.jq}/bin/jq 'del(._module)' > $out
14 options.services.loki = {
15 enable = mkEnableOption "loki";
21 User under which the Loki service runs.
25 package = lib.mkPackageOption pkgs "grafana-loki" { };
31 Group under which the Loki service runs.
37 default = "/var/lib/loki";
39 Specify the directory for Loki.
43 configuration = mkOption {
44 type = (pkgs.formats.json {}).type;
47 Specify the configuration for Loki in Nix.
51 configFile = mkOption {
52 type = types.nullOr types.path;
55 Specify a configuration file that Loki should use.
59 extraFlags = mkOption {
60 type = types.listOf types.str;
62 example = [ "--server.http-listen-port=3101" ];
64 Specify a list of additional command line flags,
65 which get escaped and are then passed to Loki.
70 config = mkIf cfg.enable {
73 (cfg.configuration == {} -> cfg.configFile != null) &&
74 (cfg.configFile != null -> cfg.configuration == {})
78 'services.loki.configuration' or
79 'services.loki.configFile'.
83 environment.systemPackages = [ cfg.package ]; # logcli
85 users.groups.${cfg.group} = { };
86 users.users.${cfg.user} = {
87 description = "Loki Service User";
94 systemd.services.loki = {
95 description = "Loki Service Daemon";
96 wantedBy = [ "multi-user.target" ];
97 after = [ "network.target" ];
100 conf = if cfg.configFile == null
102 # Config validation may fail when using extraFlags = [ "-config.expand-env=true" ].
103 # To work around this, we simply skip it when extraFlags is not empty.
104 if cfg.extraFlags == []
105 then validateConfig (prettyJSON cfg.configuration)
106 else prettyJSON cfg.configuration
108 validateConfig = file:
109 pkgs.runCommand "validate-loki-conf" {
110 nativeBuildInputs = [ cfg.package ];
112 loki -verify-config -config.file "${file}"
113 ln -s "${file}" "$out"
117 ExecStart = "${cfg.package}/bin/loki --config.file=${conf} ${escapeShellArgs cfg.extraFlags}";
122 ProtectSystem = "full";
123 DevicePolicy = "closed";
124 NoNewPrivileges = true;
125 WorkingDirectory = cfg.dataDir;