-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lesson/add composed types lesson (#13)
* Added a trivial example. * Renamed composite to composed. * Added a non-trivial example.
- Loading branch information
Showing
11 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -- . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -- . |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1d2bd12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://nixos-modules.nix.xn--q9jyb4c as production
🚀 Deployed on https://65c8494f37cc55d6f1320d10--nixos-modules-lessons.netlify.app