Skip to content

Commit

Permalink
Move dependency dir out of project directory (#12)
Browse files Browse the repository at this point in the history
This means that sbt can be used anywhere in the source tree and will
always be able to find its dependencies.
  • Loading branch information
tpwrules authored Aug 30, 2022
1 parent a3621a7 commit fe0044d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ If you're not ready to migrate, the previous version is kept in a branch called
### Changed behaviour
- `postConfigure` will now run after extracting the dependencies (was running before). The whole configure phase is used to extract the dependencies.
- The dependencies are no longer stored in a `.nix` directory, but rather in the default locations provided by sbt in `--no-share` mode.
- The dependencies are no longer stored in a `.nix` directory, but rather in a temporary global directory mirroring the structure used by sbt in `--no-share` mode.
- `overrideDepsAttrs` now takes two parameters, `final` and `prev`, instead of just `prev`.
### New options
Expand Down
7 changes: 3 additions & 4 deletions lib/archival-strategies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
fileExtension = "";
packerFragment = ''
mkdir -p $out
cp -ar project/{.sbtboot,.boot,.ivy,.coursier} $out
cp -ar $SBT_DEPS/project/. $out
'';
};
in {
Expand All @@ -20,7 +20,7 @@ in {
packerFragment = ''
tar --owner=0 --group=0 --numeric-owner --format=gnu \
--sort=name --mtime="@$SOURCE_DATE_EPOCH" \
-C project -cf "$out" .sbtboot .boot .ivy .coursier
-C $SBT_DEPS/project -cf $out .
'';
extractorFragment = deps: ''
tar -C "$target/project" -xpf ${deps}
Expand All @@ -34,7 +34,7 @@ in {
tar --owner=0 --group=0 --numeric-owner --format=gnu \
--sort=name --mtime="@$SOURCE_DATE_EPOCH" \
-I 'zstd -c --fast=3 -' \
-C project -cf "$out" .sbtboot .boot .ivy .coursier
-C $SBT_DEPS/project -cf $out .
'';
extractorFragment = deps: ''
tar -I zstd -C "$target/project" -xpf ${deps}
Expand All @@ -53,7 +53,6 @@ in {
// {
nativeBuildInputs = [stow];
extractorFragment = deps: ''
mkdir -p "$target/project"
stow -t "$target/project" -d ${builtins.dirOf deps} --no-folding ${builtins.baseNameOf deps}
'';
};
Expand Down
27 changes: 14 additions & 13 deletions lib/dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
writeShellScript,
}: {
namePrefix,
sbtEnv,
sbtEnvSetupCmds,
src,
patches ? [],
sha256,
Expand All @@ -25,8 +25,7 @@
} @ args: let
archivalStrategy = (callPackage ./archival-strategies.nix {}).${args.archivalStrategy};

mkAttrs = drv: (sbtEnv
// {
mkAttrs = drv: ({
inherit src patches;

name = namePrefix + archivalStrategy.fileExtension;
Expand All @@ -49,6 +48,8 @@
buildPhase = ''
runHook preBuild
${sbtEnvSetupCmds}
echo "running:"
${let
matches = builtins.match ''
Expand All @@ -63,32 +64,31 @@
${warmupCommand}
echo "stripping out comments containing dates"
find project -name '*.properties' -type f -exec sed -i '/^#/d' {} \;
find $SBT_DEPS/project -name '*.properties' -type f -exec sed -i '/^#/d' {} \;
echo "removing non-reproducible accessory files"
find project -name '*.lock' -type f -print0 | xargs -r0 rm -rfv
find project -name '*.log' -type f -print0 | xargs -r0 rm -rfv
find $SBT_DEPS/project -name '*.lock' -type f -print0 | xargs -r0 rm -rfv
find $SBT_DEPS/project -name '*.log' -type f -print0 | xargs -r0 rm -rfv
echo "fixing-up the compiler bridge and interface"
find project -name 'org.scala-sbt-compiler-bridge_*' -type f -print0 | xargs -r0 strip-nondeterminism
find project -name 'org.scala-sbt-compiler-interface_*' -type f -print0 | xargs -r0 strip-nondeterminism
find $SBT_DEPS/project -name 'org.scala-sbt-compiler-bridge_*' -type f -print0 | xargs -r0 strip-nondeterminism
find $SBT_DEPS/project -name 'org.scala-sbt-compiler-interface_*' -type f -print0 | xargs -r0 strip-nondeterminism
echo "removing runtime jar"
find project -name rt.jar -delete
find $SBT_DEPS/project -name rt.jar -delete
echo "removing empty directories"
find project -type d -empty -delete
find $SBT_DEPS/project -type d -empty -delete
runHook postBuild
'';

installPhase = ''
runHook preInstall
mkdir -p project/{.sbtboot,.boot,.ivy,.coursier}
${lib.optionalString optimize ''
${rdfind}/bin/rdfind -makesymlinks true -outputname /dev/null project/{.coursier,.ivy,.boot,.sbtboot}
${symlinks}/bin/symlinks -rc project
${rdfind}/bin/rdfind -makesymlinks true -outputname /dev/null $SBT_DEPS/project/{.sbtboot,.boot,.ivy,.coursier}
${symlinks}/bin/symlinks -rc $SBT_DEPS/project
''}
${archivalStrategy.packerFragment}
Expand All @@ -108,6 +108,7 @@
target="$1"
echo "extracting dependencies in $target"
mkdir -p "$target/project"
start="$(date +%s.%N)"
${archivalStrategy.extractorFragment drv}
Expand Down
25 changes: 15 additions & 10 deletions lib/sbt-derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@
drvAttrs =
removeAttrs args ["overrideDepsAttrs" "depsSha256" "depsWarmupCommand" "depsPackingStrategy" "depsOptimize"];

depsDir = ".nix";

sbtEnv = {
SBT_OPTS = (args.SBT_OPTS or "") + " --no-share";
COURSIER_CACHE = "project/.coursier";
};
# create a random temporary directory and configure sbt using environment
# variables to use directories within it as a cache dir. we use the "project"
# subdirectory so that all the dependencies can be extracted to the directory
# with build.sbt and sbt will use them in --no-share mode, if the user finds
# this convenient for testing.
sbtEnvSetupCmds = ''
export SBT_DEPS=$(mktemp -d)
export SBT_OPTS="-Dsbt.global.base=$SBT_DEPS/project/.sbtboot -Dsbt.boot.directory=$SBT_DEPS/project/.boot -Dsbt.ivy.home=$SBT_DEPS/project/.ivy $SBT_OPTS"
export COURSIER_CACHE=$SBT_DEPS/project/.coursier
mkdir -p $SBT_DEPS/project/{.sbtboot,.boot,.ivy,.coursier}
'';

dependencies = (callPackage ./dependencies.nix {}) {
inherit src patches nativeBuildInputs sbtEnv;
inherit src patches nativeBuildInputs sbtEnvSetupCmds;

namePrefix = "${pname}-sbt-dependencies";
sha256 = depsSha256;
Expand All @@ -52,8 +57,7 @@
overrideAttrs = overrideDepsAttrs;
};
in
stdenv.mkDerivation (sbtEnv
// drvAttrs
stdenv.mkDerivation (drvAttrs
// {
nativeBuildInputs = [sbt] ++ nativeBuildInputs;

Expand All @@ -62,7 +66,8 @@ in
configurePhase = ''
runHook preConfigure
${dependencies.extractor} .
${sbtEnvSetupCmds}
${dependencies.extractor} $SBT_DEPS
runHook postConfigure
'';
Expand Down

0 comments on commit fe0044d

Please sign in to comment.