···99 <a href="LICENSE"> <img src="https://img.shields.io/github/license/vic/flake-file" alt="License"/> </a>
1010</p>
11111212-# flake-file — Generate flake.nix from flake-parts modules.
1212+# flake-file — Generate flake.nix or unflake.nix from inputs defined as module options.
13131414> `flake-file` and [vic](https://bsky.app/profile/oeiuwq.bsky.social)'s [dendritic libs](https://vic.github.io/dendrix/Dendritic-Ecosystem.html#vics-dendritic-libraries) made for you with Love++ and AI--. If you like my work, consider [sponsoring](https://github.com/sponsors/vic)
15151616-**flake-file** lets you generate a clean, maintainable `flake.nix` from modular options, using [flake-parts](https://flake.parts/).
1616+**flake-file** lets you generate a clean, maintainable `flake.nix` from modular options. It works on both flakes and non-flakes environments.
17171818It makes your flake configuration modular and based on the Nix module system. This means you can use
1919`lib.mkDefault` or anything you normally do with Nix modules, and have them reflected in flake schema values.
2020+2121+> Despite the original flake-oriented name, it NOW also works on _stable Nix_, [_non flakes_](templates/unflake) environments.
20222123<table><tr><td>
22242325## Features
24262525-- Flake definition aggregated from all flake-parts modules.
2727+- Flake definition aggregated from Nix modules.
2628- Schema as [options](https://github.com/vic/flake-file/blob/main/modules/options/default.nix).
2729- Syntax for nixConfig and follows is the same as in flakes.
2830- `flake check` ensures files are up to date.
2929-- App for generator: `nix run .#write-flake`
3131+- App for `flake.nix` generator: `nix run .#write-flake`
3032- Custom do-not-edit header.
3133- Automatic flake.lock [flattening](#automatic-flakelock-flattening).
3234- Incrementally add [flake-parts-builder](#parts_templates) templates.
3335- Pick flakeModules for different feature sets.
3436- [Dendritic](https://vic.github.io/dendrix/Dendritic.html) flake template.
3737+- Works on stable Nix, [unflake](templates/unflake) environments.
35383639</td><td>
3740···6063## Who is this for?
61646265- Nix users who want to keep their `flake.nix` modular and maintainable
6363-- Anyone using [flake-parts](https://flake.parts/) and looking to automate or simplify flake input management
6666+- Anyone using Nix modules and looking to automate or simplify flake input management
6467- Teams or individuals who want to share and reuse flake modules across projects
65686669---
···148151149152> Previously, this module included `flake-aspects` and `den` as dependencies. It now provides a pure flake-parts Dendritic setup. If you need the complete [den](https://github.com/vic/den) functionality, use den's `flakeModules.dendritic` instead.
150153154154+#### [`flakeModules.unflake`](https://github.com/vic/flake-file/tree/main/modules/unflake.nix)
155155+156156+- Defines `flake-file` options.
157157+- Exposes `write-unflake` to generate `unflake.nix` or `npins`. See [templates/unflake](templates/unflake) for usage.
158158+151159### Flake Templates
152160153153-#### `default` template
161161+#### [`default`](templates/default) template
154162155163A more basic, explicit setup.
156164···178186> [!TIP]
179187> You can use the `write-flake` app as part of a devshell or git hook.
180188181181-#### `dendritic` template
189189+#### [`dendritic`](templates/dendritic) template
182190183191A template for dendritic setups; includes `flakeModules.dendritic`.
184192185185-#### `parts` template
193193+#### [`parts`](templates/parts) template
186194187195A template that uses `lib.flakeModules.flake-parts-builder`.
196196+197197+#### [`unflake`](templates/unflake) template
198198+199199+Uses [goldstein/unflake](https://codeberg.org/goldstein/unflake) to pin and fetch inputs that were defined as options for non-flakes stable Nix environments.
188200189201---
190202
+42-1
dev/modules/_lib/default.nix
···7575 }
7676 );
77777878+ nixAttr =
7979+ name: value:
8080+ let
8181+ childIsAttr = builtins.isAttrs value;
8282+ childIsOne = builtins.length (builtins.attrNames value) == 1;
8383+ nested = lib.head (lib.mapAttrsToList nixAttr value);
8484+ in
8585+ if childIsAttr && childIsOne then
8686+ {
8787+ name = "${name}.${nested.name}";
8888+ value = nested.value;
8989+ }
9090+ else
9191+ {
9292+ inherit name;
9393+ value = value;
9494+ };
9595+9696+ # expr to code
9797+ nixCode =
9898+ x:
9999+ if lib.isStringLike x then
100100+ lib.strings.escapeNixString x
101101+ else if lib.isAttrs x then
102102+ lib.pipe x [
103103+ (lib.mapAttrsToList nixAttr)
104104+ (map ({ name, value }: "${name} = ${nixCode value}; "))
105105+ (values: "{ ${lib.concatStringsSep " " values} }")
106106+ ]
107107+ else if lib.isList x then
108108+ lib.pipe x [
109109+ (lib.map nixCode)
110110+ (values: "[ ${lib.concatStringsSep " " values} ]")
111111+ ]
112112+ else if x == true then
113113+ "true"
114114+ else if x == false then
115115+ "false"
116116+ else
117117+ toString x;
118118+78119in
79120{
8080- inherit inputsExpr isNonEmptyString;
121121+ inherit inputsExpr isNonEmptyString nixCode;
81122}
···66 ...
77}:
88let
99- inherit (import ./../dev/modules/_lib lib) inputsExpr isNonEmptyString;
99+ inherit (import ./../dev/modules/_lib lib) inputsExpr isNonEmptyString nixCode;
10101111 flake-file = config.flake-file;
1212···2626 (lib.replaceString "<inputs>" flakeInputs)
2727 addHeader
2828 ];
2929-3030- nixAttr =
3131- name: value:
3232- let
3333- childIsAttr = builtins.isAttrs value;
3434- childIsOne = builtins.length (builtins.attrNames value) == 1;
3535- nested = lib.head (lib.mapAttrsToList nixAttr value);
3636- in
3737- if childIsAttr && childIsOne then
3838- {
3939- name = "${name}.${nested.name}";
4040- value = nested.value;
4141- }
4242- else
4343- {
4444- inherit name;
4545- value = value;
4646- };
4747-4848- # expr to code
4949- nixCode =
5050- x:
5151- if lib.isStringLike x then
5252- lib.strings.escapeNixString x
5353- else if lib.isAttrs x then
5454- lib.pipe x [
5555- (lib.mapAttrsToList nixAttr)
5656- (map ({ name, value }: "${name} = ${nixCode value}; "))
5757- (values: "{ ${lib.concatStringsSep " " values} }")
5858- ]
5959- else if lib.isList x then
6060- lib.pipe x [
6161- (lib.map nixCode)
6262- (values: "[ ${lib.concatStringsSep " " values} ]")
6363- ]
6464- else if x == true then
6565- "true"
6666- else if x == false then
6767- "false"
6868- else
6969- toString x;
70297130 description =
7231 if isNonEmptyString flake-file.description then
+40
templates/unflake/README.md
···11+# Unflake
22+33+This template is an example of using `flake-file.inputs` in a non-flakes project.
44+55+It uses [unflake](https://codeberg.org/goldstein/unflake) to pin and fetch inputs defined as options inside `./modules`.
66+77+## Generate `unflake.nix`
88+99+The following command is a convenience for generating `unflake.nix` by
1010+first producing a temporary `inputs.nix` from your config and then
1111+running unflake on it.
1212+1313+```shell
1414+nix-shell . -A unflake.env --run write-unflake
1515+```
1616+1717+You can also pass any unflake option:
1818+1919+```shell
2020+nix-shell . -A unflake.env --run 'write-unflake --verbose --backend nix'
2121+```
2222+2323+If you need to see the file that is being passed as `--inputs inputs.nix`
2424+to the unflake command, you can generate it with:
2525+2626+```shell
2727+# (only recommended for debugging)
2828+nix-shell . -A unflake.env --run write-inputs
2929+3030+# then, you can run unflake yourself:
3131+nix-shell https://ln-s.sh/unflake -A unflake-shell --run unflake
3232+```
3333+3434+## Using with [npins](https://github.com/andir/npins)
3535+3636+Unflake has an npins backend to use it run:
3737+3838+```shell
3939+nix-shell . -A unflake.env --run 'write-unflake --backend npins'
4040+```