Enabling flakes within your NixOS configuration is the foundational step for modern, reproducible, and manageable system definitions. This architectural shift moves the entire system into a controlled, versioned environment, eliminating the historical dependency on the global package set for core configuration. The process integrates the declarative benefits of flakes directly into the host system, ensuring that every rebuild starts from a precisely defined source.
Understanding the Flake Integration
A NixOS flake treats the operating system itself as a derivable artifact, aligning application development practices with system administration. This paradigm links the kernel, services, and userland tools to a specific commit or archive, providing deterministic rollbacks and eliminating "works on my machine" scenarios. The integration requires modifying the host system's configuration to accept an external input, effectively making the flake the single source of truth for the entire stack.
Method One: Direct Configuration in configuration.nix
The most straightforward approach involves adding a specific option to the existing configuration.nix without creating a separate repository. This method is ideal for users transitioning from traditional imports who want to test the waters of flake-based system management. The configuration explicitly points NixOS to the remote repository and revision to use, pulling the definition into the local build environment seamlessly.
Configuration Code Example
Method Two: Standalone Flake with NixOS Modules
For advanced users, creating a dedicated flake that outputs a NixOS module provides the cleanest separation of concerns. This structure allows you to manage system configurations as libraries, making it easy to compose environments or share configurations across different machines. The flake outputs a NixOS module that consumes the standard pkgs and config arguments, ensuring full compatibility with existing modules.
Flake Structure Breakdown
The root flake.nix file defines inputs, specifying the exact source for NixOS. The outputs function then constructs the system by importing the NixOS module with the provided package set. This method leverages the power of flake locking, ensuring that the exact commit hash of nixpkgs is recorded in flake.lock . Consequently, every machine using this flake achieves bit-for-bit identical configurations.
Activating and Testing the Configuration
Once the configuration is in place, the standard NixOS commands apply, but with the added robustness of flake evaluation. Running nixos-rebuild switch triggers the flake resolution process, downloading the specified inputs if they are not already cached locally. This step verifies that the remote sources are accessible and that the module outputs are correctly interpreted by the host system.