Skip to content

Commit

Permalink
Fix module env usage tip (#1423)
Browse files Browse the repository at this point in the history
* Fix module env usage tip

Fixes #1024

Use a more concise example, similar to the previous one.

Add usage example with proof.

* Improve wording

The 'also' serves no purpose. It's unclear what [previous content] it refers to.
  • Loading branch information
Kissaki committed Jun 6, 2024
1 parent f47f63a commit 934d420
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions book/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ As you can see, defining the submodule structure also shapes the command line AP

## Environment Variables

Modules can also define an environment using [`export-env`](/commands/docs/export-env.md):
Modules can define an environment using [`export-env`](/commands/docs/export-env.md):

```nu
# greetings.nu
Expand All @@ -292,22 +292,28 @@ hello Arthur, King of the Britons!
```

::: tip
You can put a complex code defining your environment without polluting the namespace of the module, for example:
The module implementation can use its own scoped environment variables without them bleeding into users scope. For example:

```nu
def tmp [] { "tmp" }
def other [] { "other" }
# greetings-local.nu
let len = (tmp | str length)
load-env {
OTHER_ENV: (other)
TMP_LEN: $len
}
export def hello [] {
$env.MYNAMELOCAL = "Arthur, King of the Britons"
$"hello ($env.MYNAMELOCAL)"
}
```

Only `$env.TMP_LEN` and `$env.OTHER_ENV` are preserved after evaluating the `export-env` module.
```nu
> use greetings-local.nu
> $env.MYNAMELOCAL
Error: nu::shell::column_not_found
[…]
> greetings-local hello
hello Arthur, King of the Britons!
```

:::

## Caveats
Expand Down

0 comments on commit 934d420

Please sign in to comment.