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
61 changes: 36 additions & 25 deletions .github/workflows/qemu-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,26 @@ concurrency:
cancel-in-progress: true

jobs:
prepare:
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
runs-on: ubuntu-24.04
permissions:
contents: write
outputs:
tag: ${{ steps.release.outputs.tag }}
steps:
- uses: actions/checkout@v6
- name: Create draft release
id: release
env:
GH_TOKEN: ${{ github.token }}
run: bash scripts/publish-vm-release.sh prepare

build:
needs: prepare
# PRs and non-main manual runs validate images without creating a release.
# The status check allows builds to proceed when prepare was skipped.
if: ${{ !cancelled() && (needs.prepare.result == 'success' || needs.prepare.result == 'skipped') }}
strategy:
fail-fast: false
matrix:
Expand All @@ -28,9 +47,11 @@ jobs:
ssh_timeout: 180
runs-on: ${{ matrix.runner }}
permissions:
contents: read
contents: write
steps:
- uses: actions/checkout@v6
- name: Test CI scripts
run: python3 -m unittest discover -s tests -p 'test_*.py' -v
- uses: cachix/install-nix-action@v31
- name: Enable unprivileged user namespaces
run: |
Expand All @@ -45,41 +66,31 @@ jobs:
gc-max-store-size-linux: 4G
- name: Build image
run: nix build .#qemu-image --out-link result
- name: Check image, boot, and SSH login
- name: Check image, offline exercises, and web rollback
env:
VM_ARCH: ${{ matrix.arch }}
VM_BOOT_TIMEOUT: ${{ matrix.boot_timeout }}
VM_SSH_TIMEOUT: ${{ matrix.ssh_timeout }}
VM_MEMORY: 2048
run: nix develop .#ci --command bash scripts/check-vm-image.sh result/nix-training-${{ matrix.arch }}.qcow2
- uses: actions/upload-artifact@v4
with:
name: nix-training-${{ matrix.arch }}
path: result/nix-training-${{ matrix.arch }}.qcow2
if-no-files-found: error
retention-days: 90
compression-level: 0
- name: Attach checked image to draft release
if: needs.prepare.result == 'success'
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ needs.prepare.outputs.tag }}
VM_ARCH: ${{ matrix.arch }}
run: bash scripts/publish-vm-release.sh upload "$RELEASE_TAG" "$VM_ARCH" "result/nix-training-$VM_ARCH.qcow2"

publish:
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
needs: build
if: needs.prepare.result == 'success'
needs: [prepare, build]
runs-on: ubuntu-24.04
permissions:
contents: write
actions: read
steps:
- uses: actions/download-artifact@v4
with:
pattern: nix-training-*
path: image
merge-multiple: true
- uses: actions/checkout@v6
- name: Publish latest working image
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release create "vm-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" \
image/nix-training-*.qcow2 \
--repo "$GITHUB_REPOSITORY" \
--target "$GITHUB_SHA" \
--title "Nix training QEMU image" \
--notes "Built and boot-tested from commit $GITHUB_SHA. Login: gambi / gambi. Nix flakes enabled." \
--latest
RELEASE_TAG: ${{ needs.prepare.outputs.tag }}
run: bash scripts/publish-vm-release.sh publish "$RELEASE_TAG"
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ result-*

# Ignore NixOS interactive test driver history
**/.nixos-test-history

