mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 08:35:43 +02:00
Allow using global config with hm and nixos (#48)
* remove useless nixvim file * reorganize flake outputs * use global config file with home-manager and nixos
This commit is contained in:
parent
931db3e83f
commit
f2a103da30
8 changed files with 146 additions and 355 deletions
43
flake.nix
43
flake.nix
|
@ -6,7 +6,6 @@
|
|||
inputs.nmdSrc.url = "gitlab:rycee/nmd";
|
||||
inputs.nmdSrc.flake = false;
|
||||
|
||||
# TODO: Use flake-utils to support all architectures
|
||||
outputs = { self, nixpkgs, nmdSrc, flake-utils, ... }@inputs:
|
||||
with nixpkgs.lib;
|
||||
with builtins;
|
||||
|
@ -30,24 +29,11 @@
|
|||
./plugins/default.nix
|
||||
];
|
||||
|
||||
nixvimOption = pkgs: mkOption {
|
||||
type = types.submodule ((modules pkgs) ++ [{
|
||||
options.enable = mkEnableOption "Enable nixvim";
|
||||
}]);
|
||||
};
|
||||
|
||||
build = pkgs:
|
||||
configuration:
|
||||
let
|
||||
eval = evalModules {
|
||||
modules = modules pkgs ++ [{ config = configuration; }];
|
||||
};
|
||||
in
|
||||
eval.config.output;
|
||||
|
||||
flakeOutput =
|
||||
flake-utils.lib.eachDefaultSystem
|
||||
(system: rec {
|
||||
(system: let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
in {
|
||||
packages.docs = import ./docs {
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
lib = nixpkgs.lib;
|
||||
|
@ -55,28 +41,11 @@
|
|||
inherit nmdSrc;
|
||||
};
|
||||
|
||||
nixosModules.nixvim = { pkgs, config, lib, ... }: {
|
||||
options.programs.nixvim = nixvimOption pkgs;
|
||||
config = mkIf config.programs.nixvim.enable {
|
||||
environment.systemPackages = [
|
||||
config.programs.nixvim.output
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
homeManagerModules.nixvim = { pkgs, config, lib, ... }: {
|
||||
options.programs.nixvim = nixvimOption pkgs;
|
||||
config = mkIf config.programs.nixvim.enable {
|
||||
home.packages = [
|
||||
config.programs.nixvim.output
|
||||
];
|
||||
};
|
||||
};
|
||||
legacyPackages.makeNixvim = import ./wrappers/standalone.nix pkgs (modules pkgs);
|
||||
});
|
||||
in
|
||||
flakeOutput // {
|
||||
inherit build;
|
||||
homeManagerModules.nixvim = flakeOutput.homeManagerModules.x86_64-linux.nixvim;
|
||||
nixosModules.nixvim = flakeOutput.nixosModules.x86_64-linux.nixvim;
|
||||
nixosModules.nixvim = import ./wrappers/nixos.nix modules;
|
||||
homeManagerModules.nixvim = import ./wrappers/hm.nix modules;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -65,9 +65,21 @@ in
|
|||
description = "Extra contents for init.vim";
|
||||
};
|
||||
|
||||
output = mkOption {
|
||||
wrapRc = mkOption {
|
||||
type = types.bool;
|
||||
description = "Should the config be included in the wrapper script";
|
||||
default = false;
|
||||
};
|
||||
|
||||
finalPackage = mkOption {
|
||||
type = types.package;
|
||||
description = "Final package built by nixvim";
|
||||
description = "Wrapped neovim";
|
||||
readOnly = true;
|
||||
};
|
||||
|
||||
initContent = mkOption {
|
||||
type = types.str;
|
||||
description = "The content of the init.vim file";
|
||||
readOnly = true;
|
||||
visible = false;
|
||||
};
|
||||
|
@ -113,9 +125,10 @@ in
|
|||
|
||||
wrappedNeovim = pkgs.wrapNeovimUnstable config.package (neovimConfig // {
|
||||
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " " + extraWrapperArgs;
|
||||
inherit (config) wrapRc;
|
||||
});
|
||||
in
|
||||
{
|
||||
output = wrappedNeovim;
|
||||
in {
|
||||
finalPackage = wrappedNeovim;
|
||||
initContent = neovimConfig.neovimRcContent;
|
||||
};
|
||||
}
|
||||
|
|
306
nixvim.nix
306
nixvim.nix
|
@ -1,306 +0,0 @@
|
|||
{ nixos ? false, nixOnDroid ? false, homeManager ? false }:
|
||||
{ pkgs, lib, config, ... }:
|
||||
with lib;
|
||||
let
|
||||
cfg = config.programs.nixvim;
|
||||
|
||||
pluginWithConfigType = types.submodule {
|
||||
options = {
|
||||
config = mkOption {
|
||||
type = types.lines;
|
||||
description = "vimscript for this plugin to be placed in init.vim";
|
||||
default = "";
|
||||
};
|
||||
|
||||
optional = mkEnableOption "optional" // {
|
||||
description = "Don't load by default (load with :packadd)";
|
||||
};
|
||||
|
||||
plugin = mkOption {
|
||||
type = types.package;
|
||||
description = "vim plugin";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
mapOption = types.oneOf [
|
||||
types.str
|
||||
(types.submodule {
|
||||
options = {
|
||||
silent = mkOption {
|
||||
type = types.bool;
|
||||
description = "Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
|
||||
default = false;
|
||||
};
|
||||
|
||||
nowait = mkOption {
|
||||
type = types.bool;
|
||||
description = "Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
|
||||
default = false;
|
||||
};
|
||||
|
||||
script = mkOption {
|
||||
type = types.bool;
|
||||
description = "Equivalent to adding <script> to a map.";
|
||||
default = false;
|
||||
};
|
||||
|
||||
expr = mkOption {
|
||||
type = types.bool;
|
||||
description = "Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
|
||||
default = false;
|
||||
};
|
||||
|
||||
unique = mkOption {
|
||||
type = types.bool;
|
||||
description = "Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
|
||||
default = false;
|
||||
};
|
||||
|
||||
noremap = mkOption {
|
||||
type = types.bool;
|
||||
description = "Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
|
||||
default = true;
|
||||
};
|
||||
|
||||
action = mkOption {
|
||||
type = types.str;
|
||||
description = "The action to execute.";
|
||||
};
|
||||
|
||||
description = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
description = "A textual description of this keybind, to be shown in which-key, if you have it.";
|
||||
default = null;
|
||||
};
|
||||
};
|
||||
})
|
||||
];
|
||||
|
||||
mapOptions = mode: mkOption {
|
||||
description = "Mappings for ${mode} mode";
|
||||
type = types.attrsOf mapOption;
|
||||
default = { };
|
||||
};
|
||||
|
||||
helpers = import ./plugins/helpers.nix { lib = lib; };
|
||||
in
|
||||
{
|
||||
options = {
|
||||
programs.nixvim = {
|
||||
enable = mkEnableOption "enable NixVim";
|
||||
|
||||
package = mkOption {
|
||||
type = types.nullOr types.package;
|
||||
default = null;
|
||||
description = "The package to use for neovim.";
|
||||
};
|
||||
|
||||
extraPlugins = mkOption {
|
||||
type = with types; listOf (either package pluginWithConfigType);
|
||||
default = [ ];
|
||||
description = "List of vim plugins to install.";
|
||||
};
|
||||
|
||||
colorscheme = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
description = "The name of the colorscheme";
|
||||
default = null;
|
||||
};
|
||||
|
||||
extraConfigLua = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
description = "Extra contents for init.lua";
|
||||
};
|
||||
|
||||
extraLuaPreConfig = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
description = "Extra contents for init.lua before everything else";
|
||||
};
|
||||
|
||||
extraLuaPostConfig = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
description = "Extra contents for init.lua after everything else";
|
||||
};
|
||||
|
||||
extraConfigVim = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
description = "Extra contents for init.vim";
|
||||
};
|
||||
|
||||
extraPackages = mkOption {
|
||||
type = types.listOf types.package;
|
||||
default = [ ];
|
||||
example = "[ pkgs.shfmt ]";
|
||||
description = "Extra packages to be made available to neovim";
|
||||
};
|
||||
|
||||
configure = mkOption {
|
||||
type = types.attrsOf types.anything;
|
||||
default = { };
|
||||
description = "Internal option";
|
||||
};
|
||||
|
||||
options = mkOption {
|
||||
type = types.attrsOf types.anything;
|
||||
default = { };
|
||||
description = "The configuration options, e.g. line numbers";
|
||||
};
|
||||
|
||||
globals = mkOption {
|
||||
type = types.attrsOf types.anything;
|
||||
default = { };
|
||||
description = "Global variables";
|
||||
};
|
||||
|
||||
maps = mkOption {
|
||||
type = types.submodule {
|
||||
options = {
|
||||
normal = mapOptions "normal";
|
||||
insert = mapOptions "insert";
|
||||
select = mapOptions "select";
|
||||
visual = mapOptions "visual and select";
|
||||
terminal = mapOptions "terminal";
|
||||
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
|
||||
|
||||
visualOnly = mapOptions "visual only";
|
||||
operator = mapOptions "operator-pending";
|
||||
insertCommand = mapOptions "insert and command-line";
|
||||
lang = mapOptions "insert, command-line and lang-arg";
|
||||
command = mapOptions "command-line";
|
||||
};
|
||||
};
|
||||
default = { };
|
||||
description = ''
|
||||
Custom keybindings for any mode.
|
||||
|
||||
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp.
|
||||
'';
|
||||
|
||||
example = ''
|
||||
maps = {
|
||||
normalVisualOp.";" = ":"; # Same as noremap ; :
|
||||
normal."<leader>m" = {
|
||||
silent = true;
|
||||
action = "<cmd>make<CR>";
|
||||
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
|
||||
};
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
imports = [
|
||||
./plugins
|
||||
];
|
||||
|
||||
config =
|
||||
let
|
||||
neovimConfig = pkgs.neovimUtils.makeNeovimConfig {
|
||||
configure = cfg.configure;
|
||||
plugins = cfg.extraPlugins;
|
||||
};
|
||||
|
||||
extraWrapperArgs = optionalString (cfg.extraPackages != [ ])
|
||||
''--prefix PATH : "${makeBinPath cfg.extraPackages}"'';
|
||||
|
||||
package = if (cfg.package != null) then cfg.package else pkgs.neovim;
|
||||
|
||||
wrappedNeovim = pkgs.wrapNeovimUnstable package (neovimConfig // {
|
||||
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs + " "
|
||||
+ extraWrapperArgs;
|
||||
});
|
||||
|
||||
luaGlobals = optionalString (cfg.globals != { }) ''
|
||||
-- Set up globals {{{
|
||||
local __nixvim_globals = ${helpers.toLuaObject cfg.globals}
|
||||
|
||||
for k,v in pairs(__nixvim_globals) do
|
||||
vim.g[k] = v
|
||||
end
|
||||
-- }}}
|
||||
'' + optionalString (cfg.options != { }) ''
|
||||
-- Set up options {{{
|
||||
local __nixvim_options = ${helpers.toLuaObject cfg.options}
|
||||
|
||||
for k,v in pairs(__nixvim_options) do
|
||||
vim.o[k] = v
|
||||
end
|
||||
-- }}}
|
||||
'' + optionalString (mappings != [ ]) ''
|
||||
-- Set up keybinds {{{
|
||||
local __nixvim_binds = ${helpers.toLuaObject mappings}
|
||||
|
||||
for i, map in ipairs(__nixvim_binds) do
|
||||
vim.api.nvim_set_keymap(map.mode, map.key, map.action, map.config)
|
||||
end
|
||||
-- }}}
|
||||
'';
|
||||
|
||||
configure = {
|
||||
# Make sure that globals are set before plugins are setup.
|
||||
# This is becuase you might want to define variables or global functions
|
||||
# that the plugin configuration depend upon.
|
||||
customRC = cfg.extraConfigVim + ''
|
||||
lua <<EOF
|
||||
${cfg.extraLuaPreConfig}
|
||||
${luaGlobals}
|
||||
${cfg.extraConfigLua}
|
||||
'' +
|
||||
# Set colorscheme after setting globals.
|
||||
# Some colorschemes depends on variables being set before setting the colorscheme.
|
||||
(optionalString (cfg.colorscheme != "" && cfg.colorscheme != null) ''
|
||||
vim.cmd([[colorscheme ${cfg.colorscheme}]])
|
||||
'') +
|
||||
''
|
||||
${cfg.extraLuaPostConfig}
|
||||
EOF
|
||||
'';
|
||||
|
||||
packages.nixvim = {
|
||||
start = filter (f: f != null) (map
|
||||
(x:
|
||||
if x ? plugin && x.optional == true then null else (x.plugin or x))
|
||||
cfg.extraPlugins);
|
||||
opt = filter (f: f != null)
|
||||
(map (x: if x ? plugin && x.optional == true then x.plugin else null)
|
||||
cfg.extraPlugins);
|
||||
};
|
||||
};
|
||||
|
||||
mappings =
|
||||
(helpers.genMaps "" cfg.maps.normalVisualOp) ++
|
||||
(helpers.genMaps "n" cfg.maps.normal) ++
|
||||
(helpers.genMaps "i" cfg.maps.insert) ++
|
||||
(helpers.genMaps "v" cfg.maps.visual) ++
|
||||
(helpers.genMaps "x" cfg.maps.visualOnly) ++
|
||||
(helpers.genMaps "s" cfg.maps.select) ++
|
||||
(helpers.genMaps "t" cfg.maps.terminal) ++
|
||||
(helpers.genMaps "o" cfg.maps.operator) ++
|
||||
(helpers.genMaps "l" cfg.maps.lang) ++
|
||||
(helpers.genMaps "!" cfg.maps.insertCommand) ++
|
||||
(helpers.genMaps "c" cfg.maps.command);
|
||||
|
||||
in
|
||||
mkIf cfg.enable (if nixos then {
|
||||
environment.systemPackages = [ wrappedNeovim ];
|
||||
programs.neovim = {
|
||||
configure = configure;
|
||||
};
|
||||
|
||||
environment.etc."xdg/nvim/sysinit.vim".text = neovimConfig.neovimRcContent;
|
||||
} else
|
||||
(if homeManager then {
|
||||
programs.neovim = {
|
||||
enable = true;
|
||||
package = mkIf (cfg.package != null) cfg.package;
|
||||
extraPackages = cfg.extraPackages;
|
||||
extraConfig = configure.customRC;
|
||||
plugins = cfg.extraPlugins;
|
||||
};
|
||||
} else { }));
|
||||
}
|
59
tests/flake.lock
generated
59
tests/flake.lock
generated
|
@ -30,6 +30,21 @@
|
|||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils_3": {
|
||||
"locked": {
|
||||
"lastModified": 1659877975,
|
||||
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1663491030,
|
||||
|
@ -82,12 +97,31 @@
|
|||
},
|
||||
"locked": {
|
||||
"lastModified": 0,
|
||||
"narHash": "sha256-NTQaGxll4rko4eOOxtCODb2mnkwVMNyJNqwsRCc89h4=",
|
||||
"path": "/nix/store/a1ngrmrp1wbv12682s0c2dp60b7gljm8-source",
|
||||
"narHash": "sha256-Yx2qpy5nrZawU4JlnykTX4OxqIHt7lxHZz1NWdMMWcQ=",
|
||||
"path": "/nix/store/wq6fxdzm72jz96zywn7ymigf2yrkff0a-source",
|
||||
"type": "path"
|
||||
},
|
||||
"original": {
|
||||
"path": "/nix/store/a1ngrmrp1wbv12682s0c2dp60b7gljm8-source",
|
||||
"path": "/nix/store/wq6fxdzm72jz96zywn7ymigf2yrkff0a-source",
|
||||
"type": "path"
|
||||
}
|
||||
},
|
||||
"nixvim-stable": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils_3",
|
||||
"nixpkgs": [
|
||||
"nixpkgs-stable"
|
||||
],
|
||||
"nmdSrc": "nmdSrc_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 0,
|
||||
"narHash": "sha256-Yx2qpy5nrZawU4JlnykTX4OxqIHt7lxHZz1NWdMMWcQ=",
|
||||
"path": "/nix/store/wq6fxdzm72jz96zywn7ymigf2yrkff0a-source",
|
||||
"type": "path"
|
||||
},
|
||||
"original": {
|
||||
"path": "/nix/store/wq6fxdzm72jz96zywn7ymigf2yrkff0a-source",
|
||||
"type": "path"
|
||||
}
|
||||
},
|
||||
|
@ -107,12 +141,29 @@
|
|||
"type": "gitlab"
|
||||
}
|
||||
},
|
||||
"nmdSrc_2": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1654807200,
|
||||
"narHash": "sha256-RNLq09vfj21TyYuUCeD6BNTNC6Ew8bLhQULZytN4Xx8=",
|
||||
"owner": "rycee",
|
||||
"repo": "nmd",
|
||||
"rev": "91dee681dd1c478d6040a00835d73c0f4a4c5c29",
|
||||
"type": "gitlab"
|
||||
},
|
||||
"original": {
|
||||
"owner": "rycee",
|
||||
"repo": "nmd",
|
||||
"type": "gitlab"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"nixpkgs-stable": "nixpkgs-stable",
|
||||
"nixvim": "nixvim"
|
||||
"nixvim": "nixvim",
|
||||
"nixvim-stable": "nixvim-stable"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -5,15 +5,19 @@
|
|||
inputs.nixvim.url = "./..";
|
||||
|
||||
inputs.nixpkgs-stable.url = "github:NixOS/nixpkgs/nixos-22.05";
|
||||
inputs.nixvim-stable = {
|
||||
url = "./..";
|
||||
inputs.nixpkgs.follows = "nixpkgs-stable";
|
||||
};
|
||||
|
||||
outputs = { self, nixvim, nixpkgs, flake-utils, nixpkgs-stable, ... }:
|
||||
outputs = { self, nixvim, nixvim-stable, nixpkgs, flake-utils, nixpkgs-stable, ... }:
|
||||
(flake-utils.lib.eachDefaultSystem
|
||||
(system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
pkgs-stable = import nixpkgs-stable { inherit system; };
|
||||
build = nixvim.build pkgs;
|
||||
build-stable = nixvim.build pkgs-stable;
|
||||
build = nixvim.legacyPackages.${system}.makeNixvim;
|
||||
build-stable = nixvim-stable.legacyPackages.${system}.makeNixvim;
|
||||
in
|
||||
rec {
|
||||
# A plain nixvim configuration
|
||||
|
|
23
wrappers/hm.nix
Normal file
23
wrappers/hm.nix
Normal file
|
@ -0,0 +1,23 @@
|
|||
modules:
|
||||
{ pkgs, config, lib, ... }:
|
||||
|
||||
let
|
||||
inherit (lib) mkEnableOption mkOption mkIf mkMerge types;
|
||||
cfg = config.programs.nixvim;
|
||||
in {
|
||||
options = {
|
||||
programs.nixvim = lib.mkOption {
|
||||
type = types.submodule ((modules pkgs) ++ [{
|
||||
options.enable = mkEnableOption "nixvim";
|
||||
}]);
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable
|
||||
(mkMerge [
|
||||
{ home.packages = [ cfg.finalPackage ]; }
|
||||
(mkIf (!cfg.wrapRc) {
|
||||
xdg.configFile."nvim/init.vim".text = cfg.initContent;
|
||||
})
|
||||
]);
|
||||
}
|
24
wrappers/nixos.nix
Normal file
24
wrappers/nixos.nix
Normal file
|
@ -0,0 +1,24 @@
|
|||
modules:
|
||||
{ pkgs, config, lib, ... }:
|
||||
|
||||
let
|
||||
inherit (lib) mkEnableOption mkOption mkMerge mkIf types;
|
||||
cfg = config.programs.nixvim;
|
||||
in {
|
||||
options = {
|
||||
programs.nixvim = lib.mkOption {
|
||||
type = types.submodule ((modules pkgs) ++ [{
|
||||
options.enable = mkEnableOption "nixvim";
|
||||
}]);
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable
|
||||
(mkMerge [
|
||||
{ environment.systemPackages = [ cfg.finalPackage ]; }
|
||||
(mkIf (!cfg.wrapRc) {
|
||||
environment.etc."nvim/sysinit.vim".text = cfg.initContent;
|
||||
environment.variables."VIM" = "/etc/nvim";
|
||||
})
|
||||
]);
|
||||
}
|
13
wrappers/standalone.nix
Normal file
13
wrappers/standalone.nix
Normal file
|
@ -0,0 +1,13 @@
|
|||
pkgs: modules: configuration:
|
||||
|
||||
let
|
||||
|
||||
inherit (pkgs) lib;
|
||||
|
||||
wrap = { wrapRc = true; };
|
||||
|
||||
eval = lib.evalModules {
|
||||
modules = modules ++ [ { config = configuration; } wrap ];
|
||||
};
|
||||
|
||||
in eval.config.finalPackage
|
Loading…
Add table
Add a link
Reference in a new issue