Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zsh: refactor zsh configuration for better order control over .zshrc #6479

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Emin017
Copy link

@Emin017 Emin017 commented Feb 17, 2025

Description

  • Refactor zsh.nix to use mkMerge and mkOrder for better control over .zshrc content ordering
  • Users can add content anywhere by using lib.mkOrder, lib.mkBefore and lib.mkAfter custom configurations
  • Added new test files (zshrc-contents-insert.nix, zshrc-contents-insert-before.nix, zshrc-contents-insert-after.nix) to verify the behavior of initExtra with mkBefore, mkAfter, and default insertion.

Checklist

  • Change is backwards compatible.

  • Code formatted with ./format.

  • Code tested through nix-shell --pure tests -A run.all
    or nix build --reference-lock-file flake.lock ./tests#test-all using Flakes.

  • 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.

Maintainer CC

Add a new option called `initExtraLast` to the zsh module. This option
allows users to specify shell script code that will be executed as the
last step during interactive zsh shell initialization.

Signed-off-by: Qiming Chu <[email protected]>
@Emin017 Emin017 changed the title zsh: implement shellInitLast zsh: implement initExtraLast Feb 17, 2025
Copy link
Collaborator

@teto teto left a comment

Choose a reason for hiding this comment

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

this is getting out of hand. We would have
initExtraBeforeCompInit
initExtraLast
initExtra
. I suggest you split all the additions to zshrc with various mkOrder so one can insert his code wherever he wants

@Emin017 Emin017 marked this pull request as draft February 19, 2025 13:39
@Emin017 Emin017 changed the title zsh: implement initExtraLast zsh: add initContents option for custom .zshrc content insertion Feb 20, 2025
@Emin017 Emin017 marked this pull request as ready for review February 20, 2025 14:10
@Emin017 Emin017 marked this pull request as draft February 20, 2025 14:37
- Users can add content anywhere by using `lib.mkOrder`, `lib.mkBefore`
and `lib.mkAfter` custom configurations.
- Add test cases to verify the insertion of content before and after
existing configurations in `.zshrc`.

Signed-off-by: Qiming Chu <[email protected]>
@Emin017 Emin017 marked this pull request as ready for review February 21, 2025 00:14
@Emin017 Emin017 requested a review from teto February 21, 2025 00:14
@@ -616,158 +622,162 @@ in
++ optional cfg.enableCompletion pkgs.nix-zsh-completions
++ optional cfg.oh-my-zsh.enable cfg.oh-my-zsh.package;

home.file."${relToDotDir ".zshrc"}".text = concatStringsSep "\n" ([
programs.zsh.initContents = mkMerge [
Copy link
Collaborator

Choose a reason for hiding this comment

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

the introduction of initContents is something I would like to avoid.
Can we keep home.file."${relToDotDir ".zshrc"}".text or use initExtra instead ?

Copy link
Author

Choose a reason for hiding this comment

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

In the new commit, I've modified the implementation to use initExtra instead of introducing initContents as you requested.

My concern is:
To maintain compatibility, we should ensure that:

  • The final .zshrc content maintains the same order of components
  • Any user-provided initExtra content still appears in the same location (Just as they used before)

I'm having a bit of a headache assigning this content order (I want to the default insertion order of initExtra is on the same location like before), do we need to consider the compatibility of the initExtra option? Or is this fine for now?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The final .zshrc content maintains the same order of components

That would be ideal but it doesn't have to be 100% exact. The improvement you submitted solves several open issues so we can have small changes and the tradeoff would still be good.

Any user-provided initExtra content still appears in the same location (Just as they used before)

not sure what you mean. Seems like a good thing.

Or is this fine for now?

I think it is but I am not a specialist of the merging of elements in the module, I might have missed something.

@teto
Copy link
Collaborator

teto commented Mar 4, 2025

thanks for taking on this huge work. I am quite looking forward to it.

- Renamed `initContents` to `initExtra` for consistency and clarity
- Updated related test files to reflect the change
- Added a new test file `zshrc-contents-insert.nix` to ensure proper
functionality

Signed-off-by: Qiming Chu <[email protected]>
@Emin017 Emin017 force-pushed the zsh-shellInitLast branch from 776daea to f1fdc33 Compare March 5, 2025 14:58
@Emin017 Emin017 changed the title zsh: add initContents option for custom .zshrc content insertion zsh: refactor zsh configuration for better order control over .zshrc Mar 5, 2025
@teto
Copy link
Collaborator

teto commented Mar 5, 2025

on a quick glance it looks fine by me. I have follow up ideas but merging this first is better, it's the hardest part. diffing my ~/.zshrc with and without this just shows a few different newlines which is fine. I would like to let this cook a few days to give the opportunity for users with different zsh setups test this since it's a "dangerous" change :)

Emin017 added 2 commits March 6, 2025 08:18
Changed the initialization order of `zprof` from 500 to 400 in `zsh.nix`
to ensure it loads before other shell initialization steps, as it
benchmarks the shell initialization process.

Signed-off-by: Qiming Chu <[email protected]>
This commit introduces a new test file `zshrc-contents-init-zprof.nix`
to verify the initialization of `zprof` in the `.zshrc` file. The test
ensures that the `.zshrc` file exists and contains the expected
`zmodload zsh/zprof` command.

Signed-off-by: Qiming Chu <[email protected]>
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.

2 participants