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,65 +1,82 @@
# 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;
getUrl = cask: (cask.variations.${mac-version} or cask).url; isBinary = lib.hasAttr "binary" artifacts;
getHash = cask: (cask.variations.${mac-version} or cask).sha256; # TODO: convert it to SRI isApp = lib.hasAttr "app" artifacts;
isPkg = lib.hasAttr "pkg" artifacts;
caskToDerivation = cask: pkgs.stdenv.mkDerivation rec { url = (cask.variations.${mac-version} or cask).url;
hash = (cask.variations.${mac-version} or cask).sha256;
in
pkgs.stdenv.mkDerivation rec {
pname = cask.token; pname = cask.token;
version = cask.version; inherit (cask) version;
src = pkgs.fetchurl { src = pkgs.fetchurl {
url = (getUrl cask); inherit url;
sha256 = pkgs.lib.optionalString ((getHash cask) != "no_check") (getHash cask); sha256 = pkgs.lib.optionalString (hash != "no_check") hash;
}; };
nativeBuildInputs = with pkgs; [ nativeBuildInputs =
with pkgs;
[
undmg undmg
unzip unzip
gzip gzip
_7zz _7zz
makeWrapper makeWrapper
] ++ pkgs.lib.optional (hasPkg cask) (with pkgs; [ ]
++ pkgs.lib.optional isPkg (
with pkgs;
[
xar xar
cpio cpio
]); ]
);
unpackPhase = unpackPhase =
if (hasPkg cask) then '' if isPkg then ''
xar -xf $src xar -xf $src
for pkg in $(cat Distribution | grep -oE "#.+\.pkg" | sed -e "s/^#//" -e "s/$/\/Payload/"); do for pkg in $(cat Distribution | grep -oE "#.+\.pkg" | sed -e "s/^#//" -e "s/$/\/Payload/"); do
zcat $pkg | cpio -i zcat $pkg | cpio -i
done done
'' else if (hasApp cask) then '' '' else if isApp then ''
undmg $src || 7zz x -snld $src undmg $src || 7zz x -snld $src
'' else if (hasBinary cask) then '' '' else if isBinary then ''
if [ "$(file --mime-type -b "$src")" == "application/gzip" ]; then if [ "$(file --mime-type -b "$src")" == "application/gzip" ]; then
gunzip $src -c > ${getBinary cask} gunzip $src -c > ${getBinary artifacts}
elif [ "$(file --mime-type -b "$src")" == "application/x-mach-binary" ]; then elif [ "$(file --mime-type -b "$src")" == "application/x-mach-binary" ]; then
cp $src ${getBinary cask} cp $src ${getBinary artifacts}
fi fi
'' else ""; '' else "";
sourceRoot = pkgs.lib.optionalString (hasApp cask) (getApp cask); sourceRoot = pkgs.lib.optionalString isApp (getApp artifacts);
# Patching shebangs invalidates code signing
dontPatchShebangs = true;
installPhase = installPhase =
if (hasPkg cask) then '' if isPkg then ''
mkdir -p $out/Applications mkdir -p $out/Applications
cp -R Applications/* $out/Applications/ cp -R Applications/* $out/Applications/
@ -72,7 +89,7 @@ let
mkdir -p $out/Library mkdir -p $out/Library
cp -R Library/* $out/Library/ cp -R Library/* $out/Library/
fi fi
'' else if (hasApp cask) then '' '' else if isApp then ''
mkdir -p "$out/Applications/${sourceRoot}" mkdir -p "$out/Applications/${sourceRoot}"
cp -R . "$out/Applications/${sourceRoot}" cp -R . "$out/Applications/${sourceRoot}"
@ -81,25 +98,25 @@ let
elif [[ -e "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" ]]; then 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} makeWrapper "$out/Applications/${sourceRoot}/Contents/MacOS/${pkgs.lib.removeSuffix ".app" sourceRoot}" $out/bin/${cask.token}
fi fi
'' else if (hasBinary cask && !hasApp cask) then '' '' else if (isBinary && !isApp) then ''
mkdir -p $out/bin mkdir -p $out/bin
cp -R ./* $out/bin cp -R ./* $out/bin
'' else ""; '' else "";
meta = { meta = {
homepage = cask.homepage; inherit (cask) homepage;
description = cask.desc; description = cask.desc;
platforms = pkgs.lib.platforms.darwin; platforms = pkgs.lib.platforms.darwin;
mainProgram = if (hasBinary cask && !hasApp cask) then (getBinary cask) else (cask.token); mainProgram = if (isBinary && !isApp) then (getBinary artifacts) else (cask.token);
}; };
}; };
casks = builtins.fromJSON (builtins.readFile (brew-api + "/cask.json")); casks = lib.importJSON (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); );
} }