1 { config, lib, pkgs, ... }:
5 cfg = config.services.osquery;
6 dirname = path: with lib.strings; with lib.lists; concatStringsSep "/"
7 (init (splitString "/" (normalizePath path)));
9 # conf is the osquery configuration file used when the --config_plugin=filesystem.
10 # filesystem is the osquery default value for the config_plugin flag.
11 conf = pkgs.writeText "osquery.conf" (builtins.toJSON cfg.settings);
13 # flagfile is the file containing osquery command line flags to be
14 # provided to the application using the special --flagfile option.
15 flagfile = pkgs.writeText "osquery.flags"
16 (concatStringsSep "\n"
17 (mapAttrsToList (name: value: "--${name}=${value}")
18 # Use the conf derivation if not otherwise specified.
19 ({ config_path = conf; } // cfg.flags)));
21 osqueryi = pkgs.runCommand "osqueryi" { nativeBuildInputs = [ pkgs.makeWrapper ]; } ''
23 makeWrapper ${pkgs.osquery}/bin/osqueryi $out/bin/osqueryi \
24 --add-flags "--flagfile ${flagfile} --disable-database"
28 options.services.osquery = {
29 enable = mkEnableOption "osqueryd daemon";
34 Configuration to be written to the osqueryd JSON configuration file.
35 To understand the configuration format, refer to https://osquery.readthedocs.io/en/stable/deployment/configuration/#configuration-components.
46 Attribute set of flag names and values to be written to the osqueryd flagfile.
47 For more information, refer to https://osquery.readthedocs.io/en/stable/installation/cli-flags.
50 config_refresh = "10";
54 freeformType = attrsOf str;
56 database_path = mkOption {
57 default = "/var/lib/osquery/osquery.db";
59 description = "Path used for the database file.";
62 logger_path = mkOption {
63 default = "/var/log/osquery";
65 description = "Base directory used for logging.";
69 default = "/run/osquery/osqueryd.pid";
71 description = "Path used for pid file.";
79 config = mkIf cfg.enable {
80 environment.systemPackages = [ osqueryi ];
81 systemd.services.osqueryd = {
82 after = [ "network.target" "syslog.service" ];
83 description = "The osquery daemon";
85 ExecStart = "${pkgs.osquery}/bin/osqueryd --flagfile ${flagfile}";
86 PIDFile = cfg.flags.pidfile;
87 LogsDirectory = cfg.flags.logger_path;
88 StateDirectory = dirname cfg.flags.database_path;
91 wantedBy = [ "multi-user.target" ];
93 systemd.tmpfiles.settings."10-osquery".${dirname (cfg.flags.pidfile)}.d = {