diff --git a/packages/helix-git/default.nix b/packages/helix-git/default.nix new file mode 100644 index 0000000..06b718b --- /dev/null +++ b/packages/helix-git/default.nix @@ -0,0 +1,50 @@ +{ + lib, + fetchFromGitHub, + rustPlatform, + helix, + helix-unwrapped, +}: + +# Helix from git, built with nixpkgs' packaging instead of the upstream flake. The +# flake fetches every tree-sitter grammar without a hash at evaluation time. Here +# they are pinned in grammars.json, so they are fetched once and kept in the store. +# +# To update: change rev and hash, set cargoHash to lib.fakeHash, build and copy the +# expected hash, then run ./update-grammars.sh. +let + unwrapped = helix-unwrapped.overrideAttrs ( + final: prev: { + version = "25.07.1-unstable-${final.src.rev}"; + + src = fetchFromGitHub { + owner = "helix-editor"; + repo = "helix"; + rev = "f6f3eb1fe4a73933466f30175843059a4381a985"; + hash = "sha256-yfNBfV6HWAQU/xZXwrFoedsSYsDmCDIlTHRxAZI6teY="; + }; + + cargoDeps = rustPlatform.fetchCargoVendor { + inherit (final) src; + name = "${final.pname}-${final.version}-vendor"; + hash = lib.fakeHash; + }; + + # The mdbook 0.5 patch from nixpkgs targets the release + patches = [ ]; + + # Report the commit in `hx --version` + env = prev.env // { + HELIX_NIX_BUILD_REV = final.src.rev; + }; + + # The version check expects the release version string + doInstallCheck = false; + } + ); +in +(helix.override { + helix-unwrapped = unwrapped; + lockedGrammars = lib.importJSON ./grammars.json; +}).overrideAttrs + { passthru.unwrapped = unwrapped; } diff --git a/packages/helix-git/generate-grammars.py b/packages/helix-git/generate-grammars.py new file mode 100644 index 0000000..935cab5 --- /dev/null +++ b/packages/helix-git/generate-grammars.py @@ -0,0 +1,79 @@ +""" +Lock the tree-sitter grammars of a Helix languages.toml into grammars.json, in the +format nixpkgs' helix package takes as `lockedGrammars`. + +Based on nixpkgs' pkgs/by-name/he/helix/generate_grammars.py, but each fetch gets a +timeout and falls back to fetching over git. Some forges (e.g. Codeberg) can hang +while generating archives. + +Usage: generate-grammars.py +""" + +import asyncio +import json +import os +import sys +import tomllib + +TIMEOUT = 120 + + +async def nurl(url, rev, *extra): + proc = await asyncio.create_subprocess_exec( + "nurl", *extra, url, rev, "--json", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + try: + stdout, stderr = await asyncio.wait_for(proc.communicate(), TIMEOUT) + except asyncio.TimeoutError: + proc.kill() + await proc.wait() + raise RuntimeError(f"timed out after {TIMEOUT}s") + if proc.returncode != 0: + raise RuntimeError(stderr.decode().strip()) + return json.loads(stdout) + + +async def lock(grammar, semaphore): + source = grammar["source"] + async with semaphore: + try: + result = await nurl(source["git"], source["rev"]) + except RuntimeError as e: + print(f" {grammar['name']}: {e}, retrying over git", file=sys.stderr) + result = await nurl(source["git"], source["rev"], "-f", "fetchgit") + print(f" locked {grammar['name']}", file=sys.stderr) + return grammar["name"].replace("_", "-"), { + "nurl": result, + "subpath": source.get("subpath"), + } + + +async def main(languages_toml, output): + with open(languages_toml, "rb") as f: + grammars = [ + g + for g in tomllib.load(f).get("grammar", []) + if "git" in g.get("source", {}) and "rev" in g["source"] + ] + print(f"Locking {len(grammars)} grammars", file=sys.stderr) + + semaphore = asyncio.Semaphore(os.cpu_count() or 4) + results = await asyncio.gather( + *(lock(g, semaphore) for g in grammars), return_exceptions=True + ) + + failed = [(g["name"], r) for g, r in zip(grammars, results) if isinstance(r, Exception)] + if failed: + for name, error in failed: + print(f"failed: {name}: {error}", file=sys.stderr) + sys.exit(1) + + with open(output, "w") as f: + json.dump(dict(sorted(results)), f, indent=2) + f.write("\n") + + +if __name__ == "__main__": + asyncio.run(main(*sys.argv[1:3])) diff --git a/packages/helix-git/grammars.json b/packages/helix-git/grammars.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/packages/helix-git/grammars.json @@ -0,0 +1 @@ +{} diff --git a/packages/helix-git/update-grammars.sh b/packages/helix-git/update-grammars.sh new file mode 100755 index 0000000..bb50194 --- /dev/null +++ b/packages/helix-git/update-grammars.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# Regenerate grammars.json for the Helix revision pinned in default.nix +set -euo pipefail + +dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +flake="$(git -C "$dir" rev-parse --show-toplevel)" + +src="$(nix build --no-link --print-out-paths "$flake#helix-git.unwrapped.src")" + +nix shell --inputs-from "$flake" nixpkgs#nurl nixpkgs#nix-prefetch-git nixpkgs#python3 -c \ + python3 -I "$dir/generate-grammars.py" "$src/languages.toml" "$dir/grammars.json"