1 { config, lib, pkgs, ... }: with lib;
3 cfg = config.services.promtail;
5 prettyJSON = conf: pkgs.runCommandLocal "promtail-config.json" {} ''
6 echo '${builtins.toJSON conf}' | ${pkgs.buildPackages.jq}/bin/jq 'del(._module)' > $out
9 allowSystemdJournal = cfg.configuration ? scrape_configs && lib.any (v: v ? journal) cfg.configuration.scrape_configs;
11 allowPositionsFile = !lib.hasPrefix "/var/cache/promtail" positionsFile;
12 positionsFile = cfg.configuration.positions.filename;
14 options.services.promtail = with types; {
15 enable = mkEnableOption "the Promtail ingresser";
18 configuration = mkOption {
19 type = (pkgs.formats.json {}).type;
21 Specify the configuration for Promtail in Nix.
25 extraFlags = mkOption {
28 example = [ "--server.http-listen-port=3101" ];
30 Specify a list of additional command line flags,
31 which get escaped and are then passed to Loki.
36 config = mkIf cfg.enable {
37 services.promtail.configuration.positions.filename = mkDefault "/var/cache/promtail/positions.yaml";
39 systemd.services.promtail = {
40 description = "Promtail log ingress";
41 wantedBy = [ "multi-user.target" ];
42 stopIfChanged = false;
45 ${lib.getExe pkgs.promtail} -config.file=${prettyJSON cfg.configuration} -check-syntax
49 Restart = "on-failure";
52 ExecStart = "${pkgs.promtail}/bin/promtail -config.file=${prettyJSON cfg.configuration} ${escapeShellArgs cfg.extraFlags}";
54 ProtectSystem = "strict";
57 PrivateDevices = true;
58 ProtectKernelTunables = true;
59 ProtectControlGroups = true;
60 RestrictSUIDSGID = true;
62 CacheDirectory = "promtail";
63 ReadWritePaths = lib.optional allowPositionsFile (builtins.dirOf positionsFile);
68 CapabilityBoundingSet = "";
69 NoNewPrivileges = true;
71 ProtectKernelModules = true;
72 SystemCallArchitectures = "native";
73 ProtectKernelLogs = true;
76 LockPersonality = true;
77 ProtectHostname = true;
78 RestrictRealtime = true;
79 MemoryDenyWriteExecute = true;
82 SupplementaryGroups = lib.optional (allowSystemdJournal) "systemd-journal";
83 } // (optionalAttrs (!pkgs.stdenv.hostPlatform.isAarch64) { # FIXME: figure out why this breaks on aarch64
84 SystemCallFilter = "@system-service";
88 users.groups.promtail = {};
89 users.users.promtail = {
90 description = "Promtail service user";