How do you use the terms module and function?

:wave:
I’m curious in how you choose the term module or function when writing documentation in the general context of nix.

Do you see either term has having a restricted meaning bound by context? e.g. modules only making sense in the context of NixOS.
Or how do you employ either term?

my experience context

I recently learned how to factorize nix expressions by using modules/functions.
I am extending a flake that packages Rust applications.
As I read through the NixOS 23.11 section on Abstractions, I found myself confused about functions and modules.
Perhaps I am confused about overlapping semantics of nix, nixpkgs, NixOS.

1 Like

Hi @efx. Language lawyering is an integral part of the documentation business, so here we go! :wink:

The module system is not specific to NixOS, only most prominently used there. It’s indeed confusing that modules are also functions, and even more confusing that there is a notation where modules are not functions.

And, just in case, note that functions are primitives in the Nix language, while the module system is a library written in the Nix language, and thus modules are expressions that follow a particular pattern required by the module system.

The term “module” in the Nix ecosystem only make sense in the context of the module system. There is nothing else that is called that way. In practice we usually ignore that modules are also functions, because in a sense that’s an implementation detail.

This pattern denotes a module (fine print would show all the other ways to write a module):

{ config, lib, ... }:
{
  # ...
}

See Module system deep dive — nix.dev documentation for some of the detail and NixOS 24.05 manual | Nix & NixOS for some more.

This pattern denotes a function (fine print would show all the variants of specifying arguments):

arg: body

See Nix language basics — nix.dev documentation for excruciating detail (some of which should probably move to the reference manual…).

6 Likes

I laughed aloud. This is fantastic!

Thank you for the clarifying and helpful response. A reminder that functions are primitives has helped me better place how I learn about refactoring nix expressions. The links outline some places I can dig in to deepen my understanding.

Your affirmation of modules being complex underscores what I just read on nix.dev:

Building software is a complex undertaking, and Nix both exposes and allows managing this complexity with the Nix language.

An elucidating and helpful line!

To summarize, modules are a category of abstraction. In some ways, modules is shorthand for module system. Functions are primitives.

These terms and distinctions echo some themes in Tom Bereknyei’s talk “Units of Composition: recipes, overlays, and packages”

1 Like