Contracts in Nix

Nix gets “contracts” to catch errors early — fans cheer, purists groan

TLDR: Nix adds “contracts,” simple checks that catch bad config data early and work with yants. The community split: purists say it’s not a real type system, while pragmatists and newcomers are excited to use it when it counts—plus bonus drama about unreadable fonts and a cheeky domain name.

Nix, the config tool beloved by power users, just dropped “contracts” — simple checks that act like types to catch bad data before it breaks stuff. Think: guardrails for your settings, not a full-blown math-y type system. The devs say it’s now compatible with yants, a helper library, and the demo shows it validating a JSON file to stop weird errors early. Non-nerd translation: Nix can now politely tell you “this looks wrong” before your build explodes.

Cue the drama. Purists rolled in hot: “Meh. Runtime assertions are a poor substitute” snarled one commenter, accusing the feature of pretending to be a real type system. Pragmatists clapped back: another user admitted Nix’s loose typing “rarely causes problems,” but said these occasional checks are perfect for messy inputs. Optimists were hyped: one user paused a DIY project due to the lack of types and now says, “I’ll give it a try.”

And because it’s the internet, chaos ensued. One commenter complained the site’s font is unreadable on NixOS + Firefox, which became its own subplot. Another discovered the domain name means “ass” in Russian, and the thread derailed into giggles.

Bottom line: Nix gets training wheels. The community? Split between “finally useful” and “not real types,” with a side of font flamewar and butt jokes. Nix | npm docs example

Key Points

  • A Nix library provides “contracts,” runtime validators to annotate legacy Nix code with type-like checks.
  • Contracts are now compatible with yants, improving ecosystem integration.
  • An example validates package.json fields using enum and setOf validators, simplifying subsequent logic by trusting validated data.
  • Validators (e.g., Url, not Null) can be composed; declare converts a validator into a named functor with check and name fields.
  • Contract-defined types interoperate with nixpkgs lib.types and mkOption via mkOptionType, enabling type enforcement in Nix options.

Hottest takes

“Runtime assertions are a poor substitute” — lmm
“As something you use occasionally… this could be really cool” — bjackman
“This site’s domain… literally means ass” — sermah
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.