Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
/ dendritic Public
forked from mightyiam/dendritic

A Nix flake-parts usage pattern in which every Nix file is a flake-parts module

License

Notifications You must be signed in to change notification settings

vic/dendritic

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

logo

The Dendritic Pattern

A Nixpkgs module system usage pattern.

See our annotated example.

Testimonials

I adore this idea by @mightyiam of every file is a flake parts module and I think I will adopt it everywhere.

—Daniel Firth (source)

Massive, very interesting!

—Pol Dellaiera (source)

I’ve adopted your method. Really preferring it.

—gerred (source)

Background

NixOS, home-manager and nix-darwin are popular projects that allow the user to produce derivations that can be customized by evaluating a Nixpkgs module system configuration.

Figuring out a practical and expressive architecture for a codebase that provides configurations had proven to cost many a Nix user numerous rounds of re-architecturing.

Factors contributing to the complexity of the matter:

  • Multiple configurations
  • Sharing of modules across configurations
  • Multiple configuration classes (nixos, home-manager, etc.)
  • Configuration nesting such as home-manager within NixOS or within nix-darwin
  • Cross-cutting concerns that span multiple configuration classes
  • Accessing values such as functions, constants and packages across files

The pattern

The dendritic pattern reconciles these factors using yet another application of the Nixpkgs module system—a top-level configuration. The top-level configuration facilitates the declaration and evaluation of lower-level configurations, such as NixOS, home-manager and nix-darwin.

Commonly, this top-level configuration is a flake-parts configuration, but it does not have to be. Alternatives to flake-parts may exist. Also, the module system can be used directly via (lib.evalModules).

In the dendritic pattern each and every Nix file is a module of the top-level configuration. In other words, each and every Nix file is a Nixpkgs module system module that is imported directly into the evaluation of the top-level configuration. Each and every Nix file also

  • implements a single feature
  • ...across all configurations that that feature applies to
  • is at a path that serves to name that feature

The pattern typically involves storing lower-level configurations and modules such as NixOS, home-manager and nix-darwin as option values in the top-level configuration. Pervasively, the lower-level module options are of the type deferredModule that is inlcuded in Nixpkgs. A primary benefit of this type to the pattern are its value merge semantics. Lower-level modules take part in the evaluation of any number of lower-level configurations. flake-parts includes an optional module for storing lower-level modules: flake-parts.modules.

Required skills

Real examples

See also

Community

Anti patterns

specialArgs pass-thru

In a non-dendritic pattern some Nix files may be modules that are lower-level (such as NixOS or home-manager). Often they require access to values that are defined outside of their config evaluation. Those values are often passed through to such evaluations via the specialArgs argument of lib.evalModules wrappers like lib.nixosSystem.

For example, scripts/foo.nix defines a script called script-foo which is then included in environment.systemPackages in nixos/laptop.nix. script-foo is made available in nixos/laptop.nix by injecting it (or a superset of it, such as the flake self may be) via specialArgs. This might occur even once deeper from the NixOS evaluation into a nested home-manager evaluation (this time via extraSpecialArgs).

In the dendritic pattern every file is a top-level module and can therefore add values to the top-level config. In turn, every file can also read from the top-level config. This makes the sharing of values between files seem trivial in comparison.

About

A Nix flake-parts usage pattern in which every Nix file is a flake-parts module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published