Unverified Commit 84031f0c authored by Vladimír Čunát's avatar Vladimír Čunát
Browse files

neovim: improve lpeg patch for darwin (#351423)

parents 795a26cf 81f06029
Loading
Loading
Loading
Loading
+209 −137
Original line number Diff line number Diff line
{ lib, stdenv, fetchFromGitHub, removeReferencesTo, cmake, gettext, msgpack-c, darwin
, libuv, lua, pkg-config
, unibilium
, libvterm-neovim
, tree-sitter
, fetchurl
, buildPackages
, treesitter-parsers ? import ./treesitter-parsers.nix { inherit fetchurl; }
, fixDarwinDylibNames
, glibcLocales ? null, procps ? null
{
  lib,
  stdenv,
  fetchFromGitHub,
  removeReferencesTo,
  cmake,
  gettext,
  msgpack-c,
  darwin,
  libuv,
  lua,
  pkg-config,
  unibilium,
  libvterm-neovim,
  tree-sitter,
  fetchurl,
  buildPackages,
  treesitter-parsers ? import ./treesitter-parsers.nix { inherit fetchurl; },
  fixDarwinDylibNames,
  glibcLocales ? null,
  procps ? null,

  # now defaults to false because some tests can be flaky (clipboard etc), see
  # also: https://github.com/neovim/neovim/issues/16233
, nodejs ? null, fish ? null, python3 ? null
  nodejs ? null,
  fish ? null,
  python3 ? null,
}:
stdenv.mkDerivation (finalAttrs:
stdenv.mkDerivation (
  finalAttrs:
  let
  nvim-lpeg-dylib = luapkgs: if stdenv.hostPlatform.isDarwin
    then (luapkgs.lpeg.overrideAttrs (oa: {
    nvim-lpeg-dylib =
      luapkgs:
      if stdenv.hostPlatform.isDarwin then
        let
          luaLibDir = "$out/lib/lua/${lib.versions.majorMinor luapkgs.lua.luaversion}";
        in
        (luapkgs.lpeg.overrideAttrs (oa: {
          preConfigure = ''
            # neovim wants clang .dylib
        sed -i makefile -e "s/CC = gcc/CC = clang/"
        sed -i makefile -e "s/-bundle/-dynamiclib/"
            substituteInPlace Makefile \
              --replace-fail "CC = gcc" "CC = clang" \
              --replace-fail "-bundle" "-dynamiclib" \
              --replace-fail "lpeg.so" "lpeg.dylib"
          '';
          preBuild = ''
            # there seems to be implicit calls to Makefile from luarocks, we need to
            # add a stage to build our dylib
            make macosx
        mkdir -p $out/lib
        mv lpeg.so $out/lib/lpeg.dylib
            mkdir -p ${luaLibDir}
            mv lpeg.dylib ${luaLibDir}/lpeg.dylib
          '';
          postInstall = ''
            rm -f ${luaLibDir}/lpeg.so
          '';
          nativeBuildInputs =
        oa.nativeBuildInputs
        ++ (
          lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames
        );
            oa.nativeBuildInputs ++ (lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames);
        }))
    else luapkgs.lpeg;
  requiredLuaPkgs = ps: (with ps; [
      else
        luapkgs.lpeg;
    requiredLuaPkgs =
      ps:
      (
        with ps;
        [
          (nvim-lpeg-dylib ps)
          luabitop
          mpack
  ] ++ lib.optionals finalAttrs.finalPackage.doCheck [
        ]
        ++ lib.optionals finalAttrs.finalPackage.doCheck [
          luv
          coxpcall
          busted
@@ -52,18 +79,22 @@ stdenv.mkDerivation (finalAttrs:
    neovimLuaEnv = lua.withPackages requiredLuaPkgs;
    neovimLuaEnvOnBuild = lua.luaOnBuild.withPackages requiredLuaPkgs;
    codegenLua =
    if lua.luaOnBuild.pkgs.isLuaJIT
      then
        let deterministicLuajit =
          lua.luaOnBuild.override {
      if lua.luaOnBuild.pkgs.isLuaJIT then
        let
          deterministicLuajit = lua.luaOnBuild.override {
            deterministicStringIds = true;
            self = deterministicLuajit;
          };
        in deterministicLuajit.withPackages(ps: [ ps.mpack (nvim-lpeg-dylib ps) ])
      else lua.luaOnBuild;

        in
        deterministicLuajit.withPackages (ps: [
          ps.mpack
          (nvim-lpeg-dylib ps)
        ])
      else
        lua.luaOnBuild;

in {
  in
  {
    pname = "neovim-unwrapped";
    version = "0.10.2";

@@ -86,12 +117,22 @@ in {
    dontFixCmake = true;

    inherit lua;
    treesitter-parsers = treesitter-parsers //
      { markdown = treesitter-parsers.markdown // { location = "tree-sitter-markdown"; }; } //
      { markdown_inline = treesitter-parsers.markdown // { language = "markdown_inline"; location = "tree-sitter-markdown-inline"; }; }
      ;
    treesitter-parsers =
      treesitter-parsers
      // {
        markdown = treesitter-parsers.markdown // {
          location = "tree-sitter-markdown";
        };
      }
      // {
        markdown_inline = treesitter-parsers.markdown // {
          language = "markdown_inline";
          location = "tree-sitter-markdown-inline";
        };
      };

    buildInputs = [
    buildInputs =
      [
        libuv
        libvterm-neovim
        # This is actually a c library, hence it's not included in neovimLuaEnv,
@@ -103,9 +144,12 @@ in {
        neovimLuaEnv
        tree-sitter
        unibilium
    ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.libutil ]
      ++ lib.optionals finalAttrs.finalPackage.doCheck [ glibcLocales procps ]
    ;
      ]
      ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.libutil ]
      ++ lib.optionals finalAttrs.finalPackage.doCheck [
        glibcLocales
        procps
      ];

    doCheck = false;

@@ -125,18 +169,27 @@ in {
    ];

    # extra programs test via `make functionaltest`
    nativeCheckInputs = let
      pyEnv = python3.withPackages(ps: with ps; [ pynvim msgpack ]);
    in [
    nativeCheckInputs =
      let
        pyEnv = python3.withPackages (
          ps: with ps; [
            pynvim
            msgpack
          ]
        );
      in
      [
        fish
        nodejs
        pyEnv # for src/clint.py
      ];

    # nvim --version output retains compilation flags and references to build tools
    postPatch = ''
    postPatch =
      ''
        substituteInPlace src/nvim/version.c --replace NVIM_VERSION_CFLAGS "";
    '' + lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
      ''
      + lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
        sed -i runtime/CMakeLists.txt \
          -e "s|\".*/bin/nvim|\${stdenv.hostPlatform.emulator buildPackages} &|g"
        sed -i src/nvim/po/CMakeLists.txt \
@@ -146,14 +199,21 @@ in {
      find "$out" -type f -exec remove-references-to -t ${stdenv.cc} '{}' +
    '';
    # check that the above patching actually works
    outputChecks = let
    outputChecks =
      let
        disallowedRequisites = [ stdenv.cc ] ++ lib.optional (lua != codegenLua) codegenLua;
    in {
      out = { inherit disallowedRequisites; };
      debug = { inherit disallowedRequisites; };
      in
      {
        out = {
          inherit disallowedRequisites;
        };
        debug = {
          inherit disallowedRequisites;
        };
      };

    cmakeFlags = [
    cmakeFlags =
      [
        # Don't use downloaded dependencies. At the end of the configurePhase one
        # can spot that cmake says this option was "not used by the project".
        # That's because all dependencies were found and
@@ -167,22 +227,27 @@ in {
        "-DLUA_PRG=${neovimLuaEnvOnBuild}/bin/luajit"
      ];

    preConfigure = lib.optionalString stdenv.hostPlatform.isDarwin ''
    preConfigure =
      lib.optionalString stdenv.hostPlatform.isDarwin ''
        substituteInPlace src/nvim/CMakeLists.txt --replace "    util" ""
    '' + ''
      ''
      + ''
        mkdir -p $out/lib/nvim/parser
    '' + lib.concatStrings (lib.mapAttrsToList
      (language: grammar: ''
      ''
      + lib.concatStrings (
        lib.mapAttrsToList (language: grammar: ''
          ln -s \
          ${tree-sitter.buildGrammar {
            ${
              tree-sitter.buildGrammar {
                inherit (grammar) src;
                version = "neovim-${finalAttrs.version}";
                language = grammar.language or language;
                location = grammar.location or null;
          }}/parser \
              }
            }/parser \
            $out/lib/nvim/parser/${language}.so
      '')
      finalAttrs.treesitter-parsers);
        '') finalAttrs.treesitter-parsers
      );

    shellHook = ''
      export VIMRUNTIME=$PWD/runtime
@@ -207,8 +272,15 @@ in {
      # Contributions committed after b17d96 are licensed under Apache 2.0 unless
      # those contributions were copied from Vim (identified in the commit logs
      # by the vim-patch token). See LICENSE for details."
      license = with lib.licenses; [ asl20 vim ];
      maintainers = with lib.maintainers; [ manveru rvolosatovs ];
      license = with lib.licenses; [
        asl20
        vim
      ];
      maintainers = with lib.maintainers; [
        manveru
        rvolosatovs
      ];
      platforms = lib.platforms.unix;
    };
  })
  }
)