Haumea Documentation

repository·main·Indexed 19 days ago

https://github.com/nix-community/haumea

A filesystem-based module system for Nix that maps directory structures into attribute sets. Haumea provides automatic imports, visibility rules via underscore prefixes, and hierarchical references using self, super, and root. It includes a flexible API for loading files via custom loaders and matchers (supporting Nix, JSON, TOML, and regex), as well as transformers like hoistAttrs and hoistLists to manipulate the resulting attribute tree.

Tokens
3.8K
Snippets
18
Records
31
Agent score
64%

What's inside Haumea

  1. What is Haumea?

    main
    Haumea is a tool designed to help users manage and interact with Nix flakes and Nix configurations more effectively. It provides a structured way to explore, manage, and potentially automate tasks related to Nix ecosystem components, specifically focusing on making Nix flake management more intuitive.
  2. Key features of Haumea

    main

    Haumea provides several core benefits for Nix developers:

    • Automatic Imports: Eliminates the need for manual import ./path/to/file.nix statements by automatically loading files into an attribute set.
    • Module Abstractions: Introduces self, super, and root to facilitate self-referencing, fixed points, and hierarchical access.
    • Organized Layout: The attribute set structure matches the filesystem layout by default.
    • Extensibility: Users can customize the loading process using a loader or manipulate the resulting tree using a transformer.
  3. What is Haumea?

    main

    Haumea is a filesystem-based module system for Nix. Unlike NixOS modules, Haumea functions more like the module systems found in traditional programming languages, providing support for file hierarchies and visibility.

    Its primary function is to map a directory of Nix files into a structured attribute set, where the directory structure is preserved in the resulting attribute tree.

  4. How Haumea maps files to attribute sets

    main

    Haumea automatically converts a directory tree of .nix files into a nested attribute set. By default, the resulting attribute set mirrors the file hierarchy.

    Files prefixed with __ (e.g., __internal.nix) or located in directories like _utils/ are handled according to visibility rules (though the specific mapping for these prefixes is part of the internal module logic), allowing you to organize code without manual imports.

    # Example mapping:
    # From:
    # ├─ foo/
    # │  ├─ bar.nix
    # │  ├─ baz.nix
    # │  └─ __internal.nix
    # ├─ bar.nix
    # └─ _utils/
    #    └─ foo.nix
    
    # To:
    {
      foo = {
        bar = <...>;
        baz = <...>;
      };
      bar = <...>;
    }
  5. How Matchers work in Haumea

    main

    Matchers allow Haumea to load non-Nix files by defining how to identify a file and how to parse it. Matchers are used with the loader option of the load function.

    How matching works

    When Haumea evaluates a file, it passes the filename to the matches function. The filename is processed by removing up to two preceding underscores (_). For example:

    • bar.nix becomes bar.nix
    • foo/__bar.nix becomes bar.nix

    How loading works

    The loader function in a matcher works identically to a standard loader function passed to load. It receives the standard context ({ self, super, root, ... }) and the Path to the file.

    Attribute naming convention

    When using matchers, the resulting attribute name in the loaded set is the filename with its last extension removed.

    • foo.nix $\rightarrow$ foo
    • bar/_foo.nix $\rightarrow$ foo
    • baz/foo $\rightarrow$ foo
    • far.bar.baz $\rightarrow$ far.bar
  6. Understand `self`, `super`, and `root` in loaded files

    main

    When a file loaded by load is a function, Haumea automatically passes three reserved arguments to it:

    • self: Represents the current file's own attribute set (the result of the function's own evaluation).
    • super: Represents the attribute set of the directory containing the current file.
    • root: Represents the entire attribute set produced by the load call at the src root.

    Example Usage

    If foo/bar.nix contains:

    { self, super, root }: {
      a = 42;
      b = self.a * 2;
    }
    • self.a will be 42.
    • self.b will be 84.
    • super will contain the other files in the foo/ directory (e.g., bar, baz, and __internal files).
    • root will contain the full structure of the src directory.
    {
      self, super, root
    }: {
      a = 42;
      b = self.a * 2;
    }
  7. Initialize a new project using the Haumea template

    main

    You can quickly scaffold a new Nix library project using the Haumea template. This generates a flake.nix and other necessary files in your current directory.

    To initialize in the current directory:

    nix flake init -t github:nix-community/haumea

    To initialize in a new directory:

    nix flake new <dir> -t github:nix-community/haumea
    nix flake init -t github:nix-community/haumea