repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/direnv.nu

Raw
def direnv [] {
    [
        {
            condition: {|before, after| ($before != $after) and ($after | path join '.env' | path exists) }
            code: "
                open .env
                | lines
                | parse -r '(?P<k>.+?)=(?P<v>.+)'
                | reduce -f {} {|x, acc| $acc | upsert $x.k $x.v}
                | load-env
            "
        }
    ]
}

export-env {
    $env.config = ( $env.config | upsert hooks.env_change.PWD { |config|
        let o = ($config | get -i hooks.env_change.PWD)
        let val = (direnv)
        if $o == null {
            $val
        } else {
            $o | append $val
        }
    })
}