what does launchctl config user path do?
Asked Answered
M

1

16

I was having a strange problem with PATH environment variable in MacOS that I spent several hours to debug:

  • Some time ago, when I was trying to fix the issue IntelliJ terminal PATH variable not the same with iTerm, I followed an online article and executed this:

    sudo launchctl config user path $PATH

  • Apparently this command sets and persists the value of PATH variable at that moment of time somewhere and that variable is loaded even before my shell is loaded whenever I start a new zsh session. Only recently I recognized this issue because I removed some paths location setting in my zshrc and the PATH variable still didn't reflect

  • My question is where does that command store the PATH variable value? and how does it load that value before my shell is loaded?

(For people who wonder how I fixed the issue: I executed the command again to set path to empty value: sudo launchctl config user path '')

Mate answered 1/8, 2018 at 14:49 Comment(0)
D
22

The sudo launchctl config user path <...> command updates /private/var/db/com.apple.xpc.launchd/config/user.plist:

$ cat /private/var/db/com.apple.xpc.launchd/config/user.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>PathEnvironmentVariable</key>
    <string>/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
</plist>

Tested on my system, which is currently macOS 14.2.1 (AppleSilicon). You can replace user with system to operate on the system-wide preferences. Both require sudo, oddly enough.

You can query launchd's current custom PATH setting (will return an empty string if you haven't configured one) with:

launchctl getenv PATH

You can query the default PATH by executing:

sysctl -n user.cs_path

To undo any customizations, you can issue:

sudo defaults delete /private/var/db/com.apple.xpc.launchd/config/user.plist PathEnvironmentVariable

(requires a reboot to take effect)

Denishadenison answered 28/12, 2021 at 17:29 Comment(5)
out of curiosity, how did you find out about this? there are not many documentation online about launchctlMate
@PhuongNguyen: I brute forced it: I added a nonexistent dummy path using the command, e.g. xyzzy and then used ripgrep to search the entire filesystem for that string. Took a while but it found it.Denishadenison
Do you know if there a way to make it persist across reboots?Trincomalee
@szx: It does persist across reboots, but unfortunately it doesn't apply to apps which are automatically restarted after reboots. See apple.stackexchange.com/a/198282.Valentia
launchctl getenv PATH doesn't return the value set with launchctl config. The only way to see this value that I found was to do launchctrl dumpstate and it will appear in the environment section under type = login entry.Kwei

© 2022 - 2024 — McMap. All rights reserved.