yabai: build from source

Co-authored-by: Zhong Jianxin <azuwis@gmail.com>
Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman
2024-10-31 11:11:48 -05:00
parent 345d05a2ff
commit dc38d0a8f8

View File

@@ -1,38 +1,39 @@
{
lib,
stdenv,
fetchFromGitHub,
fetchzip,
apple-sdk_15,
common-updater-scripts,
curl,
bintools-unwrapped,
cups,
fetchFromGitHub,
installShellFiles,
jq,
llvmPackages,
nix-update-script,
stdenv,
versionCheckHook,
writeShellScript,
xxd,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "yabai";
version = "7.1.25";
src =
finalAttrs.passthru.sources.${stdenv.hostPlatform.system}
or (throw "Unsupported system: ${stdenv.hostPlatform.system}");
src = fetchFromGitHub {
owner = "asmvik";
repo = "yabai";
tag = "v${finalAttrs.version}";
hash = "sha256-61knfbahxxlJnVZy47347slsjUGiQUJyZh58G97SDkE=";
};
__structuredAttrs = true;
strictDeps = true;
nativeBuildInputs = [
installShellFiles
]
++ lib.optionals stdenv.hostPlatform.isx86_64 [
xxd
];
buildInputs = lib.optionals stdenv.hostPlatform.isx86_64 [
buildInputs = [
apple-sdk_15
];
dontConfigure = true;
dontBuild = stdenv.hostPlatform.isAarch64;
enableParallelBuilding = true;
installPhase = ''
@@ -41,48 +42,47 @@ stdenv.mkDerivation (finalAttrs: {
mkdir -p $out/{bin,share/icons/hicolor/scalable/apps}
cp ./bin/yabai $out/bin/yabai
${lib.optionalString stdenv.hostPlatform.isx86_64 "cp ./assets/icon/icon.svg $out/share/icons/hicolor/scalable/apps/yabai.svg"}
cp ./assets/icon/icon.svg $out/share/icons/hicolor/scalable/apps/yabai.svg
installManPage ./doc/yabai.1
runHook postInstall
'';
# yabai's makefile builds universal (x86_64 + arm64/arm64e) binaries with
# `xcrun clang`. Collapse it to the host arch and use plain `clang`, since the
# scripting addition (arm64e) is compiled in preBuild with the unwrapped clang,
# which needs the SDK/clang/CUPS include paths passed explicitly.
postPatch =
lib.optionalString stdenv.hostPlatform.isx86_64 # bash
''
# aarch64 code is compiled on all targets, which causes our Apple SDK headers to error out.
# Since multilib doesn't work on darwin i dont know of a better way of handling this.
substituteInPlace makefile \
--replace-fail "-arch arm64e" "" \
--replace-fail "-arch arm64" ""
'';
let
arch = stdenv.hostPlatform.darwinArch;
# The scripting addition is injected into arm64e system processes, so on
# aarch64 it must be arm64e even though the main binary stays arm64.
archSA = "${arch}${lib.optionalString stdenv.hostPlatform.isAarch64 "e"}";
clangFlags = lib.concatStringsSep " " [
"-isystem $(SDKROOT)/usr/include"
"-isystem ${llvmPackages.libclang.lib}/lib/clang/*/include"
"-isystem ${lib.getDev cups}/include"
"-F$(SDKROOT)/System/Library/Frameworks"
"-L$(SDKROOT)/usr/lib"
];
in
''
substituteInPlace makefile \
--replace-fail "-arch x86_64 -arch arm64e" "-arch ${archSA}" \
--replace-fail "-arch x86_64 -arch arm64" "-arch ${arch}" \
--replace-fail 'xcrun clang' 'clang ${clangFlags}'
'';
# The cc-wrapper can't target arm64e, so build the scripting addition (the only
# arm64e part) with the unwrapped clang.
preBuild = lib.optionalString stdenv.hostPlatform.isAarch64 ''
make ./src/osax/payload_bin.c ./src/osax/loader_bin.c "PATH=${bintools-unwrapped}/bin:${llvmPackages.clang-unwrapped}/bin:$PATH"
'';
nativeInstallCheckInputs = [ versionCheckHook ];
doInstallCheck = true;
passthru = {
sources = {
# Unfortunately compiling yabai from source on aarch64-darwin is a bit complicated. We use the precompiled binary instead for now.
# See the comments on https://github.com/NixOS/nixpkgs/pull/188322 for more information.
"aarch64-darwin" = fetchzip {
url = "https://github.com/asmvik/yabai/releases/download/v${finalAttrs.version}/yabai-v${finalAttrs.version}.tar.gz";
hash = "sha256-hGZLoY5zZjtVtjMbYQ4VHqAuAD835Tdfq6YX122/9Ik=";
};
"x86_64-darwin" = fetchFromGitHub {
owner = "asmvik";
repo = "yabai";
rev = "v${finalAttrs.version}";
hash = "sha256-61knfbahxxlJnVZy47347slsjUGiQUJyZh58G97SDkE=";
};
};
updateScript = writeShellScript "update-yabai" ''
NEW_VERSION=$(${lib.getExe curl} --silent https://api.github.com/repos/asmvik/yabai/releases/latest | ${lib.getExe jq} '.tag_name | ltrimstr("v")' --raw-output)
for platform in ${lib.escapeShellArgs finalAttrs.meta.platforms}; do
${lib.getExe' common-updater-scripts "update-source-version"} "yabai" "$NEW_VERSION" --ignore-same-version --source-key="sources.$platform"
done
'';
};
passthru.updateScript = nix-update-script { };
meta = {
description = "Tiling window manager for macOS based on binary space partitioning";
@@ -95,16 +95,13 @@ stdenv.mkDerivation (finalAttrs: {
homepage = "https://github.com/asmvik/yabai";
changelog = "https://github.com/asmvik/yabai/blob/v${finalAttrs.version}/CHANGELOG.md";
license = lib.licenses.mit;
platforms = builtins.attrNames finalAttrs.passthru.sources;
platforms = lib.platforms.darwin;
mainProgram = "yabai";
maintainers = with lib.maintainers; [
cmacrae
shardy
khaneliman
];
sourceProvenance =
with lib.sourceTypes;
lib.optionals stdenv.hostPlatform.isx86_64 [ fromSource ]
++ lib.optionals stdenv.hostPlatform.isAarch64 [ binaryNativeCode ];
sourceProvenance = [ lib.sourceTypes.fromSource ];
};
})