vimPluginsUpdater: init

The nixpkgs documentation mentions how to update out of tree plugins but
one problem is that it requires a nixpkgs clone.
This makes it more convenient.
I've had the need to generate vim plugins and lua overlays for other
projects unrelated to nix and this will make updates easier (aka just
run `nix run nixpkgs#vimPluginsUpdater -- --proc=1` or with the legacy commands:
`nix-shell -p vimPluginsUpdater --run vim-plugins-updater`.

I added an optional "nixpkgs" argument to command line parser, which is the path
towards a nixpkgs checkout. By default the current folder.

update-luarocks-packages: format with black

Changed files
+202 -134
doc
languages-frameworks
maintainers
pkgs
applications
editors
vim
top-level
+6 -5
doc/languages-frameworks/vim.section.md
···
## Adding new plugins to nixpkgs {#adding-new-plugins-to-nixpkgs}
-
Nix expressions for Vim plugins are stored in [pkgs/applications/editors/vim/plugins](https://github.com/NixOS/nixpkgs/tree/master/pkgs/applications/editors/vim/plugins). For the vast majority of plugins, Nix expressions are automatically generated by running [`./update.py`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/update.py). This creates a [generated.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/generated.nix) file based on the plugins listed in [vim-plugin-names](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/vim-plugin-names).
-
After running `./update.py`, if nvim-treesitter received an update, also run [`nvim-treesitter/update.py`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/update.py) to update the tree sitter grammars for `nvim-treesitter`.
Some plugins require overrides in order to function properly. Overrides are placed in [overrides.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/overrides.nix). Overrides are most often required when a plugin requires some dependencies, or extra steps are required during the build process. For example `deoplete-fish` requires both `deoplete-nvim` and `vim-fish`, and so the following override was added:
···
Alternatively, set the number of processes to a lower count to avoid rate-limiting.
```sh
-
./pkgs/applications/editors/vim/plugins/update.py --proc 1
```
## How to maintain an out-of-tree overlay of vim plugins ? {#vim-out-of-tree-overlays}
···
plugin list:
```
-
pkgs/applications/editors/vim/plugins/update.py -i vim-plugin-names -o generated.nix --no-commit
```
with the contents of `vim-plugin-names` being for example:
···
```nix
myVimPlugins = pkgs.vimPlugins.extend (
-
(pkgs.callPackage generated.nix {})
);
```
···
## Adding new plugins to nixpkgs {#adding-new-plugins-to-nixpkgs}
+
Nix expressions for Vim plugins are stored in [pkgs/applications/editors/vim/plugins](https://github.com/NixOS/nixpkgs/tree/master/pkgs/applications/editors/vim/plugins). For the vast majority of plugins, Nix expressions are automatically generated by running [`nix-shell -p vimPluginsUpdater --run vim-plugins-updater`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/updater.nix). This creates a [generated.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/generated.nix) file based on the plugins listed in [vim-plugin-names](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/vim-plugin-names).
+
After running the updater, if nvim-treesitter received an update, also run [`nvim-treesitter/update.py`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/update.py) to update the tree sitter grammars for `nvim-treesitter`.
Some plugins require overrides in order to function properly. Overrides are placed in [overrides.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/overrides.nix). Overrides are most often required when a plugin requires some dependencies, or extra steps are required during the build process. For example `deoplete-fish` requires both `deoplete-nvim` and `vim-fish`, and so the following override was added:
···
Alternatively, set the number of processes to a lower count to avoid rate-limiting.
```sh
+
+
nix-shell -p vimPluginsUpdater --run 'vim-plugins-updater --proc 1'
```
## How to maintain an out-of-tree overlay of vim plugins ? {#vim-out-of-tree-overlays}
···
plugin list:
```
+
nix-shell -p vimPluginsUpdater --run vim-plugins-updater -i vim-plugin-names -o generated.nix --no-commit
```
with the contents of `vim-plugin-names` being for example:
···
```nix
myVimPlugins = pkgs.vimPlugins.extend (
+
(pkgs.callPackage ./generated.nix {})
);
```
+29 -11
maintainers/scripts/pluginupdate.py
···
return plugins
-
def run_nix_expr(expr):
-
with CleanEnvironment() as nix_path:
cmd = [
"nix",
"eval",
···
"""CSV spec"""
print("the update member function should be overriden in subclasses")
-
def get_current_plugins(self) -> List[Plugin]:
"""To fill the cache"""
-
data = run_nix_expr(self.get_plugins)
plugins = []
for name, attr in data.items():
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
···
raise NotImplementedError()
def get_update(self, input_file: str, outfile: str, config: FetchConfig):
-
cache: Cache = Cache(self.get_current_plugins(), self.cache_file)
_prefetch = functools.partial(prefetch, cache=cache)
def update() -> dict:
···
),
)
common.add_argument(
"--input-names",
"-i",
dest="input_file",
···
command = args.command or "update"
log.setLevel(LOG_LEVELS[args.debug])
log.info("Chose to run command: %s", command)
-
if not args.no_commit:
-
self.nixpkgs_repo = git.Repo(self.root, search_parent_directories=True)
getattr(self, command)(args)
class CleanEnvironment(object):
def __enter__(self) -> str:
-
self.old_environ = os.environ.copy()
local_pkgs = str(Path(__file__).parent.parent.parent)
self.empty_config = NamedTemporaryFile()
self.empty_config.write(b"{}")
self.empty_config.flush()
-
os.environ["NIXPKGS_CONFIG"] = self.empty_config.name
-
return f"localpkgs={local_pkgs}"
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
os.environ.update(self.old_environ)
···
def update_plugins(editor: Editor, args):
-
"""The main entry function of this module. All input arguments are grouped in the `Editor`."""
log.info("Start updating plugins")
fetch_config = FetchConfig(args.proc, args.github_token)
···
return plugins
+
+
def run_nix_expr(expr, nixpkgs: str):
+
'''
+
:param expr nix expression to fetch current plugins
+
:param nixpkgs Path towards a nixpkgs checkout
+
'''
+
# local_pkgs = str(Path(__file__).parent.parent.parent)
+
with CleanEnvironment(nixpkgs) as nix_path:
cmd = [
"nix",
"eval",
···
"""CSV spec"""
print("the update member function should be overriden in subclasses")
+
def get_current_plugins(self, nixpkgs) -> List[Plugin]:
"""To fill the cache"""
+
data = run_nix_expr(self.get_plugins, nixpkgs)
plugins = []
for name, attr in data.items():
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
···
raise NotImplementedError()
def get_update(self, input_file: str, outfile: str, config: FetchConfig):
+
cache: Cache = Cache(self.get_current_plugins(self.nixpkgs), self.cache_file)
_prefetch = functools.partial(prefetch, cache=cache)
def update() -> dict:
···
),
)
common.add_argument(
+
"--nixpkgs",
+
type=str,
+
default=os.getcwd(),
+
help="Adjust log level",
+
)
+
common.add_argument(
"--input-names",
"-i",
dest="input_file",
···
command = args.command or "update"
log.setLevel(LOG_LEVELS[args.debug])
log.info("Chose to run command: %s", command)
+
self.nixpkgs = args.nixpkgs
+
self.nixpkgs_repo = git.Repo(args.nixpkgs, search_parent_directories=True)
getattr(self, command)(args)
class CleanEnvironment(object):
+
def __init__(self, nixpkgs):
+
self.local_pkgs = nixpkgs
+
def __enter__(self) -> str:
+
"""
local_pkgs = str(Path(__file__).parent.parent.parent)
+
"""
+
self.old_environ = os.environ.copy()
self.empty_config = NamedTemporaryFile()
self.empty_config.write(b"{}")
self.empty_config.flush()
+
# os.environ["NIXPKGS_CONFIG"] = self.empty_config.name
+
return f"localpkgs={self.local_pkgs}"
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
os.environ.update(self.old_environ)
···
def update_plugins(editor: Editor, args):
+
"""The main entry function of this module.
+
All input arguments are grouped in the `Editor`."""
log.info("Start updating plugins")
fetch_config = FetchConfig(args.proc, args.github_token)
+53 -45
maintainers/scripts/update-luarocks-packages
···
#!nix-shell update-luarocks-shell.nix -i python3
# format:
-
# $ nix run nixpkgs.python3Packages.black -c black update.py
# type-check:
-
# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
# linted:
-
# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py
import inspect
import os
···
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
-
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent # type: ignore
import pluginupdate
from pluginupdate import update_plugins, FetchConfig, CleanEnvironment
-
PKG_LIST="maintainers/scripts/luarocks-packages.csv"
-
TMP_FILE="$(mktemp)"
-
GENERATED_NIXFILE="pkgs/development/lua-modules/generated-packages.nix"
-
LUAROCKS_CONFIG="maintainers/scripts/luarocks-config.lua"
HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
Regenerate it with:
···
You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
*/
-
""".format(GENERATED_NIXFILE=GENERATED_NIXFILE)
-
FOOTER="""
}
/* GENERATED - do not edit this file */
"""
@dataclass
class LuaPlugin:
name: str
-
'''Name of the plugin, as seen on luarocks.org'''
src: str
-
'''address to the git repository'''
ref: Optional[str]
-
'''git reference (branch name/tag)'''
version: Optional[str]
-
'''Set it to pin a package '''
server: Optional[str]
-
'''luarocks.org registers packages under different manifests.
Its value can be 'http://luarocks.org/dev'
-
'''
luaversion: Optional[str]
-
'''Attribue of the lua interpreter if a package is available only for a specific lua version'''
maintainers: Optional[str]
-
''' Optional string listing maintainers separated by spaces'''
@property
def normalized_name(self) -> str:
return self.name.replace(".", "-")
# rename Editor to LangUpdate/ EcosystemUpdater
class LuaEditor(pluginupdate.Editor):
···
def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
luaPackages = []
-
csvfilename=input_file
log.info("Loading package descriptions from %s", csvfilename)
-
with open(csvfilename, newline='') as csvfile:
-
reader = csv.DictReader(csvfile,)
for row in reader:
# name,server,version,luaversion,maintainers
plugin = LuaPlugin(**row)
···
def update(self, args):
update_plugins(self, args)
-
def generate_nix(
-
self,
-
results: List[Tuple[LuaPlugin, str]],
-
outfilename: str
-
):
-
with tempfile.NamedTemporaryFile("w+") as f:
f.write(HEADER)
header2 = textwrap.dedent(
-
# header2 = inspect.cleandoc(
-
"""
{ self, stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
final: prev:
{
-
""")
f.write(header2)
-
for (plugin, nix_expr) in results:
f.write(f"{plugin.normalized_name} = {nix_expr}")
f.write(FOOTER)
f.flush()
···
# luaPackages.append(plugin)
pass
def generate_pkg_nix(plug: LuaPlugin):
-
'''
Generate nix expression for a luarocks package
Our cache key associates "p.name-p.version" to its rockspec
-
'''
log.debug("Generating nix expression for %s", plug.name)
custom_env = os.environ.copy()
-
custom_env['LUAROCKS_CONFIG'] = LUAROCKS_CONFIG
# we add --dev else luarocks wont find all the "scm" (=dev) versions of the
# packages
-
# , "--dev"
-
cmd = [ "luarocks", "nix" ]
if plug.maintainers:
cmd.append(f"--maintainers={plug.maintainers}")
···
# if plug.server == "src":
if plug.src != "":
if plug.src is None:
-
msg = "src must be set when 'version' is set to \"src\" for package %s" % plug.name
log.error(msg)
raise RuntimeError(msg)
log.debug("Updating from source %s", plug.src)
···
else:
cmd.append(plug.name)
if plug.version and plug.version != "src":
-
cmd.append(plug.version)
if plug.server != "src" and plug.server:
···
if plug.luaversion:
cmd.append(f"--lua-version={plug.luaversion}")
-
log.debug("running %s", ' '.join(cmd))
output = subprocess.check_output(cmd, env=custom_env, text=True)
output = "callPackage(" + output.strip() + ") {};\n\n"
return (plug, output)
-
def main():
-
editor = LuaEditor("lua", ROOT, '',
-
default_in = ROOT.joinpath(PKG_LIST),
-
default_out = ROOT.joinpath(GENERATED_NIXFILE)
-
)
editor.run()
-
if __name__ == "__main__":
main()
# vim: set ft=python noet fdm=manual fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :
···
#!nix-shell update-luarocks-shell.nix -i python3
# format:
+
# $ nix run nixpkgs#python3Packages.black -- update.py
# type-check:
+
# $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted:
+
# $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
import inspect
import os
···
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
+
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent # type: ignore
import pluginupdate
from pluginupdate import update_plugins, FetchConfig, CleanEnvironment
+
PKG_LIST = "maintainers/scripts/luarocks-packages.csv"
+
TMP_FILE = "$(mktemp)"
+
GENERATED_NIXFILE = "pkgs/development/lua-modules/generated-packages.nix"
+
LUAROCKS_CONFIG = "maintainers/scripts/luarocks-config.lua"
HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
Regenerate it with:
···
You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
*/
+
""".format(
+
GENERATED_NIXFILE=GENERATED_NIXFILE
+
)
+
FOOTER = """
}
/* GENERATED - do not edit this file */
"""
+
@dataclass
class LuaPlugin:
name: str
+
"""Name of the plugin, as seen on luarocks.org"""
src: str
+
"""address to the git repository"""
ref: Optional[str]
+
"""git reference (branch name/tag)"""
version: Optional[str]
+
"""Set it to pin a package """
server: Optional[str]
+
"""luarocks.org registers packages under different manifests.
Its value can be 'http://luarocks.org/dev'
+
"""
luaversion: Optional[str]
+
"""Attribue of the lua interpreter if a package is available only for a specific lua version"""
maintainers: Optional[str]
+
""" Optional string listing maintainers separated by spaces"""
@property
def normalized_name(self) -> str:
return self.name.replace(".", "-")
+
# rename Editor to LangUpdate/ EcosystemUpdater
class LuaEditor(pluginupdate.Editor):
···
def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
luaPackages = []
+
csvfilename = input_file
log.info("Loading package descriptions from %s", csvfilename)
+
with open(csvfilename, newline="") as csvfile:
+
reader = csv.DictReader(
+
csvfile,
+
)
for row in reader:
# name,server,version,luaversion,maintainers
plugin = LuaPlugin(**row)
···
def update(self, args):
update_plugins(self, args)
+
def generate_nix(self, results: List[Tuple[LuaPlugin, str]], outfilename: str):
with tempfile.NamedTemporaryFile("w+") as f:
f.write(HEADER)
header2 = textwrap.dedent(
+
# header2 = inspect.cleandoc(
+
"""
{ self, stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
final: prev:
{
+
"""
+
)
f.write(header2)
+
for plugin, nix_expr in results:
f.write(f"{plugin.normalized_name} = {nix_expr}")
f.write(FOOTER)
f.flush()
···
# luaPackages.append(plugin)
pass
+
def generate_pkg_nix(plug: LuaPlugin):
+
"""
Generate nix expression for a luarocks package
Our cache key associates "p.name-p.version" to its rockspec
+
"""
log.debug("Generating nix expression for %s", plug.name)
custom_env = os.environ.copy()
+
custom_env["LUAROCKS_CONFIG"] = LUAROCKS_CONFIG
# we add --dev else luarocks wont find all the "scm" (=dev) versions of the
# packages
+
# , "--dev"
+
cmd = ["luarocks", "nix"]
if plug.maintainers:
cmd.append(f"--maintainers={plug.maintainers}")
···
# if plug.server == "src":
if plug.src != "":
if plug.src is None:
+
msg = (
+
"src must be set when 'version' is set to \"src\" for package %s"
+
% plug.name
+
)
log.error(msg)
raise RuntimeError(msg)
log.debug("Updating from source %s", plug.src)
···
else:
cmd.append(plug.name)
if plug.version and plug.version != "src":
cmd.append(plug.version)
if plug.server != "src" and plug.server:
···
if plug.luaversion:
cmd.append(f"--lua-version={plug.luaversion}")
+
log.debug("running %s", " ".join(cmd))
output = subprocess.check_output(cmd, env=custom_env, text=True)
output = "callPackage(" + output.strip() + ") {};\n\n"
return (plug, output)
+
def main():
+
editor = LuaEditor(
+
"lua",
+
ROOT,
+
"",
+
default_in=ROOT.joinpath(PKG_LIST),
+
default_out=ROOT.joinpath(GENERATED_NIXFILE),
+
)
editor.run()
+
if __name__ == "__main__":
main()
# vim: set ft=python noet fdm=manual fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :
+16 -12
pkgs/applications/editors/vim/plugins/nvim-treesitter/update.py
···
from os import environ
from os.path import dirname, join
-
lockfile = json.load(open(join(environ["NVIM_TREESITTER"], "lockfile.json")))
-
configs = json.loads(
subprocess.check_output(
[
···
return generated
-
generated_file = """# generated by pkgs/applications/editors/vim/plugins/nvim-treesitter/update.py
-
{ buildGrammar, """
-
generated_file += subprocess.check_output(["nurl", "-Ls", ", "], text=True)
-
generated_file += """ }:
-
{
-
"""
-
for generated in ThreadPoolExecutor().map(generate_grammar, lockfile.items()):
-
generated_file += generated
-
generated_file += "}\n"
-
open(join(dirname(__file__), "generated.nix"), "w").write(generated_file)
···
from os import environ
from os.path import dirname, join
configs = json.loads(
subprocess.check_output(
[
···
return generated
+
def update_grammars(lockfile: str):
+
generated_file = """# generated by pkgs/applications/editors/vim/plugins/nvim-treesitter/update.py
+
{ buildGrammar, """
+
generated_file += subprocess.check_output(["nurl", "-Ls", ", "], text=True)
+
generated_file += """ }:
+
{
+
"""
+
for generated in ThreadPoolExecutor().map(generate_grammar, lockfile.items()):
+
generated_file += generated
+
generated_file += "}\n"
+
generated_file += "}\n"
+
open(join(dirname(__file__), "generated.nix"), "w").write(generated_file)
+
+
if __name__ == "__main__":
+
# TODO add lockfile
+
update_grammars()
-16
pkgs/applications/editors/vim/plugins/update-shell.nix
···
-
{ pkgs ? import ../../../../.. { } }:
-
-
# Ideally, pkgs points to default.nix file of Nixpkgs official tree
-
with pkgs;
-
let
-
pyEnv = python3.withPackages (ps: [ ps.gitpython ]);
-
in
-
-
mkShell {
-
packages = [
-
bash
-
pyEnv
-
nix
-
nix-prefetch-scripts
-
];
-
}
···
+44 -45
pkgs/applications/editors/vim/plugins/update.py
···
-
#!/usr/bin/env nix-shell
-
#!nix-shell update-shell.nix -i python3
-
# format:
-
# $ nix run nixpkgs.python3Packages.black -c black update.py
# type-check:
-
# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
# linted:
-
# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py
-
# If you see `HTTP Error 429: too many requests` errors while running this script,
-
# refer to:
#
# https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs
#
-
# (or the equivalent file /doc/languages-frameworks/vim.section.md from Nixpkgs master tree).
#
import inspect
import os
-
import sys
import logging
-
import subprocess
import textwrap
from typing import List, Tuple
from pathlib import Path
-
import git
log = logging.getLogger()
···
# Import plugin update library from maintainers/scripts/pluginupdate.py
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
-
# Ideally, ROOT.(parent^5) points to root of Nixpkgs official tree
-
sys.path.insert(
-
0, os.path.join(ROOT.parent.parent.parent.parent.parent, "maintainers", "scripts")
-
)
import pluginupdate
from pluginupdate import run_nix_expr, PluginDesc
-
nvim_treesitter = importlib.import_module("treesitter")
-
-
GET_PLUGINS_LUA = """
-
with import <localpkgs> {};
-
lib.attrNames lua51Packages"""
HEADER = (
"# GENERATED by ./pkgs/applications/editors/vim/plugins/update.py. Do not edit!"
)
-
-
def isNeovimPlugin(plug: pluginupdate.Plugin) -> bool:
-
"""
-
Whether it's a neovim-only plugin
-
We can check if it's available in lua packages
-
"""
-
global luaPlugins
-
if plug.normalized_name in luaPlugins:
-
log.debug("%s is a neovim plugin", plug)
-
return True
-
return False
class VimEditor(pluginupdate.Editor):
···
):
sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower())
nvim_treesitter_rev = pluginupdate.run_nix_expr(
-
"(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev"
)
with open(outfile, "w+") as f:
···
content = self.plugin2nix(pdesc, plugin)
f.write(content)
if (
-
plugin.name == "nvim-treesitter"
-
and plugin.commit != nvim_treesitter_rev
):
self.nvim_treesitter_updated = True
f.write("\n}\n")
print(f"updated {outfile}")
def plugin2nix(self, pdesc: PluginDesc, plugin: pluginupdate.Plugin) -> str:
repo = pdesc.repo
-
isNeovim = isNeovimPlugin(plugin)
content = f" {plugin.normalized_name} = "
src_nix = repo.as_nix(plugin)
···
if self.nvim_treesitter_updated:
print("updating nvim-treesitter grammars")
nvim_treesitter_dir = ROOT.joinpath("nvim-treesitter")
-
# subprocess.check_call([nvim_treesitter_dir.joinpath("update.py")])
-
nvim_treesitter.update_grammars()
if self.nixpkgs_repo:
index = self.nixpkgs_repo.index
for diff in index.diff(None):
-
if (
-
diff.a_path
-
== "pkgs/applications/editors/vim/plugins/nvim-treesitter/generated.nix"
-
):
msg = "vimPlugins.nvim-treesitter: update grammars"
print(f"committing to nixpkgs: {msg}")
index.add([str(nvim_treesitter_dir.joinpath("generated.nix"))])
···
def main():
global luaPlugins
-
luaPlugins = run_nix_expr(GET_PLUGINS_LUA)
-
with open(f"{ROOT}/get-plugins.nix") as f:
GET_PLUGINS = f.read()
-
editor = VimEditor("vim", ROOT, GET_PLUGINS)
editor.run()
···
+
#!/usr/bin/env python
+
# run with:
+
# $ nix run .\#vimPluginsUpdater
# format:
+
# $ nix run nixpkgs#python3Packages.black -- update.py
# type-check:
+
# $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted:
+
# $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
+
# If you see `HTTP Error 429: too many requests` errors while running this
+
# script, refer to:
#
# https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs
#
+
# (or the equivalent file /doc/languages-frameworks/vim.section.md
+
# from Nixpkgs master tree).
#
import inspect
import os
import logging
import textwrap
+
import json
from typing import List, Tuple
from pathlib import Path
log = logging.getLogger()
···
# Import plugin update library from maintainers/scripts/pluginupdate.py
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
import pluginupdate
+
import importlib
from pluginupdate import run_nix_expr, PluginDesc
+
from treesitter import update_grammars
HEADER = (
"# GENERATED by ./pkgs/applications/editors/vim/plugins/update.py. Do not edit!"
)
+
NVIM_TREESITTER_GENERATED_NIX = \
+
"pkgs/applications/editors/vim/plugins/nvim-treesitter/generated.nix"
class VimEditor(pluginupdate.Editor):
···
):
sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower())
nvim_treesitter_rev = pluginupdate.run_nix_expr(
+
"(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev",
+
self.nixpkgs
)
with open(outfile, "w+") as f:
···
content = self.plugin2nix(pdesc, plugin)
f.write(content)
if (
+
plugin.name == "nvim-treesitter" and plugin.commit != nvim_treesitter_rev
):
self.nvim_treesitter_updated = True
f.write("\n}\n")
print(f"updated {outfile}")
def plugin2nix(self, pdesc: PluginDesc, plugin: pluginupdate.Plugin) -> str:
+
GET_PLUGINS_LUA = """
+
with import <localpkgs> {};
+
lib.attrNames lua51Packages"""
+
luaPlugins = run_nix_expr(GET_PLUGINS_LUA, self.nixpkgs)
+
repo = pdesc.repo
+
+
def _isNeovimPlugin(plug: pluginupdate.Plugin) -> bool:
+
"""
+
Whether it's a neovim-only plugin
+
We can check if it's available in lua packages
+
"""
+
# global luaPlugins
+
if plug.normalized_name in luaPlugins:
+
log.debug("%s is a neovim plugin", plug)
+
return True
+
return False
+
+
isNeovim = _isNeovimPlugin(plugin)
content = f" {plugin.normalized_name} = "
src_nix = repo.as_nix(plugin)
···
if self.nvim_treesitter_updated:
print("updating nvim-treesitter grammars")
nvim_treesitter_dir = ROOT.joinpath("nvim-treesitter")
+
lockfile = json.load(open(args.nixpkgs.join(NVIM_TREESITTER_GENERATED_FILE, "lockfile.json")))
+
+
nvim_treesitter.update_grammars(lockfile)
if self.nixpkgs_repo:
index = self.nixpkgs_repo.index
for diff in index.diff(None):
+
if diff.a_path == NVIM_TREESITTER_GENERATED_NIX:
msg = "vimPlugins.nvim-treesitter: update grammars"
print(f"committing to nixpkgs: {msg}")
index.add([str(nvim_treesitter_dir.joinpath("generated.nix"))])
···
def main():
+
global luaPlugins
+
log.debug(f"Loading from {ROOT}/../get-plugins.nix")
+
with open(f"{ROOT}/../get-plugins.nix") as f:
GET_PLUGINS = f.read()
+
editor = VimEditor("vim", Path("pkgs/applications/editors/vim/plugins"), GET_PLUGINS)
editor.run()
+47
pkgs/applications/editors/vim/plugins/updater.nix
···
···
+
{ buildPythonApplication
+
, nix
+
, makeWrapper
+
, python3Packages
+
, lib
+
+
# optional
+
, vimPlugins
+
, neovim
+
}:
+
let
+
my_neovim = neovim.override {
+
configure.packages.all.start = [ vimPlugins.nvim-treesitter ];
+
};
+
+
in
+
buildPythonApplication {
+
format = "other";
+
pname = "vim-plugins-updater";
+
version = "0.1";
+
+
nativeBuildInputs = [
+
makeWrapper
+
python3Packages.wrapPython
+
];
+
+
pythonPath = [
+
python3Packages.gitpython
+
];
+
+
dontUnpack = true;
+
+
installPhase = ''
+
mkdir -p $out/bin $out/lib
+
cp ${./update.py} $out/bin/vim-plugins-updater
+
cp ${./get-plugins.nix} $out/get-plugins.nix
+
cp ${./nvim-treesitter/update.py} $out/lib/treesitter.py
+
cp ${../../../../../maintainers/scripts/pluginupdate.py} $out/lib/pluginupdate.py
+
+
# wrap python scripts
+
makeWrapperArgs+=( --prefix PATH : "${lib.makeBinPath [ nix my_neovim ]}" --prefix PYTHONPATH : "$out/lib" )
+
wrapPythonPrograms
+
'';
+
+
meta.mainProgram = "vim-plugins-updater";
+
}
+
+7
pkgs/top-level/all-packages.nix
···
common-updater-scripts = callPackage ../common-updater/scripts.nix { };
genericUpdater = callPackage ../common-updater/generic-updater.nix { };
_experimental-update-script-combinators = callPackage ../common-updater/combinators.nix { };
···
common-updater-scripts = callPackage ../common-updater/scripts.nix { };
+
# vimPluginsUpdater = callPackage ../applications/editors/vim/plugins/updater.nix {
+
# inherit (writers) writePython3Bin;
+
# };
+
vimPluginsUpdater = callPackage ../applications/editors/vim/plugins/updater.nix {
+
inherit (python3Packages) buildPythonApplication ;
+
};
+
genericUpdater = callPackage ../common-updater/generic-updater.nix { };
_experimental-update-script-combinators = callPackage ../common-updater/combinators.nix { };