Skip to content

Commit

Permalink
Add more detail to last section
Browse files Browse the repository at this point in the history
  • Loading branch information
spacekookie committed Dec 1, 2022
1 parent 4526021 commit 3ee0836
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 15 deletions.
2 changes: 1 addition & 1 deletion courses/day-04-cross/examples/manual-cross.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import <nixpkgs> {
twith import <nixpkgs> {
crossSystem.system = "aarch64-linux";
};

Expand Down
14 changes: 5 additions & 9 deletions courses/day-05-modules/10-module-basics.org
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,13 @@ Modules don't _exactly_ solve this problem, but they avoid it!

#+BEGIN_SRC nix
{
config = {
services = {
nginx = {
enable = true;
user = "www";
group = "uwu";
};
services = {
nginx = {
enable = true;
user = "www";
group = "uwu";
};
};

options = {};
}
#+END_SRC

Expand Down
4 changes: 2 additions & 2 deletions courses/day-05-modules/20-using-modules.org
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
❤ (theia) ~> sudo journalctl -u helloService.service
-- Logs begin at Sat 2020-10-24 16:54:30 CEST, end at Tue 2021-01-19 15:37:37 CET. --
Jan 19 15:36:38 uwu systemd[1]: Starting helloService.service...
Jan 19 15:36:38 uwu hello[1302]: Hello, nyantec!
Jan 19 15:36:38 uwu hello[1302]: Hello, $company_name!
Jan 19 15:36:38 uwu systemd[1]: helloService.service: Succeeded.
Jan 19 15:36:38 uwu systemd[1]: Finished helloService.service.
#+END_SRC
Expand All @@ -53,7 +53,7 @@
Create a user with a SSH key access.

The NixOS ~users~ module also ensures that users are created, and
retured (UIDs remains in-use!) when configuration is removed.
retired (UIDs remains in-use!) when configuration is removed.

#+BEGIN_SRC nix
{ config, ... }:
Expand Down
121 changes: 121 additions & 0 deletions courses/day-05-modules/60-advanced-types.org
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,125 @@
}
#+END_SRC

** Submodules

\\

+ Allow for staticly typed attribute sets
+ Remember the Nixlings examples - typed attribute sets could have
helped there!
+ Submodules also help generate documentation (in NixOS anyway)
+ Types are implemented as functions in ~lib~

** Three main submodule types

\\

+ *lib.types.submodule* - create basic submodule
+ *lib.types.listOf* - create a list of typed things
+ *lib.types.attrsOf* - create an attrset of typed things

**

#+BEGIN_SRC nix
{ lib, ... }:
{
options.services.cow-server = {
enable = lib.mkEnableOption "enable the cow server";
greeter = lib.mkOption {
type = lib.types.submodule {
options = { msg = lib.mkOption { type = lib.types.str; };
pkg = lib.mkOption { type = lib.types.package; }; };
};
};
};
}
#+END_SRC

** The listOf type

\\

+ Creates a typed list of things
+ Unlike ~[ ]~ this only allows _one type_ of thing
+ Useful for services that can be configured in multiples

**

#+BEGIN_SRC nix
{ lib, ... }:
with lib; # incule lib to cut down on boilerplate
{
options.services.cow-server.greeters = {
type = with types; listOf (submodule {
options = {
msg = lib.mkOption {
type = lib.mkOption { type = lib.types.str; };
pkg = lib.mkOption { type = lib.types.package; };
};
}
});
description = "Set of greeters to print messages for";
};
}
#+END_SRC

** Using ~listOf~ modules

#+BEGIN_SRC nix
{ pkgs, ... }:
let pkg = pkgs.my-custom-cowsay;
in
{
# ...
services.cow-server.greeters = [
{ msg = "Hello, Nixlings"; inherit pkg; }
{ msg = "Hello, World!"; inherit pkg; }
];
}
#+END_SRC

** The attrsOf type

\\

+ Creates a typed attribute set of things
+ Unlike ~{ }~ this only allows _one type_ of thing
+ _Very_ useful for services that can be configured in multiples

**

#+BEGIN_SRC nix
{ lib, ... }:
with lib; {
options.services.cow-server.greeters = {
type = with types; attrsOf (submodule {
options = { /* ... */ };
});
};
}
#+END_SRC

**

#+BEGIN_SRC nix
{ pkgs, ... }:
let pkg = pkgs.my-custom-cowsay;
in
{
# ...
services.cow-server.greeters = {
nixlings = { msg = "Hello, Nixlings"; inherit pkg; };
world = { msg = "Hello, World!"; inherit pkg; };
};
}
#+END_SRC

** You have used ~attrsOf~ already

\\

+ _Many_ modules in NixOS rely on this (for example nginx)
+

** [[./README.org][Back to index]]
55 changes: 55 additions & 0 deletions courses/day-05-modules/70-exercise-modules.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#+Title: Building a module abstraction
#+SETUPFILE: ../../reveal.setup

** home-manager

\\

+ https://github.com/nix-community/home-manager
+ ~nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager~
+ ~nix-channel --add https://github.com/nix-community/home-manager/archive/release-22.05.tar.gz home-manager~
+ ~nix-shell '<home-manager>' -A install~

** A lot of modules generate configuration

\\

+ Pick a random config file (in INI, json, or yml format) from /etc
on your computer
+ You can generate files in your home directory with home-manager
+ Create a custom module to generate it
+ ~lib.generators~ can help generate various file formats
+ ~lib.generators.toJSON { } { foo = 1; }~ results in ~{ "foo": 1 }~

**

\\

#+BEGIN_SRC nix
{ lib, config, ... }:
with lib; # incule lib to cut down on boilerplate
let cg = config.services.my-config-generator;
in
{
options.services.my-config-generator = {
enable = mkEnableOption "Enable config generator";

etcName = mkOption { type = types.path; };

default = {
type = types.listOf (submodule {
options = {
type = lib.mkOption { type = lib.str; };
};
});
};

config = lib.mkIf cg.enable {
environment.etc."${cg.etcName}".text = /* generate file here */
};
};
}
#+END_SRC

**

10 changes: 7 additions & 3 deletions courses/day-05-modules/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ Today we will cover the following subjects:
+ Advanced type shennigans


** Sections
** Slides

1. [[./10-module-basics.org][Module basics]]
2. [[./20-using-modules.org][Using modules]]
3. [[./30-exercise-configuration.org][(Exercise) Building your first NixOS system]]
4. [[./40-module-anatomy.org][Module anatomy]]
5. [[./60-advanced-types.org][Advanced types]]
6. [[./70-exercise-modules.org][(Exercise) Building a module abstraction]]

** Homework

3. [[./30-exercise-configuration.org][Building your first NixOS system]]
6. [[./70-exercise-modules.org][Building a module abstraction]]

0 comments on commit 3ee0836

Please sign in to comment.