bscpkgs/garlic/stages/unit.nix

103 lines
2.2 KiB
Nix
Raw Normal View History

2020-10-09 01:00:55 +08:00
{
stdenv
, bash
2020-10-14 22:27:47 +08:00
, writeText
2020-10-09 01:00:55 +08:00
}:
{
stages
, conf
}:
with stdenv.lib;
let
dStages = foldr (stageFn: {conf, prevStage, stages}: {
conf = conf;
2020-10-09 21:55:37 +08:00
prevStage = stageFn {nextStage=prevStage; conf=conf;};
stages = [ (stageFn {nextStage=prevStage; conf=conf;}) ] ++ stages;
2020-10-09 01:00:55 +08:00
})
{conf=conf; stages=[]; prevStage=null;} stages;
stageProgram = stage:
if stage ? programPath
then "${stage}${stage.programPath}" else "${stage}";
linkStages = imap1 (i: s: {
num = "${toString i}";
name = "${toString i}-${baseNameOf s.name}";
stage = s;
programPath = stageProgram s;
}) dStages.stages;
2020-10-09 01:48:20 +08:00
desc = builtins.concatStringsSep "\n"
(map (x: "# ${x.stage}") linkStages);
2020-10-09 01:00:55 +08:00
firstStage = (x: x.programPath) (elemAt linkStages 0);
2020-10-14 22:27:47 +08:00
jsonConf = writeText "garlic_config.json" (builtins.toJSON conf);
2020-10-09 01:00:55 +08:00
in
2020-11-17 18:12:12 +08:00
builtins.trace "evaluating unit ${conf.unitName}"
2020-10-09 01:00:55 +08:00
stdenv.mkDerivation {
name = "unit";
preferLocalBuild = true;
phases = [ "installPhase" ];
2020-10-09 01:48:20 +08:00
inherit desc;
2020-10-09 01:00:55 +08:00
installPhase = ''
cat > $out << EOF
#!/bin/sh -e
2020-10-09 01:48:20 +08:00
${desc}
2020-10-09 01:00:55 +08:00
if [ -z "\$GARLIC_OUT" ]; then
>&2 echo "GARLIC_OUT not defined, aborting"
exit 1
fi
2020-10-30 00:42:03 +08:00
if [ -z "\$GARLIC_EXPERIMENT" ]; then
>&2 echo "GARLIC_EXPERIMENT not defined, aborting"
exit 1
fi
if [ -z "\$GARLIC_INDEX" ]; then
>&2 echo "GARLIC_INDEX not defined, aborting"
exit 1
fi
cd "\$GARLIC_OUT"
2020-10-09 01:00:55 +08:00
# Set the experiment unit in the environment
export GARLIC_UNIT=$(basename $out)
2020-10-30 00:42:03 +08:00
# Create an index entry
rm -f "\$GARLIC_INDEX/${conf.unitName}" \
"\$GARLIC_INDEX/${conf.expName}"
ln -Tfs "../out/\$GARLIC_UNIT" \
2020-10-30 00:42:03 +08:00
"\$GARLIC_INDEX/${conf.unitName}"
ln -Tfs "../out/\$GARLIC_EXPERIMENT" \
2020-10-30 00:42:03 +08:00
"\$GARLIC_INDEX/${conf.expName}"
if [ -e "\$GARLIC_UNIT" ]; then
>&2 echo "skipping, unit path already exists: \$GARLIC_UNIT"
exit 0
fi
2020-10-09 01:00:55 +08:00
# And change the working directory
mkdir \$GARLIC_UNIT
cd \$GARLIC_UNIT
2020-10-14 22:27:47 +08:00
# Copy the configuration for the unit to the output path
cp ${jsonConf} garlic_config.json
2020-10-09 01:00:55 +08:00
# Finally, execute the first stage:
exec ${firstStage}
EOF
chmod +x $out
'';
}