← Retour au blog
tech 23 June 2026

nix-build in under 100 lines: Unveiling and Execution

Explore how the Nix build process, often seen as magical, can be simplified in under 100 lines of Go code. A journey through derivations and Nix's internal mechanics.

Article inspired by the original source
nix-build in under 100 lines ↗ fzakaria.com

# Introduction

Nix is often seen as a black box by those who use it. When you type nix build, a privileged process performs inscrutable actions through a Unix socket, and you end up with a path in /nix/store. But what if I told you that all this magic could be reduced to a simple execution?

Derivations: The Essence of Nix

A derivation, or .drv, is simply a build plan. Using Nix, you can instantiate a derivation as simple as possible. Let's take the example of a hello.nix file that creates a file with the text "Hello World".

``nix { name = "hello"; system = builtins.currentSystem; builder = "/bin/sh"; args = [ "-c" "echo 'Hello World' > $out" ]; } ``

By using nix-instantiate, we can transform this file into a readable JSON derivation.

The Four Steps of Realization

Realizing a derivation breaks down into four steps:

  1. First, realize its dependencies (inputDrvs).
  2. Clean the environment to retain only a known set of variables.
  3. Set $out to the store path the build must create.
  4. Execute the builder and check that it has produced $out.

Implementation in Go: Under 100 Lines

Here's how you could replicate the nix-build process using Go in under 100 lines.

```go package main

import ( "encoding/json" "fmt" "os" "os/exec" "strings" )

const store = "/nix/store"

// Structure for a derivation ``` [...]

Running this program will show you that what many consider magic is essentially a series of commands executed in the right order.

Conclusion

Reducing nix-build to a handful of lines of code shows us that complexity can often be simplified by better understanding it. If you want to see how Nix can transform your project, let's discuss it in 15 minutes.

Nix nix-build Go automation derivation
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call