anvil-editor: init at 0.4
[NixPkgs.git] / pkgs / applications / networking / instant-messengers / discord / darwin.nix
blobe81f1f488c07b3cbc604485e6e33ace6a98979ce
2   pname,
3   version,
4   src,
5   meta,
6   stdenv,
7   binaryName,
8   desktopName,
9   lib,
10   undmg,
11   makeWrapper,
12   writeScript,
13   python3,
14   runCommand,
15   branch,
16   withOpenASAR ? false,
17   openasar,
18   withVencord ? false,
19   vencord,
22 let
23   disableBreakingUpdates =
24     runCommand "disable-breaking-updates.py"
25       {
26         pythonInterpreter = "${python3.interpreter}";
27         configDirName = lib.toLower binaryName;
28         meta.mainProgram = "disable-breaking-updates.py";
29       }
30       ''
31         mkdir -p $out/bin
32         cp ${./disable-breaking-updates.py} $out/bin/disable-breaking-updates.py
33         substituteAllInPlace $out/bin/disable-breaking-updates.py
34         chmod +x $out/bin/disable-breaking-updates.py
35       '';
37 stdenv.mkDerivation {
38   inherit
39     pname
40     version
41     src
42     meta
43     ;
45   nativeBuildInputs = [
46     undmg
47     makeWrapper
48   ];
50   sourceRoot = ".";
52   installPhase = ''
53     runHook preInstall
55     mkdir -p $out/Applications
56     cp -r "${desktopName}.app" $out/Applications
58     # wrap executable to $out/bin
59     mkdir -p $out/bin
60     makeWrapper "$out/Applications/${desktopName}.app/Contents/MacOS/${binaryName}" "$out/bin/${binaryName}" \
61       --run ${lib.getExe disableBreakingUpdates}
63     runHook postInstall
64   '';
66   postInstall =
67     lib.strings.optionalString withOpenASAR ''
68       cp -f ${openasar} $out/Applications/${desktopName}.app/Contents/Resources/app.asar
69     ''
70     + lib.strings.optionalString withVencord ''
71       mv $out/Applications/${desktopName}.app/Contents/Resources/app.asar $out/Applications/${desktopName}.app/Contents/Resources/_app.asar
72       mkdir $out/Applications/${desktopName}.app/Contents/Resources/app.asar
73       echo '{"name":"discord","main":"index.js"}' > $out/Applications/${desktopName}.app/Contents/Resources/app.asar/package.json
74       echo 'require("${vencord}/patcher.js")' > $out/Applications/${desktopName}.app/Contents/Resources/app.asar/index.js
75     '';
77   passthru = {
78     # make it possible to run disableBreakingUpdates standalone
79     inherit disableBreakingUpdates;
80     updateScript = writeScript "discord-update-script" ''
81       #!/usr/bin/env nix-shell
82       #!nix-shell -i bash -p curl gnugrep common-updater-scripts
83       set -x
84       set -eou pipefail;
85       url=$(curl -sI -o /dev/null -w '%header{location}' "https://discord.com/api/download/${branch}?platform=osx&format=dmg")
86       version=$(echo $url | grep -oP '/\K(\d+\.){2}\d+')
87       update-source-version ${
88         lib.optionalString (!stdenv.buildPlatform.isDarwin) "pkgsCross.aarch64-darwin."
89       }${pname} "$version" --file=./pkgs/applications/networking/instant-messengers/discord/default.nix --version-key=${branch}
90     '';
91   };