Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 1.46 KB

70-exercise-modules.org

File metadata and controls

55 lines (41 loc) · 1.46 KB

Building a module abstraction

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 }


{ 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 */
    };
  };
}