skip to content
vvhybe
the blog

, a 1-minute read

Nix flakes, on one page

What a flake is, how to turn them on, what each nix command looks for in one, and the two mistakes everyone makes first.

A flake is a folder with a flake.nix at its root. It says what the project needs (its inputs) and what it gives (its outputs), and a flake.lock beside it pins every input to an exact revision. Same lock, same build, on any machine.

turn them on

On NixOS, in the system's configuration:

nix.settings.experimental-features = [ "nix-command" "flakes" ];

Anywhere else, in ~/.config/nix/nix.conf, or /etc/nix/nix.conf for the whole machine:

experimental-features = nix-command flakes

a first flake

mkdir hello && cd hello
nix flake init

nix flake init writes a flake.nix from the default template. A small one looks like this:

{
  description = "My first flake";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = { self, nixpkgs }:
    let pkgs = nixpkgs.legacyPackages.x86_64-linux;
    in {
      packages.x86_64-linux.default = pkgs.hello;
      devShells.x86_64-linux.default = pkgs.mkShell {
        packages = [ pkgs.nodejs pkgs.neovim ];
      };
    };
}

which command reads what

commandlooks for
nix buildpackages.<system>.default
nix runapps.<system>.default, else packages.<system>.default
nix developdevShells.<system>.default
nixos-rebuild switch --flake .#namenixosConfigurations.name
home-manager switch --flake .#namehomeConfigurations.name

<system> is the platform, written out: x86_64-linux, aarch64-darwin.

inputs, and the lock

Each input is a URL. Two versions of nixpkgs side by side are just two inputs:

inputs = {
  nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  nixpkgs-stable.url = "github:NixOS/nixpkgs/nixos-25.05";
};

The first time the flake is used, Nix writes flake.lock with the exact commit of each input. Commit it. nix flake update moves the inputs forward and rewrites it; nix flake metadata shows what's pinned.

the whole machine, as a flake

nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
  system = "x86_64-linux";
  modules = [ ./configuration.nix ];
};
sudo nixos-rebuild switch --flake .#nixos

the two mistakes everyone makes first

  • A new file the flake can't see. In a git repo, Nix only sees the files git knows about: git add it. Staging is enough; it doesn't need a commit.
  • Something read from outside the flake. Evaluation is pure: no NIX_PATH, no <nixpkgs>, no builtins.currentSystem, nothing fetched that isn't pinned. Declare it as an input instead.

The rest is in nix flake --help and on the NixOS wiki's Flakes page.

First published on my wiki, 12 March 2025; rewritten here, 25 November 2026. The original, on my wiki