# Writable development-environment state and rendered workspace pages
.devenv/
/04-shells/*/index.html

# Python regression-test bytecode
__pycache__/
55 changes: 55 additions & 0 deletions 00-getting-started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 00 · Getting started with a flake

**Goal:** build and run a package before unpacking the language behind it. Allow 10–15 minutes.

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.

## Run the example

From the repository root:

```sh
cd 00-getting-started
nix flake show
nix run
nix run -- --greeting="Hello, Gambiconf!"
nix build
./result/bin/hello
nix flake metadata
```

Expected: `nix flake show` lists a default package for each supported system; `nix run` prints `Hello, world!`; the next command prints your greeting. `nix build` creates a `result` symlink into `/nix/store`. Nix can download an existing output instead of compiling it.

Commands here run from this directory. The repository root has a separate flake for building training images and maintaining the course. Each exercise has its own flake and can be copied independently.

## Four things to recognize

| Part | Meaning |
|---|---|
| `inputs.nixpkgs` | Where the package collection comes from |
| `flake.lock` | The exact resolved dependency, including its revision and hash |
| `packages.<system>.default` | What `nix build` selects on your architecture |
| `result` | A symlink to the realised output, not the build recipe |

The URL names a moving channel, but the **supplied lock file pins its resolution**. Keep it; do not update it during the workshop. The exercises use the same nixpkgs source and revision to reuse downloads. A remote command such as `nix run nixpkgs#hello` does not automatically use this directory's lock.

`forAllSystems` repeats the package definition for x86_64 Linux, ARM64 Linux and Apple Silicon macOS. This nixpkgs pin no longer supports Intel macOS; use the x86_64 Linux training VM on an Intel Mac. Treat the loop as supplied scaffolding for now; [module 01](../01-nix-language/README.md) explains the language and [module 05](../05-flakes/README.md) explores the output structure.

## A small experiment

1. Run `nix build` again. Already available outputs are reused.
2. Change the argument to `--greeting`. This changes program execution, not the build inputs.
3. Inspect `result` with `readlink result`. Module 02 explains that path.

The default package here comes from nixpkgs. In module 03 you will write your own recipe and compile GNU Hello from source.

## Git and new files

Inside a Git repository, Nix normally sees tracked files, including their current edits. When adding a file, run `git add <file>` before referring to it from the flake. A commit is not required to try it. A dirty-tree warning is normal while experimenting.

**Next:** [01 · Nix language](../01-nix-language/README.md).

---
Sources:
- <https://nix.dev/concepts/flakes.html>
- <https://nix.dev/manual/nix/2.34/command-ref/new-cli/nix3-flake.html>
24 changes: 24 additions & 0 deletions 00-getting-started/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions 00-getting-started/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
description = "First steps: build and run a locked package";

inputs.nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst";

outputs =
{ nixpkgs, ... }:
let
forAllSystems = nixpkgs.lib.genAttrs [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
];
in
{
packages = forAllSystems (system: {
default = nixpkgs.legacyPackages.${system}.hello;
});
};
}
4 changes: 3 additions & 1 deletion 01-nix-language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The Nix language exists to create and compose **derivations**, which are precise

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

1. [types.md](types.md): the ten value types, and why paths are special
2. [syntax.md](syntax.md): literals, sets, `let`/`inherit`, functions, `if`/`assert`/`with`, and identifiers
3. [operators.md](operators.md): the precedence table, `//`, `?`, and `+` on paths and strings
Expand All @@ -30,7 +32,7 @@ x: x + 1 x: y: x + y { a, b ? 2, ... }@args: a + b
import ./foo.nix map (x: x * 2) [ 1 2 3 ]
```

Try everything interactively with `nix repl`.
Try expressions interactively with `nix repl`. From `00-getting-started/`, use `:lf .` to load the locked flake, then inspect `inputs.nixpkgs` and `outputs.packages`. Lookup paths such as `<nixpkgs>` are language reference material; the exercises resolve dependencies through flake inputs.

**Next:** [02 · The Nix Store](../02-nix-store/README.md). Once you know the language, see where its values end up.

Expand Down
14 changes: 14 additions & 0 deletions 02-nix-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ The Nix store is an abstraction for **immutable file system data that can depend

**Prerequisite:** [01 · The Nix Language](../01-nix-language/README.md)

## Inspect something you already built

From `00-getting-started/`:

```sh
nix build
readlink result
nix path-info --closure-size ./result
nix store ls ./result/bin
nix-store --query --references ./result
```

The result is immutable and references other store objects. The `nix-store` query is a low-level inspection tool; builds and dependency selection throughout this course use the flake CLI. Do not collect the workshop's preloaded dependencies while following the exercises.

## Reading order
The store model, bottom-up:
1. [file-system-objects.md](file-system-objects.md): files, directories, and symlinks; flat vs NAR hashing
Expand Down
23 changes: 23 additions & 0 deletions 03-derivations/00-raw-derivation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# A raw derivation · 5 minutes

From this directory:

```sh
nix derivation show
nix build -L
cat result
readlink result
```

Expected: a JSON description containing the builder, arguments and environment, followed by an output containing `Hello from a derivation!`. The output is a **file**, not a directory.

Open `greeting.nix`. `system` comes from the flake's per-system scaffold, and the builder is Bash in the store. Its builtin `printf` needs no external command or `PATH`. `GREETING` becomes an environment variable; Nix supplies `$out`.

Change `GREETING`, inspect `nix derivation show`, then build again. Both the derivation and output paths change. An unchanged build reuses its result.

**Troubleshoot:** replace the build command with `exit 1`, run `nix build -L`, and inspect the failure. Restore the command before continuing. Host paths such as `/bin/sh` are not a portable substitute for a declared builder in the store.

**Next:** [GNU Hello](../01-gnu-hello/README.md).

---
Source: <https://nix.dev/manual/nix/2.34/language/derivations.html>
24 changes: 24 additions & 0 deletions 03-derivations/00-raw-derivation/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions 03-derivations/00-raw-derivation/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
description = "A primitive derivation that writes a greeting";

inputs.nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst";

outputs =
{ nixpkgs, ... }:
let
forAllSystems = nixpkgs.lib.genAttrs [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
];
in
{
packages = forAllSystems (system: {
default = import ./greeting.nix {
inherit system;
inherit (nixpkgs.legacyPackages.${system}) bash;
};
});
};
}
12 changes: 12 additions & 0 deletions 03-derivations/00-raw-derivation/greeting.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ bash, system }:

builtins.derivation {
name = "greeting";
inherit system;
builder = "${bash}/bin/bash";
GREETING = "Hello from a derivation!";
args = [
"-c"
''printf '%s\n' "$GREETING" > "$out"''
];
}
40 changes: 40 additions & 0 deletions 03-derivations/01-gnu-hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Build GNU Hello · 10–15 minutes

This recipe is adapted from [Hausdorff Labs' GNU Hello example](https://github.com/hausdorff-labs/nixos-presentation/tree/main/examples/hello): the same version, source archive, hash and enabled checks, with a standalone flake supplying dependencies.

```sh
nix flake show
nix build -L
./result/bin/hello
nix run -- --greeting="Hello, Gambiconf!"
nix log .#hello
```

Expected: `Hello, world!`, then your greeting. The build logs show unpacking, configuring, compiling, checking and installing. Checks are enabled **inside `mkDerivation`**, not as a top-level flake output.

## Follow the dependency chain

`flake.nix` takes the locked nixpkgs package set and explicitly passes `stdenv` and `fetchurl` to `hello.nix`. That function returns a derivation. `stdenv` recognizes Hello's Autotools project, so no custom build phases are needed.

Look for `configure`, `make`, the test results and installation under `$out`. Running `nix build` again reuses the available output. Changing the runtime greeting does not change the derivation.

## Hash experiment

In `hello.nix`, temporarily replace `sha256` with `""`, then run `nix build -L`. The fetcher reports a mismatch and the hash it actually computed. Restore the supplied hash and build again. A cached archive with the correct hash is not evidence that arbitrary network access is available to normal builders.

## Enter the build environment

```sh
nix develop .#hello
type -a cc make
exit
```

This enters the package's build environment without running its complete build. It is different from installing Hello as a shell tool. Module 04 returns to this distinction.

**Next:** [build a static site](../02-static-site/README.md).

---
Sources:
- <https://github.com/hausdorff-labs/nixos-presentation/blob/main/examples/hello/hello.nix>
- <https://nix.dev/tutorials/packaging-existing-software.html>
24 changes: 24 additions & 0 deletions 03-derivations/01-gnu-hello/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading