@@ -0,0 +1,87 @@
1+{ stdenv, lib, runCommand, yj }:
2+let
3+ # HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON
4+ # before parsing
5+ languages-json = runCommand "languages-toml-to-json" { } ''
6+ ${yj}/bin/yj -t < ${./languages.toml} > $out
7+ '';
8+ languagesConfig =
9+ builtins.fromJSON (builtins.readFile (builtins.toPath languages-json));
10+ isGitGrammar = (grammar:
11+ builtins.hasAttr "source" grammar && builtins.hasAttr "git" grammar.source
12+ && builtins.hasAttr "rev" grammar.source);
13+ gitGrammars = builtins.filter isGitGrammar languagesConfig.grammar;
14+ buildGrammar = grammar:
15+ let
16+ source = builtins.fetchGit {
17+ url = grammar.source.git;
18+ rev = grammar.source.rev;
19+ allRefs = true;
20+ };
21+ in stdenv.mkDerivation rec {
22+ # see https://github.com/NixOS/nixpkgs/blob/fbdd1a7c0bc29af5325e0d7dd70e804a972eb465/pkgs/development/tools/parsing/tree-sitter/grammar.nix
23+
24+ pname = "helix-tree-sitter-${grammar.name}";
25+ version = grammar.source.rev;
26+
27+ src = if builtins.hasAttr "subpath" grammar.source then
28+ "${source}/${grammar.source.subpath}"
29+ else
30+ source;
31+
32+ dontUnpack = true;
33+ dontConfigure = true;
34+
35+ FLAGS = [
36+ "-I${src}/src"
37+ "-g"
38+ "-O3"
39+ "-fPIC"
40+ "-fno-exceptions"
41+ "-Wl,-z,relro,-z,now"
42+ ];
43+
44+ NAME = grammar.name;
45+
46+ buildPhase = ''
47+ runHook preBuild
48+
49+ if [[ -e "$src/src/scanner.cc" ]]; then
50+ $CXX -c "$src/src/scanner.cc" -o scanner.o $FLAGS
51+ elif [[ -e "$src/src/scanner.c" ]]; then
52+ $CC -c "$src/src/scanner.c" -o scanner.o $FLAGS
53+ fi
54+
55+ $CC -c "$src/src/parser.c" -o parser.o $FLAGS
56+ $CXX -shared -o $NAME.so *.o
57+
58+ ls -al
59+
60+ runHook postBuild
61+ '';
62+
63+ installPhase = ''
64+ runHook preInstall
65+ mkdir $out
66+ mv $NAME.so $out/
67+ runHook postInstall
68+ '';
69+
70+ # Strip failed on darwin: strip: error: symbols referenced by indirect symbol table entries that can't be stripped
71+ fixupPhase = lib.optionalString stdenv.isLinux ''
72+ runHook preFixup
73+ $STRIP $out/$NAME.so
74+ runHook postFixup
75+ '';
76+ };
77+ builtGrammars = builtins.map (grammar: {
78+ inherit (grammar) name;
79+ artifact = buildGrammar grammar;
80+ }) gitGrammars;
81+ grammarLinks = builtins.map (grammar:
82+ "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
83+ builtGrammars;
84+in runCommand "consolidated-helix-grammars" { } ''
85+ mkdir -p $out
86+ ${builtins.concatStringsSep "\n" grammarLinks}
87+''