Skip to content

Commit b022cc4

Browse files
committed
wip: refactor extending modules -> defferred module
1 parent 202edad commit b022cc4

File tree

11 files changed

+271
-321
lines changed

11 files changed

+271
-321
lines changed

lib/plugins/utils.nix

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,22 @@
124124
};
125125
};
126126

127-
docs.options.${docsfile} = {
128-
title = lib.last loc;
129-
description = lib.concatLines (
130-
[
131-
"**URL:** [${url}](${url})"
132-
""
133-
]
134-
++ lib.optionals (maintainers != [ ]) [
135-
"**Maintainers:** ${maintainersString}"
136-
""
137-
]
138-
++ lib.optionals (description != null && description != "") [
139-
"---"
140-
""
141-
description
142-
]
143-
);
144-
};
127+
docs.pages.${docsfile}.text = lib.mkMerge (
128+
[
129+
"# ${lib.last loc}"
130+
""
131+
"**URL:** [${url}](${url})"
132+
""
133+
]
134+
++ lib.optionals (maintainers != [ ]) [
135+
"**Maintainers:** ${maintainersString}"
136+
""
137+
]
138+
++ lib.optionals (description != null && description != "") [
139+
"---"
140+
""
141+
description
142+
]
143+
);
145144
};
146145
}

