Skip to content
SeerLite edited this page Jul 31, 2022 · 8 revisions

GNU Guix is a functional package manager in the same vein as Nix.

Stdlib

Direnv includes Guix support in its stdlib:

# Usage: use_guix [...]
#
# Load environment variables from `guix environment`.
# Any arguments given will be passed to guix environment. For example,
# `use guix hello` would setup an environment with the dependencies of
# the hello package. To create an environment including hello, the
# `--ad-hoc` flag is used `use guix --ad-hoc hello`. Other options
# include `--load` which allows loading an environment from a
# file. For a full list of options, consult the documentation for the
# `guix environment` command.

Speeding Things Up

Normally use guix can be frustratingly slow to use. To cache the result of calling use guix, replace the stdlib's definition of use_guix by adding the following to ${XDG_CONFIG_HOME:-$HOME/.config}/direnv/direnvrc:

use_guix() {
  local cache_dir="$(direnv_layout_dir)/.guix-profile"
  if [[ -e "$cache_dir/etc/profile" ]]; then
    # shellcheck disable=SC1091
    source "$cache_dir/etc/profile"
  else
    mkdir -p "$(direnv_layout_dir)"
    eval "$(guix environment --root="$cache_dir" "$@" --search-paths)"
  fi
}

The results will be cached in a profile under ./.guix-profile to speed up subsequent loading. Remember to remove it when you make changes or need to upgrade packages!

Alternatively, have direnv use the newer guix shell which has the caching functionality builtin. See the section below.

Moving from guix environment to guix shell

Please note that Guix has deprecated guix environment in favor of guix shell. Per #888, Direnv will continue to use the older command for the time being. You can still use guix shell in your envrc in the meantime, just call the command explicitly, i.e. eval $(guix shell --search-paths ...).

Clone this wiki locally