How to Install Apache Solr on nixOS Latest

Apache Solr is a powerful, open-source search engine based on Lucene, designed to help you find the information you need quickly and efficiently. In this tutorial, we'll walk you through the process of installing Apache Solr on nixOS Latest.

Before you begin

Before you begin, you'll need a few things:

  • A nixOS Latest installation.
  • A compatible version of Java (Java 8 or higher).

If you're not sure if you have Java installed, you can check by running the following command:

java -version

If Java is not installed, you can install it using nix-env:

$ nix-env -iA nixos.jdk

Step 1: Download Apache Solr

First, download the latest version of Apache Solr from the official website at https://lucene.apache.org/solr/downloads.html. In this tutorial, we'll be using version 8.10.0.

You can download the tarball into your home directory using wget:

$ wget https://downloads.apache.org/lucene/solr/8.10.0/solr-8.10.0.tgz

Step 2: Extract the tarball

Next, we need to extract the tarball into the appropriate directory. We'll create a new directory called solr in /opt/ and extract the downloaded tarball into it.

$ sudo mkdir /opt/solr
$ sudo tar -xzf solr-8.10.0.tgz -C /opt/solr --strip-components=1

Step 3: Configure Solr

Now that Solr is installed, we need to configure it. We'll start by creating a new file called solr.service in /etc/nixos/services/:

$ sudo nano /etc/nixos/services/solr.service

Then, paste in the following code:

{ config, lib, systemd }:

let
  solrHome = "/var/lib/solr";
  solrBin = "/opt/solr/bin/solr";
  solrEnv = "SOLR_HOME=${solrHome} SOLR_PID_DIR=${solrHome}/run";
  solrArgs = "-Xms512m -Xmx512m";

in {
  options = {
    enable = mkOption {
      type = types.bool;
      default = false;
      description = "Enable the Solr service.";
    };

    port = mkOption {
      type = types.int;
      default = 8983;
      description = "The port number Solr should listen on.";
    };
  };

  config = mkIf config.services.solr.enable {
    systemd.services.solr = {
      description = "Solr search engine";
      after = [ "network.target" ];
      environment = {
        "JAVA_HOME" = "/run/current-system/sw";  
        "PATH" = "${lib.makeBinPath [ "/run/current-system/sw/bin" ]}";
        "SOLR_HTTP_PORT" = toString config.services.solr.port;
        "SOLR_JAVA_MEM" = solrArgs;
        "SOLR_OPTS" = "-Dsolr.allow.unsafe.resourceloading=true";
        "SOLR_HOME" = solrHome;
      };
      execStart = "${solrEnv} ${solrBin} start";
      execStop = "${solrEnv} ${solrBin} stop";
      restart = if config.services.solr.restartOnFailure then "on-failure" else "no";
      user = "solr";
      group = "solr";
      permissionsStartOnly = "true";  
      runtimeDirectory = "solr";
      privateTmp = "true";
      protectSystem = "full";
      readWritePaths = [
        solrHome
      ];  
    };

    users.users.solr = {
      description = "User for running Solr engine";
      uid = 201;
      home = solrHome;
      createHome = true;
      group = "solr";
      extraGroups = [ "systemd-journal" ];
      shell = "/bin/false";
    };

    fileSystems = {       
      "${solrHome}" = {
        device = "tmpfs";
        options = [ "mode=700" "uid=201" "gid=201" "size=50%" ];
      };
    };

    systemd.tmpfiles.rules = {
      "${solrHome}" = "d 700 solr solr";
      "${solrHome}/run" = "d 700 solr solr";
    };
  };
}

This configuration file specifies the options and settings we want for the Solr service, including the Solr port number, the Solr home directory, and the memory allocation.

Step 4: Configure nixOS to use Solr

Now that Solr is installed and configured, we need to tell nixOS to use it as a service.

To do this, add the following two lines to your /etc/nixos/configuration.nix file:

services.solr.enable = true;
services.solr.port = 8983;

Step 5: Restart and Start Solr service

Finally, we need to restart the nixOS system for the changes to take effect:

$ sudo nixos-rebuild switch

After the system has restarted, you can start the Solr service by running:

$ sudo systemctl start solr

You can verify that Solr is running by visiting http://localhost:8983/solr/ in your web browser.

Conclusion

Congratulations, you've successfully installed and configured Apache Solr on nixOS Latest! You can now use Solr to index and search your data with ease.