1{ config, lib, pkgs, ... }: with lib;
2let
3 cfg = config.services.promtail;
4
5 prettyJSON = conf: pkgs.runCommandLocal "promtail-config.json" {} ''
6 echo '${builtins.toJSON conf}' | ${pkgs.buildPackages.jq}/bin/jq 'del(._module)' > $out
7 '';
8
9 allowSystemdJournal = cfg.configuration ? scrape_configs && lib.any (v: v ? journal) cfg.configuration.scrape_configs;
10
11 allowPositionsFile = !lib.hasPrefix "/var/cache/promtail" positionsFile;
12 positionsFile = cfg.configuration.positions.filename;
13in {
14 options.services.promtail = with types; {
15 enable = mkEnableOption "the Promtail ingresser";
16
17
18 configuration = mkOption {
19 type = (pkgs.formats.json {}).type;
20 description = ''
21 Specify the configuration for Promtail in Nix.
22 '';
23 };
24
25 extraFlags = mkOption {
26 type = listOf str;
27 default = [];
28 example = [ "--server.http-listen-port=3101" ];
29 description = ''
30 Specify a list of additional command line flags,
31 which get escaped and are then passed to Loki.
32 '';
33 };
34 };
35
36 config = mkIf cfg.enable {
37 services.promtail.configuration.positions.filename = mkDefault "/var/cache/promtail/positions.yaml";
38
39 systemd.services.promtail = {
40 description = "Promtail log ingress";
41 wantedBy = [ "multi-user.target" ];
42 stopIfChanged = false;
43
44 preStart = ''
45 ${lib.getExe pkgs.promtail} -config.file=${prettyJSON cfg.configuration} -check-syntax
46 '';
47
48 serviceConfig = {
49 Restart = "on-failure";
50 TimeoutStopSec = 10;
51
52 ExecStart = "${pkgs.promtail}/bin/promtail -config.file=${prettyJSON cfg.configuration} ${escapeShellArgs cfg.extraFlags}";
53
54 ProtectSystem = "strict";
55 ProtectHome = true;
56 PrivateTmp = true;
57 PrivateDevices = true;
58 ProtectKernelTunables = true;
59 ProtectControlGroups = true;
60 RestrictSUIDSGID = true;
61 PrivateMounts = true;
62 CacheDirectory = "promtail";
63 ReadWritePaths = lib.optional allowPositionsFile (builtins.dirOf positionsFile);
64
65 User = "promtail";
66 Group = "promtail";
67
68 CapabilityBoundingSet = "";
69 NoNewPrivileges = true;
70
71 ProtectKernelModules = true;
72 SystemCallArchitectures = "native";
73 ProtectKernelLogs = true;
74 ProtectClock = true;
75
76 LockPersonality = true;
77 ProtectHostname = true;
78 RestrictRealtime = true;
79 MemoryDenyWriteExecute = true;
80 PrivateUsers = true;
81
82 SupplementaryGroups = lib.optional (allowSystemdJournal) "systemd-journal";
83 } // (optionalAttrs (!pkgs.stdenv.isAarch64) { # FIXME: figure out why this breaks on aarch64
84 SystemCallFilter = "@system-service";
85 });
86 };
87
88 users.groups.promtail = {};
89 users.users.promtail = {
90 description = "Promtail service user";
91 isSystemUser = true;
92 group = "promtail";
93 };
94 };
95}