Skip to content

Commit f2c4759

Browse files
committed
Add command :NixShell
Add a command to call 'nix-shell' and then update the editor's environment as if it was inside the new shell. It's sometimes useful to fire up a nix-shell just to download a package for using it once. It's annoying however when it's a library or any other dependency of ':make', which can't be called from inside the shell. Example: :NixShell -p hello :term hello There's no rollback command but it could be written if it's ever needed. Calling the commande several times will nest environments the same way as opening a shell inside a shell.
1 parent 7d23e97 commit f2c4759

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

plugin/nix.vim

+20
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,24 @@ function! nix#edit(attr)
2222
endif
2323
endfunction
2424

25+
" Takes a list of lines of the form 'key=val'.
26+
function! nix#set_env(lines)
27+
for l in a:lines
28+
let [ key; val ] = split(l, "=", 1)
29+
call setenv(key, join(val, "="))
30+
endfor
31+
endfunction
32+
33+
" See command :NixShell
34+
function! nix#add_pkg_to_env(args)
35+
echo "nix-shell " . a:args
36+
let l:env = systemlist("nix-shell --run env " . a:args)
37+
call nix#set_env(l:env)
38+
echo "Environment updated"
39+
endfunction
40+
2541
command! -bang -nargs=* NixEdit call nix#edit(<q-args>)
42+
43+
" Call 'nix-shell' with the given arguments and update the environment as if
44+
" we were inside the spawned shell.
45+
command! -nargs=* NixShell call nix#add_pkg_to_env(<q-args>)

0 commit comments

Comments
 (0)