Skip to content

Commit

Permalink
lib.showOptionParent: init
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Jan 3, 2025
1 parent 782a0e6 commit 05147cf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ let
getValues getFiles
optionAttrSetToDocList optionAttrSetToDocList'
scrubOptionValue literalExpression literalExample
showOption showOptionWithDefLocs showFiles
showOption showOptionParent showOptionWithDefLocs showFiles
unknownModule mkOption mkPackageOption mkPackageOptionMD
literalMD;
inherit (self.types) isType setType defaultTypeMerge defaultFunctor
Expand Down
34 changes: 34 additions & 0 deletions lib/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,40 @@ rec {
then part
else lib.strings.escapeNixIdentifier part;
in (concatStringsSep ".") (map escapeOptionPart parts);

/**
Show the option path, except for the last `n` elements.
This can be used to refer to groups of options in documentation.
# Inputs
- `opt`: The option path.
- `n`: The number of elements to exclude from the end.
# Example
```nix
showOptionParent config.services.foo.settings.bar 1
=> "services.foo.settings"
```
```nix
mkOption {
type = types.str;
description = "The settings file for foo.";
default = tomlFormat.generate "foo.toml" opt.settings.value;
defaultText = lib.literalMD ''
Generated TOML file based on `${showOptionParent opt.settings.bar 1}`
'';
}
```
*/
showOptionParent = opt: n:
showOption (lib.dropEnd n opt);

showFiles = files: concatStringsSep " and " (map (f: "`${f}'") files);

showDefs = defs: concatMapStrings (def:
Expand Down
2 changes: 2 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ checkConfigOutput '^true$' config.assertion ./gvariant.nix

checkConfigOutput '"ok"' config.result ./specialArgs-lib.nix

checkConfigOutput '"ok"' config.result ./misc.nix

# https://github.com/NixOS/nixpkgs/pull/131205
# We currently throw this error already in `config`, but throwing in `config.wrong1` would be acceptable.
checkConfigError 'It seems as if you.re trying to declare an option by placing it into .config. rather than .options.' config.wrong1 ./error-mkOption-in-config.nix
Expand Down
16 changes: 16 additions & 0 deletions lib/tests/modules/misc.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ config, lib, options, ... }:

{
options = {
result = lib.mkOption { };
};

config.result =
assert options._module.args.loc == [ "_module" "args" ];
assert lib.showOption options._module.args.loc == "_module.args";
assert lib.showOptionParent options._module.args.loc 1 == "_module";
assert lib.showOptionParent options._module.args.loc 2 == "";
assert lib.showOptionParent options._module.args.loc 3 == "";
"ok";

}

0 comments on commit 05147cf

Please sign in to comment.