Skip to content

Commit 91fa8e3

Browse files
authored
Add cookbook virtual env overlay docs (#1894)
1 parent 49d26d0 commit 91fa8e3

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

cookbook/modules.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,69 @@ For example, if you defined our known externals in our `git.nu` as `export exter
6060
You would need to call `use completions git *` to get the desired subcommands. For this reason, using `main` as outlined in the step above is the preferred way to define subcommands.
6161
:::
6262

63-
## Setting environment + aliases (conda style)
63+
## Overlay and "Virtual Environments"
6464

65-
`def --env` commands, `export-env` block and aliases can be used to dynamically manipulate "virtual environments" (a concept well known from Python).
65+
[Overlays](/book/overlays.md) are layers of definitions. We can make use of them to establish a temporary virtual environment, with custom environment variables, which we discard at the end.
6666

67-
We use it in our [official virtualenv integration](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu). Another example is our [unofficial Conda module](https://github.com/nushell/nu_scripts/blob/main/modules/virtual_environments/conda.nu).
67+
Our goals in this example are:
6868

69-
::: warning
70-
Work in progress
71-
:::
69+
* Activate a set of environment variables from a file called `env.json`
70+
* Work in this context
71+
* Discard the environment - restoring the original environment
72+
73+
First, let's prepare an `env.json` for testing:
74+
75+
```nu
76+
{ A: 1 B: 2 } | save env.json
77+
78+
$env.A = 'zzz'
79+
80+
print $"('A' in $env) ('B' in $env)"
81+
# => true false
82+
```
83+
84+
Now let's create a module `env` with a `load` command that loads the environment from `env.json`, and use it as an overlay:
85+
86+
```nu
87+
'export def --env load [] { open env.json | load-env }' | save env.nu
88+
89+
overlay use ./env.nu
90+
91+
overlay list
92+
# => ╭───┬──────╮
93+
# => │ 0 │ zero │
94+
# => │ 1 │ env │
95+
# => ╰───┴──────╯
96+
```
97+
98+
Now we load the `env.json` file:
99+
100+
```nu
101+
load
102+
103+
print $"($env.A) ($env.B)"
104+
# => 1 2
105+
```
106+
107+
To hide the overlay:
108+
109+
```nu
110+
overlay hide env
111+
112+
print $"('A' in $env) ('B' in $env)"
113+
# => true false
114+
```
115+
116+
Note that - as documented in [Overlays](/book/overlays.md) - reactivating the overlay will recover the loaded environment variables,
117+
not create a new context for as long as the Nushell session remains active, despite `overlay list` no longer listing the overlay.
118+
119+
More related information and specifically about environment variables and their modification can be found in [Environment](/book/environment.md), [Modules](/book/modules.md), [Overlay](/book/overlays.md),
120+
and the respective command documentation of [`def --env`](/commands/docs/def.md), [`export def --env`](/commands/docs/export_def.md), [`load-env`](/commands/docs/load-env.md), and [`export-env`](/commands/docs/export-env.md).
121+
122+
### Elaborate Virtual Environments
123+
124+
This kind of overlaying environments can be used to scope more elaborate virtual environments, including changing the `PATH` environment variable, or other tool settings defined in environment variables or files.
125+
126+
Tools like conda or Python virtualenv manage and isolate sets of environment variables.
127+
The [official virtualenv integration](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) makes use of these concepts.
128+
And our nu_scripts repository has a an [unofficial Conda module](https://github.com/nushell/nu_scripts/tree/main/modules/virtual_environments).

0 commit comments

Comments
 (0)