summaryrefslogtreecommitdiff
path: root/nushell
diff options
context:
space:
mode:
Diffstat (limited to 'nushell')
-rw-r--r--nushell/autoload/mise.nu57
-rw-r--r--nushell/autoload/zoxide.nu70
-rw-r--r--nushell/config.nu126
-rw-r--r--nushell/env.nu68
4 files changed, 321 insertions, 0 deletions
diff --git a/nushell/autoload/mise.nu b/nushell/autoload/mise.nu
new file mode 100644
index 0000000..15c7780
--- /dev/null
+++ b/nushell/autoload/mise.nu
@@ -0,0 +1,57 @@
+def "parse vars" [] {
+ $in | from csv --noheaders --no-infer | rename 'op' 'name' 'value'
+}
+
+def --env "update-env" [] {
+ for $var in $in {
+ if $var.op == "set" {
+ if ($var.name | str uppercase) == 'PATH' {
+ $env.PATH = ($var.value | split row (char esep))
+ } else {
+ load-env {($var.name): $var.value}
+ }
+ } else if $var.op == "hide" and $var.name in $env {
+ hide-env $var.name
+ }
+ }
+}
+export-env {
+
+ '' | parse vars | update-env
+ $env.MISE_SHELL = "nu"
+ let mise_hook = {
+ condition: { "MISE_SHELL" in $env }
+ code: { mise_hook }
+ }
+ add-hook hooks.pre_prompt $mise_hook
+ add-hook hooks.env_change.PWD $mise_hook
+}
+
+def --env add-hook [field: cell-path new_hook: any] {
+ let field = $field | split cell-path | update optional true | into cell-path
+ let old_config = $env.config? | default {}
+ let old_hooks = $old_config | get $field | default []
+ $env.config = ($old_config | upsert $field ($old_hooks ++ [$new_hook]))
+}
+
+export def --env --wrapped main [command?: string, --help, ...rest: string] {
+ let commands = ["deactivate", "shell", "sh"]
+
+ if ($command == null) {
+ ^"/Users/eltrac/.local/bin/mise"
+ } else if ($command == "activate") {
+ $env.MISE_SHELL = "nu"
+ } else if ($command in $commands) {
+ ^"/Users/eltrac/.local/bin/mise" $command ...$rest
+ | parse vars
+ | update-env
+ } else {
+ ^"/Users/eltrac/.local/bin/mise" $command ...$rest
+ }
+}
+
+def --env mise_hook [] {
+ ^"/Users/eltrac/.local/bin/mise" hook-env -s nu
+ | parse vars
+ | update-env
+}
diff --git a/nushell/autoload/zoxide.nu b/nushell/autoload/zoxide.nu
new file mode 100644
index 0000000..e22671c
--- /dev/null
+++ b/nushell/autoload/zoxide.nu
@@ -0,0 +1,70 @@
+# Code generated by zoxide. DO NOT EDIT.
+
+# =============================================================================
+#
+# Hook configuration for zoxide.
+#
+
+# Initialize hook to add new entries to the database.
+export-env {
+ $env.config = (
+ $env.config?
+ | default {}
+ | upsert hooks { default {} }
+ | upsert hooks.env_change { default {} }
+ | upsert hooks.env_change.PWD { default [] }
+ )
+ let __zoxide_hooked = (
+ $env.config.hooks.env_change.PWD | any { try { get __zoxide_hook } catch { false } }
+ )
+ if not $__zoxide_hooked {
+ $env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append {
+ __zoxide_hook: true,
+ code: {|_, dir| ^zoxide add -- $dir}
+ })
+ }
+}
+
+# =============================================================================
+#
+# When using zoxide with --no-cmd, alias these internal functions as desired.
+#
+
+# Jump to a directory using only keywords.
+export def --env --wrapped __zoxide_z [...rest: directory] {
+ let path = match $rest {
+ [] => {'~'},
+ [ '-' ] => {'-'},
+ [ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg}
+ _ => {
+ ^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
+ }
+ }
+ cd $path
+}
+
+# Jump to a directory using interactive search.
+export def --env --wrapped __zoxide_zi [...rest: string] {
+ cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")'
+}
+
+# =============================================================================
+#
+# Commands for zoxide. Disable these using --no-cmd.
+#
+
+export alias z = __zoxide_z
+export alias zi = __zoxide_zi
+
+# =============================================================================
+#
+# Add this to your env file (find it by running `$nu.env-path` in Nushell):
+#
+# zoxide init nushell | save -f ~/.zoxide.nu
+#
+# Now, add this to the end of your config file (find it by running
+# `$nu.config-path` in Nushell):
+#
+# source ~/.zoxide.nu
+#
+# Note: zoxide only supports Nushell v0.89.0+.
diff --git a/nushell/config.nu b/nushell/config.nu
new file mode 100644
index 0000000..0475a0e
--- /dev/null
+++ b/nushell/config.nu
@@ -0,0 +1,126 @@
+$env.config.buffer_editor = 'nvim'
+$env.config.show_banner = false
+
+# ---- Aliases ----
+alias vim = nvim
+alias vi = nvim
+alias eml = aerc
+alias tinypng = tinifier
+alias lg = lazygit
+alias rm = trash
+alias you-get = you-get --cookies ~/.local/share/you-get/www.bilibili.com_cookies.txt
+alias fjc = fj --host codeberg.org
+alias fjs = fj --host src.eltr.ac
+alias plantuml = java -jar ~/.local/jar/plantuml.jar
+alias hugo-tag = uv run ~/Projects/snippets/hugo-tag-wrangler/script.py
+alias vimulti = bb ~/Projects/vimulti/vimulti.clj
+alias nvimj = vimulti java-edition
+alias switch-proxy = bb ~/Projects/snippets/QoL/switch-proxy.clj
+alias swp = switch-proxy
+alias wtr = weather
+alias nuconf = config nu
+alias nuenv = config env
+
+# ---- Custom commands ----
+
+# Remind to use mosh instead of ssh
+def ssh [...rest: string] {
+ print "⚠️ You should use mosh!"
+ ^ssh ...$rest
+}
+
+# cd into one dir and list its content
+def peek [...paths: path] {
+ if ($paths | is-empty) {
+ print $"📂 Subdirectories of ($env.PWD):"
+ ls | where type == dir | get name | print
+ } else {
+ for dir in $paths {
+ if ($dir | path type) == dir {
+ print $"📁 ($dir):"
+ facad $dir
+ print ""
+ } else {
+ print $"peek: '($dir)' is not a directory"
+ }
+ }
+ }
+}
+
+# mkdir and cd
+def --env mkcd [dir: path] {
+ mkdir $dir
+ cd $dir
+}
+
+# cd ../
+def --env back [] {
+ cd ..
+}
+
+# git visualization
+def gitvis [] {
+ git log --oneline --graph --all --decorate
+}
+
+# check what apps are not managed by homebrew
+def brewoutcasts [] {
+ let apps = (ls /Applications | where type == dir and name ends-with '.app')
+ let total = ($apps | length)
+ let formulae = (brew list --formula | str trim)
+ let casks = (brew list --cask | str trim)
+ mut unmanaged = []
+ for app in $apps {
+ let name = ($app.name | str replace -r '\.app$' '')
+ let base = ($name | str lowercase)
+ let normalized = (match $base {
+ 'hidden bar' => 'hiddenbar'
+ 'helium' => 'helium-browser'
+ _ => ($base | str replace -a ' ' '-' | str replace -a '.' '-')
+ })
+ if $base == 'safari' { continue }
+ let managed = ($formulae | any {|f| ($f | str lowercase) == $normalized }) or ($casks | any {|c| ($c | str lowercase) == $normalized })
+ if (not $managed) {
+ print $"\n($name) is NOT managed by Homebrew"
+ print $"-- Searching Homebrew for ($name)..."
+ brew search $normalized | print
+ $unmanaged = ($unmanaged | append $name)
+ }
+ }
+ print ""
+ print $"-- Summary: ($unmanaged | length) out of ($total) applications are not managed by Homebrew"
+}
+
+# yazi: cd into the dir yazi was closed in
+def --env y [...args: string] {
+ let tmp = (mktemp -t 'yazi-cwd.XXXXXX' | str trim)
+ command yazi ...$args --cwd-file=$tmp
+ let cwd = (open --raw $tmp | str trim)
+ if ($cwd | is-not-empty) and ($cwd != $env.PWD) { cd $cwd }
+ rm -f $tmp
+}
+
+def weather [] {
+ curl wttr.in/Shapingba
+}
+
+# thefuck is a bash/zsh hook; nu shim shows the suggested fix
+def fuck [] {
+ let last = (history | get command | str trim | last)
+ thefuck $last
+}
+
+# ---- Startup side effects ----
+try { ^ssh-add ~/.ssh/id_ed25519_git o+e>| null } catch { }
+
+# ---- zoxide ----
+if (which zoxide | is-not-empty) {
+ mkdir ~/.config/nushell/autoload
+ zoxide init nushell | save -f ~/.config/nushell/autoload/zoxide.nu
+}
+
+# ---- mise ----
+if (which mise | is-not-empty) {
+ mkdir ~/.config/nushell/autoload
+ mise activate nu | str replace -a 'str upcase' 'str uppercase' | save -f ~/.config/nushell/autoload/mise.nu
+}
diff --git a/nushell/env.nu b/nushell/env.nu
new file mode 100644
index 0000000..16e4511
--- /dev/null
+++ b/nushell/env.nu
@@ -0,0 +1,68 @@
+# ---- XDG base directories ----
+$env.XDG_CONFIG_HOME = ($env.XDG_CONFIG_HOME? | default ($env.HOME | path join '.config'))
+$env.XDG_DATA_HOME = ($env.XDG_DATA_HOME? | default ($env.HOME | path join '.local/share'))
+$env.XDG_STATE_HOME = ($env.XDG_STATE_HOME? | default ($env.HOME | path join '.local/state'))
+$env.XDG_CACHE_HOME = ($env.XDG_CACHE_HOME? | default ($env.HOME | path join '.cache'))
+
+# ---- Homebrew ----
+$env.HOMEBREW_PREFIX = '/opt/homebrew'
+$env.HOMEBREW_CELLAR = '/opt/homebrew/Cellar'
+$env.HOMEBREW_REPOSITORY = '/opt/homebrew'
+$env.MANPATH = (if ($env.MANPATH? | is-empty) {
+ $env.HOMEBREW_PREFIX | path join 'share/man'
+} else {
+ $'($env.HOMEBREW_PREFIX)/share/man:($env.MANPATH)'
+})
+
+# ---- Rust / cargo ----
+$env.CARGO_HOME = ($env.CARGO_HOME? | default ($env.HOME | path join '.local/share/cargo'))
+
+# ---- Apps ----
+$env.WAKATIME_HOME = ($env.XDG_CONFIG_HOME | path join 'wakatime')
+$env.BUN_INSTALL = ($env.HOME | path join '.bun')
+$env.PNPM_HOME = ($env.HOME | path join '.local/share/pnpm')
+$env.NVM_DIR = ($env.HOME | path join '.config/nvm')
+$env.EDITOR = 'nvim'
+$env.HOMEBREW_NO_ENV_HINTS = '1'
+$env.PUPPETEER_EXECUTABLE_PATH = ($env.HOME | path join '.cache/puppeteer/chrome/mac_arm-142.0.7444.59/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing')
+
+# Proxy
+$env.http_proxy = 'http://127.0.0.1:7890'
+$env.https_proxy = 'http://127.0.0.1:7890'
+$env.all_proxy = 'socks5://127.0.0.1:7890'
+$env.no_proxy = 'localhost,127.0.0.1'
+
+# GPG
+try { $env.GPG_TTY = (tty | str trim) } catch { }
+
+# Java
+try { $env.JAVA_HOME = (/usr/libexec/java_home -v 17 | str trim) } catch { }
+
+# Temporary files
+let state_tmp = ($env.XDG_STATE_HOME | path join 'tmp')
+mkdir $state_tmp
+$env.TMPDIR = $state_tmp
+
+# ---- PATH ----
+$env.PATH = ([
+ ($env.HOME | path join '.local/bin') # ~/.local/bin/env
+ $env.PNPM_HOME # pnpm
+ ($env.BUN_INSTALL | path join 'bin') # bun
+ '/opt/nanobrew/prefix/bin' # nanobrew
+ ($env.HOME | path join 'bin') # ~/bin
+ '/Library/Frameworks/Python.framework/Versions/3.13/bin' # Python 3.13 (.zprofile)
+ '/usr/local/bin' # macOS standard
+ '/opt/homebrew/bin' # Homebrew
+ '/opt/homebrew/sbin' # Homebrew
+ ($env.CARGO_HOME | path join 'bin') # cargo/rustup (.zshenv)
+ ...$env.PATH
+ ($env.HOME | path join 'Library/Python/3.9/bin') # Python 3.9 (user)
+ ($env.HOME | path join '.local/share/go/bin') # Go
+] | uniq)
+
+# pnpm
+$env.PNPM_HOME = "/Users/eltrac/.local/share/pnpm"
+$env.PATH = ($env.PATH | split row (char esep) | prepend ($env.PNPM_HOME | path join "bin") )
+# pnpm end
+
+source secrets.nu