# 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:
- First, realize its dependencies (inputDrvs).
- Clean the environment to retain only a known set of variables.
- Set
$outto the store path the build must create. - 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.