Tutorial: Installing Stalwart JMAP on NixOS Latest

Stalwart JMAP is an open-source email server that is designed to provide high-performance email services. It is a great solution for personal and business needs due to its flexibility, detailed logging, and effective management of email accounts. NixOS Latest is a modern, declarative operating system, based on the Nix package manager. This tutorial will guide you through the process of installing Stalwart JMAP on NixOS Latest.

Prerequisites

Before proceeding with the installation, you should have the following:

  • A server instance of NixOS Latest.
  • Basic knowledge of the Linux command line interface.
  • Root or sudoer access to the server instance.

Step-by-Step Guide

Follow these steps to install Stalwart JMAP on NixOS Latest:

Step 1: Install Nix

Since NixOS Latest is based on the Nix package manager, you need to make sure it is installed on your server instance. Use the following command to install Nix:

curl https://nixos.org/nix/install | sh

After successful installation, you should be able to use the nix command from the command line.

Step 2: Download the Stalwart JMAP package

Download the latest version of the Stalwart JMAP package from the official website at https://stalw.art/jmap.

wget https://stalw.art/jmap/stalwart-jmap.tar.gz

Once the download is completed, proceed to the next step.

Step 3: Extract the package

Use the following command to extract the downloaded package to the /opt directory:

sudo tar -xvzf stalwart-jmap.tar.gz -C /opt

This command will create a directory named stalwart-jmap under the /opt directory, which contains the Stalwart JMAP software.

Step 4: Create an environment file

Create a file named stalwart.env in the /opt/stalwart-jmap directory with the following contents:

STALWART_DB_PASS=<your db password>

Replace <your db password> with a password of your choice. This file will set the environment variables for Stalwart JMAP.

Step 5: Create a NixOS module

Create a file named stalwart-jmap.nix in the /etc/nixos/ directory with the following contents:

{ config, pkgs, lib, ... }:

let
  jmapDirectory = "/opt/stalwart-jmap";
  envFile = "${jmapDirectory}/stalwart.env";
in
{
  options = {
    jmapPort = {
      type = types.int;
      default = 80;
      description = "JMAP server port";
    };
  };

  config = {
    environment.etc."stalwart-jmap".text = ''
      #!/usr/bin/env sh

      . ${envFile}
      cd ${jmapDirectory}/server
      ./jmap-server.jar
    '';

    systemd.services."stalwart-jmap" = {
      description = "Stalwart JMAP server";
      after = [ "network-online.target" ];
      wantedBy = [ "multi-user.target" ];

      environmentFile = envFile;
      environmentVariables = {
        JAVA_HOME = "${pkgs.openjdk11}/jre";
      };

      restart = "always";
      restartSec = 10s;

      execStart = "${pkgs.nohup}/bin/nohup jmap-server &>> /var/log/stalwart-jmap.log &";
    };
    
    networking.firewall.allowedTCPPorts = [ config.jmapPort ];
  };
}

This module creates a configuration for the Stalwart JMAP server, integrates it with systemd, opens the specified port on the firewall, and sets up the environment.

Step 6: Enable the module

To enable the Stalwart JMAP module, add stalwart-jmap to the imports section in /etc/nixos/configuration.nix:

imports=
[
  # other imports ...
  /etc/nixos/stalwart-jmap.nix
];

Step 7: Deploy the configuration changes

Use the following command to deploy the configuration changes:

sudo nixos-rebuild switch

This command rebuilds the NixOS configuration and deploys the updated system.

Step 8: Start the JMAP server

Use the following command to start the Stalwart JMAP server:

sudo systemctl start stalwart-jmap

This command will start the server and redirect all logs to /var/log/stalwart-jmap.log.

Step 9: Verify the installation

Finally, to verify the installation, send an email to any existing email address using the JMAP server. Then, access the logging at /var/log/stalwart-jmap.log to check if the email was delivered.

Conclusion

This tutorial walked you through the process of installing Stalwart JMAP on NixOS Latest. If you followed the steps correctly, you should now have a fully functional Stalwart JMAP server running on your NixOS Latest instance. You can now configure your email accounts and start enjoying the benefits of this powerful open-source email server.