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: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotenv_if_exists
use flake . --no-pure-eval
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
target-branch: "master"
schedule:
interval: "weekly"
commit-message:
prefix: "ci:"
85 changes: 85 additions & 0 deletions .github/workflows/qemu-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: "[QEMU] Build and Publish NixOS Image"

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

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

jobs:
build:
strategy:
fail-fast: false
matrix:
include:
- arch: x86_64
runner: ubuntu-24.04
boot_timeout: 180
ssh_timeout: 60
- arch: aarch64
runner: ubuntu-24.04-arm
# The ARM runner has no KVM, so QEMU boots the guest with TCG
# emulation, which needs much longer to reach the login prompt.
boot_timeout: 900
ssh_timeout: 180
runs-on: ${{ matrix.runner }}
permissions:
contents: read
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
unshare --map-root-user true
- 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 }}-
save: ${{ github.ref == 'refs/heads/main' }}
gc-max-store-size-linux: 4G
- name: Build image
run: nix build .#qemu-image --out-link result
- name: Check image, boot, and SSH login
env:
VM_ARCH: ${{ matrix.arch }}
VM_BOOT_TIMEOUT: ${{ matrix.boot_timeout }}
VM_SSH_TIMEOUT: ${{ matrix.ssh_timeout }}
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

publish:
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
needs: 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
- 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ flycheck_*.el
# Ignore build outputs from performing a nix-build or `nix build` command
result
result-*
/images/

# Ignore automatically generated direnv output
.direnv
Expand Down
67 changes: 67 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
HOST_OS ?= $(shell uname -s)
HOST_ARCH ?= $(shell uname -m)
VM_SSH_PORT ?= 2222
TEST_SSH_PORT ?= 2223
VM_MEMORY ?= 1024

ifneq ($(filter $(HOST_ARCH),arm64 aarch64),)
VM_ARCH := aarch64
else ifneq ($(filter $(HOST_ARCH),x86_64 amd64),)
VM_ARCH := x86_64
else
$(error Unsupported host architecture: $(HOST_ARCH))
endif

ifeq ($(HOST_OS),Darwin)
VM_IMAGE := images/nix-training-$(VM_ARCH).qcow2
else ifeq ($(HOST_OS),Linux)
VM_IMAGE := result/nix-training-$(VM_ARCH).qcow2
else
$(error Unsupported host OS: $(HOST_OS))
endif

.DEFAULT_GOAL := help
.PHONY: help image vm run test

help:
@printf '%s\n' \
'make image Build or download the $(VM_ARCH) qcow2 image' \
'make vm Build the NixOS VM launcher (Linux only)' \
'make run Boot the image in QEMU (SSH on localhost:$(VM_SSH_PORT))' \
'make test Check the image, boot, SSH login, and sudo access'

ifeq ($(HOST_OS),Darwin)
image: $(VM_IMAGE)

$(VM_IMAGE):
mkdir -p '$(@D)'
curl --fail --location --retry 3 \
'https://github.com/PerplexSystems/nix-training/releases/latest/download/$(@F)' \
--output '$@.tmp'
mv '$@.tmp' '$@'
else
image:
nix build .#qemu-image --out-link result
endif

vm:
ifeq ($(HOST_OS),Darwin)
@printf '%s\n' 'The NixOS VM launcher requires Linux; use make run on macOS.' >&2
@exit 1
else
nix build .#nixosConfigurations.training-vm$(if $(filter aarch64,$(VM_ARCH)),-aarch64).config.system.build.vm --out-link result-vm
endif

run: image
@printf '%s\n' 'VM login: gambi / gambi' 'SSH: ssh -p $(VM_SSH_PORT) gambi@127.0.0.1'
ifeq ($(HOST_OS),Darwin)
VM_ARCH='$(VM_ARCH)' VM_SSH_PORT='$(VM_SSH_PORT)' VM_MEMORY='$(VM_MEMORY)' \
bash scripts/run-vm.sh '$(VM_IMAGE)'
else
VM_ARCH='$(VM_ARCH)' VM_SSH_PORT='$(VM_SSH_PORT)' VM_MEMORY='$(VM_MEMORY)' \
nix develop .#ci --command bash scripts/run-vm.sh '$(VM_IMAGE)'
endif

