Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
target-branch: "master"
target-branch: "main"
schedule:
interval: "weekly"
commit-message:
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/qemu-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ jobs:
- name: Restore and save Nix store
uses: nix-community/cache-nix-action@v7
with:
primary-key: nix-${{ matrix.arch }}-${{ hashFiles('flake.lock') }}
restore-prefixes-first-match: nix-${{ matrix.arch }}-
primary-key: nix-${{ matrix.arch }}-${{ hashFiles('flake.lock') }}-${{ github.sha }}
restore-prefixes-first-match: |
nix-${{ matrix.arch }}-${{ hashFiles('flake.lock') }}-
nix-${{ matrix.arch }}-
save: ${{ github.ref == 'refs/heads/main' }}
gc-max-store-size-linux: 4G
- name: Build all tutorials and integration tests
run: make tutorials
- name: Build image
run: nix build .#qemu-image --out-link result
- name: Check image, offline exercises, and web rollback
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/slides.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "[Slides] Build PDFs"

on:
push:
branches: [main]
pull_request:
workflow_dispatch:

concurrency:
group: slides-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: cachix/install-nix-action@v31
- name: Enable unprivileged user namespaces
run: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
- name: Restore and save Nix store
uses: nix-community/cache-nix-action@v7
with:
primary-key: slides-${{ hashFiles('flake.lock') }}-${{ github.sha }}
restore-prefixes-first-match: |
slides-${{ hashFiles('flake.lock') }}-
slides-
save: ${{ github.ref == 'refs/heads/main' }}
gc-max-store-size-linux: 2G
- name: Build and validate PDFs
run: make slides-check
- name: Link PDF artifacts
run: nix build .#slides --out-link result-slides
- name: Upload slides
uses: actions/upload-artifact@v6
with:
name: nix-training-slides
path: result-slides/*.pdf
if-no-files-found: error
18 changes: 16 additions & 2 deletions 00-getting-started/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# 00 · Getting started with a flake

**Goal:** build and run a package before unpacking the language behind it. Allow 10–15 minutes.
**Goal:** build and run a package before unpacking the language behind it. Allow
10 minutes in the core workshop; keep the Git note as post-workshop reference.

Use the [training VM](../README.md#training-vm), or an existing Nix installation with `nix-command` and `flakes` enabled. On another installation, add `experimental-features = nix-command flakes` to `~/.config/nix/nix.conf` (create the file if needed). The supplied VM already enables both.

## Words you'll see

> `flake.nix` is a file that declares inputs and outputs with a standard structure.
>
> — [nix.dev, Flakes](https://nix.dev/concepts/flakes.html)

- **flake**: a directory with a `flake.nix`. This directory is one.
- **input**: something the flake depends on. Here, the nixpkgs package collection.
- **output**: something the flake provides. Here, a package called `default`.
- **store path**: a read-only directory under `/nix/store` that holds a build result.

The [glossary](../GLOSSARY.md) has the official definitions. You don't need them yet: run the commands first.

## Run the example

From the repository root:
Expand Down Expand Up @@ -52,4 +66,4 @@ Inside a Git repository, Nix normally sees tracked files, including their curren
---
Sources:
- <https://nix.dev/concepts/flakes.html>
- <https://nix.dev/manual/nix/2.34/command-ref/new-cli/nix3-flake.html>
- <https://nix.dev/manual/nix/2.35/command-ref/new-cli/nix3-flake.html>
32 changes: 23 additions & 9 deletions 01-nix-language/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
# The Nix Language

The Nix language exists to create and compose **derivations**, which are precise descriptions of how to turn existing files into new ones. It is:
> The Nix language is designed for conveniently creating and composing derivations – precise descriptions of how contents of existing files are used to derive new files. It is a domain-specific, purely functional, lazily evaluated, dynamically typed programming language.
>
> — [nix.dev, Nix language basics](https://nix.dev/tutorials/nix-language)

- **Domain-specific:** it has path primitives, indented strings and interpolation, and string contexts for linking files.
- **Declarative:** there are no sequential steps. Dependencies flow only through data.
- **Pure:** values never change, and the same inputs give the same outputs.
- **Functional:** functions are ordinary values.
- **Lazy:** values are computed only when they are needed.
- **Dynamically typed:** type errors show up at evaluation time.
If you know JSON, you already know half of it. nix.dev puts it this way:

## Reading Order
> If you are familiar with JSON, imagine the Nix language as JSON with functions.

What each of those words means in practice:

- **Domain-specific:** built for describing files and builds. It has path values (`./site.json`), multi-line strings, and `${ }` interpolation.
- **Purely functional:** values never change once created, and functions have no side effects.
- **Lazily evaluated:** a value is computed only when something needs it.
- **Dynamically typed:** type mistakes show up when you evaluate the code, not before.

A *derivation* is a build recipe; see the [glossary](../GLOSSARY.md).

## Three-hour workshop

Use the [30-minute guided language lab](workshop.md). It teaches the constructs
needed to read the package recipes in module 03. The files below are reference
material and are not required during the timed workshop.

## Extended reading order

**Prerequisite:** [00 · Getting started](../00-getting-started/README.md). Keep its `flake.nix` open: `inputs` is an attribute set, `outputs` is a function, and `forAllSystems` applies another function to each supported system. You can use the supplied scaffold while learning these pieces.

Expand Down Expand Up @@ -38,5 +52,5 @@ Try expressions interactively with `nix repl`. From `00-getting-started/`, use `

---
Sources:
- <https://nix.dev/manual/nix/2.34/language/index.html>
- <https://nix.dev/manual/nix/2.35/language/index.html>
- For a gentler tutorial, see <https://nix.dev/tutorials/nix-language>
4 changes: 2 additions & 2 deletions 01-nix-language/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Every primitive lives in the global `builtins` set. A few are also bound at the top level. This note groups the most useful ones. The manual has the full list of ~120.

**Available without the `builtins.` prefix:** `derivation`, `import`, `map`, `toString`, `throw`, `abort`, `removeAttrs`, `baseNameOf`, `dirOf`, `isNull`, `fetchGit`, `fetchTarball`, `fetchTree`, `fromTOML`, `placeholder`, `scopedImport`, `break`, `true`, `false`, `null`.
**Available without the `builtins.` prefix:** `derivation`, `derivationStrict`, `abort`, `baseNameOf`, `break`, `dirOf`, `false`, `fetchGit`, `fetchMercurial`, `fetchTarball`, `fetchTree`, `fromTOML`, `import`, `isNull`, `map`, `null`, `placeholder`, `removeAttrs`, `scopedImport`, `throw`, `toString`, `true` (the list in the [2.35 manual](https://nix.dev/manual/nix/2.35/language/builtins.html)).

## By category

Expand Down Expand Up @@ -57,4 +57,4 @@ builtins.trace "evaluating x" 42 # prints trace, returns 4
- [../03-derivations/debugging.md](../03-derivations/debugging.md): `trace` and the debugger in practice

---
Source: <https://nix.dev/manual/nix/2.34/language/builtins.html>
Source: <https://nix.dev/manual/nix/2.35/language/builtins.html>
2 changes: 1 addition & 1 deletion 01-nix-language/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ Enable them with `extra-experimental-features = pipe-operators`.
- [strings.md](strings.md): what gets copied to the store

---
Source: <https://nix.dev/manual/nix/2.34/language/operators.html>
Source: <https://nix.dev/manual/nix/2.35/language/operators.html>
4 changes: 2 additions & 2 deletions 01-nix-language/scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ Syntax: `<identifier[/identifier…]>`

---
Sources:
- <https://nix.dev/manual/nix/2.34/language/scope.html>
- <https://nix.dev/manual/nix/2.34/language/constructs/lookup-path.html>
- <https://nix.dev/manual/nix/2.35/language/scope.html>
- <https://nix.dev/manual/nix/2.35/language/constructs/lookup-path.html>
6 changes: 3 additions & 3 deletions 01-nix-language/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ What an interpolated expression is allowed to evaluate to:
|---|---|
| string | itself |
| path | **copied to the store**. You get the store path: `"${./foo}"` → `"/nix/store/…-foo"` |
| derivation | the store path of its first output: `"${pkgs.hello}"` → `"/nix/store/…-hello-2.12.1"` |
| derivation | the store path of its first output: `"${pkgs.hello}"` -> `"/nix/store/<hash>-hello-<version>"` |
| set with `__toString` | `__toString self` (this takes precedence) |
| set with `outPath` | the value of `outPath` (this is how derivations and flake inputs work) |
| anything else | `error: cannot coerce a set to a string` |
Expand Down Expand Up @@ -53,5 +53,5 @@ Idiomatic Nix never touches context directly. It matters when you wonder *why* a

---
Sources:
- <https://nix.dev/manual/nix/2.34/language/string-interpolation.html>
- <https://nix.dev/manual/nix/2.34/language/string-context.html>
- <https://nix.dev/manual/nix/2.35/language/string-interpolation.html>
- <https://nix.dev/manual/nix/2.35/language/string-context.html>
4 changes: 2 additions & 2 deletions 01-nix-language/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ with pkgs; [ git curl ] # bring set attributes into scope

---
Sources:
- <https://nix.dev/manual/nix/2.34/language/syntax.html>
- <https://nix.dev/manual/nix/2.34/language/identifiers.html>
- <https://nix.dev/manual/nix/2.35/language/syntax.html>
- <https://nix.dev/manual/nix/2.35/language/identifiers.html>
2 changes: 1 addition & 1 deletion 01-nix-language/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ builtins.typeOf (x: x) # "lambda"
- [../02-nix-store/store-paths.md](../02-nix-store/store-paths.md): what a path becomes once it is copied to the store

---
Source: <https://nix.dev/manual/nix/2.34/language/types.html>
Source: <https://nix.dev/manual/nix/2.35/language/types.html>
178 changes: 178 additions & 0 deletions 01-nix-language/workshop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Read Nix Package Recipes - 30 Minutes

The goal is to read the package expressions used later in the workshop. Nix is
a small expression language; most apparent complexity comes from the packaging
and module libraries built with it.

Run the examples in `nix repl`. Use `:p` before an expression when laziness
hides nested values, and `:q` to exit.

## 0-5 Minutes: Values and Attribute Sets

> An attribute set is a collection of name-value-pairs, where names must be unique.
>
> — [nix.dev, Nix language basics](https://nix.dev/tutorials/nix-language#attribute-set)

Nix data is close to JSON, but lists use spaces and assignments end in
semicolons.

```nix
{
name = "training-site";
enabled = true;
topics = [ "language" "store" "packages" ];
server.port = 8080;
}
```

Try:

```nix
({ server.port = 8080; }).server.port
({ port = 8080; }).port or 80
({ port = 8080; }) ? port
```

An attribute set groups named values. A list is ordered. Neither is mutated.

## 5-9 Minutes: Local Names

> `let` expressions allow assigning names to values for repeated use.
>
> — [nix.dev, Nix language basics](https://nix.dev/tutorials/nix-language#let-in)

`let` introduces names for one expression. Definitions are not sequential.

```nix
let
greeting = "Hello";
audience = "Gambiconf";
in
{
message = "${greeting}, ${audience}!";
length = builtins.stringLength greeting;
}
```

`inherit` avoids repeating `name = name`:

```nix
let name = "training-site"; version = "1";
in { inherit name version; }
```

## 9-17 Minutes: Functions

> A function always takes exactly one argument. Argument and function body are separated by a colon (`:`).
>
> — [nix.dev, Nix language basics](https://nix.dev/tutorials/nix-language#functions)

Function application uses whitespace: `f x` calls `f` with `x`.

```nix
(x: x + 1) 2
(name: "Hello, ${name}!") "Gambiconf"
```

Packages normally take an attribute set so dependencies are named:

```nix
let
makeMessage = { greeting, audience ? "world" }:
"${greeting}, ${audience}!";
in
makeMessage { greeting = "Hello"; audience = "Nix"; }
```

Read the punctuation aloud:

- `{ greeting, audience ? "world" }:` declares a function.
- `{ greeting = "Hello"; audience = "Nix"; }` is its argument.
- `makeMessage { ... }` applies the function.

This is the shape used by `callPackage`: dependency names in a package
function are matched with names in the nixpkgs package set.

## 17-21 Minutes: Strings, Shell, and Paths

Indented strings are convenient for builder shell code:

```nix
let output = "index.html";
in ''
mkdir -p "$out"
printf '%s\n' "ready" > "$out/${output}"
''
```

`${output}` is Nix interpolation. `$out` is left for the builder's shell. A
path such as `./site.json` is a Nix path value, not the string
`"./site.json"`. Using a path as a derivation input makes its contents part of
the build description.

## 21-24 Minutes: Laziness

> The Nix language uses lazy evaluation, and `nix repl` by default only computes values when needed.
>
> — [nix.dev, Nix language basics](https://nix.dev/tutorials/nix-language#interactive-evaluation)

Nix evaluates values when they are demanded:

```nix
({ used = 1; broken = builtins.throw "not demanded"; }).used
```

This permits large package sets and recursive composition without evaluating
or building every package. Laziness does not mean a derivation builds during
ordinary language evaluation.

## 24-26 Minutes: Builtins and nixpkgs Libraries

> Nix comes with many functions that are built into the language. They are implemented in C++ as part of the Nix language interpreter.
>
> — [nix.dev, Nix language basics](https://nix.dev/tutorials/nix-language#builtins)

`builtins` is part of the Nix evaluator. `lib` and build helpers come from
nixpkgs.

```nix
builtins.map (x: x * 2) [ 1 2 3 ]
builtins.attrNames { jq = 1; nginx = 2; }
```

Later expressions use `pkgs.runCommand`, `pkgs.callPackage`, and
`pkgs.lib.genAttrs`. These are library functions, not language syntax.

## 26-30 Minutes: Read a Real Recipe

Open `../03-derivations/01-gnu-hello/hello.nix` and identify:

1. The outer function and its two named dependencies.
2. The call to `stdenv.mkDerivation`.
3. The attribute set passed to that function.
4. The fetcher call and its attribute-set argument.
5. Boolean, string, and interpolated-string values.

Then open that exercise's `flake.nix`. Do not unpack every output yet. Find
where nixpkgs is pinned, where a package set is selected for `system`, and where
the Hello function receives its dependencies.

Checkpoint: explain why `hello.nix` is a function that returns a derivation,
rather than a derivation that somehow imports its own dependencies.

## Reference Material

The full language notes cover features intentionally omitted from the timed
path:

- [Types](types.md)
- [Syntax](syntax.md)
- [Operators](operators.md)
- [Scope](scope.md)
- [Strings and context](strings.md)
- [Builtins](builtins.md)

---
Sources:
- <https://nix.dev/tutorials/nix-language>
- <https://nix.dev/manual/nix/2.35/language/index.html>
Loading
Loading