All Articles

Easy development environment with nix and direnv


Easy to maintain project dependencies and configuration with nix, without having a full nix install.


Install nix

Described here:

Simple solution:

curl -L | sh

Install direnv

Described here:

Configure nix shell

Add a directory named nix.

Configure the package source

Add the following source file with the name source.nix in the nix directory.

  nixpkgs = builtins.fetchGit {
    url = "";
    rev = "6ccc4a59c3f1b56d039d93da52696633e641bc71";

The rev value represents the commit you want to reference from the nix-pkg repo. You can find here the latest version:

Configure the nix shell

Add the configuration for the nix shell, in a file named shell.nix in the nix directory.

  sources = import ./sources.nix;
  pkgs = import sources.nixpkgs {};
pkgs.mkShell {
  buildInputs = [

Configure direnv to load the nix shell

In the folder of the project, where the nix directory is found add the direnv file, named .envrc with the following content:

#!/usr/bin/env bash

# Reload if the sources file changes
watch_file nix/sources.nix

# This will watch the shell.nix automatically and reload on changes
use nix nix/shell.nix


Once everything isconfigured, and direnv is enabled using direnv allow you can test your setup by running hello.

Published Feb 12, 2023

Software engineer.