refactor(nix/brew): Code refactor

Co-authored-by: isabel <isabel@isabelroses.com>
This commit is contained in:
Ahmad Ansori Palembani 2025-05-01 16:24:29 +07:00
parent eca345b624
commit 0a416a41f4
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -1,105 +1,122 @@
# Modified version of brew-nix/casks.nix # Modified version of brew-nix/casks.nix to support macOS version variations
# #
# REF: https://github.com/BatteredBunny/brew-nix/blob/9dfab294afea5029922bbc59a10ae487c7318d59/casks.nix # REF: https://github.com/BatteredBunny/brew-nix/blob/928e5382d927727666bcbbd27240e73eeb269b4a/casks.nix
{ system, mac-version ? "ventura", nixpkgs, brew-api, ... }: {
system,
mac-version ? "ventura",
nixpkgs,
brew-api,
...
}:
let let
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
}; };
lib = pkgs.lib;
hasBinary = cask: pkgs.lib.hasAttr "binary" (getArtifacts cask);
hasApp = cask: pkgs.lib.hasAttr "app" (getArtifacts cask);
hasPkg = cask: pkgs.lib.hasAttr "pkg" (getArtifacts cask);
getName = cask: builtins.elemAt cask.name 0; getName = cask: builtins.elemAt cask.name 0;
getBinary = artifacts: builtins.elemAt artifacts.binary 0;
getApp = artifacts: builtins.elemAt artifacts.app 0;
getBinary = cask: builtins.elemAt (getArtifacts cask).binary 0; caskToDerivation =
getApp = cask: builtins.elemAt (getArtifacts cask).app 0; cask:
getArtifacts = cask: pkgs.lib.mergeAttrsList cask.artifacts; let
artifacts = lib.mergeAttrsList cask.artifacts;
isBinary = lib.hasAttr "binary" artifacts;
isApp = lib.hasAttr "app" artifacts;
isPkg = lib.hasAttr "pkg" artifacts;
url = (cask.variations.${mac-version} or cask).url;
hash = (cask.variations.${mac-version} or cask).sha256;
in
pkgs.stdenv.mkDerivation rec {
pname = cask.token;
inherit (cask) version;
getUrl = cask: (cask.variations.${mac-version} or cask).url; src = pkgs.fetchurl {
getHash = cask: (cask.variations.${mac-version} or cask).sha256; # TODO: convert it to SRI inherit url;
sha256 = pkgs.lib.optionalString (hash != "no_check") hash;
};
caskToDerivation = cask: pkgs.stdenv.mkDerivation rec { nativeBuildInputs =
pname = cask.token; with pkgs;
version = cask.version; [
undmg
unzip
gzip
_7zz
makeWrapper
]
++ pkgs.lib.optional isPkg (
with pkgs;
[
xar
cpio
]
);
src = pkgs.fetchurl { unpackPhase =
url = (getUrl cask); if isPkg then ''
sha256 = pkgs.lib.optionalString ((getHash cask) != "no_check") (getHash cask); xar -xf $src
for pkg in $(cat Distribution | grep -oE "#.+\.pkg" | sed -e "s/^#//" -e "s/$/\/Payload/"); do
zcat $pkg | cpio -i
done
'' else if isApp then ''
undmg $src || 7zz x -snld $src
'' else if isBinary then ''
if [ "$(file --mime-type -b "$src")" == "application/gzip" ]; then
gunzip $src -c > ${getBinary artifacts}
elif [ "$(file --mime-type -b "$src")" == "application/x-mach-binary" ]; then
cp $src ${getBinary artifacts}
fi
'' else "";
sourceRoot = pkgs.lib.optionalString isApp (getApp artifacts);
# Patching shebangs invalidates code signing
dontPatchShebangs = true;
installPhase =
if isPkg then ''
mkdir -p $out/Applications
cp -R Applications/* $out/Applications/
if [ -d "Resources" ]; then
mkdir -p $out/Resources
cp -R Resources/* $out/Resources/
fi
if [ -d "Library" ]; then
mkdir -p $out/Library
cp -R Library/* $out/Library/
fi
'' else if isApp then ''
mkdir -p "$out/Applications/${sourceRoot}"
cp -R . "$out/Applications/${sourceRoot}"
if [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${getName cask}" ]]; then
makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${getName cask}" $out/bin/${cask.token}
elif [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" ]]; then
makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" $out/bin/${cask.token}
fi
'' else if (isBinary && !isApp) then ''
mkdir -p $out/bin
cp -R ./* $out/bin
'' else "";
meta = {
inherit (cask) homepage;
description = cask.desc;
platforms = pkgs.lib.platforms.darwin;
mainProgram = if (isBinary && !isApp) then (getBinary artifacts) else (cask.token);
};
}; };
nativeBuildInputs = with pkgs; [ casks = lib.importJSON (brew-api + "/cask.json");
undmg
unzip
gzip
_7zz
makeWrapper
] ++ pkgs.lib.optional (hasPkg cask) (with pkgs; [
xar
cpio
]);
unpackPhase =
if (hasPkg cask) then ''
xar -xf $src
for pkg in $(cat Distribution | grep -oE "#.+\.pkg" | sed -e "s/^#//" -e "s/$/\/Payload/"); do
zcat $pkg | cpio -i
done
'' else if (hasApp cask) then ''
undmg $src || 7zz x -snld $src
'' else if (hasBinary cask) then ''
if [ "$(file --mime-type -b "$src")" == "application/gzip" ]; then
gunzip $src -c > ${getBinary cask}
elif [ "$(file --mime-type -b "$src")" == "application/x-mach-binary" ]; then
cp $src ${getBinary cask}
fi
'' else "";
sourceRoot = pkgs.lib.optionalString (hasApp cask) (getApp cask);
installPhase =
if (hasPkg cask) then ''
mkdir -p $out/Applications
cp -R Applications/* $out/Applications/
if [ -d "Resources" ]; then
mkdir -p $out/Resources
cp -R Resources/* $out/Resources/
fi
if [ -d "Library" ]; then
mkdir -p $out/Library
cp -R Library/* $out/Library/
fi
'' else if (hasApp cask) then ''
mkdir -p "$out/Applications/${sourceRoot}"
cp -R . "$out/Applications/${sourceRoot}"
if [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${getName cask}" ]]; then
makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${getName cask}" $out/bin/${cask.token}
elif [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" ]]; then
makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" $out/bin/${cask.token}
fi
'' else if (hasBinary cask && !hasApp cask) then ''
mkdir -p $out/bin
cp -R ./* $out/bin
'' else "";
meta = {
homepage = cask.homepage;
description = cask.desc;
platforms = pkgs.lib.platforms.darwin;
mainProgram = if (hasBinary cask && !hasApp cask) then (getBinary cask) else (cask.token);
};
};
casks = builtins.fromJSON (builtins.readFile (brew-api + "/cask.json"));
in final: prev: { in final: prev: {
casks = builtins.listToAttrs (builtins.map casks = lib.listToAttrs (
(cask: { builtins.map (cask: {
name = cask.token; name = cask.token;
value = caskToDerivation cask; value = caskToDerivation cask;
}) }) casks
casks); );
} }