在大量使用命令行工具以后,有那么一些命令,我们需要让它们开机自启并在后台运行,比如 chunkwm, syncthing。这种需求可以通过 nix-darwin 配置:

launchd.user.agents = {
    syncthing = {
      serviceConfig.ProgramArguments = [ "${pkgs.syncthing}/bin/syncthing" "-no-browser" "-no-restart" ];
      serviceConfig.EnvironmentVariables = {
        HOME = "/Users/${(builtins.getEnv "USER")}";
        STNOUPGRADE = "1"; # disable spammy automatic upgrade check
      };
      serviceConfig.KeepAlive = true;
      serviceConfig.ProcessType = "Background";
      serviceConfig.StandardOutPath = "/Users/${(builtins.getEnv "USER")}/Library/Logs/Syncthing.log";
      serviceConfig.StandardErrorPath = "/Users/${(builtins.getEnv "USER")}/Library/Logs/Syncthing-Errors.log";
      serviceConfig.LowPriorityIO = true;
    };
  };

如果没有使用 nix-darwin,可以参考下方由 nix-darwin 创建的 plist 文件手动创建。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>EnvironmentVariables</key>
    <dict>
        <key>HOME</key>
        <string>/Users/bryan</string>
        <key>STNOUPGRADE</key>
        <string>1</string>
    </dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>org.nixos.syncthing</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>ProcessType</key>
    <string>Background</string>
    <key>ProgramArguments</key>
    <array>
        <string>/nix/store/qwk89idixnahz6i247cyz4xiqfyfywxk-syncthing/bin/syncthing</string>
        <string>-no-browser</string>
        <string>-no-restart</string>
    </array>
    <key>StandardErrorPath</key>
    <string>/Users/bryan/Library/Logs/Syncthing-Errors.log</string>
    <key>StandardOutPath</key>
    <string>/Users/bryan/Library/Logs/Syncthing.log</string>
</dict>
</plist>

把日志放在 Logs 文件夹里是为了方便系统自带的 Console.app 读取。