mirror of
https://github.com/DreamMaoMao/maomaowm.git
synced 2026-06-19 14:33:16 -04:00
refactor: format nix code with alejandra & rename nix files for clarity
This commit is contained in:
parent
bb86ea12ca
commit
df492dab6b
6 changed files with 298 additions and 311 deletions
481
nix/lib.nix
481
nix/lib.nix
|
|
@ -1,6 +1,6 @@
|
|||
lib:
|
||||
let
|
||||
inherit (lib)
|
||||
lib: let
|
||||
inherit
|
||||
(lib)
|
||||
attrNames
|
||||
filterAttrs
|
||||
foldl
|
||||
|
|
@ -9,304 +9,299 @@ let
|
|||
removeAttrs
|
||||
;
|
||||
|
||||
inherit (lib.strings)
|
||||
inherit
|
||||
(lib.strings)
|
||||
concatMapStrings
|
||||
hasPrefix
|
||||
;
|
||||
|
||||
/**
|
||||
Convert a structured Nix attribute set into Mango's configuration format.
|
||||
Convert a structured Nix attribute set into Mango's configuration format.
|
||||
|
||||
This function takes a nested attribute set and converts it into Mango-compatible
|
||||
configuration syntax, supporting top, bottom, and regular command sections.
|
||||
This function takes a nested attribute set and converts it into Mango-compatible
|
||||
configuration syntax, supporting top, bottom, and regular command sections.
|
||||
|
||||
Commands are flattened using the `flattenAttrs` function, and attributes are formatted as
|
||||
`key = value` pairs. Lists are expanded as duplicate keys to match Mango's expected format.
|
||||
Commands are flattened using the `flattenAttrs` function, and attributes are formatted as
|
||||
`key = value` pairs. Lists are expanded as duplicate keys to match Mango's expected format.
|
||||
|
||||
Configuration:
|
||||
Configuration:
|
||||
|
||||
* `topCommandsPrefixes` - A list of prefixes to define **top** commands (default: `[]`).
|
||||
* `bottomCommandsPrefixes` - A list of prefixes to define **bottom** commands (default: `[]`).
|
||||
* `topCommandsPrefixes` - A list of prefixes to define **top** commands (default: `[]`).
|
||||
* `bottomCommandsPrefixes` - A list of prefixes to define **bottom** commands (default: `[]`).
|
||||
|
||||
Attention:
|
||||
Attention:
|
||||
|
||||
- The function ensures top commands appear **first** and bottom commands **last**.
|
||||
- The generated configuration is a **single string**, suitable for writing to a config file.
|
||||
- Lists are converted into multiple entries, ensuring compatibility with Mango.
|
||||
- The function ensures top commands appear **first** and bottom commands **last**.
|
||||
- The generated configuration is a **single string**, suitable for writing to a config file.
|
||||
- Lists are converted into multiple entries, ensuring compatibility with Mango.
|
||||
|
||||
# Inputs
|
||||
# Inputs
|
||||
|
||||
Structured function argument:
|
||||
Structured function argument:
|
||||
|
||||
: topCommandsPrefixes (optional, default: `[]`)
|
||||
: A list of prefixes that define **top** commands. Any key starting with one of these
|
||||
prefixes will be placed at the beginning of the configuration.
|
||||
: bottomCommandsPrefixes (optional, default: `[]`)
|
||||
: A list of prefixes that define **bottom** commands. Any key starting with one of these
|
||||
prefixes will be placed at the end of the configuration.
|
||||
: topCommandsPrefixes (optional, default: `[]`)
|
||||
: A list of prefixes that define **top** commands. Any key starting with one of these
|
||||
prefixes will be placed at the beginning of the configuration.
|
||||
: bottomCommandsPrefixes (optional, default: `[]`)
|
||||
: A list of prefixes that define **bottom** commands. Any key starting with one of these
|
||||
prefixes will be placed at the end of the configuration.
|
||||
|
||||
Value:
|
||||
Value:
|
||||
|
||||
: The attribute set to be converted to Hyprland configuration format.
|
||||
: The attribute set to be converted to Hyprland configuration format.
|
||||
|
||||
# Type
|
||||
# Type
|
||||
|
||||
```
|
||||
toMango :: AttrSet -> AttrSet -> String
|
||||
```
|
||||
```
|
||||
toMango :: AttrSet -> AttrSet -> String
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
# Examples
|
||||
:::{.example}
|
||||
|
||||
## Basic mangowc configuration
|
||||
## Basic mangowc configuration
|
||||
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
blur = 1;
|
||||
blur_params_radius = 5;
|
||||
border_radius = 6;
|
||||
animations = 1;
|
||||
animation_duration_open = 400;
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
blur = 1;
|
||||
blur_params_radius = 5;
|
||||
border_radius = 6;
|
||||
animations = 1;
|
||||
animation_duration_open = 400;
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
animations = 1
|
||||
animation_duration_open = 400
|
||||
blur = 1
|
||||
blur_params_radius = 5
|
||||
border_radius = 6
|
||||
```
|
||||
|
||||
## Using nested attributes
|
||||
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
blur = 1;
|
||||
blur_params = {
|
||||
radius = 5;
|
||||
num_passes = 2;
|
||||
noise = 0.02;
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
animation_curve = {
|
||||
open = "0.46,1.0,0.29,1";
|
||||
close = "0.08,0.92,0,1";
|
||||
};
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
animations = 1
|
||||
animation_duration_open = 400
|
||||
blur = 1
|
||||
blur_params_radius = 5
|
||||
border_radius = 6
|
||||
```
|
||||
**Output:**
|
||||
```
|
||||
animation_curve_close = 0.08,0.92,0,1
|
||||
animation_curve_open = 0.46,1.0,0.29,1
|
||||
blur = 1
|
||||
blur_params_noise = 0.02
|
||||
blur_params_num_passes = 2
|
||||
blur_params_radius = 5
|
||||
```
|
||||
|
||||
## Using nested attributes
|
||||
## Using lists for duplicate keys
|
||||
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
blur = 1;
|
||||
blur_params = {
|
||||
radius = 5;
|
||||
num_passes = 2;
|
||||
noise = 0.02;
|
||||
};
|
||||
animation_curve = {
|
||||
open = "0.46,1.0,0.29,1";
|
||||
close = "0.08,0.92,0,1";
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
bind = [
|
||||
"SUPER,r,reload_config"
|
||||
"Alt,space,spawn,rofi -show drun"
|
||||
"Alt,Return,spawn,foot"
|
||||
];
|
||||
tagrule = [
|
||||
"id:1,layout_name:tile"
|
||||
"id:2,layout_name:scroller"
|
||||
];
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
bind = SUPER,r,reload_config
|
||||
bind = Alt,space,spawn,rofi -show drun
|
||||
bind = Alt,Return,spawn,foot
|
||||
tagrule = id:1,layout_name:tile
|
||||
tagrule = id:2,layout_name:scroller
|
||||
```
|
||||
|
||||
## Using keymodes (submaps)
|
||||
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
bind = [
|
||||
"SUPER,Q,killclient"
|
||||
"ALT,R,setkeymode,resize"
|
||||
];
|
||||
keymode = {
|
||||
resize = {
|
||||
bind = [
|
||||
"NONE,Left,resizewin,-10,0"
|
||||
"NONE,Right,resizewin,10,0"
|
||||
"NONE,Escape,setkeymode,default"
|
||||
];
|
||||
};
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
animation_curve_close = 0.08,0.92,0,1
|
||||
animation_curve_open = 0.46,1.0,0.29,1
|
||||
blur = 1
|
||||
blur_params_noise = 0.02
|
||||
blur_params_num_passes = 2
|
||||
blur_params_radius = 5
|
||||
```
|
||||
**Output:**
|
||||
```
|
||||
bind = SUPER,Q,killclient
|
||||
bind = ALT,R,setkeymode,resize
|
||||
|
||||
## Using lists for duplicate keys
|
||||
keymode = resize
|
||||
bind = NONE,Left,resizewin,-10,0
|
||||
bind = NONE,Right,resizewin,10,0
|
||||
bind = NONE,Escape,setkeymode,default
|
||||
```
|
||||
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
bind = [
|
||||
"SUPER,r,reload_config"
|
||||
"Alt,space,spawn,rofi -show drun"
|
||||
"Alt,Return,spawn,foot"
|
||||
];
|
||||
tagrule = [
|
||||
"id:1,layout_name:tile"
|
||||
"id:2,layout_name:scroller"
|
||||
];
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
bind = SUPER,r,reload_config
|
||||
bind = Alt,space,spawn,rofi -show drun
|
||||
bind = Alt,Return,spawn,foot
|
||||
tagrule = id:1,layout_name:tile
|
||||
tagrule = id:2,layout_name:scroller
|
||||
```
|
||||
|
||||
## Using keymodes (submaps)
|
||||
|
||||
```nix
|
||||
let
|
||||
config = {
|
||||
bind = [
|
||||
"SUPER,Q,killclient"
|
||||
"ALT,R,setkeymode,resize"
|
||||
];
|
||||
keymode = {
|
||||
resize = {
|
||||
bind = [
|
||||
"NONE,Left,resizewin,-10,0"
|
||||
"NONE,Right,resizewin,10,0"
|
||||
"NONE,Escape,setkeymode,default"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
in lib.toMango {} config
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
bind = SUPER,Q,killclient
|
||||
bind = ALT,R,setkeymode,resize
|
||||
|
||||
keymode = resize
|
||||
bind = NONE,Left,resizewin,-10,0
|
||||
bind = NONE,Right,resizewin,10,0
|
||||
bind = NONE,Escape,setkeymode,default
|
||||
```
|
||||
|
||||
:::
|
||||
:::
|
||||
*/
|
||||
toMango =
|
||||
{
|
||||
topCommandsPrefixes ? [ ],
|
||||
bottomCommandsPrefixes ? [ ],
|
||||
}:
|
||||
attrs:
|
||||
let
|
||||
toMango' =
|
||||
attrs:
|
||||
let
|
||||
# Specially configured `toKeyValue` generator with support for duplicate keys
|
||||
# and a legible key-value separator.
|
||||
mkCommands = generators.toKeyValue {
|
||||
mkKeyValue = generators.mkKeyValueDefault { } " = ";
|
||||
listsAsDuplicateKeys = true;
|
||||
indent = ""; # No indent, since we don't have nesting
|
||||
};
|
||||
toMango = {
|
||||
topCommandsPrefixes ? [],
|
||||
bottomCommandsPrefixes ? [],
|
||||
}: attrs: let
|
||||
toMango' = attrs: let
|
||||
# Specially configured `toKeyValue` generator with support for duplicate keys
|
||||
# and a legible key-value separator.
|
||||
mkCommands = generators.toKeyValue {
|
||||
mkKeyValue = generators.mkKeyValueDefault {} " = ";
|
||||
listsAsDuplicateKeys = true;
|
||||
indent = ""; # No indent, since we don't have nesting
|
||||
};
|
||||
|
||||
# Extract keymode definitions if they exist
|
||||
keymodes = attrs.keymode or { };
|
||||
attrsWithoutKeymodes = removeAttrs attrs [ "keymode" ];
|
||||
# Extract keymode definitions if they exist
|
||||
keymodes = attrs.keymode or {};
|
||||
attrsWithoutKeymodes = removeAttrs attrs ["keymode"];
|
||||
|
||||
# Generate keymode blocks
|
||||
# Format: keymode=name\nbind=...\nbind=...\n
|
||||
mkKeymodeBlock =
|
||||
name: modeAttrs:
|
||||
let
|
||||
modeCommands = flattenAttrs (p: k: "${p}_${k}") modeAttrs;
|
||||
in
|
||||
"keymode = ${name}\n${mkCommands modeCommands}";
|
||||
# Generate keymode blocks
|
||||
# Format: keymode=name\nbind=...\nbind=...\n
|
||||
mkKeymodeBlock = name: modeAttrs: let
|
||||
modeCommands = flattenAttrs (p: k: "${p}_${k}") modeAttrs;
|
||||
in "keymode = ${name}\n${mkCommands modeCommands}";
|
||||
|
||||
keymodeBlocks =
|
||||
if keymodes == { } then
|
||||
""
|
||||
else
|
||||
"\n" + concatMapStrings (name: mkKeymodeBlock name keymodes.${name} + "\n") (attrNames keymodes);
|
||||
keymodeBlocks =
|
||||
if keymodes == {}
|
||||
then ""
|
||||
else "\n" + concatMapStrings (name: mkKeymodeBlock name keymodes.${name} + "\n") (attrNames keymodes);
|
||||
|
||||
# Flatten the attrset, combining keys in a "path" like `"a_b_c" = "x"`.
|
||||
# Uses `flattenAttrs` with an underscore separator.
|
||||
commands = flattenAttrs (p: k: "${p}_${k}") attrsWithoutKeymodes;
|
||||
# Flatten the attrset, combining keys in a "path" like `"a_b_c" = "x"`.
|
||||
# Uses `flattenAttrs` with an underscore separator.
|
||||
commands = flattenAttrs (p: k: "${p}_${k}") attrsWithoutKeymodes;
|
||||
|
||||
# General filtering function to check if a key starts with any prefix in a given list.
|
||||
filterCommands = list: n: foldl (acc: prefix: acc || hasPrefix prefix n) false list;
|
||||
# General filtering function to check if a key starts with any prefix in a given list.
|
||||
filterCommands = list: n: foldl (acc: prefix: acc || hasPrefix prefix n) false list;
|
||||
|
||||
# Partition keys into top commands and the rest
|
||||
result = partition (filterCommands topCommandsPrefixes) (attrNames commands);
|
||||
topCommands = filterAttrs (n: _: builtins.elem n result.right) commands;
|
||||
remainingCommands = removeAttrs commands result.right;
|
||||
# Partition keys into top commands and the rest
|
||||
result = partition (filterCommands topCommandsPrefixes) (attrNames commands);
|
||||
topCommands = filterAttrs (n: _: builtins.elem n result.right) commands;
|
||||
remainingCommands = removeAttrs commands result.right;
|
||||
|
||||
# Partition remaining commands into bottom commands and regular commands
|
||||
result2 = partition (filterCommands bottomCommandsPrefixes) result.wrong;
|
||||
bottomCommands = filterAttrs (n: _: builtins.elem n result2.right) remainingCommands;
|
||||
regularCommands = removeAttrs remainingCommands result2.right;
|
||||
in
|
||||
# Concatenate strings from mapping `mkCommands` over top, regular, and bottom commands.
|
||||
# Keymodes are appended at the end.
|
||||
concatMapStrings mkCommands [
|
||||
topCommands
|
||||
regularCommands
|
||||
bottomCommands
|
||||
]
|
||||
+ keymodeBlocks;
|
||||
# Partition remaining commands into bottom commands and regular commands
|
||||
result2 = partition (filterCommands bottomCommandsPrefixes) result.wrong;
|
||||
bottomCommands = filterAttrs (n: _: builtins.elem n result2.right) remainingCommands;
|
||||
regularCommands = removeAttrs remainingCommands result2.right;
|
||||
in
|
||||
# Concatenate strings from mapping `mkCommands` over top, regular, and bottom commands.
|
||||
# Keymodes are appended at the end.
|
||||
concatMapStrings mkCommands [
|
||||
topCommands
|
||||
regularCommands
|
||||
bottomCommands
|
||||
]
|
||||
+ keymodeBlocks;
|
||||
in
|
||||
toMango' attrs;
|
||||
|
||||
/**
|
||||
Flatten a nested attribute set into a flat attribute set, using a custom key separator function.
|
||||
Flatten a nested attribute set into a flat attribute set, using a custom key separator function.
|
||||
|
||||
This function recursively traverses a nested attribute set and produces a flat attribute set
|
||||
where keys are joined using a user-defined function (`pred`). It allows transforming deeply
|
||||
nested structures into a single-level attribute set while preserving key-value relationships.
|
||||
This function recursively traverses a nested attribute set and produces a flat attribute set
|
||||
where keys are joined using a user-defined function (`pred`). It allows transforming deeply
|
||||
nested structures into a single-level attribute set while preserving key-value relationships.
|
||||
|
||||
Configuration:
|
||||
Configuration:
|
||||
|
||||
* `pred` - A function `(string -> string -> string)` defining how keys should be concatenated.
|
||||
* `pred` - A function `(string -> string -> string)` defining how keys should be concatenated.
|
||||
|
||||
# Inputs
|
||||
# Inputs
|
||||
|
||||
Structured function argument:
|
||||
Structured function argument:
|
||||
|
||||
: pred (required)
|
||||
: A function that determines how parent and child keys should be combined into a single key.
|
||||
It takes a `prefix` (parent key) and `key` (current key) and returns the joined key.
|
||||
: pred (required)
|
||||
: A function that determines how parent and child keys should be combined into a single key.
|
||||
It takes a `prefix` (parent key) and `key` (current key) and returns the joined key.
|
||||
|
||||
Value:
|
||||
Value:
|
||||
|
||||
: The nested attribute set to be flattened.
|
||||
: The nested attribute set to be flattened.
|
||||
|
||||
# Type
|
||||
# Type
|
||||
|
||||
```
|
||||
flattenAttrs :: (String -> String -> String) -> AttrSet -> AttrSet
|
||||
```
|
||||
```
|
||||
flattenAttrs :: (String -> String -> String) -> AttrSet -> AttrSet
|
||||
```
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
# Examples
|
||||
:::{.example}
|
||||
|
||||
```nix
|
||||
let
|
||||
nested = {
|
||||
a = "3";
|
||||
b = { c = "4"; d = "5"; };
|
||||
};
|
||||
```nix
|
||||
let
|
||||
nested = {
|
||||
a = "3";
|
||||
b = { c = "4"; d = "5"; };
|
||||
};
|
||||
|
||||
separator = (prefix: key: "${prefix}.${key}"); # Use dot notation
|
||||
in lib.flattenAttrs separator nested
|
||||
```
|
||||
separator = (prefix: key: "${prefix}.${key}"); # Use dot notation
|
||||
in lib.flattenAttrs separator nested
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```nix
|
||||
{
|
||||
"a" = "3";
|
||||
"b.c" = "4";
|
||||
"b.d" = "5";
|
||||
}
|
||||
```
|
||||
**Output:**
|
||||
```nix
|
||||
{
|
||||
"a" = "3";
|
||||
"b.c" = "4";
|
||||
"b.d" = "5";
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
:::
|
||||
*/
|
||||
flattenAttrs =
|
||||
pred: attrs:
|
||||
let
|
||||
flattenAttrs' =
|
||||
prefix: attrs:
|
||||
builtins.foldl' (
|
||||
acc: key:
|
||||
let
|
||||
value = attrs.${key};
|
||||
newKey = if prefix == "" then key else pred prefix key;
|
||||
in
|
||||
acc // (if builtins.isAttrs value then flattenAttrs' newKey value else { "${newKey}" = value; })
|
||||
) { } (builtins.attrNames attrs);
|
||||
in
|
||||
flattenAttrs = pred: attrs: let
|
||||
flattenAttrs' = prefix: attrs:
|
||||
builtins.foldl' (
|
||||
acc: key: let
|
||||
value = attrs.${key};
|
||||
newKey =
|
||||
if prefix == ""
|
||||
then key
|
||||
else pred prefix key;
|
||||
in
|
||||
acc
|
||||
// (
|
||||
if builtins.isAttrs value
|
||||
then flattenAttrs' newKey value
|
||||
else {"${newKey}" = value;}
|
||||
)
|
||||
) {} (builtins.attrNames attrs);
|
||||
in
|
||||
flattenAttrs' "" attrs;
|
||||
in
|
||||
{
|
||||
in {
|
||||
inherit flattenAttrs toMango;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue