Skip to content

Commit 0499f81

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

File tree

8 files changed

+241
-281
lines changed

8 files changed

+241
-281
lines changed

modules/docs/_util.nix

+2-78
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
22
lib,
33
config,
4-
pkgs,
54
...
65
}:
76
let
8-
inherit (config.docs.menu)
9-
sections
7+
inherit (config.docs._utils)
8+
docsPageModule
109
;
1110

1211
transformOption =
@@ -28,81 +27,6 @@ let
2827
decl;
2928
in
3029
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-
};
10630
in
10731
{
10832
options.docs._utils = lib.mkOption {

modules/docs/all.nix

-49
This file was deleted.

modules/docs/default.nix

+3-3
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

+24-69
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
}

0 commit comments

Comments
 (0)