1 { config, lib, pkgs, ... }:
6 cfg = config.services.mackerel-agent;
7 settingsFmt = pkgs.formats.toml {};
9 options.services.mackerel-agent = {
10 enable = mkEnableOption "mackerel.io agent";
12 # the upstream package runs as root, but doesn't seem to be strictly
13 # necessary for basic functionality
14 runAsRoot = mkEnableOption "running as root";
16 autoRetirement = mkEnableOption ''
17 retiring the host upon OS shutdown
20 apiKeyFile = mkOption {
22 example = "/run/keys/mackerel-api-key";
24 Path to file containing the Mackerel API key. The file should contain a
25 single line of the following form:
27 `apikey = "EXAMPLE_API_KEY"`
33 Options for mackerel-agent.conf.
36 <https://mackerel.io/docs/entry/spec/agent>
45 type = types.submodule {
46 freeformType = settingsFmt.type;
48 options.host_status = {
50 type = types.enum [ "working" "standby" "maintenance" "poweroff" ];
51 description = "Host status after agent startup.";
55 type = types.enum [ "working" "standby" "maintenance" "poweroff" ];
56 description = "Host status after agent shutdown.";
62 mkEnableOption "collecting memory usage for the agent itself";
67 config = mkIf cfg.enable {
68 environment.systemPackages = with pkgs; [ mackerel-agent ];
71 "mackerel-agent/mackerel-agent.conf".source =
72 settingsFmt.generate "mackerel-agent.conf" cfg.settings;
73 "mackerel-agent/conf.d/api-key.conf".source = cfg.apiKeyFile;
76 services.mackerel-agent.settings = {
77 root = mkDefault "/var/lib/mackerel-agent";
78 pidfile = mkDefault "/run/mackerel-agent/mackerel-agent.pid";
80 # conf.d stores the symlink to cfg.apiKeyFile
81 include = mkDefault "/etc/mackerel-agent/conf.d/*.conf";
84 # upstream service file in https://github.com/mackerelio/mackerel-agent/blob/master/packaging/rpm/src/mackerel-agent.service
85 systemd.services.mackerel-agent = {
86 description = "mackerel.io agent";
87 wants = [ "network-online.target" ];
88 after = [ "network-online.target" "nss-lookup.target" ];
89 wantedBy = [ "multi-user.target" ];
91 MACKEREL_PLUGIN_WORKDIR = mkDefault "%C/mackerel-agent";
94 DynamicUser = !cfg.runAsRoot;
95 PrivateTmp = mkDefault true;
96 CacheDirectory = "mackerel-agent";
97 ConfigurationDirectory = "mackerel-agent";
98 RuntimeDirectory = "mackerel-agent";
99 StateDirectory = "mackerel-agent";
100 ExecStart = "${pkgs.mackerel-agent}/bin/mackerel-agent supervise";
101 ExecStopPost = mkIf cfg.autoRetirement "${pkg.mackerel-agent}/bin/mackerel-agent retire -force";
102 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
103 LimitNOFILE = mkDefault 65536;
104 LimitNPROC = mkDefault 65536;
107 config.environment.etc."mackerel-agent/mackerel-agent.conf".source