I’ve recently moved to NixOS, as part of my workflow I use both tmux and neovim.
On Arch I was able to restore my neovim sessions automatically using tmux resurrect (and continuum) plugins. However I cannot seem to get it to work correctly on NixOS, specifically to restore my nvim session.
Is it trying to run /home/haseeb/.nix-profile/bin/nvim --cmd lua vim.g.loaded_node_provider=0;vim.g.loaded_perl_provider=0;vim.g.loaded_python_provider=0;vim.g.python3_host_prog='/nix/store/4n4d0f1xd14gl4pfymdcqb9pmagcyyfj-neovim-0.9.1/bin/nvim-python3';vim.g.ruby_host_prog='/nix/store/4n4d0f1xd14gl4pfymdcqb9pmagcyyfj-neovim-0.9.1/bin/nvim-ruby' ?
Has anyone got tmux resurrect to work with nvim on NixOS?
I came across the same issue and the problem is with the way that tmux-resurrect saves the names of the process it later needs to resurrect, This issue is not specific to NixOS and is caused once the name of the process deviates from the one tmux-resurrect plugin expects when it parses its state file(file named last that points at some tmux_resurrect_*).
The workaround is to leverage a tmux-resurrect hook that will be triggered each time tmux state is saved. I ended adding the
following 4 lines to the configurations of the tmux-resurrect plugin
as will be in the generated .tmux.conf
resurrect_dir=“$HOME/.tmux/resurrect”
set -g @resurrect-dir $resurrect_dir
set -g @resurrect-hook-post-save-all ‘target=$(readlink -f $resurrect_dir/last); sed “s| --cmd .*-vim-pack-dir||g; s|/etc/profiles/per-user/$USER/bin/||g” $target | sponge $target’
To add to this, I needed remove the bash variables from the strings in my tmux.nix to get it to work because tmux sanatizes the eval call to resurrect-hook-post-save-all.
So I was able to get this to work:
let
resurrectDirPath = "<example>";
usr="user";
in
...
{
plugin = resurrect;
extraConfig = ''
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-dir ${resurrectDirPath}
set -g @resurrect-hook-post-save-all 'sed "s| --cmd .*-vim-pack-dir||g; s|/etc/profiles/per-user/${usr}/bin/||g; s|/home/${usr}/.nix-profile/bin/||g" ${resurrectDirPath}/last | sponge ${resurrectDirPath}/last'
}
...
*the ellipse denotes necessary code is not present in the example above
So code stopped working (neovim restore stopped working) for some reason and I noticed $target file being created inside of the directory I was in at the moment of tmux session save.
here is a more simple and working version
set -g @resurrect-dir ${resurrectDirPath}
set -g @resurrect-hook-post-save-all ‘sed -i -E “s|(pane.nvim\s:)[^;]+;.*\s([^ ]+)$|\1nvim \2|” ${resurrectDirPath}/last’
with resurrectDirPath variable being the pathto my resurrect directory.
source can be found here.
For anyone else for who all of the above did not work, this did work for me:
extraConfig = ''
<your-other-settings>
set -g @resurrect-save-command-strategy 'ps'
set -g @resurrect-hook-post-save-all 'sed -E "s|:/nix/store/[^ ]+/bin/nvim.*nvim-ruby'\'''([[:space:]]+([^[:space:]]+))?|:nvim \\2|" -i <full-path-to-resurrect-dir>/last'
set -g @resurrect-processes '"~nvim"'
'';
I am not entirely certain why this works though. I had to do some funky stuff with the quotes because my tmux-resurrect files looked like this for neovim commands:
So to replace that last part with nvim . I had to include the single quote from nvim-ruby' near the end. I kept getting invalid octal escape errors when trying to source the tmux.conf file. Eventually I fixed it using magical combinations of quotes that eventually worked. This did made it so that any variables were not being included within the full string so the full path had to be used for the resurrect dir.