modules/docs/_util.nix

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
{
2-
lib,
3-
config,
4-
pkgs,
5-
...
6-
}:
1+
{ lib, ... }:
72
let
8-
inherit (config.docs.menu)
9-
sections
10-
;
11-
123
transformOption =
134
let
145
root = builtins.toString ../../.;
@@ -28,81 +19,6 @@ let
2819
decl;
2920
in
3021
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-
};
10622
in
10723
{
10824
options.docs._utils = lib.mkOption {
@@ -114,12 +30,6 @@ in
11430
};
11531

11632
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-
12333
/**
12434
Uses `lib.optionAttrSetToDocList` to produce a list of docs-options.
12535
@@ -160,7 +70,5 @@ in
16070
# See https://github.com/NixOS/nixpkgs/blob/61235d44/lib/options.nix#L103-L104
16171
# and https://github.com/NixOS/nixpkgs/blob/61235d44/nixos/lib/make-options-doc/default.nix#L128-L165
16272
];
163-
164-
inherit docsPageModule;
16573
};
16674
}

modules/docs/all.nix

Lines changed: 0 additions & 49 deletions
This file was deleted.

modules/docs/default.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88

99
imports = [
1010
./_util.nix
11-
./all.nix
1211
./files.nix
1312
./mdbook
1413
./menu
1514
./options.nix
15+
./pages.nix
1616
./platforms.nix
1717
];
1818

1919
config.docs.options = {
2020
docs = {
21-
menu.location = [ "docs" ];
2221
optionScopes = [ "docs" ];
23-
description = ''
22+
page.menu.location = [ "docs" ];
23+
page.text = ''
2424
Internal options used to construct these docs.
2525
'';
2626
};

modules/docs/files.nix

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,9 @@
11
{
22
lib,
3-
config,
43
pkgs,
54
...
65
}:
76
let
8-
inherit (config.docs._utils)
9-
docsPageModule
10-
;
11-
12-
docsPageType = lib.types.submodule (
13-
{
14-
name,
15-
config,
16-
options,
17-
...
18-
}:
19-
let
20-
derivationName = builtins.replaceStrings [ "/" ] [ "-" ] name;
21-
in
22-
{
23-
imports = [
24-
docsPageModule
25-
];
26-
options.text = lib.mkOption {
27-
type = with lib.types; nullOr lines;
28-
default = null;
29-
description = "Text of the file.";
30-
};
31-
config.source = lib.mkIf (config.text != null) (
32-
lib.mkDerivedConfig options.text (builtins.toFile derivationName)
33-
);
34-
}
35-
);
36-
377
user-guide = ../../docs/user-guide;
388

399
sourceTransformers = {
@@ -45,45 +15,30 @@ let
4515
};
4616
in
4717
{
48-
options.docs = {
49-
files = lib.mkOption {
50-
type = with lib.types; lazyAttrsOf docsPageType;
51-
description = ''
52-
A set of pages to include in the docs.
53-
'';
54-
default = { };
55-
};
56-
};
57-
58-
config.docs = {
59-
files =
60-
# TODO: contributing file
61-
{
62-
"" = {
63-
menu.section = "header";
64-
menu.location = [ "Home" ];
65-
source = pkgs.callPackage ./readme.nix {
66-
# TODO: get `availableVersions` and `baseHref` from module options
67-
};
18+
# TODO: contributing file
19+
docs.pages =
20+
{
21+
"" = {
22+
menu.section = "header";
23+
menu.location = [ "Home" ];
24+
source = pkgs.callPackage ./readme.nix {
25+
# TODO: get `availableVersions` and `baseHref` from module options
26+
};
27+
};
28+
}
29+
// lib.concatMapAttrs (
30+
name: type:
31+
let
32+
title = lib.removeSuffix ".md" name;
33+
transformer = sourceTransformers.${title} or lib.id;
34+
in
35+
lib.optionalAttrs (type == "regular") {
36+
"user-guide/${title}" = {
37+
menu.section = "user-guide";
38+
# TODO: define user-facing titles to show in the menu...
39+
menu.location = [ title ];
40+
source = transformer "${user-guide}/${name}";
6841
};
6942
}
70-
// lib.concatMapAttrs (
71-
name: type:
72-
let
73-
title = lib.removeSuffix ".md" name;
74-
transformer = sourceTransformers.${title} or lib.id;
75-
in
76-
lib.optionalAttrs (type == "regular") {
77-
"user-guide/${title}" = {
78-
menu.section = "user-guide";
79-
# TODO: define user-facing titles to show in the menu...
80-
menu.location = [ title ];
81-
source = transformer "${user-guide}/${name}";
82-
};
83-
}
84-
) (builtins.readDir user-guide);
85-
86-
# Register for inclusion in `all`
87-
_allInputs = [ "files" ];
88-
};
43+
) (builtins.readDir user-guide);
8944
}

modules/docs/menu/default.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ in
1616
options.docs.menu.src = lib.mkOption {
1717
type = lib.types.lines;
1818
description = ''
19-
MDBook SUMMARY menu. Generated from pages defined in
20-
${lib.concatMapStringsSep ", " (name: "`docs.${name}`") config.docs._allInputs}.
19+
MDBook SUMMARY menu. Generated from `docs.pages.<name>.menu`.
2120
2221
See MDBook's [SUMMARY.md](https://rust-lang.github.io/mdBook/format/summary.html) docs.
2322
'';

modules/docs/menu/sections.nix

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
...
55
}:
66
let
7-
inherit (config.docs._utils)
8-
docsPageLiberalType
9-
;
10-
11-
pagesBySection = builtins.groupBy (page: page.menu.section) config.docs.all;
7+
# A set of menu sections, each mapped to a list of pages
8+
# { section = [page]; }
9+
pagesBySection = builtins.groupBy (page: page.menu.section) (builtins.attrValues config.docs.pages);
1210

1311
# Converts a list of pages into a tree that defines the shape of the menu
1412
mkPagesTree =
@@ -124,7 +122,9 @@ let
124122
default = true;
125123
};
126124
pages = lib.mkOption {
127-
type = with lib.types; listOf docsPageLiberalType;
125+
# NOTE: Use attrs here to avoid defaults & internal definitions from the docsPageType
126+
# TODO: Maybe don't expose this as a module option at all? A privately scoped binding would be fine.
127+
type = with lib.types; listOf attrs;
128128
description = "Pages that belong to this section.";
129129
visible = "shallow";
130130
readOnly = true;

0 commit comments

Comments
 (0)