complete-computing-environment/direnv.org

111 lines
4.6 KiB
Org Mode

:PROPERTIES:
:ID: 45fc2a02-fcd0-40c6-a29e-897c0ee7b1c7
:ROAM_REFS: https://github.com/direnv/direnv/wiki/Nix https://direnv.net/
:END:
#+TITLE: direnv
#+filetags: :Project:CCE:
#+ARCOLOGY_KEY: cce/direnv
#+PROPERTY: header-args :mkdirp yes
#+PROPERTY: header-args:emacs-lisp :tangle direnv.el
#+ARROYO_EMACS_MODULE: direnv
#+ARROYO_MODULE_WANTS: cce/configure_packaging.org
#+ARCOLOGY_ALLOW_CRAWL: t
#+AUTO_TANGLE: t
#+begin_quote
Before each prompt, direnv checks for the existence of a .envrc file (and optionally a .env file) in the current and parent directories. If the file exists (and is authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to the current shell.
It supports hooks for all the common shells like bash, zsh, tcsh and fish. This allows project-specific environment variables without cluttering the ~/.profile file.
Because direnv is compiled into a single static executable, it is fast enough to be unnoticeable on each prompt. It is also language-agnostic and can be used to build solutions similar to rbenv, pyenv and phpenv.
#+end_quote
It also supports [[id:c75d20e6-8888-4c5a-ac97-5997e2f1c711][Nix]]-shell, though looking at the wiki page makes me want to consider using a different tool like [[https://github.com/nix-community/nix-direnv][nix-direnv]]:
#+begin_quote
Those who have installed and are using the Nix package manager often use its command-line tool nix-shell to enter into a Nix shell, which is just a Bash session with environment variables set up for a project. We can configure Direnv to load environment variables from the Nix shell for a project. Nix has support for lots of different programming languages, so once we get Direnv integrated with Nix, we can do all of our language-specific configuration with Nix. Our .envrc can be short and simple.
#+end_quote
Anyways it has an [[id:cce/emacs][Emacs]] package which works really well and refreshes the process environment when swapping buffers.
#+ARROYO_MODULE_WANTED: cce/run_hooks_after_init.org
#+begin_src emacs-lisp
;; (use-package direnv
;; :config
;; (direnv-mode))
(use-package envrc
:hook
(after-cce . envrc-global-mode))
(with-eval-after-load 'evil-leader
(evil-leader/set-key "e" #'envrc-command-map))
(provide 'cce/direnv)
#+end_src
#+ARROYO_HOME_MODULE: hm/direnv.nix
#+begin_src nix :tangle ~/arroyo-nix/hm/direnv.nix
{pkgs, ...}:
{
programs.direnv.enable = true;
programs.direnv.nix-direnv.enable = true;
}
#+end_src
#+ARROYO_NIXOS_MODULE: nixos/direnv.nix
#+ARROYO_SYSTEM_ROLE: endpoint
#+ARROYO_SYSTEM_ROLE: droid
#+begin_src nix :tangle ~/arroyo-nix/nixos/direnv.nix
{ ... }: {
nix.extraOptions = ''
keep-outputs = true
keep-derivations = true
'';
}
#+end_src
With =direnv= activated, any directory with a =shell.nix= in it and an =.envrc= file that says =use nix= in it can be allowed to spawn development environments:
#+begin_src shell
cat ~/org/arcology-fastapi/.envrc
#+end_src
#+results:
: use nix
* Direnv [[id:arroyo/system-cache][arroyo-db]] integration
:PROPERTIES:
:ID: 20230526T105534.711282
:END:
One of the issues of doing [[id:cce/literate_programming][Literate Programming with Org Babel]] is that if I work inside of my [[id:cce/org-roam][org-roam]] directory the direnv stuff won't map to the tangled directory. If I add a =ARROYO_DIRENV_DIR= file property to a document it will use the directory environment from that directory instead of the org-roam directory:
#+begin_src emacs-lisp :results none
(with-eval-after-load "arcology-localapi-commands"
(defun envrc--find-env-dir ()
"Return the relevant directory for the current buffer, or nil."
(let* ((buffer (or (buffer-base-buffer) (current-buffer)))
(mode (buffer-local-value 'major-mode buffer))
(file-name (buffer-file-name buffer))
(arroyo-maybe (when (and file-name
(equal (file-name-extension file-name) "org"))
(thread-last
file-name
(arcology-file-metadata)
(alist-get 'page)
(alist-get 'keywords)
(alist-get 'ARROYO_DIRENV_DIR))))
(buffer-directory
(if arroyo-maybe
(expand-file-name arroyo-maybe)
arroyo-maybe
(locate-dominating-file default-directory #'envrc--env-dir-p))))
buffer-directory)))
#+end_src
** NEXT make this work with new [[id:arcology/django/interfaces][Interfacing with the Arcology]]
thinking about a localhost query API too...