rio: 0.0.36 -> 0.0.37
[NixPkgs.git] / pkgs / applications / editors / sublime / 4 / common.nix
blobf8083902fdde4187c3cfb8732e3127520cbc04a8
2   buildVersion,
3   dev ? false,
4   aarch64sha256,
5   x64sha256,
6 }:
9   fetchurl,
10   stdenv,
11   lib,
12   xorg,
13   glib,
14   libglvnd,
15   glibcLocales,
16   gtk3,
17   cairo,
18   pango,
19   makeWrapper,
20   wrapGAppsHook3,
21   writeShellScript,
22   common-updater-scripts,
23   curl,
24   openssl_1_1,
25   bzip2,
26   sqlite,
29 let
30   pnameBase = "sublimetext4";
31   packageAttribute = "sublime4${lib.optionalString dev "-dev"}";
32   binaries = [
33     "sublime_text"
34     "plugin_host-3.3"
35     "plugin_host-3.8"
36     crashHandlerBinary
37   ];
38   primaryBinary = "sublime_text";
39   primaryBinaryAliases = [
40     "subl"
41     "sublime"
42     "sublime4"
43   ];
44   crashHandlerBinary =
45     if lib.versionAtLeast buildVersion "4153" then "crash_handler" else "crash_reporter";
46   downloadUrl =
47     arch: "https://download.sublimetext.com/sublime_text_build_${buildVersion}_${arch}.tar.xz";
48   versionUrl = "https://download.sublimetext.com/latest/${if dev then "dev" else "stable"}";
49   versionFile = builtins.toString ./packages.nix;
51   neededLibraries = [
52     xorg.libX11
53     xorg.libXtst
54     glib
55     libglvnd
56     openssl_1_1
57     gtk3
58     cairo
59     pango
60     curl
61   ] ++ lib.optionals (lib.versionAtLeast buildVersion "4145") [
62     sqlite
63   ];
65 let
66   binaryPackage = stdenv.mkDerivation rec {
67     pname = "${pnameBase}-bin";
68     version = buildVersion;
70     src = passthru.sources.${stdenv.hostPlatform.system};
72     dontStrip = true;
73     dontPatchELF = true;
75     buildInputs = [
76       glib
77       # for GSETTINGS_SCHEMAS_PATH
78       gtk3
79     ];
81     nativeBuildInputs = [
82       makeWrapper
83       wrapGAppsHook3
84     ];
86     buildPhase = ''
87       runHook preBuild
89       for binary in ${builtins.concatStringsSep " " binaries}; do
90         patchelf \
91           --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
92           --set-rpath ${lib.makeLibraryPath neededLibraries}:${stdenv.cc.cc.lib}/lib${lib.optionalString stdenv.is64bit "64"} \
93           $binary
94       done
96       # Rewrite pkexec argument. Note that we cannot delete bytes in binary.
97       sed -i -e 's,/bin/cp\x00,cp\x00\x00\x00\x00\x00\x00,g' ${primaryBinary}
99       runHook postBuild
100     '';
102     installPhase = ''
103       runHook preInstall
105       # No need to patch these libraries, it works well with our own
106       rm libcrypto.so.1.1 libssl.so.1.1
107       ${lib.optionalString (lib.versionAtLeast buildVersion "4145") "rm libsqlite3.so"}
109       mkdir -p $out
110       cp -r * $out/
112       runHook postInstall
113     '';
115     dontWrapGApps = true; # non-standard location, need to wrap the executables manually
117     postFixup = ''
118       sed -i 's#/usr/bin/pkexec#pkexec\x00\x00\x00\x00\x00\x00\x00\x00\x00#g' "$out/${primaryBinary}"
120       wrapProgram $out/${primaryBinary} \
121         --set LOCALE_ARCHIVE "${glibcLocales.out}/lib/locale/locale-archive" \
122         "''${gappsWrapperArgs[@]}"
123     '';
125     passthru = {
126       sources = {
127         "aarch64-linux" = fetchurl {
128           url = downloadUrl "arm64";
129           sha256 = aarch64sha256;
130         };
131         "x86_64-linux" = fetchurl {
132           url = downloadUrl "x64";
133           sha256 = x64sha256;
134         };
135       };
136     };
137   };
139 stdenv.mkDerivation (rec {
140   pname = pnameBase;
141   version = buildVersion;
143   dontUnpack = true;
145   ${primaryBinary} = binaryPackage;
147   nativeBuildInputs = [
148     makeWrapper
149   ];
151   installPhase =
152     ''
153       mkdir -p "$out/bin"
154       makeWrapper "''$${primaryBinary}/${primaryBinary}" "$out/bin/${primaryBinary}"
155     ''
156     + builtins.concatStringsSep "" (
157       map (binaryAlias: "ln -s $out/bin/${primaryBinary} $out/bin/${binaryAlias}\n") primaryBinaryAliases
158     )
159     + ''
160       mkdir -p "$out/share/applications"
162       substitute \
163         "''$${primaryBinary}/${primaryBinary}.desktop" \
164         "$out/share/applications/${primaryBinary}.desktop" \
165         --replace-fail "/opt/${primaryBinary}/${primaryBinary}" "${primaryBinary}"
167       for directory in ''$${primaryBinary}/Icon/*; do
168         size=$(basename $directory)
169         mkdir -p "$out/share/icons/hicolor/$size/apps"
170         ln -s ''$${primaryBinary}/Icon/$size/* $out/share/icons/hicolor/$size/apps
171       done
172     '';
174   passthru = {
175     updateScript =
176       let
177         script = writeShellScript "${packageAttribute}-update-script" ''
178           set -o errexit
179           PATH=${
180             lib.makeBinPath [
181               common-updater-scripts
182               curl
183             ]
184           }
186           versionFile=$1
187           latestVersion=$(curl -s "${versionUrl}")
189           if [[ "${buildVersion}" = "$latestVersion" ]]; then
190               echo "The new version same as the old version."
191               exit 0
192           fi
194           for platform in ${lib.escapeShellArgs meta.platforms}; do
195               # The script will not perform an update when the version attribute is up to date from previous platform run
196               # We need to clear it before each run
197               update-source-version "${packageAttribute}.${primaryBinary}" 0 "${lib.fakeSha256}" --file="$versionFile" --version-key=buildVersion --source-key="sources.$platform"
198               update-source-version "${packageAttribute}.${primaryBinary}" "$latestVersion" --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 = "Sophisticated text editor for code, markup and prose";
210     homepage = "https://www.sublimetext.com/";
211     maintainers = with maintainers; [
212       jtojnar
213       wmertens
214       demin-dmitriy
215       zimbatm
216     ];
217     sourceProvenance = with sourceTypes; [ binaryNativeCode ];
218     license = licenses.unfree;
219     platforms = [
220       "aarch64-linux"
221       "x86_64-linux"
222     ];
223   };