|
| 1 | +{ |
| 2 | + lib, |
| 3 | + config, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: |
| 7 | +let |
| 8 | + inherit (config.docs.menu) |
| 9 | + categories |
| 10 | + ; |
| 11 | + |
| 12 | + transformOptions = |
| 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.position = lib.mkOption { |
| 59 | + type = with lib.types; listOf str; |
| 60 | + description = "The position in the menu."; |
| 61 | + default = |
| 62 | + let |
| 63 | + list = lib.splitString "/" config.target; |
| 64 | + last = lib.last list; |
| 65 | + rest = lib.dropEnd 1 list; |
| 66 | + in |
| 67 | + if last == "index.md" then |
| 68 | + rest |
| 69 | + else if lib.hasSuffix ".md" last then |
| 70 | + rest ++ [ (lib.removeSuffix ".md" last) ] |
| 71 | + else |
| 72 | + list; |
| 73 | + defaultText = lib.literalMD '' |
| 74 | + `target`, split by `"/"`, with any trailing `"index.md` or `".md"` suffixes removed. |
| 75 | + ''; |
| 76 | + }; |
| 77 | + menu.category = lib.mkOption { |
| 78 | + type = lib.types.enum (builtins.attrNames categories); |
| 79 | + description = '' |
| 80 | + A category defined in `docs.menu.categories`. |
| 81 | +
|
| 82 | + Determines the menu category, section, etc. |
| 83 | + ''; |
| 84 | + }; |
| 85 | + }; |
| 86 | + }; |
| 87 | +in |
| 88 | +{ |
| 89 | + options.docs._utils = lib.mkOption { |
| 90 | + type = with lib.types; lazyAttrsOf raw; |
| 91 | + description = "internal utils, modules, functions, etc"; |
| 92 | + default = { }; |
| 93 | + internal = true; |
| 94 | + visible = false; |
| 95 | + }; |
| 96 | + |
| 97 | + config.docs._utils = { |
| 98 | + # Can be overridden to an externally defined pkgs, |
| 99 | + # because _module.args.pkgs will be stubbed when evaling for the docs |
| 100 | + docsPkgs = lib.mkOptionDefault pkgs; |
| 101 | + |
| 102 | + # A liberal type that permits any superset of docsPageModule |
| 103 | + docsPageLiberalType = lib.types.submodule [ |
| 104 | + { _module.check = false; } |
| 105 | + docsPageModule |
| 106 | + ]; |
| 107 | + |
| 108 | + mkOptionList = lib.flip lib.pipe [ |
| 109 | + (lib.flip builtins.removeAttrs [ "_module" ]) |
| 110 | + lib.optionAttrSetToDocList |
| 111 | + (builtins.map transformOptions) |
| 112 | + (builtins.filter (opt: opt.visible && !opt.internal)) |
| 113 | + # TODO: consider supporting `relatedPackages` |
| 114 | + # See https://github.com/NixOS/nixpkgs/blob/61235d44/lib/options.nix#L103-L104 |
| 115 | + # and https://github.com/NixOS/nixpkgs/blob/61235d44/nixos/lib/make-options-doc/default.nix#L128-L165 |
| 116 | + ]; |
| 117 | + |
| 118 | + inherit docsPageModule; |
| 119 | + }; |
| 120 | +} |
0 commit comments