Skip to content

Instantly share code, notes, and snippets.

@larsyencken
Last active June 16, 2023 09:36
Show Gist options
  • Save larsyencken/6961c509c7bbc738dbc792977f0e31a7 to your computer and use it in GitHub Desktop.
Save larsyencken/6961c509c7bbc738dbc792977f0e31a7 to your computer and use it in GitHub Desktop.
Nushell: detect Python and Node environments
# just a partial config, only look at env_change
{
hooks: {
env_change: {
PWD: [
{
# if you enter a python project
condition: {|before, after| ["pyproject.toml" "requirements.txt"] | any {|f| $f | path exists } }
# drop any prior virtualenv, then use a new one if it exists
code: "
if ('.venv/bin/python' | path exists) {
let-env PATH = ($env.PATH | split row (char esep) | filter {|p| not $p =~ '.venv' } | prepend $\"($env.PWD)/.venv/bin\")
} else {
let-env PATH = ($env.PATH | split row (char esep) | filter {|p| not $p =~ '.venv' })
}
"
},
{
# if you enter a node project with nvm
condition: {|before, after| ".nvmrc" | path exists }
# drop any prior nvm context, and use a node version from fnm if one exists
code: "(
if ($\"($env.HOME)/.fnm/node-versions/v(cat .nvmrc)/installation/bin\" | path exists) {
let-env PATH = ($env.PATH | split row (char esep) | filter {|p| not p =~ '.fnm'} | prepend $\"($env.HOME)/.fnm/node-versions/v(cat .nvmrc)/installation/bin\")
} else {
print $\"Node v(cat .nvmrc) is not installed\";
print $\"Please run: fnm install (cat .nvmrc)\"
let-env PATH = ($env.PATH | split row (char esep) | filter {|p| not p =~ '.fnm'})
}
)"
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment