Nix Tutorial 0 : Testing Nix Source Code from Tutorials - wimsio/universities GitHub Wiki
Learn how to try out Nix codeโinteractively, with files, online, and more! This guide is hands-on, practical, and works for any code sample you find.
- Using the Nix REPL
- Testing Code in Files (
nix eval
&nix-instantiate
) - Playing Online: Nix Playground
- Building Derivations and Packages
- Testing Nix Shells and Environments
- Pro Tips
- Summary Table
- Glossary
- Need More Help?
The Nix REPL is the fastest way to test Nix expressions, functions, and data.
-
Start the REPL:
nix repl
-
Try expressions:
nix-repl> let x = 2; in x + 3 5
-
Check types or builtins:
nix-repl> builtins.isList [1 2 3] true
Use the REPL for:
- Exploring syntax
- Quick checks
- Understanding built-in functions
For anything more than a quick one-liner (functions, sets, derivations), write it in a .nix
file and evaluate it.
-
Create
test.nix
:let x = 10; in x * 2
-
Run:
nix-instantiate --eval test.nix # Output: 20
Add
--json
for JSON output, or--strict
for strict evaluation.
-
Evaluate a file:
nix eval --file test.nix
-
Run a one-liner:
nix eval --expr 'let a = 5; in a * 3' # Output: 15
- Visit https://play.nix.dev/
- Paste code from any tutorial
- Click "Evaluate" and view results instantly
No install neededโperfect for beginners or sharing code!
If your tutorial uses derivation
or stdenv.mkDerivation
:
-
Create
hello.nix
:derivation { name = "hello"; system = builtins.currentSystem; builder = "/bin/sh"; args = [ "-c" "echo Hello > $out" ]; }
-
Build:
nix-build hello.nix # Symlink `result` points to the build output
-
Check output:
cat result # Should print "Hello"
For shell.nix
or flake.nix
environments:
-
Create
shell.nix
:{ pkgs ? import <nixpkgs> {} }: pkgs.mkShell { buildInputs = [ pkgs.curl pkgs.git ]; }
-
Enter the shell:
nix-shell # curl and git are now available in your shell!
- Run
nix repl '<nixpkgs>'
for full access to the Nix Packages library. - Use
import ./file.nix
to load other files in REPL or files. - Use Nix Pills for step-by-step guided practice.
- Try multi-line and
let ... in ...
everywhere, including in the REPL.
Task | Tool/Command | Best For |
---|---|---|
Quick checks / eval | nix repl |
Explore expressions |
Evaluate file | nix eval --file test.nix |
File-based code |
Run one-liner | nix eval --expr 'expr' |
Fast checks |
Build derivation | nix-build file.nix |
Build outputs |
Online playground | play.nix.dev | No local install needed |
Test dev env / shell |
nix-shell (with shell.nix ) |
Custom dev environments |
Term | Meaning |
---|---|
REPL | Interactive Read-Eval-Print Loopโtry out code live |
Derivation | A build recipe for the Nix store (result of derivation or mkDerivation) |
Nix Shell | Temporary environment with specific packages (using shell.nix ) |
nix-instantiate | Evaluates Nix expressions, mainly for legacy/testing |
nix eval | Modern tool for evaluating Nix expressions or files |
Nixpkgs | The official package set for Nix |
Flake | Standardized project definition file (experimental feature) |
- Try code at play.nix.dev.
- Have a tutorial snippet youโre stuck on? ๐ Paste it here and Iโll walk you through testing itโstep by step!
- See also: Nix Manual, NixOS Wiki