inputs@{ machine, ... }:
{
  imports = machine.program {
    name = "nvim";
    inherit inputs;
    requirements = [ "cli" ];
    home-config =
      {
        pkgs,
        flakes,
        lib,
        ...
      }:
      let
        nvim_mime_types = [
          "application/x-zerosize"
          "text/english"
          "text/plain"
          "text/x-makefile"
          "text/x-c++hdr"
          "text/x-c++src"
          "text/x-chdr"
          "text/x-csrc"
          "text/x-java"
          "text/x-moc"
          "text/x-pascal"
          "text/x-tcl"
          "text/x-tex"
          "application/x-shellscript"
          "text/x-c"
          "text/x-c++"
        ];
        desktop-entry-name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanvim";
        desktop-entry = pkgs.makeDesktopItem {
          name = desktop-entry-name;
          desktopName = "neovim";
          exec = "${./editor-hax.py} %F";
          tryExec = "${./editor-hax.py}";
          terminal = false;
          type = "Application";
          categories = [
            "Utility"
            "TextEditor"
          ];
          icon = "terminal";
          mimeTypes = nvim_mime_types;
        };
      in
      {
        home = {
          sessionVariables = {
            EDITOR = "nvim";
          };
        };

        home.file.".local/share/applications/${desktop-entry-name}.desktop" = {
          source = "${desktop-entry}/share/applications/${desktop-entry-name}.desktop";
        };

        xdg.mimeApps.associations.added = lib.mergeAttrsList (
          map (mime: {
            ${mime} = [ "${desktop-entry-name}.desktop" ];
          }) nvim_mime_types
        );

        imports = [
          flakes.nixvim.homeModules.nixvim
          ./options.nix
          ./plugins.nix
          ./keys.nix
        ];

        programs.nixvim = {
          enable = true;
          globals.mapleader = " ";
          globals.maplocalleader = " ";

          viAlias = true;
          vimAlias = true;

          # Highlight and remove extra white spaces
          # same color as cursorline as per
          # https://github.com/joshdick/onedark.vim/blob/390b893d361c356ac1b00778d849815f2aa44ae4/autoload/onedark.vim
          highlight.ExtraWhitespace.bg = "#2C323C";
          match.ExtraWhitespace = "\\s\\+$";

          clipboard.providers.wl-copy.enable = true;

          performance = {
            byteCompileLua.enable = true;
            combinePlugins = {
              enable = true;

              standalonePlugins = [
                # clashes with lualine
                "onedark.nvim"
              ];
            };
          };

          extraLuaPackages = ps: [ ps.magick ];
          extraPackages = [ pkgs.imagemagick ];

          # package = (import inputs.unstable { inherit (pkgs) system; }).neovim-unwrapped;
          package = pkgs.neovim-unwrapped;

          colorschemes.onedark = {
            enable = true;
            settings = {
              style = "deep";

              highlights = {
                # bright green doccomments
                "@lsp.type.comment".fg = "#77B767";
                "@comment.documentation.rust".fg = "#77B767";
                "@comment.documentation".fg = "#77B767";
                "@comment".fg = "#426639";
                # "Visual".bg = "#2a2e36";
                # "Cursorline".bg = "#2a2e36";
              };

            };
          };

          extraConfigLuaPre = ''
            require("neoconf").setup({})
          '';
          extraConfigLua = ''
            require("render-markdown").setup {
              latex_converter = '${pkgs.python312Packages.pylatexenc}/bin/latex2text',
            }
          ''

          #   local lspconfig = require 'lspconfig'
          #   local configs = require 'lspconfig.configs'
          #   if not configs.foo_lsp then
          #     configs.noteslsp = {
          #       default_config = {
          #         -- cmd = {'${pkgs.custom.noteslsp}/bin/noteslsp'},
          #         cmd = {'./noteslsp/target/debug/noteslsp'},
          #         filetypes = {'markdown'},
          #         root_dir = function(fname)
          #           return lspconfig.util.find_git_ancestor(fname)
          #         end,
          #         settings = {}
          #         ,
          #       },
          #     }
          #   end
          #
          #   lspconfig.noteslsp.setup{}
          # ''
          + (builtins.readFile ./config.lua);
          extraConfigLuaPost = ''

          '';
        };
      };
  };
}
