1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.journalbeat;
7
8 lt6 = builtins.compareVersions cfg.package.version "6" < 0;
9
10 journalbeatYml = pkgs.writeText "journalbeat.yml" ''
11 name: ${cfg.name}
12 tags: ${builtins.toJSON cfg.tags}
13
14 ${optionalString lt6 "journalbeat.cursor_state_file: /var/lib/${cfg.stateDir}/cursor-state"}
15
16 ${cfg.extraConfig}
17 '';
18
19in
20{
21 options = {
22
23 services.journalbeat = {
24
25 enable = mkEnableOption "journalbeat";
26
27 package = mkOption {
28 type = types.package;
29 default = pkgs.journalbeat;
30 defaultText = literalExpression "pkgs.journalbeat";
31 example = literalExpression "pkgs.journalbeat7";
32 description = ''
33 The journalbeat package to use
34 '';
35 };
36
37 name = mkOption {
38 type = types.str;
39 default = "journalbeat";
40 description = "Name of the beat";
41 };
42
43 tags = mkOption {
44 type = types.listOf types.str;
45 default = [];
46 description = "Tags to place on the shipped log messages";
47 };
48
49 stateDir = mkOption {
50 type = types.str;
51 default = "journalbeat";
52 description = ''
53 Directory below <literal>/var/lib/</literal> to store journalbeat's
54 own logs and other data. This directory will be created automatically
55 using systemd's StateDirectory mechanism.
56 '';
57 };
58
59 extraConfig = mkOption {
60 type = types.lines;
61 default = optionalString lt6 ''
62 journalbeat:
63 seek_position: cursor
64 cursor_seek_fallback: tail
65 write_cursor_state: true
66 cursor_flush_period: 5s
67 clean_field_names: true
68 convert_to_numbers: false
69 move_metadata_to_field: journal
70 default_type: journal
71 '';
72 description = "Any other configuration options you want to add";
73 };
74
75 };
76 };
77
78 config = mkIf cfg.enable {
79
80 assertions = [
81 {
82 assertion = !hasPrefix "/" cfg.stateDir;
83 message =
84 "The option services.journalbeat.stateDir shouldn't be an absolute directory." +
85 " It should be a directory relative to /var/lib/.";
86 }
87 ];
88
89 systemd.services.journalbeat = {
90 description = "Journalbeat log shipper";
91 wantedBy = [ "multi-user.target" ];
92 preStart = ''
93 mkdir -p ${cfg.stateDir}/data
94 mkdir -p ${cfg.stateDir}/logs
95 '';
96 serviceConfig = {
97 StateDirectory = cfg.stateDir;
98 ExecStart = ''
99 ${cfg.package}/bin/journalbeat \
100 -c ${journalbeatYml} \
101 -path.data /var/lib/${cfg.stateDir}/data \
102 -path.logs /var/lib/${cfg.stateDir}/logs'';
103 Restart = "always";
104 };
105 };
106 };
107}