Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sublime text 4 #119351

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
157 changes: 157 additions & 0 deletions pkgs/applications/editors/sublime/4/common.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
{ buildVersion, aarch64sha256, x64sha256, dev ? false }:

{ lib, fetchurl, stdenv, xorg, glib, libglvnd, glibcLocales, gtk3, cairo, pango, libredirect, makeWrapper, wrapGAppsHook
, pkexecPath ? "/run/wrappers/bin/pkexec"
, writeScript, common-updater-scripts, curl, jq, gnugrep
, openssl, bzip2, bash, unzip, zip
}:

let
pname = "sublimetext4";
packageAttribute = "sublime4${stdenv.lib.optionalString dev "-dev"}";
binaries = [ "sublime_text" "plugin_host-3.3" "plugin_host-3.8" "crash_reporter" ];
primaryBinary = "sublime_text";
primaryBinaryAliases = [ "subl" "sublime" "sublime4" ];
downloadUrl = "https://download.sublimetext.com/sublime_text_build_${buildVersion}_${arch}.tar.xz";
versionUrl = "https://www.sublimetext.com/updates/4/dev_update_check";
versionFile = lib.toString ./packages.nix;
archSha256 = {
"aarch64-linux" = aarch64sha256;
"x86_64-linux" = x64sha256;
}.${stdenv.hostPlatform.system};
arch = {
"aarch64-linux" = "arm64";
"x86_64-linux" = "x64";
}.${stdenv.hostPlatform.system};

