flake-fileand vic's dendritic libs made for you with Love++ and AI--. If you like my work, consider sponsoring
flake-file lets you generate a clean, maintainable flake.nix from modular options, using flake-parts.
It makes your flake configuration modular and based on the Nix module system. This means you can use
lib.mkDefault or anything you normally do with Nix modules, and have them reflected in flake schema values.
|
|
- Who?
- What?
- Getting Started
- Usage
- Available Options
- About the Flake
outputfunction - Automatic flake.lock flattening
- Migration Guide
- Development
- Nix users who want to keep their
flake.nixmodular and maintainable - Anyone using flake-parts and looking to automate or simplify flake input management
- Teams or individuals who want to share and reuse flake modules across projects
flake-file lets you make your flake.nix dynamic and modular. Instead of maintaining a single, monolithic flake.nix, you define your flake inputs in separate modules close to where their inputs are used. flake-file then automatically generates a clean, up-to-date flake.nix for you.
- Keep your flake modular: Manage flake inputs just like the rest of your Nix configuration.
- Automatic updates: Regenerate your
flake.nixwith a single command whenever your options change. - Flake as dependency manifest: Use
flake.nixonly for declaring dependencies, not for complex Nix code. - Share and reuse modules: Teams can collaborate on and share flake modules across projects, including their dependencies.
Real-world examples: vic/vix uses flake-file. Our
dev/directory also uses flake-file to test this repo. More examples on GitHub.
To get started quickly, create a new flake based on our dendritic template:
nix flake init -t github:vic/flake-file#dendritic
nix run ".#write-flake" # regenerate flake.nix and flake.lock
cat flake.nix # flake.nix built from your options
nix flake check # check that flake.nix is up to dateTip
See the Migration Guide if you're moving from an existing flake.
The following is a complete example from our templates/dendritic template. It imports all modules from flake-file.flakeModules.dendritic.
{ inputs, lib, ... }:
{
# That's it! Importing this module will add dendritic-setup inputs to your flake.
imports = [ inputs.flake-file.flakeModules.dendritic ];
# Define flake attributes on any flake-pars module:
flake-file = {
description = "My Awesome Flake";
inputs.nixpkgs.url = lib.mkDefault "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
}- Defines
flake-fileoptions. - Exposes
packages.write-flake. - Exposes flake checks for generated files.
- Adds import-tree
- Includes flake-parts-builder's
_bootstrap.nix. - Uses bootstrap to load parts from ./flake-parts
- Uses bootstrap to load ./flake-parts/_meta as flake-file configs.
- Enables automatic flake.lock flattening using spikespaz/allfollow
- Enables automatic flake.lock flattening using fzakaria/nix-auto-follow
- Includes flakeModules.default.
- Includes flakeModules.import-tree.
- Includes flakeModules.nix-auto-follow.
- Enables
flake-parts. - Enables
flake-aspects. - Enables
den. - Sets
outputfunction toimport-tree ./modules. - Adds
treefmt-nixinput. - Enables formatters:
nixfmt,deadnix, andnixf-diagnose.
A more basic, explicit setup.
# See templates/default
{ inputs, ... }: {
imports = [
inputs.flake-file.flakeModules.default
];
flake-file.inputs = {
flake-file.url = "github:vic/flake-file";
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default";
};
systems = import inputs.systems;
}Important
Use nix run .#write-flake to generate.
Tip
You can use the write-flake app as part of a devshell or git hook.
A template for dendritic setups; includes flakeModules.dendritic.
A template that uses lib.flakeModules.flake-parts-builder.
Options use the same attributes as the flake schema. See below for details.
| Option | Description |
|---|---|
flake-file.description |
Sets the flake description |
flake-file.nixConfig |
Attrset for flake-level nixConfig |
flake-file.inputs.<name>.url |
URL for a flake input |
flake-file.inputs.<name>.flake |
Boolean, is input a flake? |
flake-file.inputs.<name>.inputs.<dep>.follows |
Tree of dependencies to follow |
Example:
flake-file = {
description = "my awesome flake";
nixConfig = {}; # an attrset. currently not typed.
inputs.<name>.url = "github:foo/bar";
inputs.<name>.flake = false;
inputs.<name>.inputs.nixpkgs.follows = "nixpkgs";
};Tip
See also, options.nix.
The flake-file.output option is a literal Nix expression. You cannot convert a Nix function value into a string for including in the generated flake file.
It defaults to:
inputs: import ./outputs.nix inputsWe recommend using this default, as it keeps your flake file focused on definitions of inputs and nixConfig. All Nix logic is moved to outputs.nix. Set this option only if you want to load another file with a Nix one-liner, but not for including a large Nix code string in it.
Tired of endlessly repeating tiny flake-parts modules or copy-pasting snippets between your projects? No more!
flake-parts-builder lets you incrementally add templated parts. This is much better than normal flake templates, since flake-parts templates can be added or removed at any time, not only at project initialization.
{ inputs, ... }: {
imports = [
(inputs.flake-file.lib.flakeModules.flake-parts-builder ./flake-parts)
];
}Important
Use github:vic/flake-parts-builder/write-meta until flake-parts-builder#60 gets merged. This branch will also write each parts meta.nix file, so it can be used by flake-file to manage your flake.nix.
Warning
Only use flake-parts-builder add subcommand, since init will overwrite the flake.nix file that is already being managed by flake-file.
nix run github:vic/flake-parts-builder/write-meta -- add --write-meta --parts systems,treefmt $PWDYou can add custom commands to be run whenever your flake.nix has been written or checked.
Tip
See flake-file.write-hooks and flake-file.check-hooks options.
You can use the prune-lock options
to specify a command that flake-file will use whenever your flake.nix file is generated
to flatten your flake.lock dependency tree.
For flattening mechanisms we provide:
flakeModules.allfollowthat enables this usingspikespaz/allfollowflakeModules.nix-auto-followthat enables this usingfzakaria/nix-auto-follow
{ inputs, ... }:
{
imports = [
inputs.flake-file.flakeModules.nix-auto-follow
# or optionally
#inputs.flake-file.flakeModules.allfollow
];
}This section outlines the recommended steps for adopting flake-file in your own repository.
-
Prerequisite: Ensure you have already adopted flake-parts.
-
Add Inputs: In your current
flake.nix, add the following input:flake-file.url = "github:vic/flake-file";
-
Move Outputs: Copy the contents of your
outputsfunction into a file./outputs.nix:# outputs.nix -- this is the contents of your `outputs` function from the original flake.nix file. inputs: inputs.flake-parts.lib.mkFlake { inherit inputs; } { imports = [ ./modules/inputs.nix # Add this for step 4. # Feel free to split ./modules/inputs.nix into other modules as you see fit. # If you end having lots of modules, consider using import-tree for auto importing them. ]; }
-
Move Inputs: Copy your current flake.nix file as a flake-parts module (e.g.,
modules/inputs.nix):
Important
Make sure you git add so that new files are visible to Nix.
# modules/inputs.nix
{ inputs, ... }:
{
imports = [
inputs.flake-file.flakeModules.default # flake-file options.
];
flake-file = {
inputs = {
flake-file.url = "github:vic/flake-file";
# ... all your other original flake inputs here.
};
nixConfig = { }; # if you had any.
description = "Your flake description";
};
}- Backup: Back up your flake.nix into flake.nix.bak before regenerating it.
- Generate: Execute
nix run .#write-flaketo generate flake.nix. - Verify: Check flake.nix and if everything is okay, remove the backup file.
You are done! Now you can split dependencies from modules/inputs.nix into other flake-part modules as you see fit:
# ./modules/<name>.nix -- Replace `<name>` with some dependency.
{ inputs, lib, ... }: {
flake-file.inputs.<name>.url = ...;
# Example usage: include the flakeModule once it has been added to flake.nix.
imports = lib.optionals (inputs ? <name>) [ inputs.<name>.flakeModule ];
}Use nix develop ./dev or with direnv: use flake ./dev.
[[general commands]]
check - run flake check
fmt - format all files in repo
menu - prints this menu
regen - regenerate all flake.nix files in this repo- Found a bug or have a feature request? Open an issue.
- Contributions are welcome!
Made with <3 by @vic
