# Using direnv + guix to create environments / profiles

**URL:** <https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273>\
**Category:** Guix\
**Created:** [February 15, 2024, 12:46am UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273 "2024-02-15T00:46:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![moto](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@moto](https://forum.systemcrafters.net/u/moto)\
**Post date:** [February 15, 2024, 12:46am UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273/1 "2024-02-15T00:46:23Z")

</div>

I’ve been using `direnv` to manage guix profiles / environments, having found some resources on it and wanted to share:

- The [guix cookbook](https://guix.gnu.org/cookbook/en/html_node/Guix-environment-via-direnv.html) includes an example of how to use `direnv` though it seems out of date

- `use_guix` is in the direnv stdlib, and should just pass arguments directly to `guix shell`. Though the [direnv wiki](https://github.com/direnv/direnv/wiki/GNU-Guix) is out of date and says `guix environment`.

- The [direnv wiki](https://github.com/direnv/direnv/wiki/GNU-Guix) also provides a good example of how to use a “local cache” of the `guix shell` profile to speed up subsequent loadings once built. It relies on the `-r`/`--root=` option which creates a link to resulting profile at `FILE` and registers it as a garbage collector root. However, once the “root” is created, subsequent calls build the profile but fails at creating the link since it already exists.

This can at least be improved on I think by deleting the “cache directory”/“gc root” if the manifest is newer.

```bash
use_guix() {
  local cache_dir=".guix-profile"
  # delete =.guix-profile= if =manifest.scm= is newer
  # bash test builtin dereferences links, using =find=
  if [[-e $cache_dir && $(find manifest.scm -prune -newer $cache_dir)]]; then
    rm $cache_dir
  fi
  if [[-e "$cache_dir/etc/profile"]]; then
    source "$cache_dir/etc/profile"
  else
    echo "direnvrc: building $cache_dir"
    eval "$(guix shell -m manifest.scm -r "$cache_dir" "$@" --search-paths)"
  fi
}

```

But this presumes `manifest.scm` and not `guix.scm` and only rebuilds when the manifest changes and not if guix has changed. Nor does it account for `channels.scm` or do anything fancy with `guix time-machine`. Anyway, I’d be interested to hear about how other have approached this since there must be a better way of doing this even if it’s a separate guile script.

---

<div class="post-metadata">

**Author:** ![moto](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@moto](https://forum.systemcrafters.net/u/moto)\
**Post date:** [February 15, 2024, 12:54am UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273/2 "2024-02-15T00:54:18Z")

</div>

From #General to #Guix

---

<div class="post-metadata">

**Author:** ![ryuslash](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.systemcrafters.net/ryuslash/32/66_2.png) [@ryuslash](https://forum.systemcrafters.net/u/ryuslash)\
**Post date:** [February 16, 2024, 10:58pm UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273/3 "2024-02-16T22:58:42Z")

</div>

Sorry I have nothing to contribute here, I’ve been holding off on doing this because I haven’t used direnv before, but these are some great pointers. I’m going to have to look at all this to get this working soon, because it does seem great 😁

---

<div class="post-metadata">

**Author:** ![mirkoh](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@mirkoh](https://forum.systemcrafters.net/u/mirkoh)\
**Post date:** [February 16, 2024, 11:36pm UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273/4 "2024-02-16T23:36:14Z")

</div>

I’m following a similar approach for creating environments, using a manifest.scm, direnv and guix shell (guix shell uses manifest.scm by default).

```auto
eval $(guix shell --search-paths)

```

I haven’t had issues with the loading times (maybe because I don’t use guix gc often). I will try using the local cache approach, seems interesting.

---

<div class="post-metadata">

**Author:** ![Trisk3lion](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Trisk3lion](https://forum.systemcrafters.net/u/Trisk3lion)\
**Post date:** [March 20, 2024, 8:19pm UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273/5 "2024-03-20T20:19:26Z")

</div>

I’m mainly living in Emacs, and I like to use [buffer-env](https://github.com/astoff/buffer-env) to handle the activation of manifest.scm files when I enter the directory.

Thou direnv is a more general (and probably better) solution, but there is a charm with having it all in Emacs. 🙂

---

<div class="post-metadata">

**Author:** ![moto](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@moto](https://forum.systemcrafters.net/u/moto)\
**Post date:** [March 25, 2024, 4:56pm UTC](https://forum.systemcrafters.net/t/using-direnv-guix-to-create-environments-profiles/273/6 "2024-03-25T16:56:40Z")

</div>

I use [emacs-envrc](https://github.com/purcell/envrc) for emacs integration, but I didn’t realize that buffer-env has guix support built in, thanks for sharing it.
