Skip to content

Commit

Permalink
Move module evaluation discussion to a new lesson.
Browse files Browse the repository at this point in the history
  • Loading branch information
djacu committed Feb 12, 2024
1 parent fe3e9d9 commit 6bb802f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
47 changes: 2 additions & 45 deletions lessons/001-a-module/lesson.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,55 +102,12 @@ Option definitions are generally straight-forward bindings of values to option n
}
```

## Function Arguments

### FIXME: Talk about the automatically provided arguments

## Evaluation

### FIXME: Talk about evalModules

Note that our option declarations and option definitions do not need to exist in the same file.
When we evaluate our modules, we can simply include both files.
As long as every definition has a declaration, we can successfully evaluate our modules.
If there is an option definition that has not been declared, the module system will throw an error.

# ***OLD***

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
## Function Arguments

The vast majority of nixpkgs is made out of modules, all merged together in the top-level
`evalModules`.
### FIXME: Talk about the automatically provided arguments

[option-types-basic]: https://nixos.org/manual/nixos/stable/#sec-option-types-basic
File renamed without changes.
39 changes: 39 additions & 0 deletions lessons/module-evaluation/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Module Evaluation

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

[option-types-basic]: https://nixos.org/manual/nixos/stable/#sec-option-types-basic
File renamed without changes.
1 change: 1 addition & 0 deletions site/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ nav:
- Lessons:
- Getting Started:
- lessons/001-a-module/lesson.md
- lessons/module-evaluation/lesson.md
- Types:
- lessons/010-basic-types/lesson.md
- lessons/composed-types/lesson.md
Expand Down

0 comments on commit 6bb802f

Please sign in to comment.