diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index 6ac12e4e1387..0a38fa317305 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -542,6 +542,7 @@
./services/monitoring/kapacitor.nix
./services/monitoring/loki.nix
./services/monitoring/longview.nix
+ ./services/monitoring/mackerel-agent.nix
./services/monitoring/monit.nix
./services/monitoring/munin.nix
./services/monitoring/nagios.nix
diff --git a/nixos/modules/services/monitoring/mackerel-agent.nix b/nixos/modules/services/monitoring/mackerel-agent.nix
new file mode 100644
index 000000000000..7046de9d403c
--- /dev/null
+++ b/nixos/modules/services/monitoring/mackerel-agent.nix
@@ -0,0 +1,111 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+ cfg = config.services.mackerel-agent;
+ settingsFmt = pkgs.formats.toml {};
+in {
+ options.services.mackerel-agent = {
+ enable = mkEnableOption "mackerel.io agent";
+
+ # the upstream package runs as root, but doesn't seem to be strictly
+ # necessary for basic functionality
+ runAsRoot = mkEnableOption "Whether to run as root.";
+
+ autoRetirement = mkEnableOption ''
+ Whether to automatically retire the host upon OS shutdown.
+ '';
+
+ apiKeyFile = mkOption {
+ type = types.path;
+ default = "";
+ example = "/run/keys/mackerel-api-key";
+ description = ''
+ Path to file containing the Mackerel API key. The file should contain a
+ single line of the following form:
+
+ apikey = "EXAMPLE_API_KEY"
+ '';
+ };
+
+ settings = mkOption {
+ description = ''
+ Options for mackerel-agent.conf.
+
+ Documentation:
+
+ '';
+
+ default = {};
+ example = {
+ verbose = false;
+ silent = false;
+ };
+
+ type = types.submodule {
+ freeformType = settingsFmt.type;
+
+ options.host_status = {
+ on_start = mkOption {
+ type = types.enum [ "working" "standby" "maintenance" "poweroff" ];
+ description = "Host status after agent startup.";
+ default = "working";
+ };
+ on_stop = mkOption {
+ type = types.enum [ "working" "standby" "maintenance" "poweroff" ];
+ description = "Host status after agent shutdown.";
+ default = "poweroff";
+ };
+ };
+
+ options.diagnostic =
+ mkEnableOption "Collect memory usage for the agent itself";
+ };
+ };
+ };
+
+ config = mkIf cfg.enable {
+ environment.systemPackages = with pkgs; [ mackerel-agent ];
+
+ environment.etc = {
+ "mackerel-agent/mackerel-agent.conf".source =
+ settingsFmt.generate "mackerel-agent.conf" cfg.settings;
+ "mackerel-agent/conf.d/api-key.conf".source = cfg.apiKeyFile;
+ };
+
+ services.mackerel-agent.settings = {
+ root = mkDefault "/var/lib/mackerel-agent";
+ pidfile = mkDefault "/run/mackerel-agent/mackerel-agent.pid";
+
+ # conf.d stores the symlink to cfg.apiKeyFile
+ include = mkDefault "/etc/mackerel-agent/conf.d/*.conf";
+ };
+
+ # upstream service file in https://git.io/JUt4Q
+ systemd.services.mackerel-agent = {
+ description = "mackerel.io agent";
+ after = [ "network-online.target" "nss-lookup.target" ];
+ wantedBy = [ "multi-user.target" ];
+ environment = {
+ MACKEREL_PLUGIN_WORKDIR = mkDefault "%C/mackerel-agent";
+ };
+ serviceConfig = {
+ DynamicUser = !cfg.runAsRoot;
+ PrivateTmp = mkDefault true;
+ CacheDirectory = "mackerel-agent";
+ ConfigurationDirectory = "mackerel-agent";
+ RuntimeDirectory = "mackerel-agent";
+ StateDirectory = "mackerel-agent";
+ ExecStart = "${pkgs.mackerel-agent}/bin/mackerel-agent supervise";
+ ExecStopPost = mkIf cfg.autoRetirement "${pkg.mackerel-agent}/bin/mackerel-agent retire -force";
+ ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
+ LimitNOFILE = mkDefault 65536;
+ LimitNPROC = mkDefault 65536;
+ };
+ restartTriggers = [
+ config.environment.etc."mackerel-agent/mackerel-agent.conf".source
+ ];
+ };
+ };
+}
diff --git a/pkgs/servers/monitoring/mackerel-agent/default.nix b/pkgs/servers/monitoring/mackerel-agent/default.nix
new file mode 100644
index 000000000000..c5548fafd348
--- /dev/null
+++ b/pkgs/servers/monitoring/mackerel-agent/default.nix
@@ -0,0 +1,42 @@
+{ stdenv, lib, buildGoModule, fetchFromGitHub, makeWrapper, iproute, nettools }:
+
+buildGoModule rec {
+ pname = "mackerel-agent";
+ version = "0.69.3";
+
+ src = fetchFromGitHub {
+ owner = "mackerelio";
+ repo = pname;
+ rev = "v${version}";
+ sha256 = "0jkvqqzk6wyjsdsmn2l2cdw8pjqzswlqb9p492czhgrfy065lrqp";
+ };
+
+ nativeBuildInputs = [ makeWrapper ];
+ checkInputs = lib.optionals (!stdenv.isDarwin) [ nettools ];
+ buildInputs = lib.optionals (!stdenv.isDarwin) [ iproute ];
+
+ vendorSha256 = "0kjky2mhs6dapnr4xpjpnbibp6y8r320igddplynsfsp8vwrfp7m";
+
+ subPackages = [ "." ];
+
+ buildFlagsArray = ''
+ -ldflags=
+ -X=main.version=${version}
+ -X=main.gitcommit=v${version}
+ '';
+
+ postInstall = ''
+ wrapProgram $out/bin/mackerel-agent \
+ --prefix PATH : "${lib.makeBinPath buildInputs}"
+ '';
+
+ doCheck = true;
+
+ meta = with lib; {
+ description = "System monitoring service for mackerel.io";
+ homepage = "https://github.com/mackerelio/mackerel-agent";
+ license = licenses.asl20;
+ maintainers = with maintainers; [ midchildan ];
+ platforms = platforms.all;
+ };
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 65e7818736d3..5a3696909418 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -16872,6 +16872,8 @@ in
labelImg = callPackage ../applications/science/machine-learning/labelimg { };
+ mackerel-agent = callPackage ../servers/monitoring/mackerel-agent { };
+
mailman = callPackage ../servers/mail/mailman/wrapped.nix { };
mailman-rss = callPackage ../development/python-modules/mailman-rss { };