Skip to content

Commit 50c960c

Browse files
author
Julien Moutinho
committed
build/speed(mirage): reseparate targets into their own derivation
1 parent 74405a5 commit 50c960c

File tree

2 files changed

+128
-95
lines changed

2 files changed

+128
-95
lines changed

pkgs/by-name/dnsvizor/mirage.nix

Lines changed: 126 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,73 @@
88
writeShellApplication,
99
}:
1010

11+
let
12+
excludeDrvArgNames = [
13+
"target"
14+
"targets"
15+
"materializedDir"
16+
"monorepoQuery"
17+
"overrideUnikernel"
18+
"query"
19+
"queryArgs"
20+
"opamPackages"
21+
"mirageDir"
22+
];
23+
in
24+
1125
rec {
1226
# Description: run `mirage configure` on source,
1327
# with mirage, dune, and ocaml from `opam-nix`.
14-
configure =
15-
{
16-
pname,
17-
version,
18-
mirageDir ? ".",
19-
query,
20-
src,
21-
opamPackages ? opam-nix.queryToScope { } ({ mirage = "*"; } // query),
22-
...
23-
}:
24-
target:
25-
stdenv.mkDerivation {
26-
name = "mirage-${pname}-${target}";
27-
inherit src version;
28-
buildInputs = with opamPackages; [ mirage ];
29-
nativeBuildInputs = with opamPackages; [
30-
dune
31-
ocaml
32-
];
33-
buildPhase = ''
34-
runHook preBuild
35-
mirage configure -f ${mirageDir}/config.ml -t ${target}
36-
# Move Opam file to root so a recursive search for opam files isn't required.
37-
# Prefix it so it doesn't interfere with other packages.
38-
cp ${mirageDir}/mirage/${pname}-${target}.opam mirage-${pname}-${target}.opam
39-
runHook postBuild
40-
'';
41-
installPhase = ''
42-
runHook preInstall
43-
cp -R . $out
44-
runHook postInstall
45-
'';
46-
};
28+
configure = lib.extendMkDerivation {
29+
constructDrv = stdenv.mkDerivation;
30+
inherit excludeDrvArgNames;
31+
extendDrvArgs =
32+
finalAttrs:
33+
{
34+
pname,
35+
version,
36+
src,
37+
target,
38+
query,
39+
mirageDir ? finalAttrs.mirageDir or ".",
40+
opamPackages ? finalAttrs.opamPackages or opam-nix.queryToScope { } ({ mirage = "*"; } // query),
41+
...
42+
}:
43+
{
44+
name = "mirage-${pname}-${target}";
45+
buildInputs = with opamPackages; [ mirage ];
46+
nativeBuildInputs = with opamPackages; [
47+
dune
48+
ocaml
49+
];
50+
buildPhase = ''
51+
runHook preBuild
52+
mirage configure -f ${mirageDir}/config.ml -t ${target}
53+
# Move Opam file to root so a recursive search for opam files isn't required.
54+
# Prefix it so it doesn't interfere with other packages.
55+
cp ${mirageDir}/mirage/${pname}-${target}.opam mirage-${pname}-${target}.opam
56+
runHook postBuild
57+
'';
58+
installPhase = ''
59+
runHook preInstall
60+
cp -R . $out
61+
runHook postInstall
62+
'';
63+
};
64+
};
4765

4866
# Description: read opam files from mirage configuration
49-
# and build a unikernel in a separate output
50-
# for each one of the given targets.
67+
# and build a unikernel for the given target.
5168
build = lib.extendMkDerivation {
5269
constructDrv = stdenv.mkDerivation;
53-
excludeDrvArgNames = [
54-
"materializedDir"
55-
"monorepoQuery"
56-
"overrideUnikernel"
57-
"query"
58-
"queryArgs"
59-
];
70+
inherit excludeDrvArgNames;
6071
extendDrvArgs =
6172
finalAttrs:
6273
{
6374
pname,
6475
version,
65-
targets,
6676
src,
77+
target,
6778
monorepoQuery,
6879
materializedDir,
6980
mirageDir ? ".",
@@ -74,25 +85,20 @@ rec {
7485
}@args:
7586
let
7687
name = "mirage-${pname}";
77-
mirageConfIFD = configure args;
78-
mirageConf =
79-
target:
80-
configure (
81-
args
82-
// {
83-
opamPackages = packages target;
84-
}
85-
) target;
86-
packagesMaterialized =
87-
target: opam-nix.materializeOpamProject { } "${name}-${target}" (mirageConfIFD target) query;
88-
monorepoMaterialized =
89-
target: opam-nix.materializeBuildOpamMonorepo { } (mirageConfIFD target) monorepoQuery;
90-
monorepo =
91-
target: opam-nix.unmaterializeQueryToMonorepo { } (materializedDir + "/${target}/monorepo.json");
88+
mirageConfIFD = configure (args // { inherit target; });
89+
mirageConf = configure (
90+
args
91+
// {
92+
inherit target;
93+
opamPackages = packages;
94+
}
95+
);
96+
packagesMaterialized = opam-nix.materializeOpamProject { } "${name}-${target}" mirageConfIFD query;
97+
monorepoMaterialized = opam-nix.materializeBuildOpamMonorepo { } mirageConfIFD monorepoQuery;
98+
monorepo = opam-nix.unmaterializeQueryToMonorepo { } (materializedDir + "/${target}/monorepo.json");
9299
packages =
93-
target:
94100
(opam-nix.materializedDefsToScope {
95-
sourceMap."${name}-${target}" = finalAttrs.passthru.mirageConf.${target};
101+
sourceMap."${name}-${target}" = finalAttrs.passthru.mirageConf;
96102
} (materializedDir + "/${target}/packages.json")).overrideScope
97103
(
98104
finalOpam: previousOpam: {
@@ -112,9 +118,9 @@ rec {
112118
mkdir duniverse
113119
echo '(vendored_dirs *)' > duniverse/dune
114120
${lib.concatStringsSep "\n" (
115-
lib.mapAttrsToList (name: path: "cp -r ${path} duniverse/${lib.toLower name}") (
116-
finalAttrs.passthru.monorepo.${target}
117-
)
121+
lib.mapAttrsToList (name: path: ''
122+
cp -r ${path} duniverse/${lib.toLower name}
123+
'') finalAttrs.passthru.monorepo
118124
)}
119125
dune build ${mirageDir} --profile release
120126
runHook postBuild
@@ -138,26 +144,24 @@ rec {
138144
in
139145
{
140146
inherit name;
141-
inherit src;
142-
outputs = [ "out" ] ++ targets;
143147
installPhase = ''
144148
runHook preInstall
145-
${
146-
if stdenv.hostPlatform.isLinux && lib.elem "unix" targets then
147-
"ln -s $unix $out"
148-
else if stdenv.hostPlatform.isDarwin && lib.elem "macosx" targets then
149-
"ln -s $macosx $out"
150-
else
151-
"mkdir $out"
149+
cp -R --no-preserve=mode ${finalAttrs.passthru.packages."${name}-${target}"} $out
150+
${lib.optionalString
151+
(
152+
(stdenv.hostPlatform.isLinux && target == "unix")
153+
|| (stdenv.hostPlatform.isDarwin && target == "macosx")
154+
)
155+
''
156+
install -Dm755 $out/share/mirageos/${pname} $out/bin/${pname}
157+
rm -rf $out/share
158+
''
152159
}
153-
${lib.concatMapStringsSep "\n" (target: ''
154-
cp -R ${finalAttrs.passthru.packages.${target}."${name}-${target}"} ''$${target}
155-
'') targets}
156160
runHook postInstall
157161
'';
158162
passthru = {
159-
updateScript = writeShellApplication {
160-
name = "dnsvizor-update";
163+
updateScript = lib.getExe (writeShellApplication {
164+
name = "dnsvizor-update-${target}";
161165
runtimeInputs = [
162166
coreutils
163167
jq
@@ -166,28 +170,57 @@ rec {
166170
text = ''
167171
set -x
168172
materializedDir=$(nix --extra-experimental-features nix-command -L eval \
169-
-f. ${pname}.passthru.materializedDir)
170-
''
171-
+ lib.concatMapStringsSep "\n" (target: ''
172-
mkdir -p "${materializedDir}/${target}/"
173+
-f. ${pname}.passthru.${target}.passthru.materializedDir)
174+
mkdir -p "$materializedDir/${target}/"
173175
packagesJson=$(nix --extra-experimental-features nix-command -L build \
174176
--no-link --print-out-paths --allow-import-from-derivation --show-trace \
175-
-f. ${pname}.passthru.packagesMaterialized.${target})
176-
jq <"$packagesJson" >"''${materializedDir}/${target}/packages.json"
177+
-f. ${pname}.passthru.${target}.passthru.packagesMaterialized)
178+
jq <"$packagesJson" >"$materializedDir/${target}/packages.json"
177179
monorepoJson=$(nix --extra-experimental-features nix-command -L build \
178180
--no-link --print-out-paths --allow-import-from-derivation --show-trace \
179-
-f. ${pname}.passthru.monorepoMaterialized.${target})
180-
jq <"$monorepoJson" >"''${materializedDir}/${target}/monorepo.json"
181+
-f. ${pname}.passthru.${target}.passthru.monorepoMaterialized)
182+
jq <"$monorepoJson" >"$materializedDir/${target}/monorepo.json"
183+
'';
184+
});
185+
inherit
186+
materializedDir
187+
mirageConf
188+
mirageConfIFD
189+
monorepo
190+
monorepoMaterialized
191+
packages
192+
packagesMaterialized
193+
;
194+
};
195+
};
196+
};
197+
198+
# Description: build all given targets
199+
# or only a single if accessed in `passthru.${target}`.
200+
builds = lib.extendMkDerivation {
201+
constructDrv = stdenv.mkDerivation;
202+
inherit excludeDrvArgNames;
203+
extendDrvArgs =
204+
finalAttrs:
205+
{
206+
targets ? finalAttrs.targets or possibleTargets,
207+
...
208+
}@args:
209+
{
210+
passthru = lib.genAttrs targets (target: build (args // { inherit target; })) // {
211+
updateScript = lib.getExe (writeShellApplication {
212+
name = "dnsvizor-update";
213+
text = lib.concatMapStringsSep "\n" (target: ''
214+
${finalAttrs.passthru.${target}.passthru.updateScript}
181215
'') targets;
182-
};
183-
mirageConfIFD = lib.genAttrs targets mirageConfIFD;
184-
mirageConf = lib.genAttrs targets mirageConf;
185-
monorepoMaterialized = lib.genAttrs targets monorepoMaterialized;
186-
packagesMaterialized = lib.genAttrs targets packagesMaterialized;
187-
packages = lib.genAttrs targets packages;
188-
monorepo = lib.genAttrs targets monorepo;
189-
inherit materializedDir;
216+
});
190217
};
218+
phases = [ "installPhase" ];
219+
installPhase = ''
220+
runHook preInstall
221+
mkdir -p $out
222+
runHook postInstall
223+
'';
191224
};
192225
};
193226

pkgs/by-name/dnsvizor/package.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
let
1212
libMirage = callPackage ./mirage.nix { };
1313
in
14-
(libMirage.build (finalAttrs: {
14+
(libMirage.builds (finalAttrs: {
1515
pname = "dnsvizor";
1616
version = "0-unstable-2026-01-21";
1717
materializedDir = ./materialized;
@@ -89,7 +89,7 @@ in
8989
passthru = previousAttrs.passthru or { } // {
9090
updateScript = _experimental-update-script-combinators.sequence [
9191
(unstableGitUpdater { })
92-
(lib.getExe previousAttrs.passthru.updateScript)
92+
previousAttrs.passthru.updateScript
9393
];
9494
};
9595
}

0 commit comments

Comments
 (0)