Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package Mox as a service #677

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

themadbit
Copy link
Contributor

@themadbit themadbit commented Mar 27, 2025

  • Exposed Mox as a service to resolve issue(s).
  • Initial tests are running, and a local rebuild of NixOS with the module included runs just fine.
    i.e:

image

However, it fails to start/survive reboot with:

[themadbit@nixos:~]$ systemctl status mox
○ mox.service
     Loaded: loaded (/etc/systemd/system/mox.service; enabled; preset: ignored)
     Active: inactive (dead)

Mar 27 14:40:30 nixos systemd[1]: Dependency failed for mox.service.
Mar 27 14:40:30 nixos systemd[1]: mox.service: Job mox.service/start failed with result 'dependency'.

So, I'll leave this as a draft PR as I investigate further. Putting it out here in case you've had a similar headache and know the painkiller 😅

@themadbit themadbit changed the title Add Mox Project Package Mox as a service Mar 27, 2025
@themadbit
Copy link
Contributor Author

Update: I've run the test interactively for debugging and got access to the VM. The VM might not have a running nameserver, and I'd like to know if there's a way I can get one running as part of the machine configs before running the test(s). @eljamm, any pointers for further debugging?

ss of log:
image

@eljamm
Copy link
Contributor

eljamm commented Apr 3, 2025

Turns out that it was just a matter of modifying the service order since the dns configuration is not over by the time the mox-setup service tries to start. With the following patch, the setup completes, the server runs and the NixOS Test succeeds:

fix-mox-service-order.patch

---
 projects/Mox/module.nix | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/projects/Mox/module.nix b/projects/Mox/module.nix
index 43bb8d7..91d38d7 100644
--- a/projects/Mox/module.nix
+++ b/projects/Mox/module.nix
@@ -48,23 +48,26 @@
     systemd.services.mox-setup = {
       description = "Setup Mox Mail Server";
       wantedBy = [ "multi-user.target" ];
+      requires = [ "network-online.target" ];
+      after = [ "network-online.target" ];
       before = [ "mox.service" ];
       serviceConfig = {
         Type = "oneshot";
         RemainAfterExit = true;
+        User = "mox";
+        Group = "mox";
       };
       script = ''
         mkdir -p /var/lib/mox
         cd /var/lib/mox
         ${pkgs.mox}/bin/mox quickstart -hostname ${config.services.mox.hostname} ${config.services.mox.user}
-        chown -R mox:mox /var/lib/mox
       '';
     };

     systemd.services.mox = {
       wantedBy = [ "multi-user.target" ];
-      after = [ "network.target" "mox-setup.service" ];
-      requires = [ "mox-setup.service" ]; # This ensures mox-setup must succeed
+      after = [ "mox-setup.service" ];
+      requires = [ "mox-setup.service" ];
       serviceConfig = {
         WorkingDirectory = "/var/lib/mox";
         ExecStart = "${pkgs.mox}/bin/mox -config /var/lib/mox/config/mox.conf serve";
--
2.47.2

@themadbit
Copy link
Contributor Author

lol. Been on it since I left the office hour
I also figured out a way to get it working. But it is a very brute force way (but at least I got to learn lots of networking configs).

I added services.dnsmasq and enabled dnssec and had the mox-setup unit wait for that to be up and running.

@eljamm
Copy link
Contributor

eljamm commented Apr 3, 2025

I added services.dnsmasq and enabled dnssec and had the mox-setup unit wait for that to be up and running.

That sounds like it could be useful for a separate example config that uses DNSSEC, for those interested in that

@themadbit themadbit force-pushed the expose-mox branch 2 times, most recently from 530c09e to 077a483 Compare April 3, 2025 19:16
@themadbit
Copy link
Contributor Author

themadbit commented Apr 3, 2025

That sounds like it could be useful for a separate example config that uses DNSSEC, for those interested in that

Yess. I've stashed changes using dnsmasq to have a running service first, and then I can build upon it slowly. Ultimately a user should be able to specify all their desired config(s) which means supporting sconf. Nonetheless, the current implementation, if successfully built, a user with a working and configured domain can have Mox running and set up working emails.

@eljamm
Copy link
Contributor

eljamm commented Apr 4, 2025

I think the test fails in CI because it's not running with .driver or .driverInteractive as those give the VM an internet connection (which is required here for dns, I guess). To reproduce this, one can run:

nix build .#checks.x86_64-linux.projects/Mox/nixos/tests/basic

which will fail.

@themadbit
Copy link
Contributor Author

themadbit commented Apr 10, 2025

After many days of debugging, I got it working @eljamm
Instead of using static files, I worked through to get the quickstart command tested, as it allows better reproducibility than the alternative. I'll now clean up the commits (maybe stash some) and consider packaging it as a program.

I've refreshed my knowledge on DNSs quite much in this process. It was as good as it was heartbreaking 😅

Kindly give it a review. Thanks for the time you've dedicated to my issues.

@themadbit themadbit requested a review from eljamm April 10, 2025 04:03
Copy link
Contributor

@eljamm eljamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done @themadbit! This is shaping up really nicely.

{
options = {
services.mox = {
enable = lib.mkEnableOption "Enable Mox Mail Server";
Copy link
Contributor

@eljamm eljamm Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it easier for users to change/override the mox package if they need to

Suggested change
enable = lib.mkEnableOption "Enable Mox Mail Server";
enable = lib.mkEnableOption "Enable Mox Mail Server";
package = lib.mkPackageOption pkgs "mox" { };

pkgs,
...
}:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let
cfg = config.services.mox;
in

Comment on lines +31 to +33
environment.systemPackages = with pkgs; [
mox
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
environment.systemPackages = with pkgs; [
mox
];
environment.systemPackages = [
cfg.package
];

};
};

config = lib.mkIf config.services.mox.enable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config = lib.mkIf config.services.mox.enable {
config = lib.mkIf cfg.enable {

Comment on lines +67 to +68
mkdir -p /var/lib/mox
cd /var/lib/mox
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just use serviceConfig.stateDirectory which is relative to /var/lib

Suggested change
mkdir -p /var/lib/mox
cd /var/lib/mox

Comment on lines +44 to +46
users.groups.mox = {
name = "mox";
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
users.groups.mox = {
name = "mox";
};
users.groups.mox = { };

nodes = {
machine =
{ ... }:
rec {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rec is not necessary, here

Suggested change
rec {
{

Comment on lines +9 to +10
Mox is a modern, secure, and open-source email server that implements all modern email protocols.
It makes it easy for people and organizations to run their own email server in minutes using mox quickstart.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this is short and descriptive enough

Suggested change
Mox is a modern, secure, and open-source email server that implements all modern email protocols.
It makes it easy for people and organizations to run their own email server in minutes using mox quickstart.
Modern, secure, and open-source email server that implements all modern email protocols.

It makes it easy for people and organizations to run their own email server in minutes using mox quickstart.
'';
subgrants = [
"Mox"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Mox"
"Mox"
"Mox-API"
"Mox-Automation"

Comment on lines +21 to +27
25
80
143
443
587
993
5335
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all of these necessary? Can we add comments on why they're needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

NGI Project: Mox
2 participants