<commit_before>{ config, pkgs, ... }:

with pkgs.lib;

let
  cfg = config.services.elasticsearch;

  es_home = "/var/lib/elasticsearch";

  configFile = pkgs.writeText "elasticsearch.yml" ''
    network.host: ${cfg.host}
    network.port: ${cfg.port}
    network.tcp.port: ${cfg.tcp_port}
    cluster.name: ${cfg.cluster_name}
    ${cfg.extraConf}
  '';

in {

  ###### interface

  options.services.elasticsearch = {
    enable = mkOption {
      description = "Whether to enable elasticsearch";
      default = false;
      type = types.uniq types.bool;
    };

    host = mkOption {
      description = "Elasticsearch listen address";
      default = "127.0.0.1";
      type = types.str;
    };

    port = mkOption {
      description = "Elasticsearch port to listen for HTTP traffic";
      default = "9200";
      type = types.str;
    };

    tcp_port = mkOption {
      description = "Elasticsearch port for the node to node communication";
      default = "9300";
      type = types.str;
    };

    cluster_name = mkOption {
      description = "Elasticsearch name that identifies your cluster for auto-discovery";
      default = "elasticsearch";
      type = types.str;
    };

    extraConf = mkOption {
      description = "Extra configuration for elasticsearch";
      default = "";
      type = types.str;
      example = ''
        node.name: "elasticsearch"
        node.master: true
        node.data: false
        index.number_of_shards: 5
        index.number_of_replicas: 1
      '';
    };

    logging = mkOption {
      description = "Elasticsearch logging configuration";
      default = ''
        rootLogger: INFO, console
        logger:
          action: INFO
          com.amazonaws: WARN
        appender:
          console:
            type: console
            layout:
              type: consolePattern
              conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"
      '';
      type = types.str;
    };
  };

  ###### implementation

  config = mkIf cfg.enable {
    environment.etc = [
      { source = configFile;
        target = "elasticsearch/elasticsearch.yml"; }
      { source = pkgs.writeText "logging.yml" cfg.logging;
        target = "elasticsearch/logging.yml"; }
    ];

    systemd.services.elasticsearch = {
      description = "Elasticsearch daemon";
      wantedBy = [ "multi-user.target" ];
      after = [ "network-interfaces.target" ];
      environment = { ES_HOME = es_home; };
      serviceConfig = {
        ExecStart = "${pkgs.elasticsearch}/bin/elasticsearch -f -Des.path.conf=/etc/elasticsearch";
        User = "elasticsearch";
      };
    };

    environment.systemPackages = [ pkgs.elasticsearch ];

    users.extraUsers = singleton {
      name = "elasticsearch";
      uid = config.ids.uids.elasticsearch;
      description = "Elasticsearch daemon user";
      home = es_home;
      createHome = true;
    };
  };
}<commit_msg>nixos/elasticsearch: Make port an integer, add dataDir option, make pure<commit_after>{ config, pkgs, ... }:

with pkgs.lib;

let
  cfg = config.services.elasticsearch;

  esConfig = ''
    network.host: ${cfg.host}
    network.port: ${toString cfg.port}
    network.tcp.port: ${toString cfg.tcp_port}
    cluster.name: ${cfg.cluster_name}
    ${cfg.extraConf}
  '';

  configDir = pkgs.buildEnv {
    name = "elasticsearch-config";
    paths = [
      (pkgs.writeTextDir "elasticsearch.yml" esConfig)
      (pkgs.writeTextDir "logging.yml" cfg.logging)
    ];
  };

in {

  ###### interface

  options.services.elasticsearch = {
    enable = mkOption {
      description = "Whether to enable elasticsearch";
      default = false;
      type = types.uniq types.bool;
    };

    host = mkOption {
      description = "Elasticsearch listen address";
      default = "127.0.0.1";
      type = types.str;
    };

    port = mkOption {
      description = "Elasticsearch port to listen for HTTP traffic";
      default = 9200;
      type = types.int;
    };

    tcp_port = mkOption {
      description = "Elasticsearch port for the node to node communication";
      default = 9300;
      type = types.int;
    };

    cluster_name = mkOption {
      description = "Elasticsearch name that identifies your cluster for auto-discovery";
      default = "elasticsearch";
      type = types.str;
    };

    extraConf = mkOption {
      description = "Extra configuration for elasticsearch";
      default = "";
      type = types.str;
      example = ''
        node.name: "elasticsearch"
        node.master: true
        node.data: false
        index.number_of_shards: 5
        index.number_of_replicas: 1
      '';
    };

    logging = mkOption {
      description = "Elasticsearch logging configuration";
      default = ''
        rootLogger: INFO, console
        logger:
          action: INFO
          com.amazonaws: WARN
        appender:
          console:
            type: console
            layout:
              type: consolePattern
              conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"
      '';
      type = types.str;
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/elasticsearch";
      description = ''
        Data directory for elasticsearch.
      '';
    };
  };

  ###### implementation

  config = mkIf cfg.enable {
    systemd.services.elasticsearch = {
      description = "Elasticsearch daemon";
      wantedBy = [ "multi-user.target" ];
      after = [ "network-interfaces.target" ];
      environment = { ES_HOME = cfg.dataDir; };
      serviceConfig = {
        ExecStart = "${pkgs.elasticsearch}/bin/elasticsearch -f -Des.path.conf=${configDir}";
        User = "elasticsearch";
      };
      preStart = ''
        mkdir -m 0700 -p ${cfg.dataDir}
        if [ "$(id -u)" = 0 ]; then chown -R elasticsearch ${cfg.dataDir}; fi
      '';
    };

    environment.systemPackages = [ pkgs.elasticsearch ];

    users.extraUsers = singleton {
      name = "elasticsearch";
      uid = config.ids.uids.elasticsearch;
      description = "Elasticsearch daemon user";
      home = cfg.dataDir;
    };
  };
}
{'fraction_non_alphanumeric': 0.1242603550295858, 'fraction_numerical': 0.008382642998027613, 'mean_word_length': 2.177545691906005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3393141', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1776, 'n_words': 408}