···9393 value = value;
9494 };
95959696+ priorityComparator =
9797+ priority: a: b:
9898+ let
9999+ findPriority = name: lib.lists.findFirstIndex (p: p == name) (lib.length priority) priority;
100100+ priorityA = findPriority a;
101101+ priorityB = findPriority b;
102102+ in
103103+ if priorityA == priorityB then a < b else priorityA < priorityB;
104104+105105+ priorityMapAttrsToList =
106106+ f: priority: attrs:
107107+ lib.pipe attrs [
108108+ lib.attrsToList
109109+ (lib.sort (a: b: priorityComparator priority a.name b.name))
110110+ (map ({ name, value }: f name value))
111111+ ];
112112+113113+ styleHead =
114114+ styles:
115115+ if styles == [ ] then
116116+ {
117117+ attrSortPriority = [ ];
118118+ attrSep = " ";
119119+ }
120120+ else
121121+ lib.pipe styles [
122122+ lib.head
123123+ (
124124+ {
125125+ attrSortPriority ? [ ],
126126+ attrSep ? " ",
127127+ }:
128128+ {
129129+ inherit attrSortPriority attrSep;
130130+ }
131131+ )
132132+ ];
133133+134134+ styleTail = styles: if styles == [ ] then [ ] else lib.tail styles;
135135+96136 # expr to code
97137 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} }")
138138+ {
139139+ expr,
140140+ styles ? [ ],
141141+ }:
142142+ let
143143+ style = styleHead styles;
144144+ in
145145+ if lib.isStringLike expr then
146146+ lib.strings.escapeNixString expr
147147+ else if lib.isAttrs expr then
148148+ lib.pipe expr [
149149+ (priorityMapAttrsToList nixAttr style.attrSortPriority)
150150+ (map (
151151+ { name, value }:
152152+ "${name} = ${
153153+ nixCode {
154154+ expr = value;
155155+ styles = styleTail styles;
156156+ }
157157+ };"
158158+ ))
159159+ (values: "{ ${lib.concatStringsSep style.attrSep values} }")
106160 ]
107107- else if lib.isList x then
108108- lib.pipe x [
109109- (lib.map nixCode)
161161+ else if lib.isList expr then
162162+ lib.pipe expr [
163163+ (lib.map (
164164+ expr:
165165+ nixCode {
166166+ inherit expr;
167167+ styles = styleTail styles;
168168+ }
169169+ ))
110170 (values: "[ ${lib.concatStringsSep " " values} ]")
111171 ]
112112- else if x == true then
172172+ else if expr == true then
113173 "true"
114114- else if x == false then
174174+ else if expr == false then
115175 "false"
116176 else
117117- toString x;
177177+ toString expr;
118178119179in
120180{
121121- inherit inputsExpr isNonEmptyString nixCode;
181181+ inherit
182182+ inputsExpr
183183+ isNonEmptyString
184184+ priorityComparator
185185+ priorityMapAttrsToList
186186+ nixCode
187187+ ;
122188}
+14
docs/src/content/docs/reference/options.mdx
···5454};
5555```
56565757+## Style options
5858+5959+| Option | Type | Description |
6060+|---|---|---|
6161+| `flake-file.style.sep.flake` | `str` | The separator used between the top-level attributes of the flake |
6262+| `flake-file.style.sep.inputs` | `str` | The separator used between the top-level attributes of inputs |
6363+| `flake-file.style.sep.inputSchema` | `str` | The separator used between the top-level attributes of an input schema |
6464+| `flake-file.style.sep.nixConfig` | `str` | The separator used between the top-level attributes of nixConfig |
6565+| `flake-file.style.sortPriority.flake` | `listOf str` | When alphabetically sorting the top-level attributes of the flake, names within this list will receive a higher sort order according to its index |
6666+| `flake-file.style.sortPriority.inputs` | `listOf str` | When alphabetically sorting the top-level attributes of inputs, names within this list will receive a higher sort order according to its index |
6767+| `flake-file.style.sortPriority.inputSchema` | `listOf str` | When alphabetically sorting the top-level attributes of an input schema, names within this list will receive a higher sort order according to its index |
6868+| `flake-file.style.sortPriority.nixConfig` | `listOf str` | When alphabetically sorting the top-level attributes of nixConfig, names within this list will receive a higher sort order according to its index |
6969+7070+5771## Hook Options
58725973| Option | Type | Description |