skhd icon indicating copy to clipboard operation
skhd copied to clipboard

yabai commands only work when skhd is run from terminal emulator

Open mlegls opened this issue 2 years ago • 16 comments

I installed the release version of both skhd & yabai from Homebrew on an M1 Macbook Air, running MacOS Monterey 12.3.1.

When running skhd from brew services start skhd, no yabai command shortcut works at all, but all other command shortcuts work fine.

However, the yabai commands themselves work fine from any terminal emulator, running either zsh or bash. Also, if I just start skhd directly from a terminal emulator rather than brew services, every yabai command shortcut works correctly.

mlegls avatar Apr 08 '22 08:04 mlegls

Here are my skhdrc, yabairc, and zshrc, in case they're helpful at all

skhdrc: https://gist.github.com/mlegls/fb26506c6534d1b41ea785a2b3a35631 yabairc: https://gist.github.com/mlegls/482df0e4233d5c990525c25164d66e52 zshrc: https://gist.github.com/mlegls/028fa66ebfdabae08da00d19729d4c17

mlegls avatar Apr 08 '22 08:04 mlegls

In case someone else has the issue and this doesn't get an actual solution, my temporary workaround is just to use pm2 instead of brew services to run skhd as a daemon, and to start it on startup.

mlegls avatar Apr 08 '22 08:04 mlegls

This happens to me too on a macbook m1 max.

marcelarie avatar Jun 22 '22 18:06 marcelarie

any updates here? this is pretty annoying

macalinao avatar Jun 29 '22 17:06 macalinao

pm2 was not working properly for me, so my workaround is the following:

  1. Open Automator
  2. Create an Application
  3. Search for "Run Shell Script" on the multiple options
  4. Write a script that adds the brew binaries to the path and runs skhd like this:
export PATH=/opt/homebrew/bin:$PATH;
skhd
  1. Save the Automator application on /Applications or wherever
  2. Go to System Preferences > Security and Privacy > Accesability
  3. Open the lock to make changes.
  4. Add the created Automator app to the list and enable it.
  5. Go to System Preferences > Users & Groups > Login Items
  6. Add the created Automator app there

🤯

Now after ALL this nonsense skhd is running on each startup, and yabai commands work. I tried launchctl but yabai commands did not work.

On the first try, instead of adding /opt/homebrew/bin to the PATH, Automator did not found skhd so I used directly the location of just the skhd binary (/opt/homebrew/bin/skhd) and this worked but, again, no yabai commands.

After adding the full path of homebrew binaries to the script, both work. I just say this because it might be the reason that brew service start does not, maybe it does not find yabai, but that does not make any sense.

Anyway, hope there is a solution for this soon, I miss my Linux.

Note: Automator shows a small gear on the top bar when an app is running, if you want to hide that you can suppress the output of skhd in the script with &>/dev/null like this:

export PATH=/opt/homebrew/bin:$PATH;
skhd &>/dev/null;

marcelarie avatar Jul 08 '22 21:07 marcelarie

@marcelarie Thank you very much for this. I kinda gave up on the idea making it work with pm2 myself.

Small note on your note. The code block to prevent the gear icon didn't work for me. Instead, this worked:

export PATH=/opt/homebrew/bin:$PATH;
skhd &>/dev/null &

Gigibeau avatar Jul 08 '22 22:07 Gigibeau

This issue is probably because skhd doesnt have the required permission. In a terminal, it may work, because the terminal was explicitly given the permission. Try opening the path of which skhd, and dragging it to the Security & Privacy->Privacy->Accessibility area in system preferences to grant it the permission.

es183923 avatar Aug 02 '22 01:08 es183923

@es183923 I gave explicit permissions as you said and the issue persists.

marcelarie avatar Aug 04 '22 19:08 marcelarie

Did you try stopping the brew service, and starting it again? (brew services stop skhd && brew services start skhd)

es183923 avatar Aug 04 '22 19:08 es183923

brew services stop skhd && brew services start skhd

Before trying it, the service was not running. I use the login item with Automator as I explained before, because some commands don't work if I run the service with brew.

So what I did is add the result of which skhd, in my case being /opt/homebrew/bin/skhd, to "...Privacy->Accessibility", as you explained, and then I started the service with brew. Of course, before all this, I deleted the Automator app from the login items. I restarted the laptop and skhd runs but without multiple commands, so it is the same.

Anyway, I tried running stop and start too, and it does the same thing.

Does it work for you? If it works, which Apple device are you using?

marcelarie avatar Aug 04 '22 20:08 marcelarie

I don't know why it's not working for you. It has worked for probably thousands of people, except a certain few. If you're using an extremely old device, that could be why, but you seem to be using an Apple Silicon device. I'm using a 13" M1 Macbook pro.

Did you even start the service when trying the skhd commands?

Also, what commands are you referring to that "don't work"? It is very peculiar to have certain commands working, but not others.

es183923 avatar Aug 04 '22 20:08 es183923

