YOUR JOURNEY
17 / 18
03THINKING IN FLAKES 7 MIN READ
LESSON 17 / NIXOS MEETS FLAKES

One machine.
Locked together.

Expose a NixOS configuration as a named flake output.

Your existing modules still do their jobs. The flake chooses their Nixpkgs input and gives the assembled machine a name.

Explore the lesson
~$ Hands-on examples · Nix with flakes enabled.
THE FLAKE MODEL01 / 03
Drag to rotate

Keep the existing machine and hardware modules.

Locked Nixpkgs + existing modules
17.1WRAP THE EXISTING CONFIGURATION

Keep modules. Add an entry point.

On an existing NixOS machine, place flake.nix alongside configuration.nix. Keep all hardware, boot, filesystem, user, and stateVersion settings from that installation.

The output name workstation is chosen by you. It is the selector after # in the rebuild command; it does not have to be the same as networking.hostName. This example follows nixos-unstable; choose an appropriate maintained release branch if you want the stable release workflow.

flake.nix · alongside your existing configuration
{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  outputs = { nixpkgs, ... }: {
    nixosConfigurations.workstation = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [ ./configuration.nix ];
    };
  };
}
Next section: An explicit configuration to rebuild.
17.2SELECT THIS MACHINE

An explicit configuration to rebuild.

From that configuration directory, ensure newly referenced files are tracked if you use Git. Create and review the lock, then build before activating.

The switch command applies changes to the machine running it. Use the correct machine configuration, and commit both Nix files and the lock file after checking the result.

terminal · NixOS only
nix flake lock
sudo nixos-rebuild build --flake .#workstation
 
# After reviewing the build:
sudo nixos-rebuild switch --flake .#workstation
TAKE THIS WITH YOU

nixosConfigurations.<name> connects a locked input graph to an existing set of system modules.

UP NEXT

Put it all together

You can now follow the path from a Nix expression to a running system.

Next lesson