test: image
VM_ARCH='$(VM_ARCH)' VM_SSH_PORT='$(TEST_SSH_PORT)' \
nix develop .#ci --command bash scripts/check-vm-image.sh '$(VM_IMAGE)'
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

🌐 Read this in other languages: [🇺🇸 English](README.md) | [🇧🇷 Português](README.pt-br.md)

Notes and tutorials for our Nix training, originally created for [Gambiconf
2026](https://gambiconf.dev/ "Gambiconf 2026").
Notes and tutorials for our Nix training, originally created for [Gambiconf 2026](https://gambiconf.dev/ "Gambiconf 2026").

## What is Nix?

Expand Down Expand Up @@ -49,5 +48,57 @@ Nix runs on Linux, macOS and even Windows [Through NixOS-WSL](https://github.com
- Nix manual pages are cited at version **2.34**.
- Try the examples in `nix repl` or a scratch directory as you go.

## Training VM

Download the latest boot-tested [x86_64 or aarch64 qcow2 image](https://github.com/PerplexSystems/nix-training/releases/latest) from GitHub Releases. The [QEMU image workflow](https://github.com/PerplexSystems/nix-training/actions/workflows/qemu-image.yml) also provides both artifacts for each successful run (retained for up to 90 days). The latest release is updated only after both builds pass the image check and boot to a login prompt.

To build the same image locally on an x86_64 Linux host (with Nix):

```sh
nix build .#qemu-image
```

The Makefile detects x86_64 or ARM64 hosts and provides the matching image, interactive QEMU boot, and automated boot, SSH, and sudo checks:

```sh
make image
make run
make test
```

On Linux, `make image` builds the matching NixOS image. On macOS, it downloads the matching image from the latest release; `make run` needs QEMU, while `make test` also needs Nix. `make run` prints the guest login (`gambi` / `gambi`), forwards SSH to `127.0.0.1:2222`, and discards guest changes on exit. `make test` uses port `2223`, so both can run at once. Override `VM_SSH_PORT`, `TEST_SSH_PORT`, or `VM_MEMORY` on the `make` command line if needed. QEMU boots the image with UEFI firmware on both architectures; set `VM_FIRMWARE` to a firmware file if QEMU cannot find `edk2-x86_64-code.fd` or `edk2-aarch64-code.fd` in its data directory. `make vm` builds the local VM launcher on Linux.

For quick local experiments with Nix installed, build the VM launcher from the same guest configuration:

```sh
make vm
QEMU_KERNEL_PARAMS=console=ttyS0 ./result-vm/bin/run-nix-training-vm -nographic
```

The launcher uses your host's Nix store and writes guest state to `nix-training.qcow2` in the current directory. The downloadable image includes its own Nix store.

Run a downloaded file directly with QEMU, or use `make run` to select the correct command. This example is for x86_64; for a local build, use `result/nix-training-x86_64.qcow2` as the drive path:

```sh
qemu-system-x86_64 -m 1024 -machine q35 -drive if=pflash,format=raw,readonly=on,file=/path/to/edk2-x86_64-code.fd -drive file=nix-training-x86_64.qcow2,format=qcow2,if=virtio -nic user,hostfwd=tcp:127.0.0.1:2222-:22 -nographic
```

Log in on the console as `gambi` with password `gambi`, or connect with `ssh -p 2222 gambi@127.0.0.1`. The guest enables `nix-command` and `flakes`, so commands such as `nix flake show <flake-ref>` work inside it (remote flake inputs need network access). You can also add this to `~/.ssh/config`:

```
Host gambiconf
Hostname 127.0.0.1
User gambi
Port 2222
UserKnownHostsFile=/dev/null
StrictHostKeyChecking=no
```

Then connect to the VM and enter `gambi` when SSH prompts for the password:

```
ssh gambiconf
```

---
Source: <https://nix.dev/manual/nix/2.34/introduction.html>
Loading
Loading