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.


๐Ÿ“‹ Table of Contents

  1. Using the Nix REPL
  2. Testing Code in Files (nix eval & nix-instantiate)
  3. Playing Online: Nix Playground
  4. Building Derivations and Packages
  5. Testing Nix Shells and Environments
  6. Pro Tips
  7. Summary Table
  8. Glossary
  9. Need More Help?

1. Using the Nix REPL ๐Ÿ’ป

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

2. Testing Code in Files ๐Ÿ“‚

For anything more than a quick one-liner (functions, sets, derivations), write it in a .nix file and evaluate it.

A. With nix-instantiate

  1. Create test.nix:

    let x = 10; in x * 2
  2. Run:

    nix-instantiate --eval test.nix
    # Output: 20

    Add --json for JSON output, or --strict for strict evaluation.

B. With nix eval (Preferred for Nix 2.x and newer)

  • Evaluate a file:

    nix eval --file test.nix
  • Run a one-liner:

    nix eval --expr 'let a = 5; in a * 3'
    # Output: 15

3. Playing Online: Nix Playground ๐ŸŒ

No install neededโ€”perfect for beginners or sharing code!


4. Building Derivations and Packages ๐Ÿ—๏ธ

If your tutorial uses derivation or stdenv.mkDerivation:

  1. Create hello.nix:

    derivation {
      name = "hello";
      system = builtins.currentSystem;
      builder = "/bin/sh";
      args = [ "-c" "echo Hello > $out" ];
    }
  2. Build:

    nix-build hello.nix
    # Symlink `result` points to the build output
  3. Check output:

    cat result
    # Should print "Hello"

5. Testing Nix Shells and Environments ๐Ÿš

For shell.nix or flake.nix environments:

  1. Create shell.nix:

    { pkgs ? import <nixpkgs> {} }:
    pkgs.mkShell {
      buildInputs = [ pkgs.curl pkgs.git ];
    }
  2. Enter the shell:

    nix-shell
    # curl and git are now available in your shell!

6. Pro Tips ๐Ÿ’ก

  • 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.

7. Summary Table ๐Ÿ“Š

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

8. Glossary ๐Ÿ“š

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)

9. Need More Help? โ“

  • 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

โš ๏ธ **GitHub.com Fallback** โš ๏ธ