2021-01-29 14:21:25 +05:00
|
|
|
|
Setup Guide
|
|
|
|
|
===========
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Mail servers can be a tricky thing to set up. This guide is supposed to
|
|
|
|
|
run you through the most important steps to achieve a 10/10 score on
|
2021-03-13 21:09:23 +05:00
|
|
|
|
`<https://mail-tester.com>`_.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
What you need is:
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
- a server running NixOS with a public IP
|
|
|
|
|
- a domain name.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
In the following, we consider a server with the public IP ``1.2.3.4``
|
|
|
|
|
and the domain ``example.com``.
|
|
|
|
|
|
|
|
|
|
First, we will set the minimum DNS configuration to be able to deploy
|
|
|
|
|
an up and running mail server. Once the server is deployed, we could
|
|
|
|
|
then set all DNS entries required to send and receive mails on this
|
|
|
|
|
server.
|
|
|
|
|
|
|
|
|
|
Setup DNS A record for server
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Add a DNS record to the domain ``example.com`` with the following
|
|
|
|
|
entries
|
|
|
|
|
|
|
|
|
|
==================== ===== ==== =============
|
|
|
|
|
Name (Subdomain) TTL Type Value
|
|
|
|
|
==================== ===== ==== =============
|
|
|
|
|
``mail.example.com`` 10800 A ``1.2.3.4``
|
|
|
|
|
==================== ===== ==== =============
|
|
|
|
|
|
|
|
|
|
You can check this with
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
$ ping mail.example.com
|
|
|
|
|
64 bytes from mail.example.com (1.2.3.4): icmp_seq=1 ttl=46 time=21.3 ms
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
Note that it can take a while until a DNS entry is propagated. This
|
|
|
|
|
DNS entry is required for the Let's Encrypt certificate generation
|
|
|
|
|
(which is used in the below configuration example).
|
|
|
|
|
|
|
|
|
|
Setup the server
|
|
|
|
|
~~~~~~~~~~~~~~~~
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
The following describes a server setup that is fairly complete. Even
|
2023-01-01 22:38:07 +05:00
|
|
|
|
though there are more possible options (see the `NixOS Mailserver
|
2023-07-22 02:55:54 +05:00
|
|
|
|
options documentation <options.html>`_), these should be the most
|
2023-01-01 22:38:07 +05:00
|
|
|
|
common ones.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
.. code:: nix
|
|
|
|
|
|
2023-01-01 22:38:07 +05:00
|
|
|
|
{ config, pkgs, ... }: {
|
2020-07-03 00:02:47 +05:00
|
|
|
|
imports = [
|
|
|
|
|
(builtins.fetchTarball {
|
2023-01-01 22:38:07 +05:00
|
|
|
|
# Pick a release version you are interested in and set its hash, e.g.
|
|
|
|
|
url = "https://gitlab.com/simple-nixos-mailserver/nixos-mailserver/-/archive/nixos-23.05/nixos-mailserver-nixos-23.05.tar.gz";
|
|
|
|
|
# To get the sha256 of the nixos-mailserver tarball, we can use the nix-prefetch-url command:
|
|
|
|
|
# release="nixos-23.05"; nix-prefetch-url "https://gitlab.com/simple-nixos-mailserver/nixos-mailserver/-/archive/${release}/nixos-mailserver-${release}.tar.gz" --unpack
|
2020-07-03 00:02:47 +05:00
|
|
|
|
sha256 = "0000000000000000000000000000000000000000000000000000";
|
|
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
mailserver = {
|
|
|
|
|
enable = true;
|
2021-03-13 21:09:23 +05:00
|
|
|
|
fqdn = "mail.example.com";
|
|
|
|
|
domains = [ "example.com" ];
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
# A list of all login accounts. To create the password hashes, use
|
2022-11-28 00:14:22 +05:00
|
|
|
|
# nix-shell -p mkpasswd --run 'mkpasswd -sm bcrypt'
|
2020-07-03 00:02:47 +05:00
|
|
|
|
loginAccounts = {
|
2023-01-01 22:38:07 +05:00
|
|
|
|
"user1@example.com" = {
|
|
|
|
|
hashedPasswordFile = "/a/file/containing/a/hashed/password";
|
|
|
|
|
aliases = ["postmaster@example.com"];
|
|
|
|
|
};
|
|
|
|
|
"user2@example.com" = { ... };
|
2020-07-03 00:02:47 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# Use Let's Encrypt certificates. Note that this needs to set up a stripped
|
|
|
|
|
# down nginx and opens port 80.
|
2023-02-15 17:15:09 +05:00
|
|
|
|
certificateScheme = "acme-nginx";
|
2020-12-15 20:01:40 +05:00
|
|
|
|
};
|
2023-01-01 22:38:07 +05:00
|
|
|
|
security.acme.acceptTerms = true;
|
|
|
|
|
security.acme.defaults.email = "security@example.com";
|
2020-12-15 20:01:40 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
After a ``nixos-rebuild switch`` your server should be running all
|
|
|
|
|
mail components.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Setup all other DNS requirements
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Set rDNS (reverse DNS) entry for server
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Wherever you have rented your server, you should be able to set reverse
|
|
|
|
|
DNS entries for the IP’s you own. Add an entry resolving ``1.2.3.4``
|
2022-07-10 12:25:43 +05:00
|
|
|
|
to ``mail.example.com``.
|
|
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
|
|
We don't recommend setting up a mail server if you are not able to
|
|
|
|
|
set a reverse DNS on your public IP because sent emails would be
|
|
|
|
|
mostly marked as spam. Note that many residential ISP providers
|
|
|
|
|
don't allow you to set a reverse DNS entry.
|
2020-10-21 04:45:10 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
You can check this with
|
2020-10-21 04:45:10 +05:00
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
$ nix-shell -p bind --command "host 1.2.3.4"
|
|
|
|
|
4.3.2.1.in-addr.arpa domain name pointer mail.example.com.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Note that it can take a while until a DNS entry is propagated.
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Set a ``MX`` record
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Add a ``MX`` record to the domain ``example.com``.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
================ ==== ======== =================
|
|
|
|
|
Name (Subdomain) Type Priority Value
|
|
|
|
|
================ ==== ======== =================
|
|
|
|
|
example.com MX 10 mail.example.com
|
|
|
|
|
================ ==== ======== =================
|
2020-10-21 04:45:10 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
You can check this with
|
2020-10-21 04:45:10 +05:00
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
$ nix-shell -p bind --command "host -t mx example.com"
|
|
|
|
|
example.com mail is handled by 10 mail.example.com.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Note that it can take a while until a DNS entry is propagated.
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Set a ``SPF`` record
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-05-23 23:15:38 +05:00
|
|
|
|
Add a `SPF <https://en.wikipedia.org/wiki/Sender_Policy_Framework>`_
|
2021-03-13 21:09:23 +05:00
|
|
|
|
record to the domain ``example.com``.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
================ ===== ==== ================================
|
|
|
|
|
Name (Subdomain) TTL Type Value
|
|
|
|
|
================ ===== ==== ================================
|
|
|
|
|
example.com 10800 TXT `v=spf1 a:mail.example.com -all`
|
|
|
|
|
================ ===== ==== ================================
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
You can check this with
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
$ nix-shell -p bind --command "host -t TXT example.com"
|
|
|
|
|
example.com descriptive text "v=spf1 a:mail.example.com -all"
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Note that it can take a while until a DNS entry is propagated.
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Set ``DKIM`` signature
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
On your server, the ``opendkim`` systemd service generated a file
|
|
|
|
|
containing your DKIM public key in the file
|
|
|
|
|
``/var/dkim/example.com.mail.txt``. The content of this file looks
|
|
|
|
|
like
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
mail._domainkey IN TXT "v=DKIM1; k=rsa; s=email; p=<really-long-key>" ; ----- DKIM mail for domain.tld
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
where ``really-long-key`` is your public key.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Based on the content of this file, we can add a ``DKIM`` record to the
|
|
|
|
|
domain ``example.com``.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
=========================== ===== ==== ==============================
|
|
|
|
|
Name (Subdomain) TTL Type Value
|
|
|
|
|
=========================== ===== ==== ==============================
|
|
|
|
|
mail._domainkey.example.com 10800 TXT ``v=DKIM1; p=<really-long-key>``
|
|
|
|
|
=========================== ===== ==== ==============================
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
You can check this with
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
$ nix-shell -p bind --command "host -t txt mail._domainkey.example.com"
|
|
|
|
|
mail._domainkey.example.com descriptive text "v=DKIM1;p=<really-long-key>"
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Note that it can take a while until a DNS entry is propagated.
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Set a ``DMARC`` record
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
Add a ``DMARC`` record to the domain ``example.com``.
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
======================== ===== ==== ====================
|
|
|
|
|
Name (Subdomain) TTL Type Value
|
|
|
|
|
======================== ===== ==== ====================
|
|
|
|
|
_dmarc.example.com 10800 TXT ``v=DMARC1; p=none``
|
|
|
|
|
======================== ===== ==== ====================
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
You can check this with
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
$ nix-shell -p bind --command "host -t TXT _dmarc.example.com"
|
2021-04-13 01:32:47 +05:00
|
|
|
|
_dmarc.example.com descriptive text "v=DMARC1; p=none"
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Note that it can take a while until a DNS entry is propagated.
|
|
|
|
|
|
2021-03-13 21:09:23 +05:00
|
|
|
|
|
2021-01-29 14:24:35 +05:00
|
|
|
|
Test your Setup
|
|
|
|
|
~~~~~~~~~~~~~~~
|
2020-07-03 00:02:47 +05:00
|
|
|
|
|
|
|
|
|
Write an email to your aunt (who has been waiting for your reply far too
|
|
|
|
|
long), and sign up for some of the finest newsletters the Internet has.
|
|
|
|
|
Maybe you want to sign up for the `SNM Announcement
|
|
|
|
|
List <https://www.freelists.org/list/snm>`__?
|
|
|
|
|
|
|
|
|
|
Besides that, you can send an email to
|
|
|
|
|
`mail-tester.com <https://www.mail-tester.com/>`__ and see how you
|
|
|
|
|
score, and let `mxtoolbox.com <http://mxtoolbox.com/>`__ take a look at
|
|
|
|
|
your setup, but if you followed the steps closely then everything should
|
|
|
|
|
be awesome!
|