Nix 101 Course / Section 3: Dev shells: the productivity unlock
Premium

3.6 direnv: dev shells that load themselves

Lesson 23 of 68 · 6:23

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Installing direnv
  2. 1:12 Hooking direnv into the shell
  3. 2:25 Using .envrc to auto-load Nix shells
  4. 4:34 Making VS Code aware of direnv
Transcript

Installing direnv

0:00 Alright, so with and without Flakes we are able to define a package build, automatically derive the shell from that, stuff in any tool that we like, set environment variables inside the developer shell and what not. The thing is that we still need to run new nix shell or nix develop commands whenever we enter the project and of course this might be annoying if you have like 15 projects and always have to do nix shell here, nix develop there. It could just happen automatically, right? So I'm going to show you a tool that does exactly that for you and that is direnv which is pretty cool.

0:37 On the website it shows us how to use it and that is basically two steps. Need to install the package and then hook it into our shell. So let's hook it into the shell after we installed it. We can now say nix profile install nixpkgs#direnv which is kind of the nix profile way to install it. On a nixOS system you would put it into a config on Ubuntu system or whatever, you could also use the native package manager but why the native package manager, right?

1:10 Now direnv exists, we can run it, however it doesn't work like this by itself because

The full transcript comes with the lesson.

About this lesson

Typing nix-shell or nix develop in every project, every time, gets old fast. This lesson sets up direnv so the right development environment loads the moment you cd into a project folder and unloads when you leave. It works with flake and non-flake projects alike, and it reaches all the way into your editor.

In this video

  • Installing direnv with nix profile and hooking it into your shell configuration
  • The one-line .envrc file you commit to the repo, in its flake and non-flake variants
  • Why direnv refuses to run anything until you approve it, and how that protects you from untrusted repositories
  • Watching tools appear on entering the folder and disappear on leaving it
  • The VS Code extension that makes the editor pick up the environment too, so language servers and tools just work per project

After this lesson

You can check out any project in your organization, open it in your editor, and start working with the full toolchain loaded, without running a single setup command.