{ config, lib, pkgs, inputs, ... }:

let
  inherit (lib) mkEnableOption mkIf readFile;

  cfg = config.my.programs.nvim;
in
{
  imports = lib.my.listModulesRec ./plugins;

  options.my.programs.nvim.enable = mkEnableOption "nvim";

  config = mkIf cfg.enable {
    environment.persistence."/persist".users.moritz.directories = [
      ".cache/nvim/luac"
      ".config/github-copilot"
      ".local/share/nvim"
      ".local/state/nvim"
    ];

    home-manager.users.moritz = {

      home.packages = with pkgs; [
        xdotool # for vimtex
      ];

      programs.nixvim = {
        enable = true;
        package = inputs.neovim-nightly-overlay.packages.${pkgs.system}.default;
        vimAlias = true;
        extraConfigLuaPre = readFile ./options.lua;
        luaLoader.enable = true;
        performance = {
          byteCompileLua = {
            enable = true;
            configs = true;
            initLua = true;
            nvimRuntime = true;
            plugins = true;
          };
          combinePlugins.enable = true;
          combinePlugins.standalonePlugins = [
            "plenary.nvim"
          ];
        };
      };
    };
  };
}
