Home Explore Blog CI



nushell

1st chunk of `cookbook/direnv.md`
2c6f5578a13618628eb09f0afc21577b465528d9b6a88bcd00000001000003f4
---
title: Direnv
---

# Direnv

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

---

### Configuring direnv

To make direnv work with nushell the way it does with other shells, we can use the "hooks" functionality:

```nu
$env.config = {
  hooks: {
    pre_prompt: [{ ||
      if (which direnv | is-empty) {
        return
      }

      direnv export json | from json | default {} | load-env
      if 'ENV_CONVERSIONS' in $env and 'PATH' in $env.ENV_CONVERSIONS {
        $env.PATH = do $env.ENV_CONVERSIONS.PATH.from_string $env.PATH
      }
    }]
  }
}
```

::: tip Note
you can follow the [`nu_scripts` of Nushell](https://github.com/nushell/nu_scripts/blob/main/nu-hooks/nu-hooks/direnv/config.nu)
for the always up-to-date version of the hook above
:::

With that configuration in place, direnv should now work with nushell.

Title: Configuring Direnv for Nushell
Summary
This section describes how to configure Direnv to work with Nushell. Direnv is a tool that loads and unloads environment variables based on the current directory. The configuration involves using Nushell's 'hooks' functionality to execute `direnv export json` and load the resulting environment variables. A code snippet is provided to add a pre_prompt hook that integrates Direnv with Nushell. Note that Nushell version 0.66 or later is required.