Skip to content

Commit be303cf

Browse files
committed
update the hook and instructions on how to use it, tips on using modules in the scaffolded mod.nu
1 parent 5225edc commit be303cf

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,16 @@ source .nu_scripts/activate.nu
105105
When you're done, run `deactivate` (or `overlay hide activate`) to unload the module overlay.
106106

107107
### 2. Auto-activation Hook
108-
If you want nuance projects to automatically update your module path when you `cd` into their directory (and remove it when you leave), add the nuance env_change hook to your `config.nu` or `env.nu`:
109108

110-
```bash
111-
# Run this and append the output to your config
112-
nuance hook
109+
If you want nuance projects to automatically update your module path when you `cd` into their directory (and remove it when you leave), add the following to your `config.nu` or `env.nu`:
110+
111+
```nu
112+
mkdir ($nu.default-config-dir | path join "vendor" "autoload")
113+
nuance hook | save -f ($nu.default-config-dir | path join "vendor" "autoload" "nuance_hook.nu")
113114
```
114115

116+
You can also simply run these commands without adding them to your config or env file. You just won't receive any modifications of the hook until you run the commands again, but your shell startup time will be faster.
117+
115118
> **Note**: Due to Nushell's static scoping rules, the auto-activation hook only updates `$env.NU_LIB_DIRS`. It cannot auto-import module/script commands. For automatic loading, use the manual activation approach above.
116119
117120
## mod.toml

src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ fn cmd_init(
145145
if !mod_nu.exists() {
146146
std::fs::write(
147147
&mod_nu,
148-
"# Module entry point\n# Export your commands here with: export use <submodule>\n",
148+
r#"# Module entry point
149+
# Export your commands here with: export use <submodule>
150+
# Use installed modules with: use ../.nu_modules/module-name/module-name *
151+
"#,
149152
)?;
150153
eprintln!("Created {}", mod_nu.display());
151154
}
@@ -531,17 +534,26 @@ fn cmd_remove_global(name: String) -> Result<()> {
531534
}
532535

533536
fn cmd_hook() -> Result<()> {
534-
let hook_script = r#"# nuance auto-activate hook — add this to your config.nu (or env.nu)
537+
let hook_script = r#"# nuance auto-activate hook
538+
# Add this to your Nushell environment by running:
539+
# mkdir ($nu.default-config-dir | path join "vendor" "autoload")
540+
# nuance hook | save -f ($nu.default-config-dir | path join "vendor" "autoload" "nuance_hook.nu")
541+
# Once saved, it will be automatically sourced when you start Nushell.
542+
# You can add the above to your config.nu if you want any updates to the hook, but that may slow start time.
543+
535544
$env.config.hooks.env_change.PWD = (
536-
$env.config.hooks.env_change.PWD | default [] | append {|before, after|
545+
$env.config.hooks.env_change.PWD? | default [] | append {|before after|
546+
let before = ($before | default "")
547+
let after = ($after | default "")
548+
537549
# Remove previous directory's modules/scripts if it was a nuance project
538-
if ($before | path join "mod.toml" | path exists) {
550+
if ($before | is-not-empty) and ($before | path join "mod.toml" | path exists) {
539551
let old_modules = ($before | path join ".nu_modules")
540552
let old_scripts = ($before | path join ".nu_scripts")
541-
$env.NU_LIB_DIRS = ($env.NU_LIB_DIRS | default [] | where { |it| $it != $old_modules and $it != $old_scripts })
553+
$env.NU_LIB_DIRS = ($env.NU_LIB_DIRS | default [] | where {|it| $it != $old_modules and $it != $old_scripts })
542554
}
543555
# Add new directory's modules/scripts if it is a nuance project
544-
if ($after | path join "mod.toml" | path exists) {
556+
if ($after | is-not-empty) and ($after | path join "mod.toml" | path exists) {
545557
let new_modules = ($after | path join ".nu_modules")
546558
let new_scripts = ($after | path join ".nu_scripts")
547559
if ($new_modules | path exists) and ($new_modules not-in ($env.NU_LIB_DIRS | default [])) {

0 commit comments

Comments
 (0)