Skip to content

neovim: expose sideloadInitLua option to control the generation of init.lua#9028

Open
veselyn wants to merge 15 commits intonix-community:masterfrom
veselyn:move-luaRcContent-to-wrapper-as-args
Open

neovim: expose sideloadInitLua option to control the generation of init.lua#9028
veselyn wants to merge 15 commits intonix-community:masterfrom
veselyn:move-luaRcContent-to-wrapper-as-args

Conversation

@veselyn
Copy link
Copy Markdown

@veselyn veselyn commented Apr 3, 2026

Description

This pull request tries to resolve #8938.

Checklist

  • Change is backwards compatible.

  • Code formatted with nix fmt or
    nix-shell -p treefmt nixfmt deadnix keep-sorted nixf-diagnose --run treefmt.

  • Code tested through nix run .#tests -- test-all or
    nix-shell --pure tests -A run.all.

  • Test cases updated/added. See example.

  • Commit messages are formatted like

    {component}: {description}
    
    {long description}
    

    See CONTRIBUTING for more information and recent commit messages for examples.

  • If this PR adds a new module

    • Added myself as module maintainer. See example.
    • Generate a news entry. See News
    • Basic tests added. See Tests
  • If this PR adds an exciting new feature or contains a breaking change.

    • Generate a news entry. See News

@teto
Copy link
Copy Markdown
Collaborator

teto commented Apr 3, 2026

thanks for trying to tackle this. This wont work depending on the plugins you install that might pull some premade config.

What I think would be clearer for everyone, users and maintainers is an option (not sure how to name it: sideload, wrapConfig I welcome suggestions ) to make explicit the choice of not writing to init.lua but rather to lua/hm-generated.nix. Having a stable path make it easier to debug.

There is another issue I welcome input as well: how to load the generated config ?
We could let the user write the loading from its init.vim but it would be best to default to something working out of the box.

The following enum would cover all bases IMO:

  • preload : require hm-generated with -c (this would be the default)
  • postload: require hm-generated with --cmd
  • user: let the user require it from his imperatie init.lua

Now the annoying bit would be to test all those.

what do you think ?

@veselyn
Copy link
Copy Markdown
Author

veselyn commented Apr 3, 2026

What I think would be clearer for everyone, users and maintainers is an option (not sure how to name it: sideload, wrapConfig I welcome suggestions ) to make explicit the choice of not writing to init.lua but rather to lua/hm-generated.nix. Having a stable path make it easier to debug.

Are you thinking of writing it at /nix/store/XXX/lua/hm-generated.nix and including it in the runtime path or symlinking $HOME/.config/nvim/lua/hm-generated.nix? Because the latter will cause trouble for people that manage their configuration outside of Nix by symlinking $HOME/.config/nvim.

There is another issue I welcome input as well: how to load the generated config ? We could let the user write the loading from its init.vim but it would be best to default to something working out of the box.

The following enum would cover all bases IMO:

  • preload : require hm-generated with -c (this would be the default)
  • postload: require hm-generated with --cmd
  • user: let the user require it from his imperatie init.lua

I don't have all the context to provide an opinion. Where does the generated config come from? Does it come purely from the user by, for example, overriding plugin derivations to include custom config?

Another possibility is to use the plugin/ and after/plugin/ runtime directories and essentially package the generated config as a plugin. It might be a better solution than -c considering that -c reads the first file before executing the command.

@teto
Copy link
Copy Markdown
Collaborator

teto commented Apr 5, 2026

including it in the runtime path or symlinking $HOME/.config/nvim/lua/hm-generated.nix

that's what I had in mind. Until #8933 gets merged, it might trigger issues so we can reference the /nix store directly for now.

I don't have all the context to provide an opinion.

In a nutshell, having an heuristic for wrapping neovim or not confuses users (and maintainers) as users do a change, and all of a sudden neovim is not wrapped anymore and an init.lua tries to be created. And wrapping is nasty because then you have to wrap all your neovim variants (GUIs/wrappers etc).

Another possibility is to use the plugin/ and after/plugin/ runtime directories and essentially package the generated config as a plugin

That's an interesting idea worth exploring. I fear there might be loading order issues etc. In the short term to fix your usecase as well as the linked issue, we can do the simple immediate fix of explicit option to toggle wrapping instead of writing init.lua

If you want to explore loading the config as a plugin in another PR later, that could be interesting as well.

@veselyn veselyn changed the title neovim: remove wrapper's luaRcContent from init.lua and load via args neovim: expose wrapGeneratedConfigs option to control how generated configs are loaded Apr 6, 2026
@veselyn veselyn changed the title neovim: expose wrapGeneratedConfigs option to control how generated configs are loaded neovim: expose option to control how generated configs are loaded Apr 6, 2026
@veselyn
Copy link
Copy Markdown
Author

veselyn commented Apr 6, 2026

@teto, I made changes following your suggestion. Could you take a quick look before I write a description for the option and test cases?

@veselyn veselyn marked this pull request as ready for review April 6, 2026 15:42
@home-manager-ci home-manager-ci bot requested a review from khaneliman April 6, 2026 15:43
'';
};

wrapGeneratedConfigs = mkOption {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to avoid engraving "wrap" in the option name if it turns out later we can create a "fake" hm plugin to load the config. I kinda like sideload or sideloadInit.
I know I mentioned "preload", "postload" etc but I would prefer if we kept things simple as starter. We can change it if need arises.
The goal of this PR is already doable without upstream changes, we just need a setting (maybe boolean for now ? we can migrate to an enum when needed) to make it easier for everyone but we dont need to support every scenario.

For instance a user could already set extraMakeWrapperArgs = [ "--cmd 'lua require(${writeText "init.lua" config.programs.neovim.initLua} ] .

++ [
{
"nvim/init.lua" = mkIf (cfg.initLua != "") {
text = cfg.initLua;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add enable = !cfg.wrapGeneratedConfigs or in your case (builtins.elem cfg.wrapGeneratedConfigs [ false "user" ] . This way the user can still see the generated config and use it somewhere else but the actual init.lua wont be created.

''--cmd 'lua require("hm-generated")' ''
]
++ lib.optionals (cfg.wrapGeneratedConfigs == "postload") [
"--add-flags"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep only this one and reference lua require ${writeText config.programs.initLua}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to load it before the rest of the files from the runtime path with --cmd instead of -c? That way the load order is closer to what it would be with a regular init.lua file.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sry

@teto
Copy link
Copy Markdown
Collaborator

teto commented Apr 6, 2026

ty for taking care of this. I've added some comments. Basically:

@veselyn
Copy link
Copy Markdown
Author

veselyn commented Apr 7, 2026

The tests are still failing and require some more work. I'll pick it up again in the following days.

@teto
Copy link
Copy Markdown
Collaborator

teto commented Apr 7, 2026

looks good. As for the tests nix run .#tests seemed kinda boggy, you can run make test TEST=<NEOVIM_TEST> instead (eg TEST=neovim-plugin-config).

@veselyn veselyn changed the title neovim: expose option to control how generated configs are loaded neovim: expose sideloadInitLua option to control the generation of init.lua Apr 8, 2026
@veselyn
Copy link
Copy Markdown
Author

veselyn commented Apr 8, 2026

I added an extra test case for the sideloading, configured neovim-wrapper-args to load the init.lua file before asserting, and patched the neovim-extra-lua-* cases to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Existing file '/home/foobar/.config/nvim/init.lua' would be clobbered

2 participants