Skip to content

Commit

Permalink
lesson/add composed types lesson (#13)
Browse files Browse the repository at this point in the history
* Added a trivial example.

* Renamed composite to composed.

* Added a non-trivial example.
  • Loading branch information
djacu authored Feb 11, 2024
1 parent 982071f commit 1d2bd12
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lessons/composed-types/config-nested.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{...}: {
config = {
exampleNested1 = [
"I can use strings, integers, or booleans."
42
true
];
exampleNested2 = {
foo = "bar";
baz = "qux";
dontuse = null;
fizz = null;
buzz = null;
};
exampleNested3 = {
singlePrimes = [2 3 5 7];
fibonacci = [1 1 2 3 5 8 13];
mixed = [(-1) 0 "one"];
};
exampleNested4 = {
foo = [null 1 2 3];
bar = {
fizz = "buzz";
baz = "qux";
};
};
};
}
18 changes: 18 additions & 0 deletions lessons/composed-types/config-trivial.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{...}: {
config = {
exampleListOf = [
1
10
100
1000
];
exampleAttrsOf = {
foo = "bar";
baz = "quz";
fizz = "buzz";
};
exampleNullOr = true;
exampleEither = "I could have been an integer.";
exampleOneOf = "I could have been an integer or boolean.";
};
}
10 changes: 10 additions & 0 deletions lessons/composed-types/eval-nested.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{pkgs}:
(
pkgs.lib.evalModules {
modules = [
./options-nested.nix
./config-nested.nix
];
}
)
.config
10 changes: 10 additions & 0 deletions lessons/composed-types/eval-trivial.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{pkgs}:
(
pkgs.lib.evalModules {
modules = [
./options-trivial.nix
./config-trivial.nix
];
}
)
.config
61 changes: 61 additions & 0 deletions lessons/composed-types/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Composed Types

In this lesson, we will cover some of the composed types similar to what we did in the Basic Types lesson.
There are certainly [many more composed types][nixos-manual-composed-types], but for this lesson, we will focus on just a few.

## Trivial Example

Similar to the Basic Types lesson, this first example is simple and almost too trivial.
Again, the point create simple declared options, a configuration that is correct, and check that our output is exactly as we expect.

In the `options-trivial.nix` file, we have declared multiple composed options.

[//]: # (./options-trivial.nix)

In the `config-trivial.nix` file, we have declared values for all these options.

[//]: # (./config-trivial.nix)

In the `eval-trivial.nix` file, we evaluate our options and config and have it return the config values.

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

In the `run-trivial.sh` file, we evaluate the eval file and have it print out a nicely formatted version of the configuration.

[//]: # (./run-trivial.sh)

If you execute the run file (`./run-trivial.sh`), you should see an output that matches what we have configured.

[//]: # (self.eval-trivial)

## Nested Example

Here we have a few examples with more heavily composed types.
In the `options-nested.nix` file, we have declared options with types as follows:

1. A list of any mix of strings, integers, and booleans.
1. An attribute set where the values can be either `null` or strings.
1. An attribute set where the values are lists of a mix of integers and strings.
1. An attribute set where the values are either:
- A list of a mix of `null` values and integers.
- An attribute set where the values are strings.

[//]: # (./options-nested.nix)

In the `config-nested.nix` file, we have declared values for all these options.

[//]: # (./config-nested.nix)

In the `eval-nested.nix` file, we evaluate our options and config and have it return the config values.

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

In the `run-nested.sh` file, we evaluate the eval file and have it print out a nicely formatted version of the configuration.

[//]: # (./run-nested.sh)

If you execute the run file (`./run-nested.sh`), you should see an output that matches what we have configured.

[//]: # (self.eval-nested)

[nixos-manual-composed-types]: https://nixos.org/manual/nixos/stable/#sec-option-types-composed
26 changes: 26 additions & 0 deletions lessons/composed-types/options-nested.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{lib, ...}: let
inherit (lib) types;
in {
options = {
exampleNested1 = lib.mkOption {
type = types.listOf (types.oneOf [types.str types.int types.bool]);
description = "My example composed types.";
};
exampleNested2 = lib.mkOption {
type = types.attrsOf (types.nullOr types.str);
description = "My example composed types.";
};
exampleNested3 = lib.mkOption {
type = types.attrsOf (types.listOf (types.either types.int types.str));
description = "My example composed types.";
};
exampleNested4 = lib.mkOption {
type = types.attrsOf (
types.either
(types.listOf (types.nullOr types.int))
(types.attrsOf types.str)
);
description = "My example composed types.";
};
};
}
26 changes: 26 additions & 0 deletions lessons/composed-types/options-trivial.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{lib, ...}: let
inherit (lib) types;
in {
options = {
exampleListOf = lib.mkOption {
type = types.listOf types.int;
description = "My example list of integers.";
};
exampleAttrsOf = lib.mkOption {
type = types.attrsOf types.str;
description = "My example attrs of strings.";
};
exampleNullOr = lib.mkOption {
type = types.nullOr types.bool;
description = "My example null or boolean.";
};
exampleEither = lib.mkOption {
type = types.either types.str types.int;
description = "My example either string or integer.";
};
exampleOneOf = lib.mkOption {
type = types.oneOf [types.str types.int types.bool];
description = "My example one of string, integer, or bool.";
};
};
}
1 change: 1 addition & 0 deletions lessons/composed-types/run-nested.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nix eval -f eval-nested.nix --apply 'x: x {pkgs = import <nixpkgs> {};}' --json | nix run nixpkgs#jq -- .
1 change: 1 addition & 0 deletions lessons/composed-types/run-trivial.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nix eval -f eval-trivial.nix --apply 'x: x {pkgs = import <nixpkgs> {};}' --json | nix run nixpkgs#jq -- .
1 change: 0 additions & 1 deletion lessons/composite-types/lesson.md

This file was deleted.

2 changes: 1 addition & 1 deletion site/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ nav:
- lessons/001-a-module/lesson.md
- Types:
- lessons/010-basic-types/lesson.md
- lessons/composite-types/lesson.md
- lessons/composed-types/lesson.md
- lessons/submodule-types/lesson.md
- Misc:
- lessons/default-behavior/lesson.md
Expand Down

1 comment on commit 1d2bd12

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.