Structuring NX Configuration

Jul 7, 2024

Structuring Your NX Configuration

Overview

  • Configuring NX can be daunting, no universal method.
  • Unique configurations for every setup.
  • Avoid putting everything in one file.

Simple Modular Configuration

  1. Basic setup with configuration, hardware config, and other modules in a single host config.
  2. Import modules relatively (hierarchical structure) within the same directory.
  • Suitable for single-machine management: No conditional logic needed, modules can be removed easily.

Complex Setup

  • Multiple Machines: Share modules in a separate directory in a parent directory.
  1. Import using relative paths (e.g., double dots to go up directories).
  2. Maintain relative positioning of modules.
  • Organization Challenge: Complex imports can get messy as configurations expand.

Grouping Modules by Purpose

  • Example: Group all desktop apps into a desktop_apps module.
  • Problem: Cannot selectively disable parts like Steam on a work machine.
  • Solution: Regrouping can get tedious; look for another method.

Making Modules Toggleable

  • Use an enable option to make modules toggleable.
  • **Steps to Implement:
  1. Wrap module options with a config attribute.
  2. Include options attribute in the file: Top part for defining options, bottom part for declaring.
  3. Include lib.config and lib.options for functions and values.
  4. Use lib.makeEnableOption to create Boolean options.
  5. Use lib.makeIf to conditionally enable modules.
  • Toggle modules with a single line in configuration.nix.
  • Bundle sub-modules in parent modules, allowing for conditional logic.
  • Declare which modules are enabled by default using lib.makeDefault.

Managing Default and Override Options

  • lib.makeOverride function: Assign priority to option values.
  • Lower values have higher priority.
  • Caution: Incorrect priorities can disable essential components.
  • Safe Use: lib.makeForce ensures safe enforcement of options.

Creating a Modular NX Flake

  • **Directory Structure:
  1. Top-level flake.nix file.
  2. host directory for hosts.
  3. nix-modules directory for shared modules.
  4. Create default module to import all others.
  • Import default module to host configurations.
  • Modular System: Enable modules per host using default modules.
  • Separate modules by preference—find a style that works.

Applying to Home Manager

  • Make Home Manager modules toggleable and grouped similarly.
  • Depending on installation method (Standalone or Module), follow specific steps to make Home Manager modular.
  • Using proper imports and parameters ensures modular Home Manager setup.

Conclusion

  • Experiment and get hands dirty to find the best configuration style.
  • Join Discord for community support, leave feedback and subscribe for more videos.