OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 025

Flake structure: inputs and outputs

Understand Flake inputs, outputs, self, and the levels involved in capturing function arguments.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSFlakes: project and dependency management ↗
About 2 min readStart reading

Basic Flake structure#

A typical flake.nix is an attribute set in which outputs is a function that takes inputs and returns an output attribute set.

{
  description = "我的 NixOS 配置";

  inputs = {
    # 这里定义依赖
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    home-manager.url = "github:nix-community/home-manager";
  };

  outputs = { self, nixpkgs, ... }@inputs: {
    # 这里定义产出(系统配置、软件包、Shell 环境等)
    nixosConfigurations.my-host = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [ ./configuration.nix ];
    };
  };
}

1. Inputs#

inputs contains the external resources you want to reference.

  • nixpkgs: the entire package repository.

  • home-manager: a user-level configuration tool.

  • flake-utils: a commonly used helper library.

  • The follows keyword: very important! It makes a specified nested input follow the top-level nixpkgs, reducing duplicate nixpkgs sources in the dependency graph. Multiple package versions may still exist, and sharing an input does not guarantee compatibility across all external projects.

    home-manager.inputs.nixpkgs.follows = "nixpkgs";

2. Outputs#

outputs contains what your Flake produces.

  • nixosConfigurations: used by nixos-rebuild; your system is at its core.
  • homeConfigurations: used by home-manager.
  • devShells: development environments used by nix develop.
  • packages: software you package yourself.

What are self and @inputs here?#

  • self: represents the current Flake itself. You can use it to reference files defined within it.
  • @inputs: syntactic sugar that captures all input arguments together.

Practical tip: understand the outputs hierarchy#

Running nix flake show displays the outputs as a tree. Those levels must match exactly in your code for nixos-rebuild to find your configuration.

References & further reading

  1. Nix Flake manual ↗
  2. Nix flake update ↗
  3. Nix flake check ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Flakes: project and dependency management

  1. 01Flake structure: inputs and outputs
  2. 02flake.lock and reproducibility
  3. 03Flake Utils and project templates
  4. 04External Flake references and collaboration
  5. 05Migrating NixOS configuration to Flakes
  6. 06A tutorial on Flake projects and dependency management
  7. 07How Flake updates work
  8. 08Rolling back and manually editing flake.lock
CONTINUE READING / NEXT ARTICLEflake.lock and reproducibility

Record dependency sources with version locking while understanding the conditions for reproducible results.