1. 41
  1. 22

    hi, direnv author here :)

    1. 7

      Don’t have a question but love the tool! Thank you!

      1. 3

        thanks! :)

      2. 3

        Hi, thanks for direnv! It’s been life-changing. Thanks to direnv, my virtualenvs are automatically activated/deactivated per-directory, and project config and secrets are all in environment variables and loaded by direnv, making it easy to have parity between dev and production.

        Since you’re here, wanted to ask: you think unload hooks can ever be a thing? There’s a longstanding ticket for the feature: https://github.com/direnv/direnv/issues/129

        1. 3

          Since you’re here, wanted to ask: you think unload hooks can ever be a thing? There’s a longstanding ticket for the feature: https://github.com/direnv/direnv/issues/129

          There are too many edge cases. For example, what happens if the user just closes the shell? direnv is just a hook that is executed before every prompt and wouldn’t know about it. Or what happens if two shells are opened on the same project, how does direnv keep track?

          Users that ask for this generally want to start/stop service on demand for a project. I think the best for that is to actually open a second shell and start a process manager by hand. Then you get to see what services are running, you can start/stop them, …

          1. 1

            “not guaranteed to be called in case of closed terminal” are semantics I would welcome. I just want to hook into where it says “direnv: unloading”. My idea was to style my terminal (eg. terminal tab) by directory, and hooking into direnv’s activated inheritance seemed the perfect place to set/unset it. I can do something on directory enter with a plugin but not on directory leave without unload.

        2. 1

          I freaking love direnv. Thank you so much!

        3. 9

          This is a great use of direnv: https://jamey.thesharps.us/2019/05/29/per-project-postgres/ , i myself use it to automatically enter the virtual environment of my projects.

          1. 2

            Yes, it would be nice if more projects would support libpq environment variables. Various non-C (and non-C depencies) libraries support them, often by simply not specifying a configuration out of the box. It tends to make a lot of things easier, also when you wanna do postgres over unix sockets - authors tend to forget about that use case.

          2. 8

            Project is pretty cool, however my .profile is not clutered.

            1. 1

              .profile .shrc and other dot files are better kept minimal. I just have a script for each project, something like

              #!/usr/bin/env bash
              cd PROJECT-DIR
              PROJET-SPECIFIC-NAMESPACE-ENV-SETUP
              while (($#>0)) && [[ $1 == *=* ]];do
                      export "$1"
                      shift
              done
              if (($#>0));then
                      if [[ -x $(command -v "$1") ]];then
                              exec "$@"
                      else
                              "$@"
                      fi
              else
                      export PS1="[PROJECT] \$ "
                      exec bash
              fi
              

              It’s self contained and easy to modify and switch to an alternate one. Want a quick check of a project on a remote machine,

              ssh remote the-project-setup make check

              1. 1

                My dotfiles https://adi.onl/.*.html

                I usually just send CLI parameters PARAM1=foo PARAM2=bar program or create launcher scripts with the params preset

                    cat <<EOF > bin/dev
                    #!/usr/bin/env sh
                    export DEV=1
                    LOG=1 bin/https -p 9000
                    EOF
                
            2. 7

              Direnv is the perfect companion to nix-shell and nix develop and has native support for shell.nix with nix-direnv providing caching support as well as flakes integration. It also integrates well with emacs. In short, per-project nix-based “virtualenvs” activated on entry. Love it!

              1. 3

                I’ve been using direnv a lot since I discovered it…. great tool. I mostly use it to switch PASSWORDSTORE_DIR settings based on customer-directory I’m in, do stuff like automatically git-pull when entering project-directories etc. Can’t recommend it enough.

                1. 3

                  Is it possible to mess up a shell with this? Like putting something like ´MY_VAR=$(rm -r ~)’ ?

                  1. 10

                    Yes but you have to execute direnv allow every time the .envrc file changes, so it’s hard to just sneak it in.

                    1. 2

                      Yeah, there’s an allowlist based on the file path and (I assume a hash of) the contents:

                      sub-rosa
                      > cat .envrc 
                      use flake
                      > echo "FOO=bar" >> .envrc
                      direnv: error /home/vector/code/sub-rosa/.envrc is blocked. Run `direnv allow` to approve its content
                      
                      1. 3

                        That’s right. Both the file path and content are being hashed.

                  2. 3

                    Since I discovered this at work I started using it for side projects as well. It’s such a useful little tool to have, and it removes a lot of the pain of working with local state like Python virtualenv etc. Together with Nix, it’s really a bit of secret weapon because you can install a complete development environment which doesn’t pollute your profile, without mucking about with Docker-compose or other complex nonsense like that. And, the tools are available from your local shell.

                    1. 1

                      It’s a really really useful tool. Per-directory environment variables are great for complex projects, and I have emacs configured to load the config as well, so it also applies to any buffers I have open.