helix: 23.03 -> 23.05

Switch to self-build of the tree-sitter grammar files and
use the git repo as source directly
This commit is contained in:
Leona Maroni 2023-05-18 17:07:32 +02:00 committed by Yt
parent 055e998b00
commit 95c7a595ce
5 changed files with 179 additions and 2580 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,33 +1,33 @@
{ fetchzip, lib, rustPlatform, installShellFiles, makeWrapper }:
{ fetchFromGitHub, lib, rustPlatform, installShellFiles, makeWrapper, callPackage }:
let
version = "23.05";
src = fetchFromGitHub {
owner = "helix-editor";
repo = "helix";
rev = "${version}";
hash = "sha256-Ws9uWtZLvTwL5HNonFr4YwyPoTU8QlCvhs6IJ92aLDw=";
};
grammars = callPackage ./grammars.nix { };
in rustPlatform.buildRustPackage {
inherit src version;
rustPlatform.buildRustPackage rec {
pname = "helix";
version = "23.03";
# This release tarball includes source code for the tree-sitter grammars,
# which is not ordinarily part of the repository.
src = fetchzip {
url = "https://github.com/helix-editor/helix/releases/download/${version}/helix-${version}-source.tar.xz";
hash = "sha256-FtY2V7za3WGeUaC2t2f63CcDUEg9zAS2cGUWI0YeGwk=";
stripRoot = false;
};
# should be removed, when tree-sitter is not used as a git checkout anymore
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"tree-sitter-0.20.9" = "sha256-/PaFaASOT0Z8FpipX5uiRCjnv1kyZtg4B9+TnHA0yTY=";
};
};
cargoSha256 = "sha256-/LCtfyDAA2JuioBD/CDMv6OOxM0B9A3PpuVP/YY5oF0=";
nativeBuildInputs = [ installShellFiles makeWrapper ];
postInstall = ''
# not needed at runtime
rm -r runtime/grammars/sources
# We self build the grammar files
rm -r runtime/grammars
mkdir -p $out/lib
cp -r runtime $out/lib
ln -s ${grammars} $out/lib/runtime/grammars
installShellCompletion contrib/completion/hx.{bash,fish,zsh}
mkdir -p $out/share/{applications,icons/hicolor/256x256/apps}
cp contrib/Helix.desktop $out/share/applications
@ -37,6 +37,11 @@ rustPlatform.buildRustPackage rec {
wrapProgram $out/bin/hx --set HELIX_RUNTIME $out/lib/runtime
'';
# disable fetching and building of tree-sitter grammars in favor of the custom build process in ./grammars.nix
env.HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
passthru.updateScript = ./update.py;
meta = with lib; {
description = "A post-modern modal text editor";
homepage = "https://helix-editor.com";

View file

@ -0,0 +1,107 @@
{
stdenv,
lib,
runCommand,
includeGrammarIf ? _: true,
fetchFromGitHub,
fetchgit,
...
}: let
# similiar to https://github.com/helix-editor/helix/blob/23.05/grammars.nix
grammars = builtins.fromJSON (builtins.readFile ./language-grammars.json);
isGitGrammar = grammar:
builtins.hasAttr "source" grammar
&& builtins.hasAttr "git" grammar.source
&& builtins.hasAttr "rev" grammar.source;
isGitHubGrammar = grammar: lib.hasPrefix "https://github.com" grammar.source.git;
toGitHubFetcher = url: let
match = builtins.match "https://github\.com/([^/]*)/([^/]*)/?" url;
in {
owner = builtins.elemAt match 0;
repo = builtins.elemAt match 1;
};
gitGrammars = builtins.filter isGitGrammar grammars;
buildGrammar = grammar: let
gh = toGitHubFetcher grammar.source.git;
sourceGit = fetchgit {
url = grammar.source.git;
inherit (grammar.source) rev sha256;
};
sourceGitHub = fetchFromGitHub {
owner = gh.owner;
repo = gh.repo;
inherit (grammar.source) rev sha256;
};
source =
if isGitHubGrammar grammar
then sourceGitHub
else sourceGit;
in
stdenv.mkDerivation rec {
# similar to tree-sitter grammar generation
pname = "helix-tree-sitter-grammar-${grammar.name}";
version = grammar.source.rev;
src =
if builtins.hasAttr "subpath" grammar.source
then "${source}/${grammar.source.subpath}"
else source;
dontUnpack = true;
dontConfigure = true;
FLAGS = [
"-I${src}/src"
"-g"
"-O3"
"-fPIC"
"-fno-exceptions"
"-Wl,-z,relro,-z,now"
];
NAME = grammar.name;
buildPhase = ''
runHook preBuild
if [[ -e "$src/src/scanner.cc" ]]; then
$CXX -c "$src/src/scanner.cc" -o scanner.o $FLAGS
elif [[ -e "$src/src/scanner.c" ]]; then
$CC -c "$src/src/scanner.c" -o scanner.o $FLAGS
fi
$CC -c "$src/src/parser.c" -o parser.o $FLAGS
$CXX -shared -o $NAME.so *.o
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir $out
mv $NAME.so $out/
runHook postInstall
'';
# Strip failed on darwin: strip: error: symbols referenced by indirect symbol table entries that can't be stripped
fixupPhase = lib.optionalString stdenv.isLinux ''
runHook preFixup
$STRIP $out/$NAME.so
runHook postFixup
'';
};
grammarsToBuild = builtins.filter includeGrammarIf gitGrammars;
builtGrammars =
builtins.map (grammar: {
inherit (grammar) name;
artifact = buildGrammar grammar;
})
grammarsToBuild;
grammarLinks =
builtins.map (grammar: "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
builtGrammars;
in
runCommand "helix-tree-sitter-grammars" {} ''
mkdir -p $out
${builtins.concatStringsSep "\n" grammarLinks}
''

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,47 @@
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p nix-update python3 python3Packages.requests python3.pkgs.tomlkit nix-prefetch-git
import tomlkit
import json
import requests
import subprocess
from pathlib import Path
latest_release_url = "https://api.github.com/repos/helix-editor/helix/releases/latest"
def get_latest_release():
res = requests.get(latest_release_url)
res.raise_for_status()
return res.json()["tag_name"]
def get_grammar_config():
res = requests.get(f"https://raw.githubusercontent.com/helix-editor/helix/{version}/languages.toml")
res.raise_for_status()
return tomlkit.parse(res.text)["grammar"]
def calculate_sha256(url, rev):
out = subprocess.check_output([
"nix-prefetch-git", "--quiet",
"--url", url,
"--rev", rev])
return json.loads(out)["sha256"]
version = get_latest_release()
grammars = get_grammar_config()
for grammar in grammars:
if grammar["source"].get("git") is not None:
grammar["source"]["sha256"] = calculate_sha256(
grammar["source"]["git"], grammar["source"]["rev"])
json_grammars = json.dumps(grammars)
with open(Path(__file__).parent / "language-grammars.json", "w") as file:
file.write(json_grammars + "\n")
subprocess.run([
"nix-update", "helix",
"--version", version,
])