Hooks
Overview
Section titled “Overview”flake-file provides two hook lists:
flake-file.write-hooks— commands executed afterwrite-flakewritesflake.nix.flake-file.check-hooks— commands executed duringnix flake check.
Each hook is an attrset with an index (for ordering) and a program: a function from pkgs to a derivation with meta.mainProgram, such as one built by pkgs.writeShellApplication.
write-hooks
Section titled “write-hooks”Use write-hooks to trigger follow-up actions whenever flake.nix is regenerated — for example, updating a lock file or reformatting.
Write hooks run from the flake root with no arguments.
{ flake-file.write-hooks = [ { index = 10; program = pkgs: pkgs.writeShellApplication { name = "update-lock"; runtimeInputs = [ pkgs.nix ]; text = "nix flake update"; }; } ];}Hooks run in ascending index order, so you can coordinate between modules without knowing the full list.
check-hooks
Section titled “check-hooks”Use check-hooks to add extra validation during nix flake check — for example, checking that a derived file is up to date.
Check hooks receive a copy of the flake root containing only the files listed in flake-file.check-files (default: flake.nix and flake.lock), so that commits touching other files keep the check cacheable. Add any file your hook reads:
{ flake-file.check-files = [ "flake.nix" "flake.lock" "generated/inputs.nix" ]; }Check hooks run inside the check derivation and receive the path of the flake root as their first argument.
{ flake-file.check-hooks = [ { index = 10; program = pkgs: pkgs.writeShellApplication { name = "lint"; runtimeInputs = [ pkgs.my-linter ]; text = ''my-linter --check "$1"''; }; } ];}Practical uses
Section titled “Practical uses”- Run
nix flake updateautomatically after regeneration. - Reformat
flake.nixwith a formatter (seeflake-file.formatter). - Validate generated files in CI via check-hooks.
- Trigger allfollow or nix-auto-follow to flatten the lock file.