repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell: direnv and cleanup

a60188fdd50bdda45a55e98849e9146e5ba1e090

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (8)

nushell/auto-overlay.nu

Added
new file mode 100644
1 +# In ~/.config/nushell/config.nu
3 +# Track the directory and modules of the last loaded .numods
4 +$env.LAST_NUMODS_DIR = null
5 +$env.NU_NUMODS_OVERLAYS = []
7 +# Helper function to find the closest .numods file
8 +def find-numods-dir [start_dir: path] {
9 + let home = ($env.HOME | path expand)
10 + let parts = ($start_dir | path split)
11 + let paths = ($parts | enumerate | each { |it|
12 + let path = ($parts | first ($it.index + 1) | path join)
13 + if ($path | path join ".numods" | path exists) {
14 + $path
15 + } else {
16 + null
17 + }
18 + } | where { |p| $p != null })
20 + if ($paths | is-empty) and ($start_dir != $home) {
21 + let parent = ($start_dir | path dirname)
22 + if ($parent == $start_dir or $parent == $home) {
23 + return null
24 + }
25 + return (find-numods-dir $parent)
26 + }
28 + return ($paths | first | default null)
31 +# Helper function to load modules from .numods
32 +def load-numods [dir: path] {
33 + let numods_file = ($dir | path join ".numods")
34 + if (not ($numods_file | path exists)) {
35 + print $"No .numods in ($dir). You trying to sabotage me?"
36 + return
37 + }
39 + let modules = (open $numods_file | lines | str trim | where { |m| not ($m | is-empty) })
40 + let loaded = []
41 + for module in $modules {
42 + try {
43 + let module_path = if ($module | path exists) {
44 + $module
45 + } else {
46 + ($dir | path join $module)
47 + }
48 + if ($module_path | path exists) {
49 + source-env $module_path
50 + let module_name = ($module_path | path basename | str replace ".nu" "")
51 + $loaded = ($loaded | append $module_name)
52 + print $"Loaded module ($module_name) from ($numods_file)"
53 + } else {
54 + let found = ($env.NU_LIB_DIRS? | default [] | each { |lib_dir|
55 + let p = ($lib_dir | path join $"($module).nu")
56 + if ($p | path exists) { $p } else { null }
57 + } | where { |p| $p != null } | first | default null)
58 + if ($found | is-not-empty) {
59 + source-env $found
60 + $loaded = ($loaded | append $module)
61 + print $"Loaded named module ($module) from ($found)"
62 + } else {
63 + print $"Module ($module) not found. Check your paths, genius."
64 + }
65 + }
66 + } catch {
67 + print $"Failed to load module ($module). Cursed code or bad path?"
68 + }
69 + }
70 + $env.NU_NUMODS_OVERLAYS = $loaded
71 + $env.LAST_NUMODS_DIR = $dir
74 +# Helper function to dynamically unload overlays
75 +def unload-overlay [module_name: string] {
76 + try {
77 + # Use do to execute overlay hide dynamically
78 + do { overlay hide $module_name } | ignore
79 + print $"Unloaded module ($module_name)"
80 + } catch {
81 + print $"Failed to unload module ($module_name). It’s haunting your shell."
82 + }
85 +# Helper function to unload all modules from .numods
86 +def unload-numods [] {
87 + if ($env.LAST_NUMODS_DIR | is-empty) {
88 + return
89 + }
91 + for module in $env.NU_NUMODS_OVERLAYS {
92 + unload-overlay $module
93 + }
94 + $env.NU_NUMODS_OVERLAYS = []
95 + $env.LAST_NUMODS_DIR = null
98 +# Pre-prompt hook to manage .numods overlays
99 +$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | default [] | append {||
100 + let current_dir = ($env.PWD | path expand)
101 + let numods_dir = (find-numods-dir $current_dir)
103 + # If we found a .numods file, load its modules
104 + if ($numods_dir | is-not-empty) {
105 + if ($numods_dir != $env.LAST_NUMODS_DIR) {
106 + unload-numods
107 + load-numods $numods_dir
109 + } else if ($env.LAST_NUMODS_DIR | is-not-empty) {
110 + # If we left the .numods tree, unload overlays
111 + if not ($current_dir | str starts-with $env.LAST_NUMODS_DIR) {
112 + unload-numods
117 +# Optional: Display active overlays in prompt
118 +$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append {||
119 + let overlays = ($env.NU_NUMODS_OVERLAYS | str join ", ")
120 + if ($overlays | is-not-empty) {
121 + $env.NU_OVERLAYS = $overlays
122 + } else {
123 + $env.NU_OVERLAYS = null

nushell/config.nu

Modified
1 -def expand-home [] {
2 - let p = $in
3 - if ( $p =~ '~') {
4 - return ($p | path expand)
5 - }
6 - return $p
7 -}
9 -def direnv [] {
10 - [
11 - {
12 - condition: {|before, after| ($before != $after) and ($after | path join '.env' | path exists) }
13 - code: r#'
14 - open .env
15 - | lines
16 - | parse --regex '\s*(?P<key>.+?)\s*=\s*?(?P<value>.+)?\s*'
17 - | reduce --fold {} { | entry, acc|
18 - $acc | upsert $entry.key ( $entry.value | str trim | expand-home )
19 - }
20 - | load-env'#
21 - }
22 - ]
23 -}
25 1 $env.config = {
26 - #color_config: $base16_theme # <-- this is the theme
2 + #color_config: $nugu_theme # TODO: generate nugu theme
27 3 use_ansi_coloring: true
28 4 edit_mode: vi
29 5 show_banner: false
45 21 shell_integration: {
46 22 osc9_9: true
47 23 # unfortunatelly, this seems to be buggy on win32 with wezterm
48 - osc133: ($nu.os-info.family | str contains "unix")
24 + osc133: ($nu.os-info.family | str contains "unix")
50 26 # some experimental options
51 27 buffer_editor: ["nvim" "--clean"]
57 33 isolation: true
59 35 hooks: {
60 - env_change: {
61 - PWD: (direnv)
62 - }
36 + pre_prompt: [{ ||
37 + if (which direnv | is-empty) {
38 + return
39 + }
41 + ^direnv export json | from json | default {} | load-env
42 + if 'ENV_CONVERSIONS' in $env and 'PATH' in $env.ENV_CONVERSIONS {
43 + $env.PATH = do $env.ENV_CONVERSIONS.PATH.from_string $env.PATH
44 + }
45 + }]

nushell/env.nu

Modified
8 8 $env.TOOLS = ("~/Tools" | path expand)
9 9 $env.CARGO_HOME = ( "~/.cargo" | path expand )
10 10 $env.GO_HOME = ( "~/.go" | path expand )
11 -$env.BARTIB_FILE = ( $env.NOTES | path join 'timetrack.txt')
13 12 if $env.WIN32 {
14 13 $env.Path = (
43 42 $env.TERM = "xterm-256color"
46 -if ( not $env.WIN32 ) {
47 - let tools_bin = ( $env.TOOLS | path join bin )
48 - $env.PATH = (
49 - $env.PATH |
50 - split row (char esep) |
51 - prepend $tools_bin
52 - )
53 -}
55 45 # theme for ls and other programs, that use LS_COLORS
56 46 $env.LS_COLORS = (try { vivid generate alabaster_dark | into string } catch {""})
73 63 $env.NU_LIB_DIRS = [
74 64 ($nu.default-config-dir | path join 'scripts')
65 + ($nu.default-config-dir | path join 'modules')
77 68 # generate aliases here and now, so that we can apply conditions.

nushell/gen-aliases.nu

Modified
17 17 # Start defining your aliases here
19 19 # semantic aliases
20 -aka tree eza '--tree' '--icons' '--git' '--all' '--long'
20 +aka tree tre '-all' '--linecount' '--symbolic'
21 21 aka view wezterm imgcat
22 -aka list eza '--long' '--git'
22 +aka list ls '--long' '--all' '--du'
23 23 aka show bat
24 24 aka web firefox
25 25 aka code neovide
29 29 # pro aliases
30 30 aka ll ls '--long'
31 31 aka la ls '--long' '--all'
32 -aka ll lsd '--long' '--group-directories-first' '--classify' '--git'
33 -aka la lsd '--long' '--almost-all' '--group-directories-first' '--classify' '--git'
34 32 aka q exit
35 33 aka g git
36 34 aka gu gitu
37 35 aka vim nvim
38 36 aka vi nvim '--clean'
39 37 aka vide neovide
40 -aka bb bartib
41 -aka cal carl
43 39 aka userctl systemctl '--user'
45 41 # CUSTOM LITTLE JAVA COMMANDS
46 42 aka aes java ($env.DOTFILES + /tools/ + aes.java)
47 -aka download java ($env.DOTFILES + /tools/ + download.java)
48 -aka download_jar java ($env.DOTFILES + /tools/ + download_jar.java)

nushell/login.nu

Modified
8 8 use start.nu
9 9 use timer.nu
10 10 use dwnld.nu
11 -use packages.nu
12 11 use notes.nu
14 13 if $env.WIN32 {

nushell/scripts/direnv.nu

Deleted
deleted file mode 100644
1 -def expand-home [] {
2 - let p = $in
3 - if ( $p =~ '~') {
4 - return ($p | path expand)
5 - }
6 - return $p
7 -}
9 -def direnv [] {
10 - [
11 - {
12 - condition: {|before, after| ($before != $after) and ($after | path join '.env' | path exists) }
13 - code: r#'
14 - open .env
15 - | lines
16 - | parse --regex '\s*(?P<key>.+?)\s*=\s*?(?P<value>.+)?\s*'
17 - | reduce --fold {} { | entry, acc|
18 - $acc | upsert $entry.key ( $entry.value | str trim | expand-home )
19 - }
20 - | load-env
21 - '#
22 - }
23 - ]
24 -}
26 -export-env {
27 - $env.config = ( $env.config | upsert hooks { |config|
28 - let o = ($config | get -i hooks.env_change.PWD)
29 - let val = (direnv)
30 - let appended = if $o == null {
31 - $val
32 - } else {
33 - $o | append $val
34 - }
35 - env_change: {
36 - PWD: $appended
37 - }
38 - })
39 -}

nushell/scripts/packages.nu

Deleted
deleted file mode 100644
1 -export def main [
2 - --clear-cache
3 - --cache = "~/.cache/packages"
4 - --packages = "~/Tools/packages.csv"
5 - --prefix = "~/Tools"
6 -] {
7 - check_dependencies
8 - let prefix = $prefix | path expand
9 - let packages = $packages | path expand
10 - let packages = open --raw $packages | from csv --separator ';' --comment '#' --trim all
11 - let cache = $cache | path expand
12 - if $clear_cache {
13 - rm --recursive --trash --interactive-once $cache
14 - return
15 - }
16 - mkdir $cache
18 - print $"Downloading packages..."
19 - let $packages_file = mktemp --suffix _packages.txt --tmpdir XXX
20 - $packages | get url | save --force $packages_file
21 - run-external dlm '--maxConcurrentDownloads' 4 '--inputFile' $packages_file '--outputDir' $cache
23 - $packages | each { |package|
25 - # pipe through url parse for validation
26 - let base = $package.url | url parse | get path | path basename
27 - let cached = $cache | path join $base
29 - print $"Installing ($package.name)"
30 - if ($cached | is-archive) {
31 - print $"Decompressing ($package.name)..."
32 - run-external ouch decompress '--quiet' $cached '--dir' $prefix
33 - } else {
34 - error make {
35 - msg: $"The package ($package.name) does not seem to be an archive."
36 - }
37 - }
39 - print $"TODO: Symlinks"
40 - }
41 -}
43 -def is-archive [] {
44 - [ .zip .tar.gz .tar.bz2 .tar.xz .7z ] | any { |ext| $in | str ends-with $ext }
45 -}
47 -def remove-archive-extension [] {
48 - let uri = $in
49 - [ .zip .tar.gz .tar.bz2 .tar.xz .7z ] | reduce --fold $uri { |ext, acc|
50 - $acc | str replace $ext ''
51 - } | str trim
52 -}
54 -def check_dependencies [] {
55 - let missing = [ ouch dlm ] | any { which $in | is-empty }
56 - if $missing {
57 - error make {
58 - msg: 'Some dependency is missing!'
59 - }
60 - }
61 -}

nushell/scripts/sy.nu

Modified
2 2 # Check system status
3 3 export def main [] {
4 4 check-deps
6 - let git_status_notes = [ '-C' $env.NOTES status ]
7 - let git_status_dotfiles = [ '-C' $env.DOTFILES status ]
6 + let git_status_notes = [ '-C' $env.NOTES status '--short' '--branch' ]
7 + let git_status_dotfiles = [ '-C' $env.DOTFILES status '--short' '--branch' ]
9 9 # print git $git_status_notes
10 10 run-external git ...$git_status_notes
17 17 export def up [] {
18 18 check-deps
20 - # FIXME: make list of repos and factor sync into functions
21 20 let git_add_notes = [ '-C' $env.NOTES add '--all' ]
22 21 let git_commit_notes = [ '-C' $env.NOTES commit '--all' ]
23 - let git_push_notes = [ '-C' $env.NOTES push '-ff' ]
22 + let git_push_notes = [ '-C' $env.NOTES push ]
24 23 let git_add_dotfiles = [ '-C' $env.DOTFILES add '--all' ]
25 24 let git_commit_dotfiles = [ '-C' $env.DOTFILES commit '--all' ]
26 25 let git_push_dotfiles = [ '-C' $env.DOTFILES push ]