Setting up another domain as wildcard redirect

Hi everyone,

I was asked to share my solution to setting up another “virtual” domain for the mailserver setup in my selfprivacy installation. Here’s the code I used in my deployment.nix.

What does it do? I adds another domain to the mailserver setup, adds a a virtual map configuration to postfix that redirects all incoming mails to the actual domain equivalent, thus to existing user accounts.

{ lib, config, pkgs, ... }: {
  # The content below is static and belongs to this deployment only!
  # Do not copy this configuration file to another NixOS installation!

  system.stateVersion = lib.mkDefault "25.05";

  # --- Mailserver: Add domain(s) ---
  mailserver.domains = lib.mkForce ( [
      "your-actual-domain.xyz"
      "additional-domain.xyz"
    ]
  );

  # --- Postfix: set service configurations ---
  services.postfix = {
    enable = true; # to make sure it stays enabled
    virtualMapType = lib.mkForce "regexp"; # set map-type
    virtual = lib.mkForce ''/(.*)@additional-domain.xyz/ $1@your-actual-domain.xyz''; # define virtual alias-map as wildcard
  };

  # --- Mailserver: set indidivual redirects ---
  mailserver.extraVirtualAliases {
    "new-alias@your-actual-domain.xyz" = "user@your-actual-domain.xyz";
  }
}

So far it is running stable and without problems. Feedback is much appreciated - never implemented something like this.

2 Likes