Skip to content

feat(config): add $env.nupm.config #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

mkatychev
Copy link
Contributor

@mkatychev mkatychev commented Jun 28, 2025

Description

This PR introduces a config key for $env.nupm as well as a way to automatically add nupm plugin, script, and module directories to the relevant nushell search paths
allowing for this configuration:

# config.nu
$env.nupm.config.nu_search_path = true
# ...
use path/to/nupm
nupm install my_nu_script
source my_nu_script.nu
  • removed nested default key in BASE_NUPM_CONFIG

@mkatychev
Copy link
Contributor Author

mkatychev commented Jun 28, 2025

NOTE: this PR is based off of #122 and the diff will be noisy until that one is merged as I cannot submit a PR against a remote feature branch. Here's the actual patch:

diff --git a/README.md b/README.md
index 5dd056b..48382fc 100644
--- a/README.md
+++ b/README.md
@@ -39,22 +39,17 @@ One can change the location of the Nupm directory with `$env.nupm.home`, e.g.
 $env.nupm.home = ($env.XDG_DATA_HOME | path join "nupm")
 ```
 
-Because Nupm will install modules and scripts in `{{nupm-home}}/modules/` and `{{nupm-home}}/scripts/` respectively, it is a good idea to add these paths to `$env.NU_LIB_DIRS` and `$env.PATH` respectively, e.g. if you have `$env.nupm.home` defined:
+If you would like installed modules, scripts, and plugins to show up in [nushell search
+paths](https://www.nushell.sh/book/configuration.html#launch-stages), set the
+`nu_search_path` to `true` before calling `use nupm`:
 ```nushell
 # env.nu
