···11-{ lib, stdenv, fetchFromGitHub, darwin, xxd }:
11+{ lib
22+, stdenv
33+, fetchFromGitHub
44+, fetchzip
55+, writeShellScript
66+, installShellFiles
77+, testers
88+, yabai
99+, xxd
1010+, xcodebuild
1111+, Carbon
1212+, Cocoa
1313+, ScriptingBridge
1414+ # This needs to be from SDK 10.13 or higher, SLS APIs introduced in that version get used
1515+, SkyLight
1616+}:
21733-stdenv.mkDerivation rec {
1818+let
419 pname = "yabai";
55- version = "3.3.10";
2020+ version = "4.0.2";
62177- src = fetchFromGitHub {
88- owner = "koekeishiya";
99- repo = pname;
1010- rev = "v${version}";
1111- sha256 = "sha256-8O6//T894C32Pba3F2Z84Z6VWeCXlwml3xsXoIZGqL0=";
2222+ test-version = testers.testVersion {
2323+ package = yabai;
2424+ version = "yabai-v${version}";
1225 };
13261414- nativeBuildInputs = [ xxd ];
2727+ _meta = with lib; {
2828+ description = "A tiling window manager for macOS based on binary space partitioning";
2929+ longDescription = ''
3030+ yabai is a window management utility that is designed to work as an extension to the built-in
3131+ window manager of macOS. yabai allows you to control your windows, spaces and displays freely
3232+ using an intuitive command line interface and optionally set user-defined keyboard shortcuts
3333+ using skhd and other third-party software.
3434+ '';
3535+ homepage = "https://github.com/koekeishiya/yabai";
3636+ changelog = "https://github.com/koekeishiya/yabai/blob/v${version}/CHANGELOG.md";
3737+ license = licenses.mit;
3838+ platforms = platforms.darwin;
3939+ maintainers = with maintainers; [
4040+ cmacrae
4141+ shardy
4242+ ivar
4343+ ];
4444+ };
4545+in
4646+{
4747+ # Unfortunately compiling yabai from source on aarch64-darwin is a bit complicated. We use the precompiled binary instead for now.
4848+ # See the comments on https://github.com/NixOS/nixpkgs/pull/188322 for more information.
4949+ aarch64-darwin = stdenv.mkDerivation {
5050+ inherit pname version;
5151+5252+ src = fetchzip {
5353+ url = "https://github.com/koekeishiya/yabai/releases/download/v${version}/yabai-v${version}.tar.gz";
5454+ sha256 = "sha256-RwARzK3e0e2N3ndFNikfo8srDjeo6jsWN2xQ18bXt/I=";
5555+ };
5656+5757+ nativeBuildInputs = [
5858+ installShellFiles
5959+ ];
6060+6161+ dontConfigure = true;
6262+ dontBuild = true;
15631616- buildInputs = with darwin.apple_sdk.frameworks; [
1717- Carbon
1818- Cocoa
1919- ScriptingBridge
2020- SkyLight
2121- ];
6464+ installPhase = ''
6565+ runHook preInstall
22662323- installPhase = ''
2424- mkdir -p $out/bin
2525- mkdir -p $out/share/man/man1/
2626- cp ./bin/yabai $out/bin/yabai
2727- cp ./doc/yabai.1 $out/share/man/man1/yabai.1
2828- '';
6767+ mkdir -p $out
6868+ cp -r ./bin $out
6969+ installManPage ./doc/yabai.1
29703030- meta = with lib; {
3131- description = ''
3232- A tiling window manager for macOS based on binary space partitioning
7171+ runHook postInstall
3372 '';
3434- homepage = "https://github.com/koekeishiya/yabai";
3535- platforms = platforms.darwin;
3636- maintainers = with maintainers; [ cmacrae shardy ];
3737- license = licenses.mit;
7373+7474+ passthru.tests.version = test-version;
7575+7676+ meta = _meta // {
7777+ sourceProvenance = with lib.sourceTypes; [
7878+ binaryNativeCode
7979+ ];
8080+ };
3881 };
3939-}
8282+8383+ x86_64-darwin = stdenv.mkDerivation rec {
8484+ inherit pname version;
8585+8686+ src = fetchFromGitHub {
8787+ owner = "koekeishiya";
8888+ repo = "yabai";
8989+ rev = "v${version}";
9090+ sha256 = "sha256-DXDdjI4kkLcRUNtMoSu7fJ0f3fUty88o5ZS6lJz0cGU=";
9191+ };
9292+9393+ nativeBuildInputs = [
9494+ installShellFiles
9595+ xcodebuild
9696+ xxd
9797+ ];
9898+9999+ buildInputs = [
100100+ Carbon
101101+ Cocoa
102102+ ScriptingBridge
103103+ SkyLight
104104+ ];
105105+106106+ dontConfigure = true;
107107+ enableParallelBuilding = true;
108108+109109+ postPatch = ''
110110+ # aarch64 code is compiled on all targets, which causes our Apple SDK headers to error out.
111111+ # Since multilib doesnt work on darwin i dont know of a better way of handling this.
112112+ substituteInPlace makefile \
113113+ --replace "-arch arm64e" "" \
114114+ --replace "-arch arm64" "" \
115115+ --replace "clang" "${stdenv.cc.targetPrefix}clang"
116116+117117+ # `NSScreen::safeAreaInsets` is only available on macOS 12.0 and above, which frameworks arent packaged.
118118+ # When a lower OS version is detected upstream just returns 0, so we can hardcode that at compiletime.
119119+ # https://github.com/koekeishiya/yabai/blob/v4.0.2/src/workspace.m#L109
120120+ substituteInPlace src/workspace.m \
121121+ --replace 'return screen.safeAreaInsets.top;' 'return 0;'
122122+ '';
123123+124124+ installPhase = ''
125125+ runHook preInstall
126126+127127+ mkdir -p $out/{bin,share/icons/hicolor/scalable/apps}
128128+129129+ cp ./bin/yabai $out/bin/yabai
130130+ ln -s ${loadScriptingAddition} $out/bin/yabai-load-sa
131131+ cp ./assets/icon/icon.svg $out/share/icons/hicolor/scalable/apps/yabai.svg
132132+ installManPage ./doc/yabai.1
133133+134134+ runHook postInstall
135135+ '';
136136+137137+ # Defining this here exposes it as a passthru attribute, which is useful because it allows us to run `builtins.hashFile` on it in pure-eval mode.
138138+ # With that we can programatically generate an `/etc/sudoers.d` entry which disables the password requirement, so that a user-agent can run it at login.
139139+ loadScriptingAddition = writeShellScript "yabai-load-sa" ''
140140+ # For whatever reason the regular commands to load the scripting addition do not work, yabai will throw an error.
141141+ # The installation command mutably installs binaries to '/System', but then fails to start them. Manually running
142142+ # the bins as root does start the scripting addition, so this serves as a more user-friendly way to do that.
143143+144144+ set -euo pipefail
145145+146146+ if [[ "$EUID" != 0 ]]; then
147147+ echo "error: the scripting-addition loader must ran as root. try 'sudo $0'"
148148+ exit 1
149149+ fi
150150+151151+ loaderPath="/Library/ScriptingAdditions/yabai.osax/Contents/MacOS/mach_loader";
152152+153153+ if ! test -x "$loaderPath"; then
154154+ echo "could not locate the scripting-addition loader at '$loaderPath', installing it..."
155155+ echo "note: this may display an error"
156156+157157+ eval "$(dirname "''${BASH_SOURCE[0]}")/yabai --install-sa" || true
158158+ sleep 1
159159+ fi
160160+161161+ echo "executing loader..."
162162+ eval "$loaderPath"
163163+ echo "scripting-addition started"
164164+ '';
165165+166166+ passthru.tests.version = test-version;
167167+168168+ meta = _meta // {
169169+ longDescription = _meta.longDescription + ''
170170+ Note that due to a nix-only bug the scripting addition cannot be launched using the regular
171171+ procedure. Instead, you can use the provided `yabai-load-sa` script.
172172+ '';
173173+174174+ sourceProvenance = with lib.sourceTypes; [
175175+ fromSource
176176+ ];
177177+ };
178178+ };
179179+}.${stdenv.hostPlatform.system} or (throw "Unsupported platform ${stdenv.hostPlatform.system}")