libPath = lib.makeLibraryPath [ xorg.libX11 glib libglvnd openssl gtk3 cairo pango ];
redirects = [ "/usr/bin/pkexec=${pkexecPath}" ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the package updated in my pkgset:
https://github.com/jtojnar/nixfiles/blob/master/pkgs/sublime4/common.nix
It turned out that libredirect is more trouble than it is worth so I got rid of it:

jtojnar/nixfiles@38c413a

sublimehq/sublime_text#3502

in let
binaryPackage = stdenv.mkDerivation {
pname = "${pname}-bin";
version = buildVersion;

src = fetchurl {
url = downloadUrl;
sha256 = archSha256;
};

dontStrip = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not strip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's from this commit: d619f0e

Here they mention it's important for pre-built binary.
#15969 (comment)

I don't know why this is needed. I just copied the code from version 3 and adapted for 4.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats about dwarf fortress. If it doesn't crash sublime we should just strip it.

dontPatchELF = true;
buildInputs = [ glib gtk3 ]; # for GSETTINGS_SCHEMAS_PATH
nativeBuildInputs = [ zip unzip makeWrapper wrapGAppsHook ];

# make exec.py in Default.sublime-package use own bash with an LD_PRELOAD instead of "/bin/bash"
patchPhase = ''
runHook prePatch

# TODO: Should not be necessary even in 3
mkdir Default.sublime-package-fix
( cd Default.sublime-package-fix
unzip -q ../Packages/Default.sublime-package
substituteInPlace "exec.py" --replace \
"[\"/bin/bash\"" \
"[\"$out/sublime_bash\""
zip -q ../Packages/Default.sublime-package **/*
)
rm -r Default.sublime-package-fix

runHook postPatch
'';

buildPhase = ''
runHook preBuild

for binary in ${lib.concatStringsSep " " binaries }; do
patchelf \
--interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
--set-rpath ${libPath}:${stdenv.cc.cc.lib}/lib${lib.optionalString stdenv.is64bit "64"} \
$binary
done

# Rewrite pkexec argument. Note that we cannot delete bytes in binary.
sed -i -e 's,/bin/cp\x00,cp\x00\x00\x00\x00\x00\x00,g' ${primaryBinary}

runHook postBuild
'';

installPhase = ''
runHook preInstall

mkdir -p $out
cp -r * $out/

# We can't just call /usr/bin/env bash because a relocation error occurs
# when trying to run a build from within Sublime Text
ln -s ${bash}/bin/bash $out/sublime_bash

runHook postInstall
'';

dontWrapGApps = true; # non-standard location, need to wrap the executables manually

postFixup = ''
wrapProgram $out/sublime_bash \
--set LD_PRELOAD "${stdenv.cc.cc.lib}/lib${lib.optionalString stdenv.is64bit "64"}/libgcc_s.so.1"

wrapProgram $out/${primaryBinary} \
--set LD_PRELOAD "${libredirect}/lib/libredirect.so" \
--set NIX_REDIRECTS ${builtins.concatStringsSep ":" redirects} \
--set LOCALE_ARCHIVE "${glibcLocales.out}/lib/locale/locale-archive" \
"''${gappsWrapperArgs[@]}"

# Without this, plugin_host crashes, even though it has the rpath
for host in $out/plugin_host-{3.3,3.8}; do
wrapProgram $host --prefix LD_PRELOAD : ${stdenv.cc.cc.lib}/lib${lib.optionalString stdenv.is64bit "64"}/libgcc_s.so.1:${openssl.out}/lib/libssl.so:${bzip2.out}/lib/libbz2.so
done
'';
};
in stdenv.mkDerivation (rec {
inherit pname;
version = buildVersion;

${primaryBinary} = binaryPackage;

nativeBuildInputs = [ makeWrapper ];

installPhase = ''
mkdir -p "$out/bin"
makeWrapper "''$${primaryBinary}/${primaryBinary}" "$out/bin/${primaryBinary}"
'' + builtins.concatStringsSep "" (map (binaryAlias: "ln -s $out/bin/${primaryBinary} $out/bin/${binaryAlias}\n") primaryBinaryAliases) + ''
mkdir -p "$out/share/applications"
substitute "''$${primaryBinary}/${primaryBinary}.desktop" "$out/share/applications/${primaryBinary}.desktop" --replace "/opt/${primaryBinary}/${primaryBinary}" "$out/bin/${primaryBinary}"
for directory in ''$${primaryBinary}/Icon/*; do
size=$(basename $directory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$directory and $size could benefit from some quoting, given that they are are from the package and might contain spaces in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already iterate over * so that will not help, unless we use the ultra ugly find | read hack.

mkdir -p "$out/share/icons/hicolor/$size/apps"
ln -s ''$${primaryBinary}/Icon/$size/* $out/share/icons/hicolor/$size/apps
done
'';

passthru.updateScript = writeScript "${pname}-update-script" ''
#!${stdenv.shell}
set -o errexit
PATH=${lib.makeBinPath [ common-updater-scripts curl jq gnugrep ]}

latestVersion=$(curl -s ${versionUrl} | jq .latest_version)

if [[ "${buildVersion}" = "$latestVersion" ]]; then
echo "The new version same as the old version."
exit 0
fi

for platform in ${lib.concatStringsSep " " meta.platforms}; do
# The script will not perform an update when the version attribute is up to date from previous platform run
# We need to clear it before each run
update-source-version ${packageAttribute}.${primaryBinary} 0 0000000000000000000000000000000000000000000000000000000000000000 --file=${versionFile} --version-key=buildVersion --system=$platform
update-source-version ${packageAttribute}.${primaryBinary} $latestVersion --file=${versionFile} --version-key=buildVersion --system=$platform
done
'';

meta = with lib; {
description = "Sophisticated text editor for code, markup and prose";
homepage = "https://www.sublimetext.com/";
maintainers = with maintainers; [ jtojnar wmertens demin-dmitriy zimbatm ];
license = licenses.unfree;
platforms = [ "aarch64-linux" "x86_64-linux" ];
};
})
13 changes: 13 additions & 0 deletions pkgs/applications/editors/sublime/4/packages.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ callPackage }:

let
common = opts: callPackage (import ./common.nix opts);
in
{
sublime4-dev = common {
buildVersion = "4102";
dev = true;
x64sha256 = "1xbrh90aa785fg46iqhpxl87kxqlkfj467a8y93z4ydkhf80nwi9";
aarch64sha256 = "00km0fddlfjd7wbz0ci4fjd42aibfpdgxgaahs2bnknsza4nfhbq";
} {};
}
3 changes: 3 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25871,6 +25871,9 @@ in

sublime3-dev = sublime3Packages.sublime3-dev;

inherit (recurseIntoAttrs (callPackage ../applications/editors/sublime/4/packages.nix { }))
sublime4-dev;

inherit (callPackage ../applications/version-management/sublime-merge {})
sublime-merge
sublime-merge-dev;
Expand Down