-
-$env.NU_LIB_DIRS = [
-    ...
-    ($env.nupm.home | path join "modules")
-]
-
-$env.PATH = (
-    $env.PATH
-        | split row (char esep)
-        | ....
-        | prepend ($env.nupm.home | path join "scripts")
-        | uniq
-)
+$env.nupm = {
+  home: "path/to/my_home"
+  config: { nu_search_path: true }
+}
+# ...
+use path/to/nupm
 ```
 
 ## :rocket: usage [[toc](#table-of-content)]
diff --git a/nupm/mod.nu b/nupm/mod.nu
index 38551bd..98bc43d 100644
--- a/nupm/mod.nu
+++ b/nupm/mod.nu
@@ -13,18 +13,23 @@ export module test.nu
 
 export-env {
     # Ensure that $env.nupm is always set when running nupm. Any missing variaables are set by `$BASE_NUPM_CONFIG`
-    $env.nupm = ($env.nupm? | default {}
-        | merge $BASE_NUPM_CONFIG
-        # set missing values to default while retaining
-        # $env.nupm.default
-        | merge $BASE_NUPM_CONFIG.default
-    )
+    $env.nupm = $BASE_NUPM_CONFIG | merge deep ($env.nupm? | default {})
+    # set missing values to default while
+    # retaining defaults in $env.nupm.default
+    $env.nupm.default = $BASE_NUPM_CONFIG
     # read from registry index but don't overwrite registires already present in $env.nupm.registries
     $env.nupm.registries = $env.nupm.index-path | open-index | merge $env.nupm.registries
     $env.ENV_CONVERSIONS.nupm = {
         from_string: { |s| $s | from nuon }
         to_string: { |v| $v | to nuon }
     }
+    if $env.nupm.config.nu_search_path {
+        let nupm_lib_dirs = [modules, scripts] | each {|s| $env.nupm.home | path join $s }
+        $env.NU_LIB_DIRS = $env.NU_LIB_DIRS | prepend $nupm_lib_dirs | uniq
+
+        let nupm_plugin_dir = $env.nupm.home| path join "plugins"
+        $env.NU_PLUGIN_DIRS = $env.NU_PLUGIN_DIRS | prepend $nupm_plugin_dir | uniq
+    }
     use std/log []
 }
 
diff --git a/nupm/utils/dirs.nu b/nupm/utils/dirs.nu
index cb3942b..835d779 100644
--- a/nupm/utils/dirs.nu
+++ b/nupm/utils/dirs.nu
@@ -1,14 +1,18 @@
-# Base values for nupm that are used as defaults if not present in `$env.nupm`
 export const REGISTRY_IDX_FILENAME = "registry_index.nuon"
+# Base values for nupm that are used as defaults if not present in `$env.nupm`
 export const BASE_NUPM_CONFIG = {
-    default: {
-        home: ($nu.default-config-dir | path join nupm)
-        cache: ($nu.default-config-dir | path join nupm cache)
-        index-path: ($nu.default-config-dir | path join nupm $REGISTRY_IDX_FILENAME)
-        temp: ($nu.temp-path | path join nupm)
-        registries: {
-            nupm: 'https://raw.githubusercontent.com/nushell/nupm/main/registry/registry.nuon'
-        }
+    home: ($nu.default-config-dir | path join nupm)
+    cache: ($nu.default-config-dir | path join nupm cache)
+    index-path: ($nu.default-config-dir | path join nupm $REGISTRY_IDX_FILENAME)
+    temp: ($nu.temp-path | path join nupm)
+    registries: {
+        nupm: 'https://raw.githubusercontent.com/nushell/nupm/main/registry/registry.nuon'
+    }
+    config: {
+        # TODO
+        # sync_list: { ... }
+        # sync_on_launch: false
+        nu_search_path: false
     }
 }
 
@@ -93,7 +97,7 @@ export def cache-dir [--ensure]: nothing -> path {
 }
 
 export def tmp-dir [subdir: string, --ensure]: nothing -> path {
-    let d = $BASE_NUPM_CONFIG.default.temp
+    let d = $BASE_NUPM_CONFIG.temp
         | path join $subdir
         | path join (random chars -l 8)
 
diff --git a/tests/mod.nu b/tests/mod.nu
index 9760fa3..f9f3cec 100644
--- a/tests/mod.nu
+++ b/tests/mod.nu
@@ -140,10 +140,10 @@ export def env-vars-are-set [] {
 
     use ../nupm
 
-    assert equal $env.nupm.home $BASE_NUPM_CONFIG.default.home
-    assert equal $env.nupm.temp $BASE_NUPM_CONFIG.default.temp
-    assert equal $env.nupm.cache $BASE_NUPM_CONFIG.default.cache
-    assert equal $env.nupm.registries $BASE_NUPM_CONFIG.default.registries
+    assert equal $env.nupm.home $BASE_NUPM_CONFIG.home
+    assert equal $env.nupm.temp $BASE_NUPM_CONFIG.temp
+    assert equal $env.nupm.cache $BASE_NUPM_CONFIG.cache
+    assert equal $env.nupm.registries $BASE_NUPM_CONFIG.registries
 }
 
 export def generate-local-registry [] {
@@ -377,3 +377,31 @@ export def registry-fetch [] {
         assert ("Registry 'invalid-registry' not found" in $invalid_registry_result)
     }
 }
+
+export def config-nu-search-path [] {
+    with-test-env {
+        use ../nupm
+        # By default, nu_search_path should be false
+        assert equal $env.nupm.config.nu_search_path false
+
+        # Verify nupm directories are NOT added to NU_LIB_DIRS when disabled
+        let modules_dir = $env.nupm.home | path join modules
+        let scripts_dir = $env.nupm.home | path join scripts
+        let plugins_dir = $env.nupm.home | path join plugins
+
+        # Check that nupm dirs are not in the search paths
+        assert (not ($modules_dir in $env.NU_LIB_DIRS))
+        assert (not ($scripts_dir in $env.NU_LIB_DIRS))
+        assert (not ($plugins_dir in $env.NU_PLUGIN_DIRS))
+
+        # Manually set the config to true to test the functionality
+        $env.nupm.config.nu_search_path = true
+
+        use ../nupm
+        assert equal $env.nupm.config.nu_search_path true
+
+        assert ($modules_dir in $env.NU_LIB_DIRS)
+        assert ($scripts_dir in $env.NU_LIB_DIRS)
+        assert ($plugins_dir in $env.NU_PLUGIN_DIRS)
+    }
+}

@kubouch
Copy link
Contributor

kubouch commented Jul 12, 2025

We don't need $env.nupm.config (yet at least) because nupm is eventually expected to behave like nu_search_path with true. No need for an option. The NU_LIB/PLUGIN_DIRS handling is also better left for the overlays. $env.NU_LIB/PLUGIN_DIRS are kind of deprecated in Nushell and it's better to use the const version -- this becomes impossible to define them from within a command which means that in nupm they'll need to be defined from the overlay.

Otherwise, grouping all $env.NUPM_xxx into $env.nupm seems OK to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants