p3t33 / nixos_flake

5 stars 0 forks source link

Help with tmux-resurrect config #1

Closed Tanish2002 closed 9 months ago

Tanish2002 commented 9 months ago

Hey! I'm very new to tmux, and have been trying to set it up for a consistent development environment.

I'm currently stuck configuring tmux-resurrect and tmux-continuum. I found your config by chance but am still kinda confused as to how all this works.

I simply want my sessions to persist over reboots and if I attach to the previous session then all panes including ones running neovim should open up.

Can you please help me by explaining what you're doing? Also, I don't have any sort of session manager setup in neovim as well, is that something I need to do?

p3t33 commented 9 months ago

Hey, First thing first my config is "wrapped" inside nix syntax. So first let me know how are you trying to setup your tmux. is it plain tmux.conf you edit or is it nix generated one?

Tanish2002 commented 9 months ago

Yeah it's nix!

I was able to setup it up, and I think more or less understand what you've done in your config.

Tho I don't understand how tmux-continuum works?

You haven't enabled the boot service... Also, How do you save your nvim sessions? When I initially saw tmux-resurrect and tmux-continuum, I thougt they would auto save my session as well. But that doesn't seem to be the case. I have to manually save the session inside neovim

p3t33 commented 9 months ago

There a lot of moving parts and it is important to understand how they interact with each other.

tmux server-kill; tmux

The plugin is completely manual which means it is up to you to save a session(Prefix C-s) and it is up to you to do the resurrect(Prefix C-r). *I am not sure those are the default key bindings.

Without this fix resurrect will be able to restore all your sessions(panes, tabs....) but you will get empty panes where there should be vim. man, htop....

-  **vim sessions and resurrect**
vim is responsible for its own session management this is done using ;mksession which then creates a
Session file which is completely independent from tmux. resurrect can make sure that once vim 
is resurrected as part of tmux session it will be resurrected using the Session file it has and in this way
you are not only getting vim but you are getting it in the exact  state that is dictated by its Session file.
*There is a way to automate the session management of vim using a plugin such as vim-startify.

- **continuum** is completely depended on resurrect plugin as it is only responsible to automate
the things resurrect is doing manually.  Which means it responsible to save your current tmux sessions(only if you have a terminal where tmux is opened) based on a preset interval and
then when tmux server is restarted it is up to continuum to look at the most recent "state capture" that was done by resurrect and use it to automate the resurrect action at which point continuum will use resurrect to create new state captures...

One very annoying limitation I came across with continuum is it failing to do its function until  at lest one manual state capture has been done by the user. As I already mentioned continuum is completely depended on resurrect  so it needs it to create the first state capture, so it can  automate the rest of the process. 

This is why I have this line in my config

run-shell "if [ ! -d ~/.config/tmux/resurrect ]; then tmux new-session -d -s init-resurrect; ${pkgs.tmuxPlugins.resurrect}/share/tmux-plugins/resurrect/scripts/save.sh; fi"


Now as you pointed out "You haven't enabled the boot service..."  which, for  non nix setup is done using @continuum-boot  directive. 

To quote my own config

@continuum-boot - when set will generate a user level systemd unit file

which it will save to ${HOME}/.config/systemd/user/tmux.service and enable

it.


As we are using nix I preferred  to do it in a more declarative way and define my own 
systemd [serivce](https://github.com/p3t33/nixos_flake/blob/master/modules/nixos/services/user/tmux.nix)

Note t didn't simple copied the systemd that would have been created by @continuum-boot but instead adjusted it for nix, for a service that needs access to its environment based on the code I say for emacs daemon.   
Tanish2002 commented 9 months ago

Thanks a lot for the detailed explanation!

My main misunderstanding was that tmux-ressurrect was gonna spin up my previous nvim session as well since I use set -g @resurrect-strategy-nvim 'session' in my config.

Now I understand the resurrect will just open nvim, I need to create the session file myself.

Also the declarative systemd service makes sense.

Haven't looked a lot at vim-startify but I ended up using, auto-sessions. It seems to work fine as long as I save the file once. So usually just before detaching I do a :wa and it just works.

as for the resurrect hook I ended up using something like this since my nvim binary path isn't registed as /etc/profiles/.. but instead the /nix/store/.. path

set -g @resurrect-hook-post-save-all 'target=$(readlink -f ${resurrectDirPath}/last); sed "s| --cmd .*-vim-pack-dir||g; s|/nix/store/[^ ]*/bin/||g" $target | ${pkgs.moreutils}/bin/sponge $target'

Thanks for detailed explanation again! Closing the issue 😄

p3t33 commented 4 months ago

I had to update the sed command that cleans up the file that is used by the resurrect plugin: set -g @resurrect-hook-post-save-all 'sed -i -E "s|(pane.*nvim\s*:)[^;]+;.*\s([^ ]+)$|\1nvim \2|" ${resurrectDirPath}/last'