File size: 5,973 Bytes
8d756d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# Overlays
Overlays act as "layers" of definitions (custom commands, aliases, environment variables) that can be activated and deactivated on demand.
They resemble virtual environments found in some languages, such as Python.
_Note: To understand overlays, make sure to check [Modules](modules.md) first as overlays build on top of modules._
## Basics
First, Nushell comes with one default overlay called `zero`.
You can inspect which overlays are active with the [`overlay list`](/commands/docs/overlay_list.md) command.
You should see the default overlay listed there.
To create a new overlay, you first need a module:
```nu
> module spam {
export def foo [] {
"foo"
}
export alias bar = "bar"
export-env {
load-env { BAZ: "baz" }
}
}
```
We'll use this module throughout the chapter, so whenever you see `overlay use spam`, assume `spam` is referring to this module.
::: tip
The module can be created by any of the three methods described in [Modules](modules.md):
- "inline" modules (used in this example)
- file
- directory
:::
To create the overlay, call [`overlay use`](/commands/docs/overlay_use.md):
```nu
> overlay use spam
> foo
foo
> bar
bar
> $env.BAZ
baz
> overlay list
ββββ¬ββββββ
0 β zero
1 β spam
ββββ΄ββββββ
```
It brought the module's definitions into the current scope and evaluated the [`export-env`](/commands/docs/export-env.md) block the same way as [`use`](/commands/docs/use.md) command would (see [Modules](modules.md#environment-variables) chapter).
::: tip
In the following sections, the `>` prompt will be preceded by the name of the last active overlay.
`(spam)> some-command` means the `spam` overlay is the last active overlay when the command was typed.
:::
## Removing an Overlay
If you don't need the overlay definitions anymore, call [`overlay hide`](/commands/docs/overlay_remove.md):
```nu
(spam)> overlay hide spam
(zero)> foo
Error: Can't run executable...
(zero)> overlay list
ββββ¬ββββββ
0 β zero
ββββ΄ββββββ
```
The overlays are also scoped.
Any added overlays are removed at the end of the scope:
```nu
(zero)> do { overlay use spam; foo } # overlay is active only inside the block
foo
(zero)> overlay list
ββββ¬ββββββ
0 β zero
ββββ΄ββββββ
```
The last way to remove an overlay is to call [`overlay hide`](/commands/docs/overlay_remove.md) without an argument which will remove the last active overlay.
## Overlays Are Recordable
Any new definition (command, alias, environment variable) is recorded into the last active overlay:
```nu
(zero)> overlay use spam
(spam)> def eggs [] { "eggs" }
```
Now, the `eggs` command belongs to the `spam` overlay.
If we remove the overlay, we can't call it anymore:
```nu
(spam)> overlay hide spam
(zero)> eggs
Error: Can't run executable...
```
But we can bring it back!
```nu
(zero)> overlay use spam
(spam)> eggs
eggs
```
Overlays remember what you add to them and store that information even if you remove them.
This can let you repeatedly swap between different contexts.
::: tip
Sometimes, after adding an overlay, you might not want custom definitions to be added into it.
The solution can be to create a new empty overlay that would be used just for recording the custom changes:
```nu
(zero)> overlay use spam
(spam)> module scratchpad { }
(spam)> overlay use scratchpad
(scratchpad)> def eggs [] { "eggs" }
```
The `eggs` command is added into `scratchpad` while keeping `spam` intact.
To make it less verbose, you can use the [`overlay new`](/commands/docs/overlay_new.md) command:
```nu
(zero)> overlay use spam
(spam)> overlay new scratchpad
(scratchpad)> def eggs [] { "eggs" }
```
:::
## Prefixed Overlays
The [`overlay use`](/commands/docs/overlay_use.md) command would take all commands and aliases from the module and put them directly into the current namespace.
However, you might want to keep them as subcommands behind the module's name.
That's what `--prefix` is for:
```nu
(zero)> module spam {
export def foo [] { "foo" }
}
(zero)> overlay use --prefix spam
(spam)> spam foo
foo
```
Note that this does not apply for environment variables.
## Rename an Overlay
You can change the name of the added overlay with the `as` keyword:
```nu
(zero)> module spam { export def foo [] { "foo" } }
(zero)> overlay use spam as eggs
(eggs)> foo
foo
(eggs)> overlay hide eggs
(zero)>
```
This can be useful if you have a generic script name, such as virtualenv's `activate.nu` but you want a more descriptive name for your overlay.
## Preserving Definitions
Sometimes, you might want to remove an overlay, but keep all the custom definitions you added without having to redefine them in the next active overlay:
```nu
(zero)> overlay use spam
(spam)> def eggs [] { "eggs" }
(spam)> overlay hide --keep-custom spam
(zero)> eggs
eggs
```
The `--keep-custom` flag does exactly that.
One can also keep a list of environment variables that were defined inside an overlay, but remove the rest, using the `--keep-env` flag:
```nu
(zero)> module spam {
export def foo [] { "foo" }
export-env { $env.FOO = "foo" }
}
(zero)> overlay use spam
(spam)> overlay hide spam --keep-env [ FOO ]
(zero)> foo
Error: Can't run executable...
(zero)> $env.FOO
foo
```
## Ordering Overlays
The overlays are arranged as a stack.
If multiple overlays contain the same definition, say `foo`, the one from the last active one would take precedence.
To bring an overlay to the top of the stack, you can call [`overlay use`](/commands/docs/overlay_use.md) again:
```nu
(zero)> def foo [] { "foo-in-zero" }
(zero)> overlay use spam
(spam)> foo
foo
(spam)> overlay use zero
(zero)> foo
foo-in-zero
(zero)> overlay list
ββββ¬ββββββ
0 β spam
1 β zero
ββββ΄ββββββ
```
Now, the `zero` overlay takes precedence.
|