Introduction to Flake
Flakes is a few things:
flake.nix
: a Nix file, with a specific structure to describe inputs and outputs for a Nix project- See NixOS Wiki - Flakes - Input Schema for flake input examples
- See NixOS Wiki - Flakes - Output Schema for flake output examples
flake.lock
: a manifest that "locks" inputs and records the exact versions in use- CLI support for flake-related features
- pure (by default) evaluations
This ultimately enables:
- properly hermetic builds
- fully reproducable and portable Nix projects
- faster Nix operations due to evaluation caching enabled by pure evaluations)
This removes the need for:
- using
niv
or other tooling to lock dependencies - manually documenting or scripting to ensure
NIX_PATH
is set consistently for your team - the need for the "the impure eval tree of sorrow" that comes with all of today's Nix impurities
- NixOS Wiki - Flakes
- a somewhat haphazard collection of factoids/snippets related to flakes
- particularly look at: Flake Schema, and it's two sections: Input Schema, Output Schema
- Tweag - NixOS flakes
- this article describes how to enable flake support in
nix
andnix-daemon
- reading this article is a pre-requisite
- this README.md assumes you've enabled flakes system-wide
- omit using
boot.isContainer = true;
onconfiguration.nix
(as the article suggests) if you want to usenixos-rebuild
rather thannixos-container
Nix is in flakes mode when:
--flake
is used with thenixos-rebuild
command- or, when
nix build
is used with an argument like'.#something'
(the hash symbol separates the flake source from the attribute to build)
When in this mode:
- Nix flake commands will implicitly take a directory path, it expects a
flake.nix
inside - when you see:
nix build '.#something'
, the.
means current directory, and#something
means to build thesomething
output attribute
nixos-rebuild build --flake '.#'
- looks for
flake.nix
in.
(current dir) - since it's
nixos-rebuild
, it automatically tries to build: #nixosConfigurations.{hostname}.config.system.build.toplevel
nixos-rebuild build --flake '/code/nixos-config#mysystem'
- looks for
flake.nix
in/code/nixos-config
- since it's
nixos-rebuild
, it automatically tries to build: #nixosConfigurations.mysystem.config.system.build.toplevel
- (note that this time we specifically asked, and got to build the
mysystem
config)
nix build 'github:colemickens/nixpkgs-wayland#obs-studio'
- looks for
flake.nix
in (a checkout ofgithub.com/colemickens/nixpkgs-wayland
) - builds and run the first attribute found:
#obs-studio
#packages.{currentSystem}.obs-studio
- TODO: finish fleshing out this list
nix flake update --recreate-lock-file
- updates all inputs and recreating
flake.lock
nix flake update --update-input nixpkgs
- updates a single input to latest and recording it in
flake.lock