Skip to content

Commit b35ce57

Browse files
authored
Update Cookbook direnv instructions for new std helper (#1878)
* Update direnv for new std helper * Remove reference to nu_scripts; more tweaks * More cleanup
1 parent f7112cf commit b35ce57

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

cookbook/direnv.md

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,46 @@ title: Direnv
44

55
# Direnv
66

7-
Many people use [direnv](https://direnv.net) to load an environment upon entering a directory as well as unloading it when exiting the directory.
8-
Configuring direnv to work with nushell requires nushell version 0.66 or later.
7+
Many people use [direnv](https://direnv.net) to load an environment when entering a directory and unload it when exiting the directory.
98

10-
---
9+
## How direnv works
10+
11+
From [direnv.net](https://direnv.net):
12+
13+
> Before each prompt, direnv checks for the existence of a `.envrc` file (and optionally a `.env` file) in the current and parent directories. If the file exists (and is authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to the current shell.
14+
15+
## Configuring direnv in Nushell
16+
17+
In Nushell, it's possible to run direnv each time the prompt displays (using a [`pre_prompt` hook](/book/hooks.html)). However, it's more efficient to update only when the directory is changed using an `env_change` hook along with:
18+
19+
- [`from json`](/commands/docs/from_json.md) to convert the direnv output to structured data
20+
- [`load-env`](/commands/docs/load-env.md)
21+
- An `env-conversion` helper for the `PATH` from the [Standard Library](/book/standard_library.md)
1122

12-
### Configuring direnv
23+
::: note
1324

14-
To make direnv work with nushell the way it does with other shells, we can use the "hooks" functionality:
25+
The direnv configuration below requires Nushell 0.104 or later.
26+
27+
:::
1528

1629
```nu
17-
$env.config = {
18-
hooks: {
19-
pre_prompt: [{ ||
20-
if (which direnv | is-empty) {
21-
return
22-
}
23-
24-
direnv export json | from json | default {} | load-env
25-
if 'ENV_CONVERSIONS' in $env and 'PATH' in $env.ENV_CONVERSIONS {
26-
$env.PATH = do $env.ENV_CONVERSIONS.PATH.from_string $env.PATH
27-
}
28-
}]
30+
use std/config *
31+
32+
# Initialize the PWD hook as an empty list if it doesn't exist
33+
$env.config.hooks.env_change.PWD = $env.config.hooks.env_change.PWD? | default []
34+
35+
$env.config.hooks.env_change.PWD ++= [{||
36+
if (which direnv | is-empty) {
37+
# If direnv isn't installed, do nothing
38+
return
2939
}
30-
}
40+
41+
direnv export json | from json | default {} | load-env
42+
# If direnv changes the PATH, it will become a string and we need to re-convert it to a list
43+
$env.PATH = do (env-conversions).path.from_string $env.PATH
44+
}]
3145
```
3246

33-
::: tip Note
34-
you can follow the [`nu_scripts` of Nushell](https://github.com/nushell/nu_scripts/blob/main/nu-hooks/nu-hooks/direnv/config.nu)
35-
for the always up-to-date version of the hook above
36-
:::
47+
As with other configuration changes, this can be made permanent by adding it to your startup [configuration](/book/configuration.md).
3748

38-
With that configuration in place, direnv should now work with nushell.
49+
With this in place, direnv will now add/remove environment variables when entering/leaving a directory with an `.envrc` or `.env` file.

0 commit comments

Comments
 (0)