From 4ddd3969e5c8742dbf01c1471ad9806de22cc65f Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Sun, 18 Sep 2022 11:19:23 +0100 Subject: [PATCH] nixvim: support standalone nixvim This represents a major rearchitecture for nixvim, so I'm leaving this up to track the progress for now, and to serve as a reference for any breaking changes during transition. The main change is, of course, being able to use nixvim standalone. To do this, you should use the new build function, which takes in two arguments: the system architecture (e.g. x86_64-linux) and the configuration. For the new configuration, do not use the programs.nixvim. prefix. For module development, the main change is that you should no longer prefix your modules with programs.nixvim.. --- README.md | 19 +++ docs/default.nix | 36 ++--- flake.lock | 38 +++-- flake.nix | 157 ++++++++----------- modules/colorscheme.nix | 17 ++ modules/keymaps.nix | 131 ++++++++++++++++ modules/options.nix | 41 +++++ modules/output.nix | 109 +++++++++++++ modules/plugins.nix | 6 + plugins/bufferlines/barbar.nix | 6 +- plugins/bufferlines/bufferline.nix | 200 ++++++++++++------------ plugins/colorschemes/base16.nix | 19 ++- plugins/colorschemes/gruvbox.nix | 55 ++++--- plugins/colorschemes/nord.nix | 26 ++- plugins/colorschemes/one.nix | 17 +- plugins/colorschemes/onedark.nix | 17 +- plugins/colorschemes/tokyonight.nix | 43 +++-- plugins/completion/coq.nix | 27 ++-- plugins/completion/nvim-cmp/default.nix | 50 +++--- plugins/git/gitgutter.nix | 80 +++++----- plugins/git/neogit.nix | 34 ++-- plugins/helpers.nix | 6 +- plugins/languages/treesitter.nix | 31 ++-- plugins/null-ls/default.nix | 20 +-- plugins/null-ls/helpers.nix | 80 +++++----- plugins/nvim-lsp/default.nix | 47 +++--- plugins/nvim-lsp/helpers.nix | 47 +++--- plugins/nvim-lsp/lsp-lines.nix | 6 +- plugins/nvim-lsp/lspsaga.nix | 6 +- plugins/pluginmanagers/packer.nix | 108 +++++++------ plugins/statuslines/airline.nix | 21 +-- plugins/statuslines/lightline.nix | 55 ++++--- plugins/statuslines/lualine.nix | 41 ++--- plugins/telescope/default.nix | 46 +++--- plugins/telescope/frecency.nix | 12 +- plugins/telescope/fzf-native.nix | 10 +- plugins/telescope/fzy-native.nix | 10 +- plugins/telescope/media-files.nix | 25 ++- plugins/utils/comment-nvim.nix | 34 ++-- plugins/utils/commentary.nix | 8 +- plugins/utils/dashboard.nix | 9 +- plugins/utils/easyescape.nix | 14 +- plugins/utils/floaterm.nix | 38 +++-- plugins/utils/intellitab.nix | 14 +- plugins/utils/mark-radar.nix | 25 +-- plugins/utils/notify.nix | 40 ++--- plugins/utils/nvim-autopairs.nix | 26 +-- plugins/utils/nvim-tree.nix | 119 +++++++------- plugins/utils/specs.nix | 57 +++---- plugins/utils/undotree.nix | 45 +++--- tests/flake.lock | 104 ++++++++++++ tests/flake.nix | 82 ++++++++++ 52 files changed, 1410 insertions(+), 904 deletions(-) create mode 100644 modules/colorscheme.nix create mode 100644 modules/keymaps.nix create mode 100644 modules/options.nix create mode 100644 modules/output.nix create mode 100644 modules/plugins.nix create mode 100644 tests/flake.lock create mode 100644 tests/flake.nix diff --git a/README.md b/README.md index 7df6a5d7..f1305bd8 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,25 @@ You can now access the module using `inputs.nixvim.homeManagerModules.nixvim`, for a home-manager instalation, and `inputs.nixvim.nixosModules.nixvim`, if you're not using it. +## Usage +NixVim can be used in three ways: through the home-manager and NixOS modules, +and through the `build` function. To use the modules, just import the +`nixvim.homeManagerModules.${system}.nixvim` and +`nixvim.nixosModules.${system}.nixvim` modules, depending on which system +you're using. + +If you want to use it standalone, you can use the `build` function: + +```nix +{ pkgs, nixvim, ... }: { + environment.systemModules = [ + (nixvim.build pkgs { + colorschemes.gruvbox.enable = true; + }) + ]; +} +``` + ## How does it work? When you build the module (probably using home-manager), it will install all your plugins and generate a lua config for NeoVim with all the options diff --git a/docs/default.nix b/docs/default.nix index e37ec223..5601e5f6 100644 --- a/docs/default.nix +++ b/docs/default.nix @@ -1,14 +1,10 @@ -{ pkgs ? import {} +{ pkgs ? import { } , lib ? import -, ... }: +, nmdSrc +, nixvimModules ? [ ] +, ... +}: let - nmdSrc = pkgs.fetchFromGitLab { - name = "nmd"; - owner = "rycee"; - repo = "nmd"; - rev = "527245ff605bde88c2dd2ddae21c6479bb7cf8aa"; - sha256 = "1zi0f9y3wq4bpslx1py3sfgrgd9av41ahpandvs6rvkpisfsqqlp"; - }; nmd = import nmdSrc { inherit pkgs lib; }; scrubbedPkgsModule = { imports = [{ @@ -22,14 +18,13 @@ let nmd.buildModulesDocs ({ moduleRootPaths = [ ./.. ]; mkModuleUrl = path: - "https://github.com/pta2002/nixvim/blob/master/${path}#blob-path"; + "https://github.com/pta2002/nixvim/blob/main/${path}#blob-path"; channelName = "nixvim"; } // args); nixvimDocs = buildModulesDocs { modules = [ - (import ../nixvim.nix {}) scrubbedPkgsModule - ]; + ] ++ nixvimModules; docBook.id = "nixvim-options"; }; @@ -39,12 +34,15 @@ let documentsDirectory = ./.; documentType = "book"; chunkToc = '' - - - - - - + + + + + + ''; }; -in docs.html +in +# TODO: Parse this json or something, since docbook isn't working (and it's kind of terrible anyway) +nixvimDocs.json + diff --git a/flake.lock b/flake.lock index ef3662b4..fd46cbc6 100644 --- a/flake.lock +++ b/flake.lock @@ -1,29 +1,42 @@ { "nodes": { - "nixpkgs": { + "flake-utils": { "locked": { - "lastModified": 1641710811, - "narHash": "sha256-yVJ+CtwWZY8BnkNIJ/ue5a28yrRM6CkDF1LvmGmqqwM=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "0ecf7d414811f831060cf55707c374d54fbb1dec", + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", "type": "github" }, "original": { - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", + "owner": "numtide", + "repo": "flake-utils", "type": "github" } }, + "nixpkgs": { + "locked": { + "lastModified": 1661353537, + "narHash": "sha256-1E2IGPajOsrkR49mM5h55OtYnU0dGyre6gl60NXKITE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0e304ff0d9db453a4b230e9386418fd974d5804a", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, "nmdSrc": { "flake": false, "locked": { - "lastModified": 1637239786, - "narHash": "sha256-l2KsnY537mz0blZdqALZKrWXn9PD39CpvotgPnxyIP4=", + "lastModified": 1654807200, + "narHash": "sha256-RNLq09vfj21TyYuUCeD6BNTNC6Ew8bLhQULZytN4Xx8=", "owner": "rycee", "repo": "nmd", - "rev": "527245ff605bde88c2dd2ddae21c6479bb7cf8aa", + "rev": "91dee681dd1c478d6040a00835d73c0f4a4c5c29", "type": "gitlab" }, "original": { @@ -34,6 +47,7 @@ }, "root": { "inputs": { + "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "nmdSrc": "nmdSrc" } diff --git a/flake.nix b/flake.nix index 7cadbc9c..c95032cd 100644 --- a/flake.nix +++ b/flake.nix @@ -1,102 +1,83 @@ { description = "A neovim configuration system for NixOS"; - inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.nmdSrc.url = "gitlab:rycee/nmd"; inputs.nmdSrc.flake = false; - outputs = { self, nixpkgs, nmdSrc, ... }@inputs: rec { - packages."x86_64-linux".docs = import ./docs { - pkgs = import nixpkgs { system = "x86_64-linux"; }; - lib = nixpkgs.lib; - }; + # TODO: Use flake-utils to support all architectures + outputs = { self, nixpkgs, nmdSrc, flake-utils, ... }@inputs: + with nixpkgs.lib; + with builtins; + let + # TODO: Support nesting + nixvimModules = map (f: ./modules + "/${f}") (attrNames (builtins.readDir ./modules)); - nixosModules.nixvim = import ./nixvim.nix { nixos = true; }; - homeManagerModules.nixvim = import ./nixvim.nix { homeManager = true; }; - - # This is a simple container for testing - nixosConfigurations.container = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - modules = [ - ({ pkgs, ... }: { - boot.isContainer = true; - system.configurationRevision = nixpkgs.lib.mkIf (self ? rev) self.rev; - - users.users.test = { - isNormalUser = true; - password = ""; - }; - - imports = [ nixosModules.nixvim ]; - - programs.nixvim = { - enable = true; - package = pkgs.neovim; - colorschemes.tokyonight = { enable = true; }; - - extraPlugins = [ pkgs.vimPlugins.vim-nix ]; - - options = { - number = true; - mouse = "a"; - tabstop = 2; - shiftwidth = 2; - expandtab = true; - smarttab = true; - autoindent = true; - cindent = true; - linebreak = true; - hidden = true; + modules = pkgs: nixvimModules ++ [ + (rec { + _file = ./flake.nix; + key = _file; + config = { + _module.args = { + pkgs = mkForce pkgs; + lib = pkgs.lib; + helpers = import ./plugins/helpers.nix { lib = pkgs.lib; }; }; - - maps.normalVisualOp."รง" = ":"; - maps.normal."m" = { - silent = true; - action = "make"; - }; - - plugins.lualine = { - enable = true; - }; - - plugins.undotree.enable = true; - plugins.gitgutter.enable = true; - plugins.fugitive.enable = true; - plugins.commentary.enable = true; - plugins.startify = { - enable = true; - useUnicode = true; - }; - plugins.goyo = { - enable = true; - showLineNumbers = true; - }; - - plugins.lsp = { - enable = true; - servers.clangd.enable = true; - }; - - plugins.telescope = { - enable = true; - extensions = { frecency.enable = true; }; - }; - - plugins.nvim-autopairs = { enable = true; }; - - globals = { - vimsyn_embed = "l"; - mapleader = " "; - }; - - plugins.lspsaga.enable = true; - - plugins.treesitter.enable = true; - plugins.ledger.enable = true; }; }) + + ./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 ++ [ configuration ]; + }; + in + eval.config.output; + + flakeOutput = + flake-utils.lib.eachDefaultSystem + (system: rec { + packages.docs = import ./docs { + pkgs = import nixpkgs { inherit system; }; + lib = nixpkgs.lib; + nixvimModules = nixvimModules; + 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 + ]; + }; + }; + }); + in + flakeOutput // { + inherit build; + # TODO: Stuff for home-manager and nixos modules backwards compat, keeping the architecture as x86_64 if none is specified... + homeManagerModules.nixvim = flakeOutput.homeManagerModules.x86_64-linux.nixvim; + nixosModules.nixvim = flakeOutput.nixosModules.x86_64-linux.nixvim; }; - }; } diff --git a/modules/colorscheme.nix b/modules/colorscheme.nix new file mode 100644 index 00000000..8036d2ab --- /dev/null +++ b/modules/colorscheme.nix @@ -0,0 +1,17 @@ +{ config, lib, ... }: +with lib; +{ + options = { + colorscheme = mkOption { + type = types.nullOr types.str; + description = "The name of the colorscheme to use"; + default = null; + }; + }; + + config = { + extraConfigVim = optionalString (config.colorscheme != "" && config.colorscheme != null) '' + colorscheme ${config.colorscheme} + ''; + }; +} diff --git a/modules/keymaps.nix b/modules/keymaps.nix new file mode 100644 index 00000000..b6426a9b --- /dev/null +++ b/modules/keymaps.nix @@ -0,0 +1,131 @@ +{ config, lib, helpers, ... }: +with lib; +let + mapOption = types.oneOf [ + types.str + (types.submodule { + options = { + silent = mkOption { + type = types.bool; + description = "Whether this mapping should be silent. Equivalent to adding to a map."; + default = false; + }; + + nowait = mkOption { + type = types.bool; + description = "Whether to wait for extra input on ambiguous mappings. Equivalent to adding to a map."; + default = false; + }; + + script = mkOption { + type = types.bool; + description = "Equivalent to adding