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 (lib.mdDoc "the Promtail ingresser");
16
17
18 configuration = mkOption {
19 type = (pkgs.formats.json {}).type;
20 description = lib.mdDoc ''
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 = lib.mdDoc ''
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 serviceConfig = {
45 Restart = "on-failure";
46 TimeoutStopSec = 10;
47
48 ExecStart = "${pkgs.promtail}/bin/promtail -config.file=${prettyJSON cfg.configuration} ${escapeShellArgs cfg.extraFlags}";
49
50 ProtectSystem = "strict";
51 ProtectHome = true;
52 PrivateTmp = true;
53 PrivateDevices = true;
54 ProtectKernelTunables = true;
55 ProtectControlGroups = true;
56 RestrictSUIDSGID = true;
57 PrivateMounts = true;
58 CacheDirectory = "promtail";
59 ReadWritePaths = lib.optional allowPositionsFile (builtins.dirOf positionsFile);
60
61 User = "promtail";
62 Group = "promtail";
63
64 CapabilityBoundingSet = "";
65 NoNewPrivileges = true;
66
67 ProtectKernelModules = true;
68 SystemCallArchitectures = "native";
69 ProtectKernelLogs = true;
70 ProtectClock = true;
71
72 LockPersonality = true;
73 ProtectHostname = true;
74 RestrictRealtime = true;
75 MemoryDenyWriteExecute = true;
76 PrivateUsers = true;
77
78 SupplementaryGroups = lib.optional (allowSystemdJournal) "systemd-journal";
79 } // (optionalAttrs (!pkgs.stdenv.isAarch64) { # FIXME: figure out why this breaks on aarch64
80 SystemCallFilter = "@system-service";
81 });
82 };
83
84 users.groups.promtail = {};
85 users.users.promtail = {
86 description = "Promtail service user";
87 isSystemUser = true;
88 group = "promtail";
89 };
90 };
91}