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 configFile = if cfg.configFile != null
16 else prettyJSON cfg.configuration;
19 options.services.promtail = with types; {
20 enable = mkEnableOption "the Promtail ingresser";
22 configuration = mkOption {
23 type = (pkgs.formats.json {}).type;
25 Specify the configuration for Promtail in Nix.
26 This option will be ignored if `services.promtail.configFile` is defined.
30 configFile = mkOption {
34 Config file path for Promtail.
35 If this option is defined, the value of `services.promtail.configuration` will be ignored.
39 extraFlags = mkOption {
42 example = [ "--server.http-listen-port=3101" ];
44 Specify a list of additional command line flags,
45 which get escaped and are then passed to Loki.
50 config = mkIf cfg.enable {
51 services.promtail.configuration.positions.filename = mkDefault "/var/cache/promtail/positions.yaml";
53 systemd.services.promtail = {
54 description = "Promtail log ingress";
55 wantedBy = [ "multi-user.target" ];
56 stopIfChanged = false;
59 ${lib.getExe pkgs.promtail} -config.file=${configFile} -check-syntax
63 Restart = "on-failure";
66 ExecStart = "${pkgs.promtail}/bin/promtail -config.file=${configFile} ${escapeShellArgs cfg.extraFlags}";
68 ProtectSystem = "strict";
71 PrivateDevices = true;
72 ProtectKernelTunables = true;
73 ProtectControlGroups = true;
74 RestrictSUIDSGID = true;
76 CacheDirectory = "promtail";
77 ReadWritePaths = lib.optional allowPositionsFile (builtins.dirOf positionsFile);
82 CapabilityBoundingSet = "";
83 NoNewPrivileges = true;
85 ProtectKernelModules = true;
86 SystemCallArchitectures = "native";
87 ProtectKernelLogs = true;
90 LockPersonality = true;
91 ProtectHostname = true;
92 RestrictRealtime = true;
93 MemoryDenyWriteExecute = true;
96 SupplementaryGroups = lib.optional (allowSystemdJournal) "systemd-journal";
97 } // (optionalAttrs (!pkgs.stdenv.hostPlatform.isAarch64) { # FIXME: figure out why this breaks on aarch64
98 SystemCallFilter = "@system-service";
102 users.groups.promtail = {};
103 users.users.promtail = {
104 description = "Promtail service user";