-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcasks.nix
More file actions
151 lines (133 loc) · 4.48 KB
/
casks.nix
File metadata and controls
151 lines (133 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
pkgs,
lib ? pkgs.lib,
brew-api,
stdenvNoCC ? pkgs.stdenvNoCC,
...
}: let
getName = cask: lib.lists.elemAt cask.name 0;
getBinary = artifacts: lib.lists.elemAt artifacts.binary 0;
getApp = artifacts: lib.lists.elemAt artifacts.app 0;
getVariationData = cask: variation:
if cask ? variations && lib.attrsets.hasAttr variation cask.variations
then cask.variations."${variation}"
else throw "Variation '${variation}' not found for ${cask.token}. Available: ${builtins.toString (lib.attrsets.attrNames (cask.variations or {}))}";
caskToDerivation = cask: {variation ? null}: let
specificVariationData =
if variation != null
then getVariationData cask variation
else {};
defaultCaskData = {
url = cask.url or null;
sha256 = cask.sha256 or null;
version = cask.version or null;
artifacts = cask.artifacts or [];
};
selectedData = defaultCaskData // specificVariationData;
inherit (selectedData) url sha256 version;
artifacts = lib.attrsets.mergeAttrsList selectedData.artifacts;
isBinary = lib.attrsets.hasAttr "binary" artifacts;
isApp = lib.attrsets.hasAttr "app" artifacts;
isPkg = lib.attrsets.hasAttr "pkg" artifacts;
in
stdenvNoCC.mkDerivation (finalAttrs: {
pname = cask.token;
inherit version;
src = pkgs.fetchurl {
inherit url;
sha256 = lib.strings.optionalString (sha256 != null && sha256 != "no_check") sha256;
};
nativeBuildInputs = with pkgs;
[
undmg
unzip
gzip
_7zz
makeWrapper
]
++ lib.lists.optional isPkg (
with pkgs; [
xar
cpio
fd
]
);
unpackPhase =
if isPkg
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 isApp
then ''
undmg $src || unzip $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 = lib.strings.optionalString isApp (getApp artifacts);
# Patching shebangs invalidates code signing
dontPatchShebangs = true;
installPhase =
if isPkg
then ''
if [ -d "Applications" ]; then
mkdir -p $out/Applications
cp -R Applications/* $out/Applications/
fi
if [ -n "$(fd -d 1 -t d '\.app$' .)" ]; then
mkdir -p $out/Applications
cp -R *.app $out/Applications/
fi
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/${finalAttrs.sourceRoot}"
cp -R . "$out/Applications/${finalAttrs.sourceRoot}"
if [[ -e "$out/Applications/${finalAttrs.sourceRoot}/Contents/MacOS/${getName cask}" ]]; then
makeWrapper "$out/Applications/${finalAttrs.sourceRoot}/Contents/MacOS/${getName cask}" $out/bin/${cask.token}
elif [[ -e "$out/Applications/${finalAttrs.sourceRoot}/Contents/MacOS/${lib.strings.removeSuffix ".app" finalAttrs.sourceRoot}" ]]; then
makeWrapper "$out/Applications/${finalAttrs.sourceRoot}/Contents/MacOS/${lib.strings.removeSuffix ".app" finalAttrs.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 = lib.platforms.darwin;
mainProgram =
if (isBinary && !isApp)
then (getBinary artifacts)
else cask.token;
};
});
casks = lib.trivial.importJSON (brew-api + "/cask.json");
in
lib.attrsets.listToAttrs (
lib.lists.map (cask: {
name = cask.token;
value = lib.customisation.makeOverridable (caskToDerivation cask) {};
})
casks
)