Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion exe-nix-buildkite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
Expand Down