|
| 1 | +{ |
| 2 | + lib, |
| 3 | + config, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: |
| 7 | +let |
| 8 | + inherit (config.docs.menu) |
| 9 | + sections |
| 10 | + ; |
| 11 | + |
| 12 | + transformOption = |
| 13 | + let |
| 14 | + root = builtins.toString ../../.; |
| 15 | + mkGitHubDeclaration = user: repo: branch: subpath: { |
| 16 | + url = "https://github.com/${user}/${repo}/blob/${branch}/${subpath}"; |
| 17 | + name = "<${repo}/${subpath}>"; |
| 18 | + }; |
| 19 | + transformDeclaration = |
| 20 | + decl: |
| 21 | + if lib.hasPrefix root (builtins.toString decl) then |
| 22 | + mkGitHubDeclaration "nix-community" "nixvim" "main" ( |
| 23 | + lib.removePrefix "/" (lib.strings.removePrefix root (builtins.toString decl)) |
| 24 | + ) |
| 25 | + else if decl == "lib/modules.nix" then |
| 26 | + mkGitHubDeclaration "NixOS" "nixpkgs" "master" decl |
| 27 | + else |
| 28 | + decl; |
| 29 | + in |
| 30 | + opt: opt // { declarations = builtins.map transformDeclaration opt.declarations; }; |
| 31 | + |
| 32 | + docsPageModule = |
| 33 | + { name, config, ... }: |
| 34 | + { |
| 35 | + options = { |
| 36 | + enable = lib.mkOption { |
| 37 | + type = lib.types.bool; |
| 38 | + description = "Whether to enable this page/menu item."; |
| 39 | + default = true; |
| 40 | + example = false; |
| 41 | + }; |
| 42 | + target = lib.mkOption { |
| 43 | + type = lib.types.str; |
| 44 | + description = '' |
| 45 | + The target filepath, relative to the root of the docs. |
| 46 | + ''; |
| 47 | + default = lib.optionalString (name != "") (name + "/") + "index.md"; |
| 48 | + defaultText = lib.literalMD '' |
| 49 | + `<name>` joined with `"index.md"`. Separated by `"/"` if `<name>` is non-empty. |
| 50 | + ''; |
| 51 | + }; |
| 52 | + source = lib.mkOption { |
| 53 | + type = with lib.types; nullOr path; |
| 54 | + description = '' |
| 55 | + Markdown page. Set to null to create a menu entry without a corresponding file. |
| 56 | + ''; |
| 57 | + }; |
| 58 | + menu.location = lib.mkOption { |
| 59 | + type = with lib.types; listOf str; |
| 60 | + description = '' |
| 61 | + A location path that represents the page's position in the menu tree. |
| 62 | +
|
| 63 | + The text displayed in the menu is derived from this value, |
| 64 | + after the location of any parent nodes in the tree is removed. |
| 65 | +
|
| 66 | + For example, if this page has the location `[ "foo" "bar" ]` |
| 67 | + and there is another page with the location `[ "foo" ]`, |
| 68 | + then the menu will render as: |
| 69 | + ```markdown |
| 70 | + - foo |
| 71 | + - bar |
| 72 | + ``` |
| 73 | +
|
| 74 | + However if there was no other page with the `[ "foo" ]` location, |
| 75 | + the menu would instead render as: |
| 76 | + ```markdown |
| 77 | + - foo.bar |
| 78 | + ``` |
| 79 | + ''; |
| 80 | + default = |
| 81 | + let |
| 82 | + list = lib.splitString "/" config.target; |
| 83 | + last = lib.last list; |
| 84 | + rest = lib.dropEnd 1 list; |
| 85 | + in |
| 86 | + if last == "index.md" then |
| 87 | + rest |
| 88 | + else if lib.hasSuffix ".md" last then |
| 89 | + rest ++ [ (lib.removeSuffix ".md" last) ] |
| 90 | + else |
| 91 | + list; |
| 92 | + defaultText = lib.literalMD '' |
| 93 | + `target`, split by `"/"`, with any trailing `"index.md` or `".md"` suffixes removed. |
| 94 | + ''; |
| 95 | + }; |
| 96 | + menu.section = lib.mkOption { |
| 97 | + type = lib.types.enum (builtins.attrNames sections); |
| 98 | + description = '' |
| 99 | + Determines the menu section. |
| 100 | +
|
| 101 | + Must be a section defined in `docs.menu.sections`. |
| 102 | + ''; |
| 103 | + }; |
| 104 | + }; |
| 105 | + }; |
| 106 | +in |
| 107 | +{ |
| 108 | + options.docs._utils = lib.mkOption { |
| 109 | + type = with lib.types; lazyAttrsOf raw; |
| 110 | + description = "internal utils, modules, functions, etc"; |
| 111 | + default = { }; |
| 112 | + internal = true; |
| 113 | + visible = false; |
| 114 | + }; |
| 115 | + |
| 116 | + config.docs._utils = { |
| 117 | + # A liberal type that permits any superset of docsPageModule |
| 118 | + docsPageLiberalType = lib.types.submodule [ |
| 119 | + { _module.check = false; } |
| 120 | + docsPageModule |
| 121 | + ]; |
| 122 | + |
| 123 | + /** |
| 124 | + Uses `lib.optionAttrSetToDocList` to produce a list of docs-options. |
| 125 | +
|
| 126 | + A doc-option has the following attrs, as expected by `nixos-render-docs`: |
| 127 | +
|
| 128 | + ``` |
| 129 | + { |
| 130 | + loc, |
| 131 | + name, # rendered with `showOption loc` |
| 132 | + description, |
| 133 | + declarations, |
| 134 | + internal, |
| 135 | + visible, # normalised to a boolean |
| 136 | + readOnly, |
| 137 | + type, # normalised to `type.description` |
| 138 | + default,? # rendered with `lib.options.renderOptionValue` |
| 139 | + example,? # rendered with `lib.options.renderOptionValue` |
| 140 | + relatedPackages,? |
| 141 | + } |
| 142 | + ``` |
| 143 | +
|
| 144 | + Additionally, sub-options are recursively flattened into the list, |
| 145 | + unless `visible == "shallow"` or `visible == false`. |
| 146 | +
|
| 147 | + This function extends `lib.optionAttrSetToDocList` by also filtering out |
| 148 | + invisible and internal options, and by applying Nixvim's `transformOption` |
| 149 | + function. |
| 150 | +
|
| 151 | + The implementation is based on `pkgs.nixosOptionsDoc`: |
| 152 | + https://github.com/NixOS/nixpkgs/blob/e2078ef3/nixos/lib/make-options-doc/default.nix#L117-L126 |
| 153 | + */ |
| 154 | + mkOptionList = lib.flip lib.pipe [ |
| 155 | + (lib.flip builtins.removeAttrs [ "_module" ]) |
| 156 | + lib.optionAttrSetToDocList |
| 157 | + (builtins.map transformOption) |
| 158 | + (builtins.filter (opt: opt.visible && !opt.internal)) |
| 159 | + # TODO: consider supporting `relatedPackages` |
| 160 | + # See https://github.com/NixOS/nixpkgs/blob/61235d44/lib/options.nix#L103-L104 |
| 161 | + # and https://github.com/NixOS/nixpkgs/blob/61235d44/nixos/lib/make-options-doc/default.nix#L128-L165 |
| 162 | + ]; |
| 163 | + |
| 164 | + inherit docsPageModule; |
| 165 | + }; |
| 166 | +} |
0 commit comments