modules/top-level: move out of wrappers/modules

This commit is contained in:
Matt Sturgeon 2024-07-05 14:25:21 +01:00
parent b59fa976d0
commit 2deb61f6a5
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
10 changed files with 34 additions and 16 deletions

View file

@ -5,12 +5,6 @@ let
cfg = config.programs.nixvim;
in
{
topLevelModules = [
../modules
./modules/output.nix
./modules/files.nix
];
helpers = mkOption {
type = mkOptionType {
name = "helpers";

View file

@ -30,7 +30,10 @@ in
defaultPkgs = pkgs;
inherit helpers;
};
modules = [ ./modules/darwin.nix ] ++ shared.topLevelModules;
modules = [
./modules/darwin.nix
../modules/top-level
];
};
};
nixvim.helpers = shared.helpers;

View file

@ -32,7 +32,10 @@ in
defaultPkgs = pkgs;
inherit helpers;
};
modules = [ ./modules/hm.nix ] ++ shared.topLevelModules;
modules = [
./modules/hm.nix
../modules/top-level
];
};
};
nixvim.helpers = shared.helpers;

View file

@ -1,73 +0,0 @@
{
pkgs,
config,
lib,
helpers,
...
}:
let
inherit (lib) types;
fileModuleType = types.submoduleWith {
shorthandOnlyDefinesConfig = true;
specialArgs = {
inherit helpers;
defaultPkgs = pkgs;
};
modules = [
(
{ name, config, ... }:
{
imports = [ ../../modules ];
options.plugin = lib.mkOption {
type = types.package;
description = "A derivation with the content of the file in it";
readOnly = true;
internal = true;
};
config = {
path = name;
type = lib.mkDefault (if lib.hasSuffix ".vim" name then "vim" else "lua");
plugin = pkgs.writeTextDir config.path config.content;
};
}
)
];
};
in
{
options = {
files = lib.mkOption {
type = types.attrsOf fileModuleType;
description = "Files to include in the Vim config.";
default = { };
};
filesPlugin = lib.mkOption {
type = types.package;
description = "A derivation with all the files inside.";
internal = true;
readOnly = true;
};
};
config =
let
inherit (config) files;
concatFilesOption = attr: lib.flatten (lib.mapAttrsToList (_: builtins.getAttr attr) files);
in
{
# Each file can declare plugins/packages/warnings/assertions
extraPlugins = concatFilesOption "extraPlugins";
extraPackages = concatFilesOption "extraPackages";
warnings = concatFilesOption "warnings";
assertions = concatFilesOption "assertions";
# A directory with all the files in it
filesPlugin = pkgs.buildEnv {
name = "nixvim-config";
paths =
(lib.mapAttrsToList (_: file: file.plugin) files)
++ (lib.mapAttrsToList pkgs.writeTextDir config.extraFiles);
};
};
}

View file

@ -1,165 +0,0 @@
{
pkgs,
config,
lib,
helpers,
...
}:
with lib;
{
options = {
viAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink `vi` to `nvim` binary.
'';
};
vimAlias = mkOption {
type = types.bool;
default = false;
description = ''
Symlink `vim` to `nvim` binary.
'';
};
withRuby = mkOption {
type = types.bool;
default = true;
description = "Enable Ruby provider.";
};
withNodeJs = mkOption {
type = types.bool;
default = false;
description = "Enable Node provider.";
};
package = mkOption {
type = types.package;
default = pkgs.neovim-unwrapped;
description = "Neovim to use for NixVim.";
};
wrapRc = mkOption {
type = types.bool;
description = "Should the config be included in the wrapper script.";
default = false;
};
finalPackage = mkOption {
type = types.package;
description = "Wrapped Neovim.";
readOnly = true;
};
initPath = mkOption {
type = types.str;
description = "The path to the `init.lua` file.";
readOnly = true;
visible = false;
};
initContent = mkOption {
type = types.str;
description = "The content of the `init.lua` file.";
readOnly = true;
visible = false;
};
printInitPackage = mkOption {
type = types.package;
description = "A tool to show the content of the generated `init.lua` file.";
readOnly = true;
visible = false;
};
};
config =
let
defaultPlugin = {
plugin = null;
config = "";
optional = false;
};
normalizedPlugins = map (
x: defaultPlugin // (if x ? plugin then x else { plugin = x; })
) config.extraPlugins;
neovimConfig = pkgs.neovimUtils.makeNeovimConfig (
{
inherit (config)
extraPython3Packages
viAlias
vimAlias
withRuby
withNodeJs
;
# inherit customRC;
plugins = normalizedPlugins;
}
# Necessary to make sure the runtime path is set properly in NixOS 22.05,
# or more generally before the commit:
# cda1f8ae468 - neovim: pass packpath via the wrapper
// optionalAttrs (functionArgs pkgs.neovimUtils.makeNeovimConfig ? configure) {
configure.packages = {
nixvim = {
start = map (x: x.plugin) normalizedPlugins;
opt = [ ];
};
};
}
);
customRC =
let
hasContent = str: (builtins.match "[[:space:]]*" str) == null;
in
(optionalString (hasContent neovimConfig.neovimRcContent) ''
vim.cmd([[
${neovimConfig.neovimRcContent}
]])
'')
+ config.content;
init = helpers.writeLua "init.lua" customRC;
extraWrapperArgs = builtins.concatStringsSep " " (
(optional (config.extraPackages != [ ]) ''--prefix PATH : "${makeBinPath config.extraPackages}"'')
++ (optional config.wrapRc ''--add-flags -u --add-flags "${init}"'')
);
wrappedNeovim = pkgs.wrapNeovimUnstable config.package (
neovimConfig
// {
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " " + extraWrapperArgs;
wrapRc = false;
}
);
in
{
type = lib.mkForce "lua";
finalPackage = wrappedNeovim;
initContent = readFile init;
initPath = "${init}";
printInitPackage = pkgs.writeShellApplication {
name = "nixvim-print-init";
runtimeInputs = [ pkgs.bat ];
text = ''
bat --language=lua "${init}"
'';
};
extraConfigLuaPre = lib.optionalString config.wrapRc ''
-- Ignore the user lua configuration
vim.opt.runtimepath:remove(vim.fn.stdpath('config')) -- ~/.config/nvim
vim.opt.runtimepath:remove(vim.fn.stdpath('config') .. "/after") -- ~/.config/nvim/after
vim.opt.runtimepath:remove(vim.fn.stdpath('data') .. "/site") -- ~/.local/share/nvim/site
'';
extraPlugins = if config.wrapRc then [ config.filesPlugin ] else [ ];
};
}

View file

@ -33,7 +33,10 @@ in
defaultPkgs = pkgs;
inherit helpers;
};
modules = [ ./modules/nixos.nix ] ++ shared.topLevelModules;
modules = [
./modules/nixos.nix
../modules/top-level
];
};
};
nixvim.helpers = shared.helpers;

View file

@ -10,10 +10,6 @@ let
inherit (pkgs) lib;
helpers = getHelpers pkgs _nixvimTests;
shared = import ./_shared.nix helpers {
inherit pkgs lib;
config = { };
};
handleAssertions =
config:
@ -32,7 +28,8 @@ let
modules = [
mod
{ wrapRc = true; }
] ++ shared.topLevelModules;
../modules/top-level
];
specialArgs = {
inherit helpers;
defaultPkgs = pkgs;