I don't know why it's not working for you. It has worked for probably thousands of people, except a certain few. If you're using an extremely old device, that could be why, but you seem to be using an Apple Silicon device. I'm using a 13" M1 Macbook pro.

I use a macbook 14" M1 Ultra so I think it should work.

Did you even start the service when trying the skhd commands?

yes, tried it multiple times too.

Also, what commands are you referring to that "don't work"? It is very peculiar to have certain commands working, but not others.

yabai related commands. so skhd can run other commands but not the yabai ones.

marcelarie avatar Aug 04 '22 21:08 marcelarie

Ok I found the issue, thanks @es183923 because you are the reason I thought about it.

Basically for some reason brew service does not have the brew path.

My skhdrc file looked like this:

alt - k : yabai -m window --focus prev || yabai -m window --focus last
alt - j : yabai -m window --focus next || yabai -m window --focus first
shift + alt - q : skhd --key "cmd - w"

And I changed all the yabai and skhd commands for their absolute paths.

like this:

alt - k : /opt/homebrew/bin/yabai -m window --focus prev || /opt/homebrew/bin/yabai -m window --focus last
alt - j : /opt/homebrew/bin/yabai -m window --focus next || /opt/homebrew/bin/yabai -m window --focus first
shift + alt - q : /opt/homebrew/bin/skhd --key "cmd - w"

having to do this with each new command is not nice, but at least I am getting somewhere :)

marcelarie avatar Aug 04 '22 21:08 marcelarie

Ok I found the issue, thanks @es183923 because you are the reason I thought about it.

Basically for some reason brew service does not have the brew path.

My skhdrc file looked like this:

alt - k : yabai -m window --focus prev || yabai -m window --focus last
alt - j : yabai -m window --focus next || yabai -m window --focus first
shift + alt - q : skhd --key "cmd - w"

And I changed all the yabai and skhd commands for their absolute paths.

like this:

alt - k : /opt/homebrew/bin/yabai -m window --focus prev || /opt/homebrew/bin/yabai -m window --focus last
alt - j : /opt/homebrew/bin/yabai -m window --focus next || /opt/homebrew/bin/yabai -m window --focus first
shift + alt - q : /opt/homebrew/bin/skhd --key "cmd - w"

having to do this with each new command is not nice, but at least I am getting somewhere :)

I had the same issue and resolved it by moving my PATH additions to my .zshenv. I'm pretty sure the canonical macOS method would be to add a file to /etc/paths.d though; I haven't tried it yet, however, so I can't attest to whether it would work or not.

y-mx-b avatar Sep 02 '22 13:09 y-mx-b

Changing the skhd SHELL to /bin/bash surprisingly fixed this problem for me. Then I tried @y-mx-b's workaround which was also a success! 🎉

It turns out it's because I also have LnL7/nix-darwin installed...

For Zsh, it replaces the entire PATH via /etc/zshenv instead of appending to it, so the correct PATH override in the brew service got ignored. This is compounded by Homebrew expecting to get its PATH setup via ~/.zprofile, which is only sourced by login shells, but skhd runs commands like $SHELL -c $COMMAND. @y-mx-b's workaround works because ~/.zshenv is always sourced, including non-login shells.

For Bash, it also replaces the PATH, but it is instead done via /etc/bashrc, which is only sourced by interactive shells. The skhd command invocation creates neither a login nor interactive shell, so bash works because it avoids getting its inherited PATH overwritten.

tl;dr; nix-darwin + Zsh causes ignored PATH in the skhd brew service 😞

XA21X avatar Sep 13 '22 18:09 XA21X

Now that you mention it, this problem only cropped up for me after I installed Nix and Nix-Darwin... Damn.

y-mx-b avatar Sep 14 '22 00:09 y-mx-b

For Zsh, it replaces the entire PATH via /etc/zshenv instead of appending to it, so the correct PATH override in the brew service got ignored. This is compounded by Homebrew expecting to get its PATH setup via ~/.zprofile, which is only sourced by login shells, but skhd runs commands like $SHELL -c $COMMAND. @y-mx-b's workaround works because ~/.zshenv is always sourced, including non-login shells.

The issue is that OSX doesn't respect the contents of .zshenv by tampering with the PATH order (see https://apple.stackexchange.com/questions/432226/homebrew-path-set-in-zshenv-is-overridden), so putting exports in .zprofile was usually the solution as this was read last... Now the issue is that skhd can only access things from .zshenv... I tried hacking /etc/paths but that didn't seem to do the trick.

dakeryas avatar Jan 30 '23 10:01 dakeryas

The above solutions didn't work for me (MacBook m1 max, Ventura 13.1, using zsh, more). What did instead, was re-installing skhd under bash shell:

brew services stop skhd
brew uninstall skhd
SHELL=/bin/bash
brew install koekeishiya/formulae/skhd
brew cleanup skhd
brew services start skhd

I haven't restarted the machine yet; let's see if it persists in working :).

sspaeti avatar Feb 15 '23 07:02 sspaeti