Keyword to export definitions from module? #8974

@bobhy

Description

Related problem

I ran into this issue trying to do a conditional use std or use std * in my env.nu , experimenting with different ways to load the standard library. I found that if I put the use command in any kind of block within env.nu, the definitions get loaded, but go out of scope when the block is exited and are not available in the interactive session.

> cat ~/.config/nushell/env.nu
---- clip ----
if true {
  print "import of STD namespace"
  export use std
---- clip ---

Although the true branch executes, no additional std subcommands were defined for my interactive NU session.

import of STD namespace
〉help command std dirs
Error: nu::parser::not_found
  × Not found.
   ╭─[entry #2:1:1]
 1 │ help command std dirs
   ·      ────────┬───────
   ·              ╰── did not find anything under this name
   ╰────

I tried this without then with the export use as shown above, neither worked.
I'm not at all sure export is even relevant, since env.nu is sourced not loaded via use.

Describe the solution you'd like

Perhaps a super or global keywords which add externs, aliases, environment variables and custom commands to the next outermost block scope or to the outermost scope in the interpreter? I'd say the keyword could be restricted to use, so you could say super use or global use, but not super def or global alias.

Describe alternatives you've considered

This could be done with a source code generator or preprocessor, but that seems overkill and anti-performant.

Additional context and details

The example given above is just a dev toy, but I do think the scenario makes sense.

Suppose there's an API with a given set of verbs: open, get, put...; and different modules that each implement those verbs for different different cloud services or database backends. Then the user would want to load one of those modules, conditionally chosen based on his/her working environment. How should the user proceed?