LnL7 / nix-darwin

nix modules for darwin
MIT License
3.21k stars 459 forks source link

Waiting for the nix store to mount before attempting to run launchd daemons that depend on nix derivations #1043

Open will-lol opened 3 months ago

will-lol commented 3 months ago

The nix-daemon launchd daemon is configured to use the /bin/wait4path utility to ensure the nix store has been mounted before it runs its nix derivation.

I recently ran into an issue with the yabai scripting addition daemon where the daemon would attempt to access a script (located in the nix store) before the store had been mounted. I'd imagine that the daemon works sometimes and for some people, but I reckon we should fix this.

I added the following to my darwin configuration to fix this issue:

launchd.daemons.yabai-sa = let
  yabaiScript = pkgs.writeShellScript "yabai-sa" ''
    ${pkgs.yabai}/bin/yabai --load-sa
  '';
in {
  script = pkgs.lib.mkForce "";
  serviceConfig.RunAtLoad = true;
  serviceConfig.KeepAlive.SuccessfulExit = false;
  serviceConfig.ProgramArguments = [ "/bin/sh" "-c" "/bin/wait4path ${yabaiScript} && exec ${yabaiScript}" ];
};

I think that we should either:

I'd like to hear all of your thoughts. I'm happy to write a PR for this also, but thought some discussion would be good.

Enzime commented 3 months ago

My preference is the latter, if you could make a PR that changes launchd.daemons to automatically use wait4path that would be appreciated.

In the same PR, you could probably drop all explicit/manual usage of wait4path in nix-darwin

will-lol commented 3 months ago

Sounds good to me! I’ll tackle that next weekend.

Samasaur1 commented 3 months ago

Would also address https://github.com/LnL7/nix-darwin/issues/774 and https://github.com/LnL7/nix-darwin/issues/563

Samasaur1 commented 3 months ago

(and possibly other issues as well)