stats: 2.11.23 -> 2.11.26 (#374860)
[NixPkgs.git] / pkgs / applications / version-management / sublime-merge / common.nix
blob5398fe6b781b1ecec5305871356bd50eb1f48bc2
2   buildVersion,
3   dev ? false,
4   aarch64sha256,
5   x64sha256,
6 }:
9   fetchurl,
10   lib,
11   stdenv,
12   xorg,
13   glib,
14   libGL,
15   glibcLocales,
16   gtk3,
17   cairo,
18   pango,
19   libredirect,
20   makeWrapper,
21   wrapGAppsHook3,
22   pkexecPath ? "/run/wrappers/bin/pkexec",
23   writeShellScript,
24   common-updater-scripts,
25   curl,
26   gnugrep,
27   coreutils,
30 let
31   pnameBase = "sublime-merge";
32   packageAttribute = "sublime-merge${lib.optionalString dev "-dev"}";
33   binaries = [
34     "sublime_merge"
35     crashHandlerBinary
36     "git-credential-sublime"
37     "ssh-askpass-sublime"
38   ];
39   primaryBinary = "sublime_merge";
40   primaryBinaryAliases = [
41     "smerge"
42   ];
43   crashHandlerBinary =
44     if lib.versionAtLeast buildVersion "2086" then "crash_handler" else "crash_reporter";
45   downloadUrl =
46     arch: "https://download.sublimetext.com/sublime_merge_build_${buildVersion}_${arch}.tar.xz";
47   versionUrl = "https://www.sublimemerge.com/${if dev then "dev" else "download"}";
48   versionFile = builtins.toString ./default.nix;
50   neededLibraries = [
51     xorg.libX11
52     glib
53     gtk3
54     cairo
55     pango
56     curl
57   ];
59   redirects = [
60     "/usr/bin/pkexec=${pkexecPath}"
61     "/bin/true=${coreutils}/bin/true"
62   ];
64   binaryPackage = stdenv.mkDerivation rec {
65     pname = "${pnameBase}-bin";
66     version = buildVersion;
68     src = passthru.sources.${stdenv.hostPlatform.system};
70     dontStrip = true;
71     dontPatchELF = true;
72     buildInputs = [
73       glib
74       # for GSETTINGS_SCHEMAS_PATH
75       gtk3
76     ];
77     nativeBuildInputs = [
78       makeWrapper
79       wrapGAppsHook3
80     ];
82     buildPhase = ''
83       runHook preBuild
85       for binary in ${builtins.concatStringsSep " " binaries}; do
86         patchelf \
87           --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
88           --set-rpath ${lib.makeLibraryPath neededLibraries}:${libGL}/lib:${lib.getLib stdenv.cc.cc}/lib${lib.optionalString stdenv.hostPlatform.is64bit "64"} \
89           $binary
90       done
92       # Rewrite pkexec argument. Note that we cannot delete bytes in binary.
93       sed -i -e 's,/bin/cp\x00,cp\x00\x00\x00\x00\x00\x00,g' ${primaryBinary}
95       runHook postBuild
96     '';
98     installPhase = ''
99       runHook preInstall
101       mkdir -p $out
102       cp -r * $out/
104       runHook postInstall
105     '';
107     dontWrapGApps = true; # non-standard location, need to wrap the executables manually
109     postFixup = ''
110       wrapProgram $out/${primaryBinary} \
111         --set LD_PRELOAD "${libredirect}/lib/libredirect.so" \
112         --set NIX_REDIRECTS ${builtins.concatStringsSep ":" redirects} \
113         --set LOCALE_ARCHIVE "${glibcLocales.out}/lib/locale/locale-archive" \
114         "''${gappsWrapperArgs[@]}"
116       # We need to replace the ssh-askpass-sublime executable because the default one
117       # will not function properly, in order to work it needs to pass an argv[0] to
118       # the sublime_merge binary, and the built-in version will try to call the
119       # sublime_merge wrapper script which cannot pass through the original argv[0] to
120       # the sublime_merge binary. Thankfully the ssh-askpass-sublime functionality is
121       # very simple and can be replaced with a simple wrapper script.
122       rm $out/ssh-askpass-sublime
123       makeWrapper $out/.${primaryBinary}-wrapped $out/ssh-askpass-sublime \
124         --argv0 "/ssh-askpass-sublime"
125     '';
127     passthru = {
128       sources = {
129         "aarch64-linux" = fetchurl {
130           url = downloadUrl "arm64";
131           sha256 = aarch64sha256;
132         };
133         "x86_64-linux" = fetchurl {
134           url = downloadUrl "x64";
135           sha256 = x64sha256;
136         };
137       };
138     };
139   };
141 stdenv.mkDerivation (rec {
142   pname = pnameBase;
143   version = buildVersion;
145   dontUnpack = true;
147   ${primaryBinary} = binaryPackage;
149   nativeBuildInputs = [
150     makeWrapper
151   ];
153   installPhase =
154     ''
155       runHook preInstall
156       mkdir -p "$out/bin"
157       makeWrapper "''$${primaryBinary}/${primaryBinary}" "$out/bin/${primaryBinary}"
158     ''
159     + builtins.concatStringsSep "" (
160       map (binaryAlias: "ln -s $out/bin/${primaryBinary} $out/bin/${binaryAlias}\n") primaryBinaryAliases
161     )
162     + ''
163       mkdir -p "$out/share/applications"
165       substitute \
166         "''$${primaryBinary}/${primaryBinary}.desktop" \
167         "$out/share/applications/${primaryBinary}.desktop" \
168         --replace-fail "/opt/${primaryBinary}/${primaryBinary}" "${primaryBinary}"
170       for directory in ''$${primaryBinary}/Icon/*; do
171         size=$(basename $directory)
172         mkdir -p "$out/share/icons/hicolor/$size/apps"
173         ln -s ''$${primaryBinary}/Icon/$size/* $out/share/icons/hicolor/$size/apps
174       done
175       runHook postInstall
176     '';
178   passthru = {
179     updateScript =
180       let
181         script = writeShellScript "${packageAttribute}-update-script" ''
182           set -o errexit
183           PATH=${
184             lib.makeBinPath [
185               common-updater-scripts
186               curl
187               gnugrep
188             ]
189           }
191           versionFile=$1
192           latestVersion=$(curl -s ${versionUrl} | grep -Po '(?<=<p class="latest"><i>Version:</i> Build )([0-9]+)')
194           if [[ "${buildVersion}" = "$latestVersion" ]]; then
195               echo "The new version same as the old version."
196               exit 0
197           fi
199           for platform in ${lib.escapeShellArgs meta.platforms}; do
200               update-source-version "${packageAttribute}.${primaryBinary}" "$latestVersion" --ignore-same-version --file="$versionFile" --version-key=buildVersion --source-key="sources.$platform"
201           done
202         '';
203       in
204       [
205         script
206         versionFile
207       ];
208   };
210   meta = {
211     description = "Git client from the makers of Sublime Text";
212     homepage = "https://www.sublimemerge.com";
213     mainProgram = "sublime_merge";
214     maintainers = with lib.maintainers; [ zookatron ];
215     sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
216     license = lib.licenses.unfree;
217     platforms = [
218       "aarch64-linux"
219       "x86_64-linux"
220     ];
221   };