/
mdoc.nix
104 lines (94 loc) · 2.91 KB
/
mdoc.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{ stdenv, jre, coursier-tools, lib, linkFarm }:
let
mdoc = coursier-tools.make-runnable {
launcher = coursier-tools.coursier-fetch {
pname = "mdoc";
version = "2.5.2";
artifact = "org.scalameta:mdoc_3";
sha256 = "sha256-w9SF5/voAWmdXB9WDA0CDxI1V3xnydy58YXjMN0wE38=";
};
mainClass = "mdoc.Main";
};
mkMdocDerivation = { name, src, buildInputs ? [ ] }: stdenv.mkDerivation {
inherit name src;
dontUnpack = true;
buildInputs = [ mdoc jre ] ++ buildInputs;
COURSIER_CACHE = ".nix/COURSIER_CACHE";
buildPhase =
''
# copilot wrote this.
classpathStr=$(
if [ -z "$CLASSPATH" ]; then
echo "";
else
echo "--classpath $CLASSPATH";
fi
)
mdoc --in $src $classpathStr
'';
installPhase = "cp -r out $out";
};
mdoc_outputs =
let
# Map a directory of files to a directory of derivations.
# The result is an attrset.
mapFilesRelativeBase =
{ f # function that maps file to derivation
, baseDirectory
,
}:
builtins.mapAttrs
(filename: filetype:
# TODO: support subdirectories when needed.
assert filetype == "regular";
{
name = filename;
# Concatenating paths this way is a must in order to avoid rebuilding all posts when either of them change.
path = f (baseDirectory + "/${filename}");
}
)
(builtins.readDir baseDirectory);
# Map a directory of files to a directory of derivations.
mapFilesRelative =
{ name
, f # function that maps file to derivation
, baseDirectory
}@args:
linkFarm
name
(lib.attrValues (
mapFilesRelativeBase (builtins.removeAttrs args [ "name" ])
));
buildInputsFor = src:
let
libRegistry =
builtins.mapAttrs
(pname: value: coursier-tools.coursier-fetch (value // { inherit pname; }))
(builtins.fromJSON (builtins.readFile ./mdoc-lib-index.json));
document = builtins.readFile src;
sections = builtins.split "\\+\\+\\+" document;
frontmatter = builtins.elemAt sections 2;
decoded = builtins.fromTOML frontmatter;
libNames = (decoded.extra or { }).scalaLibs or [ ];
in
builtins.map (libname: libRegistry.${libname}) libNames;
f = (src:
mkMdocDerivation {
name = "
mdoc-${builtins.baseNameOf src}";
buildInputs = buildInputsFor src;
inherit src;
}
);
in
mapFilesRelative
{
name = "mdoc-out";
inherit f;
baseDirectory = ./mdoc;
} // builtins.mapAttrs (_: value: value.path) (mapFilesRelativeBase {
inherit f;
baseDirectory = ./mdoc;
});
in
{ inherit mdoc mdoc_outputs; }