LnL7 / nix-darwin

nix modules for darwin
MIT License
3.18k stars 456 forks source link

The PATH is not correct when config.environment.systemPath is used in launchd.user.agents.<name>.path #406

Open azuwis opened 2 years ago

azuwis commented 2 years ago

Use this nix-darwin module to test:

{ config, lib, pkgs, ... }:

{
  launchd.user.agents.test = {
    path = [ config.environment.systemPath ];
    serviceConfig.ProgramArguments = [
      "${pkgs.coreutils}/bin/env"
    ];
    serviceConfig.RunAtLoad = true;
    serviceConfig.StandardOutPath = "/tmp/test.log";
  };
}

The generated launchagent ~/Library/LaunchAgents/org.nixos.test.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>PATH</key>
        <string>$HOME/.nix-profile/bin:/etc/profiles/per-user/$USER/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/homebrew/bin</string>
    </dict>
    <key>Label</key>
    <string>org.nixos.test</string>
    <key>ProgramArguments</key>
    <array>
        <string>/nix/store/gsni67vwixsfrmklnchins4ikr7191wk-coreutils-9.0/bin/env</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/test.log</string>
</dict>
</plist>

And check /tmp/test.log:

PATH=$HOME/.nix-profile/bin:/etc/profiles/per-user/$USER/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/homebrew/bin
XPC_SERVICE_NAME=org.nixos.test
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.31vWzX4aIV/Listeners
XPC_FLAGS=0x0
LOGNAME=azuwis
USER=azuwis
HOME=/Users/azuwis
SHELL=/bin/zsh
TMPDIR=/var/folders/rl/rncj5l655ms5xrdzt8wt09dh0000gn/T/

$HOME and $USER are not expended in PATH, launchd does not support expending variables.

Services that run as normal user, such as yabai, skhd, are all set in this way, if they try to call commands in $HOME/.nix-profile/bin, they will fail.

In this case, PATH should be expended to /Users/azuwis/.nix-profile/bin:/etc/profiles/per-user/azuwis/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/homebrew/bin before putting in ~/Library/LaunchAgents/org.nixos.test.plist.

gitraar commented 5 days ago

I was going to submit a new issue about this and found this open one from almost three years ago.