Skip to content

About

Define extensible Nix module systems for specific use-cases ✨

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

24 Commits

Folders and files

Repository files navigation

Nix Kit System - Define extensible Nix module systems for specific use-cases

Nix modules sytems as first-class citizen! 🚀

You define a Kit, and evaluate Kit modules into a Kit Config.

This flake exposes kitsys, a thin wrapper around lib.evalModules to give it a name, metadata, and help standardize its uses.

Note

This flake was extracted from my dotfiles repo at (bew/dotfiles#68d297fad), where it backs my toolkit module systems for nvim, zsh, tmux, …

AI disclosure:
The initial implementation was entirely written by hand (AI:0). The extraction as a flake from my dotfiles repo, and the API rework since then, were AI-assisted (AI:W).

Motivation

The Kit system was driven by these goals:

  • A solid base for many module systems: every use-case (NixOS, NixVim, a zsh toolkit, …) needs the same scaffolding — an eval entry point, a lib option, assertions and warnings. kitsys supplies that scaffolding, so a kit only declares what is specific to it.
  • A standard way to create & share module systems: a kit is a named, class-constrained module system with a stable eval entry point, so a module system defined once can be exposed as a flake output (kits.<name>) and consumed by other flakes.
  • Easy way to extend/refine a config after evaluation: a Kit Config carries extendWith, which re-evaluates the same kit with an extra module and returns a new config. These extensions can chain, and the extra module can read the config values from the previous eval through the prevConfig module arg.

Concepts

A Kit is a thin wrapper around a Nix Module System for a specific use-case. It adds the kit's name and metadata, makes the module system easy to share, and evaluates it with extension support.

A Kit Config is the result of evaluating a Kit: it holds the resolved option values, and can be refined/extended afterwards.

A Kit module is basically a Nix module, with the same class (or none) as a specific Kit. For example any NixOS module that have class = "nixos" (or none) can be a Kit module for the NixOS Kit.

A Kit should stay system-agnostic: its definition must not depend on any particular system, so the same kit instance can be shared everywhere it is exposed. Anything system-specific — typically pkgs — belongs to an evaluation: pass it to eval as pkgs, never bake it into the kit's baseModules. eval's pkgs also provides lib (as pkgs.lib) when no other lib is set.

Quick start

Add the flake as an input:

# flake.nix
inputs.kit-system.url = "github:bew/nix-kit-system";

Defining a Kit and evaluating a Kit module for that Kit are separate steps.

The snippets below assume kitsys = inputs.kit-system.lib.kitsys, and that pkgs is the pkgs of the system being evaluated.

1. Define a kit:

mykit = kitsys.newKit {
  meta.name = "My kit for simple greetings";
  class = "mykit";
  baseModules = [ ./base.mykit-module.nix ];
};

2. Evaluate a module for a kit:

myConfig = mykit.eval {
  pkgs = pkgs; # system-specific inputs belong here, at eval time (`lib` then defaults to `pkgs.lib`)
  # or if the kit doesn't need `pkgs`, pass `lib` directly:
  # lib = inputs.nixpkgs.lib;

  configModule = ./path/to/your/bew-config.mykit-module.nix;
  # or in attrset form:
  # configModule = { /* your option values */ };
};

3. Extend an evaluated kit config when needed:

myCustomConfig = myConfig.extendWith ({ lib, prevConfig, ... }: {
  /* override or add options; `prevConfig` holds the config values from the previous eval */
});

Exposing Kits & Kit configs on your flake

A flake that defines a Kit, or evaluates a Kit Config, can expose them for other flakes to consume:

  • kits.<kitName> — a built Kit, ready to eval.
  • kitConfigs.<kitName>.<configName> — an evaluated Kit Config, ready to use (usually system-specific). It can also be a function taking {system, ...}: as input, to make a Kit Config system-agnostic.

A Kit being system-agnostic means it is exposed once, not under forAllSystems. Only the Kit Configs that need pkgs are per-system, since it is their eval call that supplies it.

{
  outputs = { kit-system, nixpkgs, ... }: let
    kitsys = kit-system.lib.kitsys;

    mykit = kitsys.newKit {
      meta.name = "My kit for simple greetings";
      class = "mykit";
      baseModules = [ ./my-base.nix ];
    };
  in {
    kits = { inherit mykit; };

    kitConfigs.mykit.bew = mykit.eval {
      lib = nixpkgs.lib;
      configModule = ./bew-config.mykit-module.nix;
    };

    # Example system-agnostic Kit Config
    kitConfigs.mykit.minimal = {system, ...}: mykit.eval {
      pkgs = nixpkgs.legacyPackages.${system};
      configModule = ./bew-config.mykit-module.nix;
    };
  };
}

Note

The system-agnostic form is a {system, ...}: … instead of system: … to potentially support more arguments in the future without breaking the interface. (or make it easily configurable by requiring other args, …)

Sample Kits

This flake also ships sample Kits, exposed as sampleKits.<name>. They are here as examples, not officially supported 😉.

A sample Kit is not ready to use: this flake deliberately keeps nixpkgs, home-manager or other dependencies out of its own inputs to really stay dependency-free, so sampleKits.<name> is a function you call with those inputs to get an actual Kit back in your flake / Nix expression:

outputs = { kit-system, nixpkgs, home-manager, ... }: let
  # Fill in the dependencies of the sample kit
  homekit = kit-system.sampleKits.homekit { inherit nixpkgs home-manager; };
in {
  # Use `homekit` how you want here 🤷
  kitConfigs.homekit.myHome = homekit.eval {
    inherit pkgs;
    configModule = ./my-config.homekit-module.nix;
  };
};

Writing Kit modules

A Kit is normally paired with several kinds of Kit modules:

  • its base module, which declares the kit's options and base configs
  • potential generic modules, which add options/configs any kit can reuse
  • config modules, the toplevel entrypoint for a Kit evaluation, yielding a Kit Config.
  • any other module written for that kit

All of them are ordinary Nix modules: they may import further modules, declare options, set values, and they all receive the module args the Kit injects.

A Kit module may declare its nominal module class with a top-level _class = "<class>";. When the Kit definition sets class, evalModules rejects a module whose _class differs.

Writing a base module

The kit's base module is usually named base.<thing>kit-module.nix or <thing>-base.kit-module.nix.

Example file names (these are suggestions):

  • base.wrapkit-module.nix, the base module for wrapkit
  • base.toolkit-module.nix, the base module for toolkit
  • zshkit-base.toolkit-module.nix, for a base module derived from another kit module

A base module declares the kit's options and implements their defaults. Such module is passed in the baseModules list of kitsys.newKit.

Example base module:

{ config, lib, ... }:
let
  cfg = config.mythingnamespace;
in {
  _class = "mykit";

  options = {
    mythingnamespace.thing = lib.mkOption {
      type = lib.types.str;
      default = "default-value";
    };
  };

  config = {
    # helpers the kit's configs may call, read back as `config.lib.myHelper`
    lib.myHelper = n: "thing is: ${cfg.thing}; given n + 1 is: ${n + 1}";

    # invariants belong in assertions (warnings are non-fatal)
    assertions = [
      {
        assertion = cfg.thing != "";
        message = "thing must not be empty";
      }
    ];
  };
}

Writing a generic module

A generic module is usually named <topic>.kit-module.nix.

Example file names (these are suggestions):

  • dynpaths.kit-module.nix, a generic module about dynamic paths
  • dir.kit-module.nix, a generic module about describing directory content

A generic module adds options or configs any kit can reuse. Such module can be passed anywhere: baseModules (in newKit), moreModules (in eval), imports in any module.

Example generic module:

{ config, lib, ... }:
let
  cfg = config.greeting;
in {
  options = {
    greeting.subject = lib.mkOption {
      description = "The subject of a greeting";
      type = lib.types.str;
      default = "world";
    };
  };

  config = {
    lib.sayGreeting = "hello, ${cfg.subject}!";
  };
}

Writing a config module or a module for a kit

A config module for a kit is usually named <what>-config.<thing>kit-module.nix, for a toplevel entrypoint of a Kit. Any other module for a kit is usually named <what>.<thing>kit-module.nix.

Example file names (these are suggestions):

  • minimal-config.zshkit-module.nix, a config module for zshkit
  • bew-config.zshkit-module.nix, a config module for zshkit
  • languages-support.nvimkit-module.nix, a module for nvimkit

A config module or module can do anything for its needs 🤷. Such module is passed as configModule to the eval function, or in any imports.

Example module:

{
  _class = "mykit";
  mythingnamespace.thing = "configured-value";
}

Defining complex Kits

A kit may need more than the default lib.evalModules setup, and these newKit args cover that:

  • customEval replaces the evalModules call each eval runs — e.g. to reuse home-manager's module system.
  • exportedToplevelFields adds extra top-level fields to each Kit Config, potentially derived from the evaluated config.

For reference, the examples/ flake builds a complete home package based on a Kit wrapping home-manager's modules (defined in sampleKits/homekit.nix).

Here is how home-manager could define a Kit for its module system:

kitsys.newKit {
  class = "homeManager";
  lib = hmLib; # home-manager's extended lib (built from the `nixpkgs` input)

  # no base modules of its own: its module system lives in the home-manager repo
  baseModules = [ ];

  # home-manager declares `lib`, `assertions` & `warnings` itself
  declareLibOption = false;
  declareAssertWarnOptions = false;

  # Reuse home-manager's module system instead of plain `evalModules`.
  # `pkgs` comes from the eval args, so the kit stays system-agnostic:
  # one kit serves every system, each `eval` call passes `pkgs`.
  customEval = args@{ lib, pkgs, class, modules, ... }:
    lib.evalModules (/* … uses pkgs, hmModules, sets modulesPath … */);

  # Expose home-manager's activation package on the Kit Config result
  # (the function gets the exact result of `customEval` above)
  exportedToplevelFields = { config, ... }: {
    inherit (config.home) activationPackage;
  };
}

Its flake also builds the resulting home: nix build ./examples#kitConfigs.homekit.example-home.activationPackage. The kit itself is system-agnostic (it defers pkgs to eval), so the flake exposes kits.homekit once, not per system.

customEval must return the raw evalModules result, or at least expose config, options and extendModules. exportedToplevelFields receives that same raw result and returns the fields to add. kitsys re-derives them on every evaluation, including each extendWith, so they survive chaining, and it throws if a returned name collides with a top-level option or with extendWith.

How it works

Each mykit.eval args call runs the kit's customEval — lib.evalModules by default — over this module list, in order:

  1. the injected kitsys base modules
  2. the Kit definition's baseModules
  3. the caller's configModule & moreModules

A Kit injects these base modules by default:

  • the lib option
  • the assertions and warnings options
  • the _kitState option (unless disableExtensionSupport = true)

Every Kit Config also carries extendWith, attached as a plain attribute rather than an option, so it never clashes with a kit's own options or their types (such as a strictly-typed lib).

myConfig.extendWith module calls prevEval.extendModules on the recorded evaluation, bumping _kitState.nestingLevel and exposing the previous config to the added module as prevConfig.

With disableExtensionSupport = true, extendWith throws a clear error.

Note

Fun fact: I originally built the extendWith mechanism myself, before knowing that evalModules results can already be extended through extendModules 😅.

API reference

The flake exposes a single function: lib.kitsys.newKit.

kitsys.newKit args (the kit definition):

Arg Default Description
meta {} Arbitrary metadata about the kit, such as meta.name.
class required Nominal module class passed to evalModules; a module declaring a different _class is rejected.
baseModules required Modules each evaluation starts from, declaring the kit's own options.
lib null Kit-wide default lib for eval; a lib passed to eval takes priority.
declareLibOption true Set false if baseModules declare lib.
declareAssertWarnOptions true Set false if baseModules declare these options.
customEval wrapper over lib.evalModules Replaces the evalModules call each eval runs, to reuse another module system. Must return a raw evalModules result, or at least expose config, options and extendModules. See Defining complex Kits.
exportedToplevelFields _ : {} Function adding extra top-level fields to each Kit Config, derived from the eval result. See Defining complex Kits.
requirePkgs false Set true for a kit whose modules need pkgs: eval then throws unless a pkgs is passed.
disableExtensionSupport false Set true for a kit where extending doesn't make sense

Kit (returned by kitsys.newKit):

Field Type Description
meta attrset The definition's meta.
eval function The kit's evaluation function, derived from the definition.

mykit.eval args:

Arg Default Description
lib newKit's lib The lib the evaluation runs with (its evalModules, assertion/warning check, and option-default helpers). A value passed here takes priority over newKit's lib; pkgs.lib is the last resort. Required when neither pkgs nor newKit's lib is given.
pkgs null System-specific pkgs for this evaluation. Forwarded to the kit's customEval; the default evaluator also injects it as _module.args.pkgs. Provides lib (as pkgs.lib) when no other lib is set.
configModule required The module holding the option values to evaluate.
moreModules [] Further modules appended after configModule.
specialArgs {} Extra module args for the evaluation — the place for system-specific inputs such as pkgs.

Any other argument is forwarded to the kit's customEval (and from there to evalModules), which rejects arguments it does not know.

Kit Config (returned by mykit.eval / extendWith):

Attr Description
extendWith Extend the config with an extra module; returns a new config. Throws if the kit has disableExtensionSupport = true.
assertions / warnings Checked at every evaluation and extension.

Any extra top-level fields the kit's exportedToplevelFields returns (such as activationPackage) are present too. A _kitState attrset is also present, holding the kit's internal evaluation state; absent with disableExtensionSupport = true.

Development

Run the unit tests:

just test

Validate the flake outputs:

nix flake check

About

Define extensible Nix module systems for specific use-cases ✨

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages