Skip to content

Commit d41fe5f

Browse files
authored
lib/options: add moduleOptions (#59)
* lib/options: add moduleOptions * lib/options!: improve moduleOptions allows to not pass the `args` variable if they are needed inside and the arguments are closer to the scope * docs/src/options/introduction: add moduleOptions * docs/src/ru/options/introduction: add moduleOpt... ...ions * docs/src/options/introduction: add semicolon * docs/src: fix second moduleOptions example i actually forgot that only `options` attribute can be assigned to a function and not something nested 💀 * docs/src: fix indent in moduleOptions examples
1 parent 4366581 commit d41fe5f

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/src/options/introduction.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ options = delib.singleEnableOption <default>;
5252
options = delib.singleCascadeEnableOption;
5353
```
5454

55+
- `moduleOptions <opts> <args>` - creates an attribute set with `<opts>` for the current module path. Usually, you need to use the module `name` once again to define all options, but this wrapper does this automatically. `<args>` is a set with [passed arguments](/modules/structure#passed-arguments). If `<opts>` is a function, then `<args>` set is automatically passed to it. See the example:
56+
57+
```nix
58+
# current module's name is "programs.category.example".
59+
60+
# if `singleEnableOption` is not enough:
61+
options = with delib; moduleOptions {
62+
enable = boolOption true;
63+
device = strOption "desktop";
64+
};
65+
66+
# `options` block above is the same as:
67+
options.programs.category.example = with delib; {
68+
enable = boolOption true;
69+
device = strOption "desktop";
70+
};
71+
72+
# but if `singleCascadeEnableOption` is not enough:
73+
options = with delib; moduleOptions ({ parent, ... }: {
74+
enable = boolOption parent.enable;
75+
device = strOption "desktop";
76+
}); # notice the parentheses
77+
78+
# once again, `options` block above is the same as:
79+
options = with delib; { parent, ... }: {
80+
programs.category.example = {
81+
enable = boolOption parent.enable;
82+
device = strOption "desktop";
83+
};
84+
};
85+
```
86+
5587
The list of current options can be found in the source code: [github:yunfachi/denix?path=lib/options.nix](https://github.com/yunfachi/denix/blob/master/lib/options.nix)
5688

5789
## Examples {#examples}

docs/src/ru/options/introduction.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ options = delib.singleEnableOption <default>;
5252
options = delib.singleCascadeEnableOption;
5353
```
5454

55+
- `moduleOptions <opts> <args>` - создаёт attribute set с `<opts>` для пути текущего модуля. Обычно, необходимо использовать `name` ещё раз, чтобы обозначить опции, но эта функция делает это автоматически. `<args>` это набор [передаваемых аргументов](/ru/modules/structure#passed-arguments). Если `<opts>` это функция, тогда `<args>` является её аргументом. Пример:
56+
57+
```nix
58+
# имя текущего модуля это "programs.category.example".
59+
60+
# если не хватает `singleEnableOption`:
61+
options = with delib; moduleOptions {
62+
enable = boolOption true;
63+
device = strOption "desktop";
64+
};
65+
66+
# блок опций выше эквивалентен блоку ниже:
67+
options.programs.category.example = with delib; {
68+
enable = boolOption true;
69+
device = strOption "desktop";
70+
};
71+
72+
# а если не хватает `singleCascadeEnableOption`:
73+
options = with delib; moduleOptions ({ parent, ... }: {
74+
enable = boolOption parent.enable;
75+
device = strOption "desktop";
76+
}); # обратите внимание на скобки
77+
78+
# снова, блок опций выше эквивалентен блоку ниже:
79+
options = with delib; { parent, ... }: {
80+
programs.category.example = {
81+
enable = boolOption parent.enable;
82+
device = strOption "desktop";
83+
};
84+
};
85+
```
86+
5587
Список актуальных опций можно найти в исходном коде: [github:yunfachi/denix?path=lib/options.nix](https://github.com/yunfachi/denix/blob/master/lib/options.nix)
5688

5789
## Примеры {#examples}

lib/options.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@ rec {
117117
default: { name, ... }: delib.attrset.setAttrByStrPath "${name}.enable" (boolOption default);
118118

119119
singleCascadeEnableOption = args: delib.singleEnableOption args.parent.enable args;
120+
121+
moduleOptions =
122+
opts: args:
123+
delib.attrset.setAttrByStrPath args.name (if lib.isFunction opts then opts args else opts);
120124
}

0 commit comments

Comments
 (0)