Skip to content

Conversation

@multivac61
Copy link
Contributor

Add Darwin (macOS) support for comin

This PR adds full Darwin/macOS support to comin, allowing it to manage nix-darwin configurations in addition to NixOS systems.

What's Changed

Core Darwin Support

  • Added runtime platform detection using runtime.GOOS to switch between Linux and Darwin implementations
  • Implemented Darwin-specific service management using launchctl instead of systemctl
  • Added Darwin machine ID detection using system_profiler to get the Hardware UUID
  • Support for darwinConfigurations alongside existing nixosConfigurations

Darwin Module

  • Created nix/darwin-module.nix that provides darwinModules.comin for nix-darwin users
  • Uses launchd.daemons instead of systemd.services for service management
  • Proper PATH configuration for launchd environment restrictions
  • Automatic state directory setup and permissions

Darwin-Specific Activation

  • Darwin uses activate and activate-user scripts instead of switch-to-configuration
  • Platform-specific derivation paths and configuration evaluation
  • Robust service restart mechanism to prevent hanging during self-updates

Service Restart Improvements

  • Fixed the issue where comin would hang when trying to restart itself during Darwin activation
  • Implemented a flag-based restart mechanism where comin exits cleanly after deployment and lets launchd restart it automatically
  • No more arbitrary timeouts or race conditions

Path Fixes

  • Used full paths for system commands (/usr/sbin/system_profiler, /bin/launchctl, etc.) to work with launchd's restricted PATH environment
  • Fixed machine ID detection and service management commands

Usage

Darwin users can now add comin to their nix-darwin flake:

{
  inputs = {
    comin.url = "github:multivac61/comin";  # or upstream after merge
    # ... other inputs
  };

  outputs = { self, nix-darwin, comin, ... }: {
    darwinConfigurations."hostname" = nix-darwin.lib.darwinSystem {
      modules = [
        comin.darwinModules.comin  # Use darwinModules, not nixosModules
        {
          networking.hostName = "hostname";
          services.comin = {
            enable = true;
            remotes = [{
              name = "origin";
              url = "https://github.com/user/darwin-config.git";
              branches.main.name = "main";
            }];
          };
        }
      ];
    };
  };
}

Testing

I've been running it on a couple of aarch64-darwin computers running one of the later macOS versions.

Copy link
Owner

@nlewo nlewo left a comment

Choose a reason for hiding this comment

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

I definitely didn't know the existence of Nix darwin! I'm really happy to see that comin could also be used to manage such systems 🎉

So, thank you very much to bring this contribution!

In the same idea, several people asked for the support of home-manager, so we can keep in mind this feature request while working on this patch: i'm pretty sure what we are doing here could be usefule for a home-manager support implementation.

A last thing is that i would like to first merge the MR #88 which is an important refactoring.

@nlewo nlewo mentioned this pull request Jun 19, 2025
- Replace timeout-based restart with flag-based clean exit
- Create restart flag when service config changes
- Exit cleanly after deployment completes if flag exists
- Let launchd handle automatic restart via KeepAlive
- Prevents hanging during self-updates on Darwin
- Remove direct runtime.GOOS usage in utility functions
- Add configurationAttr parameter propagated from initialization
- Create separate deployDarwin and deployLinux functions
- Simplify restart mechanism to just exit and let service manager
restart
- Add comprehensive tests for Darwin code paths
- Require explicit hostname configuration (no fallbacks)
- Update both NixOS and Darwin modules with hostname assertions
@multivac61 multivac61 requested a review from nlewo July 1, 2025 15:36
@multivac61
Copy link
Contributor Author

Hey @nlewo sorry to keep you waiting, I just came back to address the issues you laid out. Hope this is better.

Copy link
Owner

@nlewo nlewo left a comment

Choose a reason for hiding this comment

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

Minor comments but lgtm!

@nlewo
Copy link
Owner

nlewo commented Jul 10, 2025

I added a minor fix in the nix code and run go fmt.

Thank you!

@nlewo
Copy link
Owner

nlewo commented Jul 10, 2025

Thank you!

@nlewo nlewo merged commit aeb11d4 into nlewo:main Jul 10, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants