Compare commits

...

4 commits

Author SHA1 Message Date
Ching Pei Yang
da26ea78b2
module: remove redundant code
the bulk of the build step is moved to modules/wrapper/build
2024-12-22 21:59:24 +01:00
Ching Pei Yang
c7f4dd19ca
home-manager: use proper submodule type for settings 2024-12-22 21:58:28 +01:00
Ching Pei Yang
3b73bc0e01
wrapper: add built package as option 2024-12-22 21:49:11 +01:00
Ching Pei Yang
e11c8aa7a0
wrapper: rename build dir to environemnt 2024-12-22 21:46:17 +01:00
10 changed files with 299 additions and 255 deletions

View file

@ -31,7 +31,7 @@
}; };
homeManagerModules = { homeManagerModules = {
nvf = import ./flake/modules/home-manager.nix self.packages lib; nvf = import ./flake/modules/home-manager.nix self.packages lib inputs;
default = self.homeManagerModules.nvf; default = self.homeManagerModules.nvf;
neovim-flake = neovim-flake =
lib.warn '' lib.warn ''

View file

@ -1,5 +1,5 @@
# Home Manager module # Home Manager module
packages: lib: { packages: lib: inputs: {
config, config,
pkgs, pkgs,
... ...
@ -8,15 +8,19 @@ packages: lib: {
inherit (lib.modules) mkIf mkAliasOptionModule; inherit (lib.modules) mkIf mkAliasOptionModule;
inherit (lib.lists) optional; inherit (lib.lists) optional;
inherit (lib.options) mkOption mkEnableOption literalExpression; inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.types) attrsOf anything bool; inherit (lib.types) anything bool submoduleWith;
inherit (lib.nvim) neovimConfiguration;
inherit (lib.nvim.types) anythingConcatLists;
cfg = config.programs.nvf; cfg = config.programs.nvf;
neovimConfigured = neovimConfiguration { nvfModule = submoduleWith {
inherit pkgs; description = "Nvf module";
modules = [cfg.settings]; class = "nvf";
specialArgs = {
inherit pkgs lib inputs;
};
modules = [
{imports = import ../../modules/modules.nix {inherit pkgs lib;};}
];
}; };
in { in {
imports = [ imports = [
@ -55,7 +59,7 @@ in {
}; };
settings = mkOption { settings = mkOption {
type = attrsOf anythingConcatLists; type = nvfModule;
default = {}; default = {};
description = "Attribute set of nvf preferences."; description = "Attribute set of nvf preferences.";
example = literalExpression '' example = literalExpression ''
@ -78,7 +82,7 @@ in {
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
programs.nvf.finalPackage = neovimConfigured.neovim; programs.nvf.finalPackage = config.programs.nvf.settings.vim.build.finalPackage;
home = { home = {
sessionVariables = mkIf cfg.defaultEditor {EDITOR = "nvim";}; sessionVariables = mkIf cfg.defaultEditor {EDITOR = "nvim";};

View file

@ -9,9 +9,8 @@
extraModules ? [], extraModules ? [],
configuration ? {}, configuration ? {},
}: let }: let
inherit (pkgs) vimPlugins; inherit (lib.strings) toString;
inherit (lib.strings) isString toString; inherit (lib.lists) concatLists;
inherit (lib.lists) filter map concatLists;
# import modules.nix with `check`, `pkgs` and `lib` as arguments # import modules.nix with `check`, `pkgs` and `lib` as arguments
# check can be disabled while calling this file is called # check can be disabled while calling this file is called
@ -21,7 +20,12 @@
# evaluate the extended library with the modules # evaluate the extended library with the modules
# optionally with any additional modules passed by the user # optionally with any additional modules passed by the user
module = lib.evalModules { module = lib.evalModules {
specialArgs = extraSpecialArgs // {modulesPath = toString ./.;}; specialArgs =
extraSpecialArgs
// {
inherit inputs;
modulesPath = toString ./.;
};
modules = concatLists [ modules = concatLists [
nvimModules nvimModules
modules modules
@ -36,102 +40,11 @@
extraModules)) extraModules))
]; ];
}; };
# alias to the internal configuration
vimOptions = module.config.vim;
noBuildPlug = {pname, ...} @ attrs: let
src = inputs."plugin-${attrs.pname}";
in
{
version = src.shortRev or src.shortDirtyRev or "dirty";
outPath = src;
passthru.vimPlugin = false;
}
// attrs;
# build a vim plugin with the given name and arguments
# if the plugin is nvim-treesitter, warn the user to use buildTreesitterPlug
# instead
buildPlug = attrs: let
src = inputs."plugin-${attrs.pname}";
in
pkgs.vimUtils.buildVimPlugin (
{
version = src.shortRev or src.shortDirtyRev or "dirty";
inherit src;
}
// attrs
);
buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars);
pluginBuilders = {
nvim-treesitter = buildTreesitterPlug vimOptions.treesitter.grammars;
flutter-tools-patched = buildPlug {
pname = "flutter-tools";
patches = [../patches/flutter-tools.patch];
};
};
buildConfigPlugins = plugins:
map (
plug:
if (isString plug)
then pluginBuilders.${plug} or (noBuildPlug {pname = plug;})
else plug
) (filter (f: f != null) plugins);
# built (or "normalized") plugins that are modified
builtStartPlugins = buildConfigPlugins vimOptions.startPlugins;
builtOptPlugins = map (package: package // {optional = true;}) (buildConfigPlugins vimOptions.optPlugins);
# additional Lua and Python3 packages, mapped to their respective functions
# to conform to the format mnw expects. end user should
# only ever need to pass a list of packages, which are modified
# here
extraLuaPackages = ps: map (x: ps.${x}) vimOptions.luaPackages;
extraPython3Packages = ps: map (x: ps.${x}) vimOptions.python3Packages;
# Wrap the user's desired (unwrapped) Neovim package with arguments that'll be used to
# generate a wrapped Neovim package.
neovim-wrapped = inputs.mnw.lib.wrap pkgs {
neovim = vimOptions.package;
plugins = builtStartPlugins ++ builtOptPlugins;
appName = "nvf";
extraBinPath = vimOptions.extraPackages;
initLua = vimOptions.builtLuaConfigRC;
luaFiles = vimOptions.extraLuaFiles;
inherit (vimOptions) viAlias vimAlias withRuby withNodeJs withPython3;
inherit extraLuaPackages extraPython3Packages;
};
dummyInit = pkgs.writeText "nvf-init.lua" vimOptions.builtLuaConfigRC;
# Additional helper scripts for printing and displaying nvf configuration
# in your commandline.
printConfig = pkgs.writers.writeDashBin "nvf-print-config" "cat ${dummyInit}";
printConfigPath = pkgs.writers.writeDashBin "nvf-print-config-path" "echo -n ${dummyInit}";
in { in {
inherit (module) options config; inherit (module) options config;
inherit (module._module.args) pkgs; inherit (module._module.args) pkgs;
# Expose wrapped neovim-package for userspace # Expose wrapped neovim-package for userspace
# or module consumption. # or module consumption.
neovim = pkgs.symlinkJoin { neovim = module.config.vim.build.finalPackage;
name = "nvf-with-helpers";
paths = [neovim-wrapped printConfig printConfigPath];
postBuild = "echo Helpers added";
# Allow evaluating vimOptions, i.e., config.vim from the packages' passthru
# attribute. For example, packages.x86_64-linux.neovim.passthru.neovimConfig
# will return the configuration in full.
passthru.neovimConfig = vimOptions;
meta =
neovim-wrapped.meta
// {
description = "Wrapped Neovim package with helper scripts to print the config (path)";
};
};
} }

View file

@ -49,6 +49,7 @@
# using the configuration passed in `neovim` and `plugins` modules. # using the configuration passed in `neovim` and `plugins` modules.
wrapper = map (p: ./wrapper + "/${p}") [ wrapper = map (p: ./wrapper + "/${p}") [
"build" "build"
"environment"
"rc" "rc"
"warnings" "warnings"
"lazy" "lazy"

View file

@ -1,13 +1,111 @@
{ {
config, inputs,
lib, lib,
config,
pkgs,
... ...
}: let }
inherit (lib.attrsets) attrValues; : let
inherit (pkgs) vimPlugins;
inherit (lib.strings) isString;
inherit (lib.lists) filter map;
cfg = config.vim; # alias to the internal configuration
vimOptions = config.vim;
noBuildPlug = {pname, ...} @ attrs: let
src = inputs."plugin-${attrs.pname}";
in
{
version = src.shortRev or src.shortDirtyRev or "dirty";
outPath = src;
passthru.vimPlugin = false;
}
// attrs;
# build a vim plugin with the given name and arguments
# if the plugin is nvim-treesitter, warn the user to use buildTreesitterPlug
# instead
buildPlug = attrs: let
src = inputs."plugin-${attrs.pname}";
in
pkgs.vimUtils.buildVimPlugin (
{
version = src.shortRev or src.shortDirtyRev or "dirty";
inherit src;
}
// attrs
);
buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars);
pluginBuilders = {
nvim-treesitter = buildTreesitterPlug vimOptions.treesitter.grammars;
flutter-tools-patched = buildPlug {
pname = "flutter-tools";
patches = [../patches/flutter-tools.patch];
};
};
buildConfigPlugins = plugins:
map (
plug:
if (isString plug)
then pluginBuilders.${plug} or (noBuildPlug {pname = plug;})
else plug
) (filter (f: f != null) plugins);
# built (or "normalized") plugins that are modified
builtStartPlugins = buildConfigPlugins vimOptions.startPlugins;
builtOptPlugins = map (package: package // {optional = true;}) (buildConfigPlugins vimOptions.optPlugins);
# additional Lua and Python3 packages, mapped to their respective functions
# to conform to the format mnw expects. end user should
# only ever need to pass a list of packages, which are modified
# here
extraLuaPackages = ps: map (x: ps.${x}) vimOptions.luaPackages;
extraPython3Packages = ps: map (x: ps.${x}) vimOptions.python3Packages;
# Wrap the user's desired (unwrapped) Neovim package with arguments that'll be used to
# generate a wrapped Neovim package.
neovim-wrapped = inputs.mnw.lib.wrap pkgs {
neovim = vimOptions.package;
plugins = builtStartPlugins ++ builtOptPlugins;
appName = "nvf";
extraBinPath = vimOptions.extraPackages;
initLua = vimOptions.builtLuaConfigRC;
luaFiles = vimOptions.extraLuaFiles;
inherit (vimOptions) viAlias vimAlias withRuby withNodeJs withPython3;
inherit extraLuaPackages extraPython3Packages;
};
dummyInit = pkgs.writeText "nvf-init.lua" vimOptions.builtLuaConfigRC;
# Additional helper scripts for printing and displaying nvf configuration
# in your commandline.
printConfig = pkgs.writers.writeDashBin "nvf-print-config" "cat ${dummyInit}";
printConfigPath = pkgs.writers.writeDashBin "nvf-print-config-path" "echo -n ${dummyInit}";
# Expose wrapped neovim-package for userspace
# or module consumption.
neovim = pkgs.symlinkJoin {
name = "nvf-with-helpers";
paths = [neovim-wrapped printConfig printConfigPath];
postBuild = "echo Helpers added";
# Allow evaluating vimOptions, i.e., config.vim from the packages' passthru
# attribute. For example, packages.x86_64-linux.neovim.passthru.neovimConfig
# will return the configuration in full.
passthru.neovimConfig = vimOptions;
meta =
neovim-wrapped.meta
// {
description = "Wrapped Neovim package with helper scripts to print the config (path)";
};
};
in { in {
config = { config.vim.build = {
vim.startPlugins = map (x: x.package) (attrValues cfg.extraPlugins); finalPackage = neovim;
}; };
} }

View file

@ -1,6 +1,3 @@
{ {
imports = [ imports = [./options.nix ./config.nix];
./config.nix
./options.nix
];
} }

View file

@ -1,144 +1,12 @@
{ {lib, ...}: let
pkgs, inherit (lib.types) package;
lib, inherit (lib.options) mkOption;
...
}: let
inherit (lib.options) mkOption mkEnableOption literalMD;
inherit (lib.types) package bool str listOf attrsOf;
inherit (lib.nvim.types) pluginsOpt extraPluginType;
in { in {
options.vim = { options.vim.build = {
package = mkOption { finalPackage = mkOption {
type = package; type = package;
default = pkgs.neovim-unwrapped; readOnly = true;
description = '' description = "final output package";
The neovim package to use for the wrapper. This
corresponds to the package that will be wrapped
with your plugins and settings.
::: {.warning}
You will need to use an unwrapped package for this
option to work as intended. Using an already wrapped
package here may yield undesirable results.
:::
'';
};
viAlias = mkOption {
type = bool;
default = true;
description = "Enable the `vi` alias for `nvim`";
};
vimAlias = mkOption {
type = bool;
default = true;
description = "Enable the `vim` alias for `nvim`";
};
startPlugins = pluginsOpt {
default = ["plenary-nvim"];
example = ''
[pkgs.vimPlugins.telescope-nvim]
'';
description = ''
List of plugins to load on startup. This is used
internally to add plugins to Neovim's runtime.
To add additional plugins to your configuration, consider
using the [{option}`vim.extraPlugins`](#opt-vim.extraPlugins)
option.
'';
};
optPlugins = pluginsOpt {
default = [];
example = ''
[pkgs.vimPlugins.vim-ghost]
'';
description = ''
List of plugins to optionally load on startup.
This option has the same type definition as {option}`vim.startPlugins`
and plugins in this list are appended to {option}`vim.startPlugins` by
the wrapper during the build process.
To avoid overriding packages and dependencies provided by startPlugins, you
are recommended to use this option or {option}`vim.extraPlugins` option.
'';
};
extraPlugins = mkOption {
type = attrsOf extraPluginType;
default = {};
description = ''
A list of plugins and their configurations that will be
set up after builtin plugins.
This option takes a special type that allows you to order
your custom plugins using nvf's modified DAG library.
'';
example = literalMD ''
```nix
with pkgs.vimPlugins; {
aerial = {
package = aerial-nvim;
setup = "require('aerial').setup {}";
};
harpoon = {
package = harpoon;
setup = "require('harpoon').setup {}";
after = ["aerial"]; # place harpoon configuration after aerial
};
}
```
'';
};
extraPackages = mkOption {
type = listOf package;
default = [];
example = ''[pkgs.fzf pkgs.ripgrep]'';
description = ''
List of additional packages to make available to the Neovim
wrapper.
'';
};
# this defaults to `true` in the wrapper
# and since we pass this value to the wrapper
# with an inherit, it should be `true` here as well
withRuby =
mkEnableOption ''
Ruby support in the Neovim wrapper.
''
// {
default = true;
};
withNodeJs = mkEnableOption ''
NodeJs support in the Neovim wrapper
'';
luaPackages = mkOption {
type = listOf str;
default = [];
example = ''["magick" "serpent"]'';
description = "List of lua packages to install";
};
withPython3 = mkEnableOption ''
Python3 support in the Neovim wrapper
'';
python3Packages = mkOption {
type = listOf str;
default = [];
example = ''["pynvim"]'';
description = "List of python packages to install";
}; };
}; };
} }

View file

@ -0,0 +1,13 @@
{
config,
lib,
...
}: let
inherit (lib.attrsets) attrValues;
cfg = config.vim;
in {
config = {
vim.startPlugins = map (x: x.package) (attrValues cfg.extraPlugins);
};
}

View file

@ -0,0 +1,6 @@
{
imports = [
./config.nix
./options.nix
];
}

View file

@ -0,0 +1,144 @@
{
pkgs,
lib,
...
}: let
inherit (lib.options) mkOption mkEnableOption literalMD;
inherit (lib.types) package bool str listOf attrsOf;
inherit (lib.nvim.types) pluginsOpt extraPluginType;
in {
options.vim = {
package = mkOption {
type = package;
default = pkgs.neovim-unwrapped;
description = ''
The neovim package to use for the wrapper. This
corresponds to the package that will be wrapped
with your plugins and settings.
::: {.warning}
You will need to use an unwrapped package for this
option to work as intended. Using an already wrapped
package here may yield undesirable results.
:::
'';
};
viAlias = mkOption {
type = bool;
default = true;
description = "Enable the `vi` alias for `nvim`";
};
vimAlias = mkOption {
type = bool;
default = true;
description = "Enable the `vim` alias for `nvim`";
};
startPlugins = pluginsOpt {
default = ["plenary-nvim"];
example = ''
[pkgs.vimPlugins.telescope-nvim]
'';
description = ''
List of plugins to load on startup. This is used
internally to add plugins to Neovim's runtime.
To add additional plugins to your configuration, consider
using the [{option}`vim.extraPlugins`](#opt-vim.extraPlugins)
option.
'';
};
optPlugins = pluginsOpt {
default = [];
example = ''
[pkgs.vimPlugins.vim-ghost]
'';
description = ''
List of plugins to optionally load on startup.
This option has the same type definition as {option}`vim.startPlugins`
and plugins in this list are appended to {option}`vim.startPlugins` by
the wrapper during the build process.
To avoid overriding packages and dependencies provided by startPlugins, you
are recommended to use this option or {option}`vim.extraPlugins` option.
'';
};
extraPlugins = mkOption {
type = attrsOf extraPluginType;
default = {};
description = ''
A list of plugins and their configurations that will be
set up after builtin plugins.
This option takes a special type that allows you to order
your custom plugins using nvf's modified DAG library.
'';
example = literalMD ''
```nix
with pkgs.vimPlugins; {
aerial = {
package = aerial-nvim;
setup = "require('aerial').setup {}";
};
harpoon = {
package = harpoon;
setup = "require('harpoon').setup {}";
after = ["aerial"]; # place harpoon configuration after aerial
};
}
```
'';
};
extraPackages = mkOption {
type = listOf package;
default = [];
example = ''[pkgs.fzf pkgs.ripgrep]'';
description = ''
List of additional packages to make available to the Neovim
wrapper.
'';
};
# this defaults to `true` in the wrapper
# and since we pass this value to the wrapper
# with an inherit, it should be `true` here as well
withRuby =
mkEnableOption ''
Ruby support in the Neovim wrapper.
''
// {
default = true;
};
withNodeJs = mkEnableOption ''
NodeJs support in the Neovim wrapper
'';
luaPackages = mkOption {
type = listOf str;
default = [];
example = ''["magick" "serpent"]'';
description = "List of lua packages to install";
};
withPython3 = mkEnableOption ''
Python3 support in the Neovim wrapper
'';
python3Packages = mkOption {
type = listOf str;
default = [];
example = ''["pynvim"]'';
description = "List of python packages to install";
};
};
}