anvil-editor: init at 0.4
[NixPkgs.git] / pkgs / applications / version-management / sublime-merge / common.nix
blob3c6edb20297f99740c170429ca797b9a5a97ab3d
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       mkdir -p "$out/bin"
156       makeWrapper "''$${primaryBinary}/${primaryBinary}" "$out/bin/${primaryBinary}"
157     ''
158     + builtins.concatStringsSep "" (
159       map (binaryAlias: "ln -s $out/bin/${primaryBinary} $out/bin/${binaryAlias}\n") primaryBinaryAliases
160     )
161     + ''
162       mkdir -p "$out/share/applications"
164       substitute \
165         "''$${primaryBinary}/${primaryBinary}.desktop" \
166         "$out/share/applications/${primaryBinary}.desktop" \
167         --replace-fail "/opt/${primaryBinary}/${primaryBinary}" "${primaryBinary}"
169       for directory in ''$${primaryBinary}/Icon/*; do
170         size=$(basename $directory)
171         mkdir -p "$out/share/icons/hicolor/$size/apps"
172         ln -s ''$${primaryBinary}/Icon/$size/* $out/share/icons/hicolor/$size/apps
173       done
174     '';
176   passthru = {
177     updateScript =
178       let
179         script = writeShellScript "${packageAttribute}-update-script" ''
180           set -o errexit
181           PATH=${
182             lib.makeBinPath [
183               common-updater-scripts
184               curl
185               gnugrep
186             ]
187           }
189           versionFile=$1
190           latestVersion=$(curl -s ${versionUrl} | grep -Po '(?<=<p class="latest"><i>Version:</i> Build )([0-9]+)')
192           if [[ "${buildVersion}" = "$latestVersion" ]]; then
193               echo "The new version same as the old version."
194               exit 0
195           fi
197           for platform in ${lib.escapeShellArgs meta.platforms}; do
198               update-source-version "${packageAttribute}.${primaryBinary}" "$latestVersion" --ignore-same-version --file="$versionFile" --version-key=buildVersion --source-key="sources.$platform"
199           done
200         '';
201       in
202       [
203         script
204         versionFile
205       ];
206   };
208   meta = with lib; {
209     description = "Git client from the makers of Sublime Text";
210     homepage = "https://www.sublimemerge.com";
211     maintainers = with maintainers; [ zookatron ];
212     sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
213     license = licenses.unfree;
214     platforms = [
215       "aarch64-linux"
216       "x86_64-linux"
217     ];
218   };