Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson/add options argument #31

Merged
merged 2 commits into from
Mar 2, 2024
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
6 changes: 6 additions & 0 deletions lessons/options-argument/config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{...}: {
config = {
name = "Boaty McBoatface";
origin = "England";
};
}
11 changes: 11 additions & 0 deletions lessons/options-argument/eval.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{pkgs}:
(
pkgs.lib.evalModules {
modules = [
./options.nix
./config.nix
];
}
)
.config
.greeting
35 changes: 35 additions & 0 deletions lessons/options-argument/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Options argument

There is another attribute that is automatically provided by the module system, `options`.
`options` provides information about all of the option declarations in the module.
It has much more information than `config` which only gives you the final value.
It can tell you what the type is, what the default value is, whether or not it is defined, and many more things.

!!! note
This is the `options` function argument attribute which is not to be confused by the `options` attribute where we declare options.

Let us take a look at using the information provided by `options` to create conditional logic.
In our `options.nix` file, we have the same option declarations that we have seen in previous lessons.
However, we have change how `greeting` is defined.
Now the definition is conditionally generated depending on which options we define.
If we set a value for `name`, then we will get an additional line in our output.
If we do not, then that line is completely omitted.

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

In our `config.nix`, we define values for `name` and `origin`, but not `title`.
This is fine because use of the title definition is contingent on whether or not it is defined—remember nix is *lazy*!

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

Setup an `eval.nix` to evaluate our modules and return the `config.greeting` attribute.

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

Create a `run.sh` run script to evaluate the `eval.nix` file.

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

And if we run the script (`./run.sh`), we have our configuration.

[//]: # (self.eval)
38 changes: 38 additions & 0 deletions lessons/options-argument/options.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
lib,
options,
config,
...
}: let
cfg = config;
opts = options;
in {
options = {
name = lib.mkOption {
type = lib.types.str;
};
title = lib.mkOption {
type = lib.types.str;
};
origin = lib.mkOption {
type = lib.types.str;
};
greeting = lib.mkOption {
type = lib.types.str;
};
};

config = {
greeting =
lib.concatStringsSep
"\n"
(
[
"Hello"
]
++ (lib.optional opts.name.isDefined "My name is ${cfg.name}.")
++ (lib.optional opts.title.isDefined "I am a ${cfg.title}.")
++ (lib.optional opts.origin.isDefined "I am from ${cfg.origin}.")
);
};
}
3 changes: 3 additions & 0 deletions lessons/options-argument/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nix eval -f eval.nix \
--apply 'x: x {pkgs = import <nixpkgs> {};}' \
--json | nix run nixpkgs#jq -- -r
1 change: 1 addition & 0 deletions site/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ nav:
- lessons/a-basic-module/lesson.md
- lessons/option-dependencies/lesson.md
- lessons/splitting-modules/lesson.md
- lessons/options-argument/lesson.md
- lessons/extra-arguments/lesson.md
- mkOption:
- lessons/default-values/lesson.md
Expand Down
Loading