From 9621e9ab80a038cd11c7cfcae4df46a59d62b16a Mon Sep 17 00:00:00 2001
From: Guillaume Desforges <guillaume.desforges.pro@gmail.com>
Date: Sun, 5 Feb 2023 12:09:26 +0100
Subject: [PATCH] programs.neovim: add extraLuaConfig (#3639)

* programs.neovim: add extraLuaConfig

Add a configuration option to add custom lua configuration lines to
`lua.init`.

* apply review: formatting

* apply review: fix test
---
 modules/programs/neovim.nix                   | 14 ++++++++++-
 tests/modules/programs/neovim/default.nix     |  1 +
 .../programs/neovim/extra-lua-init.nix        | 23 +++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 tests/modules/programs/neovim/extra-lua-init.nix

diff --git a/modules/programs/neovim.nix b/modules/programs/neovim.nix
index c8edd2257..df4b487d6 100644
--- a/modules/programs/neovim.nix
+++ b/modules/programs/neovim.nix
@@ -261,6 +261,17 @@ in {
         '';
       };
 
+      extraLuaConfig = mkOption {
+        type = types.lines;
+        default = "";
+        example = ''
+          vim.opt.nobackup = true
+        '';
+        description = ''
+          Custom lua lines.
+        '';
+      };
+
       extraPackages = mkOption {
         type = with types; listOf package;
         default = [ ];
@@ -394,7 +405,8 @@ in {
               "vim.cmd [[source ${
                 pkgs.writeText "nvim-init-home-manager.vim"
                 neovimConfig.neovimRcContent
-              }]]" + lib.optionalString hasLuaConfig
+              }]]" + config.programs.neovim.extraLuaConfig
+              + lib.optionalString hasLuaConfig
               config.programs.neovim.generatedConfigs.lua;
           in mkIf (luaRcContent != "") { text = luaRcContent; };
 
diff --git a/tests/modules/programs/neovim/default.nix b/tests/modules/programs/neovim/default.nix
index 3950dc8b8..d3224666b 100644
--- a/tests/modules/programs/neovim/default.nix
+++ b/tests/modules/programs/neovim/default.nix
@@ -5,4 +5,5 @@
 
   # waiting for a nixpkgs patch
   neovim-no-init = ./no-init.nix;
+  neovim-extra-lua-init = ./extra-lua-init.nix;
 }
diff --git a/tests/modules/programs/neovim/extra-lua-init.nix b/tests/modules/programs/neovim/extra-lua-init.nix
new file mode 100644
index 000000000..2abaa6e9e
--- /dev/null
+++ b/tests/modules/programs/neovim/extra-lua-init.nix
@@ -0,0 +1,23 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+{
+  config = {
+    programs.neovim = {
+      enable = true;
+
+      extraLuaConfig = ''
+        -- extraLuaConfig
+      '';
+    };
+    nmt.script = ''
+      nvimFolder="home-files/.config/nvim"
+      assertFileContent "$nvimFolder/init.lua" ${
+        pkgs.writeText "init.lua-expected" ''
+          -- extraLuaConfig
+        ''
+      }
+    '';
+  };
+}