nushell/crates/nu_plugin_example
Devyn Cairns f6faf73e02
Allow plugins to set environment variables in their caller's scope (#12204)
# Description

Adds the `AddEnvVar` plugin call, which allows plugins to set
environment variables in the caller's scope. This is the first engine
call that mutates the caller's stack, and opens the door to more
operations like this if needed.

This also comes with an extra benefit: in doing this, I needed to
refactor how context was handled, and I was able to avoid cloning
`EngineInterface` / `Stack` / `Call` in most cases that plugin calls are
used. They now only need to be cloned if the plugin call returns a
stream. The performance increase is welcome (5.5x faster on `inc`!):

```nushell
# Before
> timeit { 1..100 | each { |i| $"2.0.($i)" | inc -p } }
405ms 941µs 952ns
# After
> timeit { 1..100 | each { |i| $"2.0.($i)" | inc -p } }
73ms 68µs 749ns
```

# User-Facing Changes
- New engine call: `add_env_var()`
- Performance enhancement for plugin calls

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting
- [x] Document env manipulation in plugins guide
- [x] Document `AddEnvVar` in plugin protocol
2024-03-15 06:45:45 -05:00
..
src Allow plugins to set environment variables in their caller's scope (#12204) 2024-03-15 06:45:45 -05:00
Cargo.toml Bump version to 0.91.1 (#12085) 2024-03-06 23:08:14 +01:00
README.md Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00

Plugin Example

Crate with a simple example of the Plugin trait that needs to be implemented in order to create a binary that can be registered into nushell declaration list

example config

This subcommand demonstrates sending configuration from the nushell $env.config to a plugin.

To register from after building nushell run:

register target/debug/nu_plugin_example

The configuration for the plugin lives in $env.config.plugins.example:

$env.config = {
  plugins: {
    example: [
      some
      values
    ]
  }
}

To list plugin values run:

nu-example-config