From f8db5170ecbcddb79ad71c51c92d4d85bf263739 Mon Sep 17 00:00:00 2001 From: Daniel Baker Date: Thu, 8 Feb 2024 08:01:42 -0800 Subject: [PATCH] Added a lesson on basic nixos module option types. --- lessons/010-basic-types/config.nix | 8 ++++++++ lessons/010-basic-types/eval.nix | 15 +++++++++++++++ lessons/010-basic-types/lesson.md | 18 ++++++++++++++++++ lessons/010-basic-types/options.nix | 22 ++++++++++++++++++++++ lessons/010-basic-types/run | 1 + 5 files changed, 64 insertions(+) create mode 100644 lessons/010-basic-types/config.nix create mode 100644 lessons/010-basic-types/eval.nix create mode 100644 lessons/010-basic-types/lesson.md create mode 100644 lessons/010-basic-types/options.nix create mode 100755 lessons/010-basic-types/run diff --git a/lessons/010-basic-types/config.nix b/lessons/010-basic-types/config.nix new file mode 100644 index 0000000..91233a9 --- /dev/null +++ b/lessons/010-basic-types/config.nix @@ -0,0 +1,8 @@ +{...}: { + config = { + exBool = true; + exInt = 42; + exEnum = "left"; + exStr = "hello"; + }; +} diff --git a/lessons/010-basic-types/eval.nix b/lessons/010-basic-types/eval.nix new file mode 100644 index 0000000..c625a86 --- /dev/null +++ b/lessons/010-basic-types/eval.nix @@ -0,0 +1,15 @@ +let + pkgs = import {}; + inherit (pkgs) lib; +in + #lib.generators.toPretty + #{} + ( + pkgs.lib.evalModules { + modules = [ + ./options.nix + ./config.nix + ]; + } + ) + .config diff --git a/lessons/010-basic-types/lesson.md b/lessons/010-basic-types/lesson.md new file mode 100644 index 0000000..1ba849d --- /dev/null +++ b/lessons/010-basic-types/lesson.md @@ -0,0 +1,18 @@ +# Basic Types + +In this lesson, we will cover some basic types. +There are certainly [many more types][nixos-manual-basic-types], but for this lesson, we will focus on just a few. + +Notice in the [options][options] file, we have declared boolean, enumeration, integer, and string options. + +[//]: # (./options.nix) + +Notice in the [config][config] file, we have declared values for all these options. + +[//]: # (./config.nix) + +If you execute the run file (`./run`), you should see an output that matches what we have configured. + +[nixos-manual-basic-types]: https://nixos.org/manual/nixos/stable/#sec-option-types +[options]: ./options.nix +[config]: ./config.nix diff --git a/lessons/010-basic-types/options.nix b/lessons/010-basic-types/options.nix new file mode 100644 index 0000000..c09b944 --- /dev/null +++ b/lessons/010-basic-types/options.nix @@ -0,0 +1,22 @@ +{lib, ...}: let + inherit (lib) types; +in { + options = { + exBool = lib.mkOption { + type = types.bool; + description = "My example boolean."; + }; + exEnum = lib.mkOption { + type = types.enum ["left" "right"]; + description = "My example enumeration."; + }; + exInt = lib.mkOption { + type = types.int; + description = "My example integer."; + }; + exStr = lib.mkOption { + type = types.str; + description = "My example string."; + }; + }; +} diff --git a/lessons/010-basic-types/run b/lessons/010-basic-types/run new file mode 100755 index 0000000..586fc86 --- /dev/null +++ b/lessons/010-basic-types/run @@ -0,0 +1 @@ +nix eval -f eval.nix --json | nix run nixpkgs#jq -- .