From 48b6b199708e418bf56bed158062bfac3ea9aa76 Mon Sep 17 00:00:00 2001 From: Julius de Bruijn Date: Thu, 9 Dec 2021 14:30:12 +0100 Subject: [PATCH 1/8] emacsPackages.tree-sitter: fix grammar/native executor --- .../emacs/elisp-packages/manual-packages.nix | 3 + .../tree-sitter-langs/default.nix | 55 ++++++++++++++ .../emacs/elisp-packages/tsc/default.nix | 73 +++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix create mode 100644 pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix b/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix index 56a3b696852d..6d8dbce2b32b 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix @@ -233,6 +233,9 @@ sv-kalender = callPackage ./sv-kalender { }; + tree-sitter-langs = callPackage ./tree-sitter-langs { }; + tsc = callPackage ./tsc { }; + youtube-dl = callPackage ./youtube-dl { }; # From old emacsPackages (pre emacsPackagesNg) diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix new file mode 100644 index 000000000000..1dbcf605adf9 --- /dev/null +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix @@ -0,0 +1,55 @@ +{ lib +, pkgs +, symlinkJoin +, fetchzip +, melpaBuild +, stdenv +, fetchFromGitHub +, writeText +}: + +let + version = "0.10.14"; + tree-sitter-grammars = stdenv.mkDerivation rec { + name = "tree-sitter-grammars"; + + inherit version; + + src = fetchzip { + name = "tree-sitter-grammars-linux-${version}.tar.gz"; + url = "https://github.com/emacs-tree-sitter/tree-sitter-langs/releases/download/${version}/${src.name}"; + sha256 = "sha256-J8VplZWhyWN8ur74Ep0CTl4nPtESzfs2Gh6MxfY5Zqc="; + stripRoot = false; + }; + + installPhase = '' + install -d $out/langs/bin + echo -n $version > $out/langs/bin/BUNDLE-VERSION + install -m444 * $out/langs/bin + ''; + }; +in melpaBuild rec { + inherit version; + + pname = "tree-sitter-langs"; + commit = version; + + src = fetchFromGitHub { + owner = "emacs-tree-sitter"; + repo = "tree-sitter-langs"; + rev = version; + sha256 = "sha256-uKfkhcm1k2Ov4fSr7ALVnpQoX/l9ssEWMn761pa7Y/c="; + }; + + recipe = writeText "recipe" '' + (tree-sitter-langs + :repo "emacs-tree-sitter/tree-sitter-langs" + :fetcher github + :files (:defaults "queries")) + ''; + + postPatch = '' + substituteInPlace ./tree-sitter-langs-build.el \ + --replace "tree-sitter-langs-grammar-dir tree-sitter-langs--dir" "tree-sitter-langs-grammar-dir \"${tree-sitter-grammars}/langs\"" + ''; +} diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix new file mode 100644 index 000000000000..aefb7d02004d --- /dev/null +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix @@ -0,0 +1,73 @@ +{ lib +, symlinkJoin +, melpaBuild +, fetchFromGitHub +, rustPlatform +, writeText +, clang +, llvmPackages +}: +with lib.licenses; +with rustPlatform; +with llvmPackages; + +let + version = "0.16.1"; + + src = fetchFromGitHub { + owner = "emacs-tree-sitter"; + repo = "elisp-tree-sitter"; + rev = version; + sha256 = "sha256-tAohHdAsy/HTFFPSNOo0UyrdolH8h0KF2ekFXuLltBE="; + }; + + tsc = melpaBuild rec { + inherit src; + inherit version; + + pname = "tsc"; + commit = version; + + sourceRoot = "source/core"; + + recipe = writeText "recipe" '' + (tsc + :repo "emacs-tree-sitter/elisp-tree-sitter" + :fetcher github) + ''; + }; + + tsc-dyn = buildRustPackage { + inherit version; + inherit src; + + pname = "tsc-dyn"; + + nativeBuildInputs = [ clang ]; + sourceRoot = "source/core"; + + configurePhase = '' + export LIBCLANG_PATH="${libclang.lib}/lib" + ''; + + postInstall = '' + LIB=($out/lib/libtsc_dyn.*) + TSC_PATH=$out/share/emacs/site-lisp/elpa/tsc-${version} + install -d $TSC_PATH + install -m444 $out/lib/libtsc_dyn.* $TSC_PATH/''${LIB/*libtsc_/tsc-} + echo -n $version > $TSC_PATH/DYN-VERSION + rm -r $out/lib + ''; + + cargoSha256 = "sha256-7UOhs3wx6fGvqPjNxUKoEHwPtiJ5zgLFPwDSvhYlmis="; + }; +in symlinkJoin { + name = "tsc"; + paths = [ tsc tsc-dyn ]; + + meta = { + description = "The core APIs of the Emacs binding for tree-sitter."; + license = mit; + maintainers = with maintainers; [ pimeys ]; + }; +} From 4567405cd4a6bcc9f5010d93eeec9c3aa3873c79 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sat, 23 Apr 2022 22:46:58 +1200 Subject: [PATCH 2/8] emacs.pkgs.tsc: Remove `with` from top-level scope This is a huge anti-pattern that makes it next to impossible to know which attrset is providing which attr and it's very easy to make mistakes. Case in point: This package didn't properly scope `lib.maintainers`. --- .../editors/emacs/elisp-packages/tsc/default.nix | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix index aefb7d02004d..3f686f96febe 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix @@ -7,9 +7,6 @@ , clang , llvmPackages }: -with lib.licenses; -with rustPlatform; -with llvmPackages; let version = "0.16.1"; @@ -37,7 +34,7 @@ let ''; }; - tsc-dyn = buildRustPackage { + tsc-dyn = rustPlatform.buildRustPackage { inherit version; inherit src; @@ -47,7 +44,7 @@ let sourceRoot = "source/core"; configurePhase = '' - export LIBCLANG_PATH="${libclang.lib}/lib" + export LIBCLANG_PATH="${llvmPackages.libclang.lib}/lib" ''; postInstall = '' @@ -61,13 +58,14 @@ let cargoSha256 = "sha256-7UOhs3wx6fGvqPjNxUKoEHwPtiJ5zgLFPwDSvhYlmis="; }; + in symlinkJoin { - name = "tsc"; + name = "tsc-${version}"; paths = [ tsc tsc-dyn ]; meta = { description = "The core APIs of the Emacs binding for tree-sitter."; - license = mit; - maintainers = with maintainers; [ pimeys ]; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ pimeys ]; }; } From 2c61820661e1633d4be0a090bdd3332988fb8272 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sun, 24 Apr 2022 00:11:50 +1200 Subject: [PATCH 3/8] emacs.pkgs.tsc/tree-sitter-langs: Remove `rec` from derivations These are not required. --- .../emacs/elisp-packages/tree-sitter-langs/default.nix | 10 ++++++---- .../editors/emacs/elisp-packages/tsc/default.nix | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix index 1dbcf605adf9..ffc3f5337496 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix @@ -10,14 +10,15 @@ let version = "0.10.14"; - tree-sitter-grammars = stdenv.mkDerivation rec { + + tree-sitter-grammars = stdenv.mkDerivation { name = "tree-sitter-grammars"; inherit version; - src = fetchzip { + src = fetchzip rec { name = "tree-sitter-grammars-linux-${version}.tar.gz"; - url = "https://github.com/emacs-tree-sitter/tree-sitter-langs/releases/download/${version}/${src.name}"; + url = "https://github.com/emacs-tree-sitter/tree-sitter-langs/releases/download/${version}/${name}"; sha256 = "sha256-J8VplZWhyWN8ur74Ep0CTl4nPtESzfs2Gh6MxfY5Zqc="; stripRoot = false; }; @@ -28,7 +29,8 @@ let install -m444 * $out/langs/bin ''; }; -in melpaBuild rec { + +in melpaBuild { inherit version; pname = "tree-sitter-langs"; diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix index 3f686f96febe..dcb950824d0d 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix @@ -18,7 +18,7 @@ let sha256 = "sha256-tAohHdAsy/HTFFPSNOo0UyrdolH8h0KF2ekFXuLltBE="; }; - tsc = melpaBuild rec { + tsc = melpaBuild { inherit src; inherit version; From 028a9348963e73a5998aeb1b3265b005ff06b6b2 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sun, 24 Apr 2022 02:08:29 +1200 Subject: [PATCH 4/8] emacs.pkgs.tsc: Add update script --- .../emacs/elisp-packages/tsc/default.nix | 21 +++ .../emacs/elisp-packages/tsc/update.py | 122 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 pkgs/applications/editors/emacs/elisp-packages/tsc/update.py diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix index dcb950824d0d..ae0c5c28736c 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix @@ -6,6 +6,12 @@ , writeText , clang , llvmPackages + +, runtimeShell +, writeScript +, python3 +, nix-prefetch-github +, nix }: let @@ -63,6 +69,21 @@ in symlinkJoin { name = "tsc-${version}"; paths = [ tsc tsc-dyn ]; + passthru = { + updateScript = let + pythonEnv = python3.withPackages(ps: [ ps.requests ]); + in writeScript "tsc-update" '' + #!${runtimeShell} + set -euo pipefail + export PATH=${lib.makeBinPath [ + nix-prefetch-github + nix + pythonEnv + ]}:$PATH + exec python3 ${builtins.toString ./update.py} ${builtins.toString ./.} + ''; + }; + meta = { description = "The core APIs of the Emacs binding for tree-sitter."; license = lib.licenses.mit; diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/update.py b/pkgs/applications/editors/emacs/elisp-packages/tsc/update.py new file mode 100644 index 000000000000..ce2054f909ef --- /dev/null +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/update.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 +from textwrap import dedent +from os.path import ( + abspath, + dirname, + join, +) +from typing import ( + Dict, + Any, +) +import subprocess +import tempfile +import json +import sys +import re + +import requests + + +def eval_drv(nixpkgs: str, expr: str) -> Any: + expr = "\n".join( + ( + "with (import %s {});" % nixpkgs, + expr, + ) + ) + + with tempfile.NamedTemporaryFile(mode="w") as f: + f.write(dedent(expr)) + f.flush() + p = subprocess.run( + ["nix-instantiate", "--json", f.name], stdout=subprocess.PIPE, check=True + ) + + return p.stdout.decode().strip() + + +def get_src(tag_name: str) -> Dict[str, str]: + p = subprocess.run( + [ + "nix-prefetch-github", + "--rev", + tag_name, + "--json", + "emacs-tree-sitter", + "elisp-tree-sitter", + ], + stdout=subprocess.PIPE, + check=True, + ) + src = json.loads(p.stdout) + + fields = ["owner", "repo", "rev", "sha256"] + + return {f: src[f] for f in fields} + + +def get_cargo_sha256(drv_path: str): + # Note: No check=True since we expect this command to fail + p = subprocess.run(["nix-store", "-r", drv_path], stderr=subprocess.PIPE) + + stderr = p.stderr.decode() + lines = iter(stderr.split("\n")) + + for l in lines: + if l.startswith("error: hash mismatch in fixed-output derivation"): + break + else: + raise ValueError("Did not find expected hash mismatch message") + + for l in lines: + m = re.match(r"\s+got:\s+(.+)$", l) + if m: + return m.group(1) + + raise ValueError("Could not extract actual sha256 hash: ", stderr) + + +if __name__ == "__main__": + cwd = sys.argv[1] + + nixpkgs = abspath(join(cwd, "../../../../../..")) + + tag_name = requests.get( + "https://api.github.com/repos/emacs-tree-sitter/elisp-tree-sitter/releases/latest" + ).json()["tag_name"] + + src = get_src(tag_name) + + with tempfile.NamedTemporaryFile(mode="w") as f: + json.dump(src, f) + f.flush() + + drv_path = eval_drv( + nixpkgs, + """ + rustPlatform.buildRustPackage { + pname = "tsc-dyn"; + version = "%s"; + nativeBuildInputs = [ clang ]; + src = fetchFromGitHub (lib.importJSON %s); + sourceRoot = "source/core"; + cargoSha256 = lib.fakeSha256; + } + """ + % (tag_name, f.name), + ) + + cargo_sha256 = get_cargo_sha256(drv_path) + + with open(join(cwd, "src.json"), mode="w") as f: + json.dump( + { + "src": src, + "version": tag_name, + "cargoSha256": cargo_sha256, + }, + f, + indent=2, + ) + f.write("\n") From 00af59359fd4ce22922b438527ebe71fefea586e Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sun, 24 Apr 2022 02:10:38 +1200 Subject: [PATCH 5/8] emacs.pkgs.tsc: 0.16.1 -> 0.18.0 Use auto-updater JSON --- .../editors/emacs/elisp-packages/tsc/default.nix | 13 +++++-------- .../editors/emacs/elisp-packages/tsc/src.json | 10 ++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 pkgs/applications/editors/emacs/elisp-packages/tsc/src.json diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix index ae0c5c28736c..a9462298979d 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/default.nix @@ -15,14 +15,11 @@ }: let - version = "0.16.1"; - src = fetchFromGitHub { - owner = "emacs-tree-sitter"; - repo = "elisp-tree-sitter"; - rev = version; - sha256 = "sha256-tAohHdAsy/HTFFPSNOo0UyrdolH8h0KF2ekFXuLltBE="; - }; + srcMeta = lib.importJSON ./src.json; + inherit (srcMeta) version; + + src = fetchFromGitHub srcMeta.src; tsc = melpaBuild { inherit src; @@ -62,7 +59,7 @@ let rm -r $out/lib ''; - cargoSha256 = "sha256-7UOhs3wx6fGvqPjNxUKoEHwPtiJ5zgLFPwDSvhYlmis="; + inherit (srcMeta) cargoSha256; }; in symlinkJoin { diff --git a/pkgs/applications/editors/emacs/elisp-packages/tsc/src.json b/pkgs/applications/editors/emacs/elisp-packages/tsc/src.json new file mode 100644 index 000000000000..6aa6fee1830a --- /dev/null +++ b/pkgs/applications/editors/emacs/elisp-packages/tsc/src.json @@ -0,0 +1,10 @@ +{ + "src": { + "owner": "emacs-tree-sitter", + "repo": "elisp-tree-sitter", + "rev": "909717c685ff5a2327fa2ca8fb8a25216129361c", + "sha256": "LrakDpP3ZhRQqz47dPcyoQnu5lROdaNlxGaQfQT6u+k=" + }, + "version": "0.18.0", + "cargoSha256": "sha256-IRCZqszBkGF8anF/kpcPOzHdOP4lAtJBAp6FS5tAOx8=" +} From b15c703495742190e6d5cf37ac0980391c323428 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sun, 24 Apr 2022 02:41:43 +1200 Subject: [PATCH 6/8] emacs.pkgs.tree-sitter-langs: Use grammars from nixpkgs and sources from melpa --- .../tree-sitter-langs/default.nix | 94 +++++++++++-------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix index ffc3f5337496..93b4436e9520 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix @@ -6,52 +6,66 @@ , stdenv , fetchFromGitHub , writeText +, melpaStablePackages +, runCommand +, tree-sitter-grammars }: let - version = "0.10.14"; - tree-sitter-grammars = stdenv.mkDerivation { - name = "tree-sitter-grammars"; + inherit (melpaStablePackages) tree-sitter-langs; - inherit version; + # Note: Commented grammars are in upstream bundle but missing from our packaged grammars + grammars = [ + "agda" + "bash" + "c" + "c-sharp" + "cpp" + "css" + # "d" + "elixir" + "elm" + "fluent" + "go" + "haskell" + "hcl" + "html" + # "janet-simple" + "java" + "javascript" + "jsdoc" + "json" + "julia" + "nix" + "ocaml" + "ocaml-interface" + # "pgn" + "php" + "prisma" + "python" + "ruby" + "rust" + "scala" + "swift" + "tsx" + "typescript" + "verilog" + "zig" + ]; - src = fetchzip rec { - name = "tree-sitter-grammars-linux-${version}.tar.gz"; - url = "https://github.com/emacs-tree-sitter/tree-sitter-langs/releases/download/${version}/${name}"; - sha256 = "sha256-J8VplZWhyWN8ur74Ep0CTl4nPtESzfs2Gh6MxfY5Zqc="; - stripRoot = false; - }; + grammarDir = runCommand "emacs-tree-sitter-grammars" { + # Fake same version number as upstream language bundle to prevent triggering downloads + inherit (tree-sitter-langs) version; + } ('' + install -d $out/langs/bin + echo -n $version > $out/langs/bin/BUNDLE-VERSION + '' + lib.concatStringsSep "\n" (map (g: "ln -s ${tree-sitter-grammars."tree-sitter-${g}"}/parser $out/langs/bin/${g}.so") grammars)); - installPhase = '' - install -d $out/langs/bin - echo -n $version > $out/langs/bin/BUNDLE-VERSION - install -m444 * $out/langs/bin - ''; - }; - -in melpaBuild { - inherit version; - - pname = "tree-sitter-langs"; - commit = version; - - src = fetchFromGitHub { - owner = "emacs-tree-sitter"; - repo = "tree-sitter-langs"; - rev = version; - sha256 = "sha256-uKfkhcm1k2Ov4fSr7ALVnpQoX/l9ssEWMn761pa7Y/c="; - }; - - recipe = writeText "recipe" '' - (tree-sitter-langs - :repo "emacs-tree-sitter/tree-sitter-langs" - :fetcher github - :files (:defaults "queries")) - ''; - - postPatch = '' +in +melpaStablePackages.tree-sitter-langs.overrideAttrs(old: { + postPatch = old.postPatch or "" + '' substituteInPlace ./tree-sitter-langs-build.el \ - --replace "tree-sitter-langs-grammar-dir tree-sitter-langs--dir" "tree-sitter-langs-grammar-dir \"${tree-sitter-grammars}/langs\"" + --replace "tree-sitter-langs-grammar-dir tree-sitter-langs--dir" "tree-sitter-langs-grammar-dir \"${grammarDir}/langs\"" ''; -} +}) From b4f90318fecf53ca26a104a39ac42c914582dfe4 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sun, 24 Apr 2022 07:18:42 +1200 Subject: [PATCH 7/8] emacs.pkgs.tree-sitter-langs: Create script to keep grammars up to date with upstream defaults --- .../tree-sitter-langs/default-grammars.json | 32 ++++++++ .../tree-sitter-langs/default.nix | 49 ++---------- .../tree-sitter-langs/update-defaults.py | 74 +++++++++++++++++++ 3 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json create mode 100755 pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json new file mode 100644 index 000000000000..6a5608cbf8d5 --- /dev/null +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json @@ -0,0 +1,32 @@ +[ + "tree-sitter-agda", + "tree-sitter-bash", + "tree-sitter-c", + "tree-sitter-c-sharp", + "tree-sitter-cpp", + "tree-sitter-css", + "tree-sitter-elixir", + "tree-sitter-elm", + "tree-sitter-fluent", + "tree-sitter-go", + "tree-sitter-haskell", + "tree-sitter-hcl", + "tree-sitter-html", + "tree-sitter-java", + "tree-sitter-javascript", + "tree-sitter-jsdoc", + "tree-sitter-json", + "tree-sitter-julia", + "tree-sitter-nix", + "tree-sitter-ocaml", + "tree-sitter-php", + "tree-sitter-prisma", + "tree-sitter-python", + "tree-sitter-ruby", + "tree-sitter-rust", + "tree-sitter-scala", + "tree-sitter-swift", + "tree-sitter-typescript", + "tree-sitter-verilog", + "tree-sitter-zig" +] diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix index 93b4436e9520..99fa87abf018 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix @@ -12,55 +12,22 @@ }: let - inherit (melpaStablePackages) tree-sitter-langs; - # Note: Commented grammars are in upstream bundle but missing from our packaged grammars - grammars = [ - "agda" - "bash" - "c" - "c-sharp" - "cpp" - "css" - # "d" - "elixir" - "elm" - "fluent" - "go" - "haskell" - "hcl" - "html" - # "janet-simple" - "java" - "javascript" - "jsdoc" - "json" - "julia" - "nix" - "ocaml" - "ocaml-interface" - # "pgn" - "php" - "prisma" - "python" - "ruby" - "rust" - "scala" - "swift" - "tsx" - "typescript" - "verilog" - "zig" - ]; + grammars = map (g: tree-sitter-grammars.${g}) (lib.importJSON ./default-grammars.json); + + libSuffix = if stdenv.isDarwin then "dylib" else "so"; + soName = g: lib.removeSuffix "-grammar" (lib.removePrefix "tree-sitter-" g.pname) + "." + libSuffix; grammarDir = runCommand "emacs-tree-sitter-grammars" { - # Fake same version number as upstream language bundle to prevent triggering downloads + # Fake same version number as upstream language bundle to prevent triggering runtime downloads inherit (tree-sitter-langs) version; } ('' install -d $out/langs/bin echo -n $version > $out/langs/bin/BUNDLE-VERSION - '' + lib.concatStringsSep "\n" (map (g: "ln -s ${tree-sitter-grammars."tree-sitter-${g}"}/parser $out/langs/bin/${g}.so") grammars)); + '' + lib.concatStringsSep "\n" (map ( + g: "ln -s ${g}/parser $out/langs/bin/${soName g}") grammars + )); in melpaStablePackages.tree-sitter-langs.overrideAttrs(old: { diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py new file mode 100755 index 000000000000..19bcb8989c30 --- /dev/null +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py @@ -0,0 +1,74 @@ +#!/usr/bin/env nix-shell +#! nix-shell ../../../../../../. -i python3 -p python3 -p nix +from os.path import ( + dirname, + abspath, + join, +) +from typing import ( + List, + Any, +) +import subprocess +import json +import sys +import os + + +def fmt_grammar(grammar: str) -> str: + return "tree-sitter-" + grammar + + +def eval_expr(nixpkgs: str, expr: str) -> Any: + p = subprocess.run( + [ + "nix-instantiate", + "--json", + "--eval", + "--expr", + ("with import %s {}; %s" % (nixpkgs, expr)), + ], + check=True, + stdout=subprocess.PIPE, + ) + return json.loads(p.stdout) + + +def check_grammar_exists(nixpkgs: str, grammar: str) -> bool: + return eval_expr( + nixpkgs, f'lib.hasAttr "{fmt_grammar(grammar)}" tree-sitter-grammars' + ) + + +def build_attr(nixpkgs, attr: str) -> str: + return ( + subprocess.run( + ["nix-build", "--no-out-link", nixpkgs, "-A", attr], + check=True, + stdout=subprocess.PIPE, + ) + .stdout.decode() + .strip() + ) + + +if __name__ == "__main__": + cwd = dirname(abspath(__file__)) + nixpkgs = abspath(join(cwd, "../../../../../..")) + + src_dir = build_attr(nixpkgs, "emacs.pkgs.tree-sitter-langs.src") + + existing: List[str] = [] + + grammars = os.listdir(join(src_dir, "repos")) + for g in grammars: + exists = check_grammar_exists(nixpkgs, g) + if exists: + existing.append(fmt_grammar(g)) + else: + sys.stderr.write("Missing grammar: " + fmt_grammar(g) + "\n") + sys.stderr.flush() + + with open(join(cwd, "default-grammars.json"), mode="w") as f: + json.dump(sorted(existing), f, indent=2) + f.write("\n") From daee75e5b32a00c9d5261be4de29ff9a8ccc1dd2 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sun, 24 Apr 2022 07:44:40 +1200 Subject: [PATCH 8/8] emacs.pkgs.tree-sitter-langs: Make language plugins configurable By using an expression like: ``` nix emacs.pkgs.tree-sitter-langs.passthru.withPlugins (p: [ p.tree-sitter-agda ]) ``` --- .../editors/emacs/elisp-packages/manual-packages.nix | 2 +- .../elisp-packages/tree-sitter-langs/default.nix | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix b/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix index 6d8dbce2b32b..4dc546c6263d 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/manual-packages.nix @@ -233,7 +233,7 @@ sv-kalender = callPackage ./sv-kalender { }; - tree-sitter-langs = callPackage ./tree-sitter-langs { }; + tree-sitter-langs = callPackage ./tree-sitter-langs { final = self; }; tsc = callPackage ./tsc { }; youtube-dl = callPackage ./youtube-dl { }; diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix index 99fa87abf018..e62a37565c83 100644 --- a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix +++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix @@ -9,13 +9,13 @@ , melpaStablePackages , runCommand , tree-sitter-grammars +, plugins ? map (g: tree-sitter-grammars.${g}) (lib.importJSON ./default-grammars.json) +, final }: let inherit (melpaStablePackages) tree-sitter-langs; - grammars = map (g: tree-sitter-grammars.${g}) (lib.importJSON ./default-grammars.json); - libSuffix = if stdenv.isDarwin then "dylib" else "so"; soName = g: lib.removeSuffix "-grammar" (lib.removePrefix "tree-sitter-" g.pname) + "." + libSuffix; @@ -26,7 +26,7 @@ let install -d $out/langs/bin echo -n $version > $out/langs/bin/BUNDLE-VERSION '' + lib.concatStringsSep "\n" (map ( - g: "ln -s ${g}/parser $out/langs/bin/${soName g}") grammars + g: "ln -s ${g}/parser $out/langs/bin/${soName g}") plugins )); in @@ -35,4 +35,10 @@ melpaStablePackages.tree-sitter-langs.overrideAttrs(old: { substituteInPlace ./tree-sitter-langs-build.el \ --replace "tree-sitter-langs-grammar-dir tree-sitter-langs--dir" "tree-sitter-langs-grammar-dir \"${grammarDir}/langs\"" ''; + + passthru = old.passthru or {} // { + inherit plugins; + withPlugins = fn: final.tree-sitter-langs.override { plugins = fn tree-sitter-grammars; }; + }; + })