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 (lib.mdDoc "loki");
20 description = lib.mdDoc ''
21 User under which the Loki service runs.
28 description = lib.mdDoc ''
29 Group under which the Loki service runs.
35 default = "/var/lib/loki";
36 description = lib.mdDoc ''
37 Specify the directory for Loki.
41 configuration = mkOption {
42 type = (pkgs.formats.json {}).type;
44 description = lib.mdDoc ''
45 Specify the configuration for Loki in Nix.
49 configFile = mkOption {
50 type = types.nullOr types.path;
52 description = lib.mdDoc ''
53 Specify a configuration file that Loki should use.
57 extraFlags = mkOption {
58 type = types.listOf types.str;
60 example = [ "--server.http-listen-port=3101" ];
61 description = lib.mdDoc ''
62 Specify a list of additional command line flags,
63 which get escaped and are then passed to Loki.
68 config = mkIf cfg.enable {
71 (cfg.configuration == {} -> cfg.configFile != null) &&
72 (cfg.configFile != null -> cfg.configuration == {})
76 'services.loki.configuration' or
77 'services.loki.configFile'.
81 environment.systemPackages = [ pkgs.grafana-loki ]; # logcli
83 users.groups.${cfg.group} = { };
84 users.users.${cfg.user} = {
85 description = "Loki Service User";
92 systemd.services.loki = {
93 description = "Loki Service Daemon";
94 wantedBy = [ "multi-user.target" ];
97 conf = if cfg.configFile == null
98 then prettyJSON cfg.configuration
102 ExecStart = "${pkgs.grafana-loki}/bin/loki --config.file=${conf} ${escapeShellArgs cfg.extraFlags}";
107 ProtectSystem = "full";
108 DevicePolicy = "closed";
109 NoNewPrivileges = true;
110 WorkingDirectory = cfg.dataDir;