Skip to content
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

Conda Module with Better Performance #366

Closed
neur1n opened this issue Jan 30, 2023 · 2 comments
Closed

Conda Module with Better Performance #366

neur1n opened this issue Jan 30, 2023 · 2 comments

Comments

@neur1n
Copy link
Contributor

neur1n commented Jan 30, 2023

I implemented a conda.nu module for activating and deactivating conda envs but I'm not sure if this is suitable for a PR since it is quite different from the existing one:

  1. The original Path/PATH environment variable, root directory of Conda and available envs are cached once calling use conda.nu.
  2. Mamba is supported and preferred over Conda, but the supporting might be incomplete since I've dive into their differences.
  3. There is not option to change your prompt. One must use $env.CONDA_CURR to customize the prompt.

On my PC, using this new module to activate a Conda env costs ~20ms while the old one costs ~1500ms (by checking $env.CMD_DURATION_MS). The code is listed and attached.

export-env {
  let-env CONDA_BASE_PATH = (if ((sys).host.name == "Windows") {$env.Path} else {$env.PATH})

  let info = (
      if not (which mamba | is-empty) {
        (mamba info --envs --json | from json)
      } else if not (which conda | is-empty) {
        (conda info --envs --json | from json)
      } else {
        ('{"root_prefix": "", "envs": ""}' | from json)
      })

  let-env CONDA_ROOT = $info.root_prefix

  let-env CONDA_ENVS = ($info.envs | reduce -f {} {|it, acc|
      if $it == $info.root_prefix {
        $acc | upsert "base" $it
      } else {
        $acc | upsert ($it | path basename) $it
      }})

  let-env CONDA_CURR = null
}

export def-env activate [name: string] {
  if ($env.CONDA_ROOT | is-empty) {
    echo "Neither Conda nor Mamba is valid."
    return
  }

  if not $name in $env.CONDA_ENVS {
    echo $"Environment ($name) is invalid. Available:"
    echo $env.CONDA_ENVS
    return
  }

  let new_path = (
    if ((sys).host.name == "Windows") {
      update-path-windows ($env.CONDA_ENVS | get $name)
    } else {
      update-path-linux ($env.CONDA_ENVS | get $name)
    })

  load-env ({CONDA_CURR: $name} | merge $new_path)
}

export def-env deactivate [] {
  if ($env.CONDA_ROOT | is-empty) {
    echo "Neither Conda nor Mamba is valid."
    return
  }

  let-env CONDA_CURR = null

  load-env {Path: $env.CONDA_BASE_PATH, PATH: $env.CONDA_BASE_PATH}
}

def update-path-linux [env_path: path] {
  let env_path = [
    $env_path,
    ([$env_path, "bin"] | path join)
  ]

  return {
    Path: ($env.PATH | prepend $env_path),
    PATH: ($env.PATH | prepend $env_path)
  }
}

def update-path-windows [env_path: path] {
  let env_path = [
    $env_path,
    ([$env_path, "Scripts"] | path join),
  ]

  return {
    Path: ($env.Path | prepend $env_path),
    PATH: ($env.Path | prepend $env_path),
  }
}

conda.txt

@kubouch
Copy link
Contributor

kubouch commented Jan 30, 2023

You can add your module alongside the current one (or organize them into separate directories so both can be named conda.nu) and explain the differences in a README. We can have both.

@neur1n
Copy link
Contributor Author

neur1n commented Jan 30, 2023

You can add your module alongside the current one (or organize them into separate directories so both can be named conda.nu) and explain the differences in a README. We can have both.

Great! I'll create a PR later.

@neur1n neur1n closed this as completed Jan 30, 2023
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

No branches or pull requests

2 participants