From 85a5df03911be8f7845921a503ffb707bfc6bab4 Mon Sep 17 00:00:00 2001
From: Yusuf Bera Ertan <y.bera003.06@protonmail.com>
Date: Wed, 27 Jul 2022 17:52:07 +0000
Subject: [PATCH] build(nix): add a way to override what grammars get built
 (#3141)

---
 flake.nix    | 178 ++++++++++++++++++++++++++++++---------------------
 grammars.nix |   5 +-
 2 files changed, 109 insertions(+), 74 deletions(-)

diff --git a/flake.nix b/flake.nix
index fdeed2aa..63501f9c 100644
--- a/flake.nix
+++ b/flake.nix
@@ -18,83 +18,115 @@
     nixpkgs,
     nixCargoIntegration,
     ...
-  }:
-    nixCargoIntegration.lib.makeOutputs {
-      root = ./.;
-      renameOutputs = {"helix-term" = "helix";};
-      # Set default app to hx (binary is from helix-term release build)
-      # Set default package to helix-term release build
-      defaultOutputs = {
-        app = "hx";
-        package = "helix";
-      };
-      overrides = {
-        cCompiler = common:
-          with common.pkgs;
-            if stdenv.isLinux
-            then gcc
-            else clang;
-        crateOverrides = common: _: {
-          helix-term = prev: let
-            inherit (common) pkgs;
-            mkRootPath = rel:
-              builtins.path {
-                path = "${common.root}/${rel}";
-                name = rel;
-              };
-            grammars = pkgs.callPackage ./grammars.nix {};
-            runtimeDir = pkgs.runCommandNoCC "helix-runtime" {} ''
-              mkdir -p $out
-              ln -s ${mkRootPath "runtime"}/* $out
-              rm -r $out/grammars
-              ln -s ${grammars} $out/grammars
-            '';
-          in {
-            # disable fetching and building of tree-sitter grammars in the helix-term build.rs
-            HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
-            # link languages and theme toml files since helix-term expects them (for tests)
-            preConfigure =
-              pkgs.lib.concatMapStringsSep
-              "\n"
-              (path: "ln -sf ${mkRootPath path} ..")
-              ["languages.toml" "theme.toml" "base16_theme.toml"];
-            buildInputs = (prev.buildInputs or []) ++ [common.cCompiler.cc.lib];
-            nativeBuildInputs = [pkgs.makeWrapper];
+  }: let
+    outputs = config:
+      nixCargoIntegration.lib.makeOutputs {
+        root = ./.;
+        renameOutputs = {"helix-term" = "helix";};
+        # Set default app to hx (binary is from helix-term release build)
+        # Set default package to helix-term release build
+        defaultOutputs = {
+          app = "hx";
+          package = "helix";
+        };
+        overrides = {
+          cCompiler = common:
+            with common.pkgs;
+              if stdenv.isLinux
+              then gcc
+              else clang;
+          crateOverrides = common: _: {
+            helix-term = prev: let
+              inherit (common) pkgs;
+              mkRootPath = rel:
+                builtins.path {
+                  path = "${common.root}/${rel}";
+                  name = rel;
+                };
+              grammars = pkgs.callPackage ./grammars.nix config;
+              runtimeDir = pkgs.runCommandNoCC "helix-runtime" {} ''
+                mkdir -p $out
+                ln -s ${mkRootPath "runtime"}/* $out
+                rm -r $out/grammars
+                ln -s ${grammars} $out/grammars
+              '';
+              overridedAttrs = {
+                # disable fetching and building of tree-sitter grammars in the helix-term build.rs
+                HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
+                # link languages and theme toml files since helix-term expects them (for tests)
+                preConfigure =
+                  pkgs.lib.concatMapStringsSep
+                  "\n"
+                  (path: "ln -sf ${mkRootPath path} ..")
+                  ["languages.toml" "theme.toml" "base16_theme.toml"];
+                buildInputs = (prev.buildInputs or []) ++ [common.cCompiler.cc.lib];
+                nativeBuildInputs = [pkgs.makeWrapper];
 
-            postFixup = ''
-              if [ -f "$out/bin/hx" ]; then
-                wrapProgram "$out/bin/hx" ''${makeWrapperArgs[@]} --set HELIX_RUNTIME "${runtimeDir}"
-              fi
-            '';
+                postFixup = ''
+                  if [ -f "$out/bin/hx" ]; then
+                    wrapProgram "$out/bin/hx" ''${makeWrapperArgs[@]} --set HELIX_RUNTIME "${runtimeDir}"
+                  fi
+                '';
+              };
+            in
+              overridedAttrs
+              // (
+                pkgs.lib.optionalAttrs
+                (config ? makeWrapperArgs)
+                {inherit (config) makeWrapperArgs;}
+              );
+          };
+          shell = common: prev: {
+            packages =
+              prev.packages
+              ++ (
+                with common.pkgs; [lld_13 lldb cargo-tarpaulin cargo-flamegraph rust-analyzer]
+              );
+            env =
+              prev.env
+              ++ [
+                {
+                  name = "HELIX_RUNTIME";
+                  eval = "$PWD/runtime";
+                }
+                {
+                  name = "RUST_BACKTRACE";
+                  value = "1";
+                }
+                {
+                  name = "RUSTFLAGS";
+                  value =
+                    if common.pkgs.stdenv.isLinux
+                    then "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment"
+                    else "";
+                }
+              ];
           };
         };
-        shell = common: prev: {
-          packages =
-            prev.packages
-            ++ (
-              with common.pkgs; [lld_13 lldb cargo-tarpaulin cargo-flamegraph rust-analyzer]
-            );
-          env =
-            prev.env
-            ++ [
-              {
-                name = "HELIX_RUNTIME";
-                eval = "$PWD/runtime";
-              }
-              {
-                name = "RUST_BACKTRACE";
-                value = "1";
-              }
-              {
-                name = "RUSTFLAGS";
-                value =
-                  if common.pkgs.stdenv.isLinux
-                  then "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment"
-                  else "";
-              }
-            ];
-        };
       };
+    defaultOutputs = outputs {};
+    makeOverridableHelix = system: old:
+      old
+      // {
+        override = args:
+          makeOverridableHelix
+          system
+          (outputs args).packages.${system}.helix;
+      };
+  in
+    defaultOutputs
+    // {
+      packages =
+        nixpkgs.lib.mapAttrs
+        (
+          system: packages:
+            packages
+            // rec {
+              default = helix;
+              helix = makeOverridableHelix system packages.helix;
+            }
+        )
+        defaultOutputs.packages;
     };
 
   nixConfig = {
diff --git a/grammars.nix b/grammars.nix
index 2f50662e..066fa69d 100644
--- a/grammars.nix
+++ b/grammars.nix
@@ -4,6 +4,8 @@
   runCommandLocal,
   runCommandNoCC,
   yj,
+  includeGrammarIf ? _: true,
+  ...
 }: let
   # HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON
   # before parsing
@@ -102,12 +104,13 @@
         runHook postFixup
       '';
     };
+  grammarsToBuild = builtins.filter includeGrammarIf gitGrammars;
   builtGrammars =
     builtins.map (grammar: {
       inherit (grammar) name;
       artifact = buildGrammar grammar;
     })
-    gitGrammars;
+    grammarsToBuild;
   grammarLinks =
     builtins.map (grammar: "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
     builtGrammars;