diff --git a/exe-nix-buildkite/Main.hs b/exe-nix-buildkite/Main.hs index 7d07200..c5bc199 100644 --- a/exe-nix-buildkite/Main.hs +++ b/exe-nix-buildkite/Main.hs @@ -88,6 +88,12 @@ nixBuildDryRun jobsExpr = withTime "nix-build --dry-run" $ ExitSuccess -> pure res ExitFailure err -> error $ "nix-build --dry run failed with exit code: " ++ show err +copyDrvsToCache :: [String] -> String -> IO () +copyDrvsToCache jobs cache = withTime "copying drvs to cache" do + hPutStrLn stderr "copying drvs to cache" + callProcess "nix" $ ["copy", "--to", cache] ++ jobs + + -- Note: [nix-build --dry-run output] -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- The output of `nix-build --dry-run` looks like this (on stderr! not stdout): @@ -126,6 +132,8 @@ main = do Just _ -> error "SKIP_ALREADY_BUILT only accepts 'true' or 'false'." Nothing -> False + mCopyDrvToCache <- lookupEnv "COPY_DRV_TO_CACHE" + -- Run nix-instantiate on the jobs expression to instantiate .drvs for all -- things that may need to be built. inputDrvPaths <- nubOrd <$> nixInstantiate jobsExpr @@ -179,15 +187,26 @@ main = do step label drvPath = object [ "label" .= unpack label - , "command" .= String (pack $ unwords $ [ "nix-store" ] <> postBuildHook <> [ "-r", drvPath ]) + , "command" .= String (pack command) , "key" .= stepify drvPath , "depends_on" .= dependencies ] where + command = copyFromCache ++ realise + quote str = "'" ++ str ++ "'" + copyFromCache = case mCopyDrvToCache of + Nothing -> "" + Just cache -> unwords $ ["nix", "copy", "--from", quote cache, drvPath, "&&"] + realise = unwords $ [ "nix-store" ] <> postBuildHook <> [ "-r", drvPath ] dependencies = map stepify $ maybe [] S.toList $ Map.lookup drvPath closureG Data.ByteString.Lazy.putStr $ encode $ object [ "steps" .= steps ] + case mCopyDrvToCache of + Nothing -> pure () + Just cache -> copyDrvsToCache (S.toList jobSet) cache + + stepify :: String -> String stepify = take 99 . map replace . takeBaseName where diff --git a/hooks/command b/hooks/command index 608cbc0..7fcdec0 100755 --- a/hooks/command +++ b/hooks/command @@ -12,6 +12,10 @@ if [[ -n "${BUILDKITE_PLUGIN_NIX_BUILDKITE_SKIP_ALREADY_BUILT:-}" ]]; then export SKIP_ALREADY_BUILT=true fi +if [[ -n "${BUILDKITE_PLUGIN_NIX_BUILDKITE_COPY_DRV_TO_CACHE:-}" ]]; then + export COPY_DRV_TO_CACHE="$BUILDKITE_PLUGIN_NIX_BUILDKITE_COPY_DRV_TO_CACHE" +fi + echo "--- :nixos: Running nix-buildkite" nix-build -E "(import $DIR/../jobs.nix).nix-buildkite" -o nix-buildkite PIPELINE=$( ./nix-buildkite/bin/nix-buildkite "$NIX_FILE" )