Skip to content

Commit

Permalink
add lesson 1 a module
Browse files Browse the repository at this point in the history
  • Loading branch information
ibizaman committed Feb 9, 2024
1 parent 628ec75 commit 942a190
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lessons/001-a-module/eval.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let
pkgs = import <nixpkgs> {};

mymodule = {
imports = [
];
options = {
};
config = {
};
};
in
(
pkgs.lib.evalModules {
modules = [
mymodule
];
}
)
.config
39 changes: 39 additions & 0 deletions lessons/001-a-module/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# A Module

In this lesson, we will cover what a module is composed of.

In the [eval][eval] file, we have declared an attrset called `mymodule` which is composed of 3
fields: `imports`, `options` and `config`. This is a module with default values for those fields.
You can omit any of those fields and they will use those same default values.

[//]: # (./eval.nix)

If you execute the run file (`./run`), you will see printed the empty JSON object `{}`.

[eval]: ./eval.nix

## Module Fields

The `options` field lets you define variables that can be used in the `config` section.

The `config` field assigns values to options defined in other modules. We call this using an option.

The `imports` field lets you import other modules from a module.

We will see what goes in those fields in later lessons.

## Evaluating Modules

In the [eval][eval] file, we use a function called `evalModules`. This function takes an attrset as
argument which can contain the field named `modules`. In there, we can put as many module as we
want.

`evalModules` will then merge all `imports`, `options` and `config` fields of all given modules
following some rules we will see in the next lessons. It then produces an attrset whose only
interesting field - the result of merging all modules - is found in the `config` field. This is the
one we print when executing the `./run` file.

## Nixpkgs

The vast majority of nixpkgs is made out of modules, all merged together in the top-level
`evalModules`.
1 change: 1 addition & 0 deletions lessons/001-a-module/run
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nix eval -f eval.nix --json | nix run nixpkgs#jq -- .
2 changes: 2 additions & 0 deletions site/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edit_uri: tree/main/site
nav:
- Home: index.md
- Lessons:
- Getting Started:
- lessons/001-a-module/lesson.md
- Types:
- lessons/010-basic-types/lesson.md

Expand Down

0 comments on commit 942a190

Please sign in to comment.