/ nushell / config.nu
config.nu
  1  let carapace_completer = {|spans|
  2    ^carapace $spans.0 nushell ...$spans | from json
  3  }
  4  
  5  $env.config = {
  6    show_banner: false
  7    completions: {
  8      case_sensitive: false
  9      quick: true
 10      partial: true
 11      algorithm: "fuzzy"
 12      external: {
 13        enable: true
 14        max_results: 100
 15        completer: $carapace_completer
 16      }
 17    }
 18    hooks: {
 19      pre_prompt: [
 20        { ||
 21          if (which direnv | is-empty) { return }
 22  
 23          direnv export json
 24          | from json
 25          | default {}
 26          | load-env
 27  
 28          if ('ENV_CONVERSIONS' in $env) and ('PATH' in $env.ENV_CONVERSIONS) {
 29            $env.PATH = do $env.ENV_CONVERSIONS.PATH.from_string $env.PATH
 30          }
 31        }
 32      ]
 33    }
 34  }
 35  
 36  source $"($nu.home-path)/.cargo/env.nu"
 37  
 38  # --- Aliases ---
 39  alias vi = vim
 40  alias vim = nvim
 41  alias nano = nvim
 42  alias emacs = emacs -nw
 43  alias lj = lazyjj
 44  alias sioyek = sioyek --shared-database-path `/Volumes/X10 Pro/sioyek/shared.db`
 45  # alias jh = 'jj --help'
 46  # alias je = 'jj edit'
 47  # alias js = 'jj st'
 48  # alias jsq = 'jj squash'
 49  
 50  source ~/.cache/carapace/init.nu
 51  
 52  
 53  
 54  def clean [] {
 55    ^docker system prune --force --all --volumes
 56    cd projects
 57    ^cargo sweep --time 1 --recursive
 58  }
 59  
 60  
 61  export-env {
 62  
 63    $env.MISE_SHELL = "nu"
 64    let mise_hook = {
 65      condition: { "MISE_SHELL" in $env }
 66      code: { mise_hook }
 67    }
 68    add-hook hooks.pre_prompt $mise_hook
 69    add-hook hooks.env_change.PWD $mise_hook
 70  }
 71  
 72  def --env add-hook [field: cell-path new_hook: any] {
 73    let field = $field | split cell-path | update optional true | into cell-path
 74    let old_config = $env.config? | default {}
 75    let old_hooks = $old_config | get $field | default []
 76    $env.config = ($old_config | upsert $field ($old_hooks ++ [$new_hook]))
 77  }
 78  
 79  def "parse vars" [] {
 80    $in | from csv --noheaders --no-infer | rename 'op' 'name' 'value'
 81  }
 82  
 83  export def --env --wrapped main [command?: string, --help, ...rest: string] {
 84    let commands = ["deactivate", "shell", "sh"]
 85  
 86    if ($command == null) {
 87      ^"/opt/homebrew/bin/mise"
 88    } else if ($command == "activate") {
 89      $env.MISE_SHELL = "nu"
 90    } else if ($command in $commands) {
 91      ^"/opt/homebrew/bin/mise" $command ...$rest
 92      | parse vars
 93      | update-env
 94    } else {
 95      ^"/opt/homebrew/bin/mise" $command ...$rest
 96    }
 97  }
 98  
 99  def --env "update-env" [] {
100    for $var in $in {
101      if $var.op == "set" {
102        if ($var.name | str upcase) == 'PATH' {
103          $env.PATH = ($var.value | split row (char esep))
104        } else {
105          load-env {($var.name): $var.value}
106        }
107      } else if $var.op == "hide" {
108        hide-env $var.name
109      }
110    }
111  }
112  
113  def --env mise_hook [] {
114    ^"/opt/homebrew/bin/mise" hook-env -s nu
115      | parse vars
116      | update-env
117  }
118