Skip to content

Commit

Permalink
Use lib.makeScope for consistent package resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tadfisher committed Jul 16, 2024
1 parent 6e37e6e commit 0160c94
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ produced by =gradle2nix=. It performs the following:

- =lockFile= :: Path to the lock file generated by =gradle2nix=
(e.g. =gradle.lock=).
- =gradlePackage= :: The Gradle package to use. Default is
- =gradle= :: The Gradle package to use. Default is
=pkgs.gradle=.
- =buildJdk= :: Override the default JDK used to run Gradle itself.
- =fetchers= :: Override functions which fetch dependency
Expand Down
63 changes: 6 additions & 57 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,11 @@
pkgs ? import <nixpkgs> { },
}:

with pkgs;

let
buildMavenRepo = callPackage ./nix/build-maven-repo.nix { };

gradleSetupHook = makeSetupHook {
name = "gradle-setup-hook";
propagatedBuildInputs = [ gradle ];
} ./nix/setup-hook.sh;

buildGradlePackage = callPackage ./nix/build-gradle-package.nix {
inherit buildMavenRepo gradleSetupHook;
};

gradle2nix = buildGradlePackage {
pname = "gradle2nix";
version = "2.0.0";
lockFile = ./gradle.lock;

src = lib.cleanSourceWith {
filter = lib.cleanSourceFilter;
src = lib.cleanSourceWith {
filter =
path: type:
let
baseName = baseNameOf path;
in
!(
(type == "directory" && (baseName == "build" || baseName == ".idea" || baseName == ".gradle"))
|| (lib.hasSuffix ".iml" baseName)
);
src = ./.;
};
};

gradleInstallFlags = [ ":app:installDist" ];

postInstall = ''
mkdir -p $out/{bin,/lib/gradle2nix}
cp -r app/build/install/gradle2nix/* $out/lib/gradle2nix/
rm $out/lib/gradle2nix/bin/gradle2nix.bat
ln -sf $out/lib/gradle2nix/bin/gradle2nix $out/bin
'';

passthru = {
inherit buildGradlePackage buildMavenRepo gradleSetupHook;
};

meta = with lib; {
inherit (gradle.meta) platforms;
description = "Wrap Gradle builds with Nix";
homepage = "https://github.com/tadfisher/gradle2nix";
license = licenses.asl20;
maintainers = with maintainers; [ tadfisher ];
mainProgram = "gradle2nix";
};
};
scope = pkgs.callPackage ./nix { };
in
gradle2nix
scope.gradle2nix.overrideAttrs (attrs: {
passthru = (attrs.passthru or { }) // {
inherit (scope) buildGradlePackage buildMavenRepo gradleSetupHook;
};
})
13 changes: 6 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,26 @@
system:
let
pkgs = nixpkgs.legacyPackages.${system};
scope = pkgs.callPackage ./nix { };
inherit (nixpkgs) lib;
in
{
builders = {
inherit (self.packages.${system}.gradle2nix) buildGradlePackage buildMavenRepo;
inherit (scope) buildGradlePackage buildMavenRepo;
default = self.packages.${system}.buildGradlePackage;
};

packages = {
inherit (self.packages.${system}.gradle2nix) gradleSetupHook;
gradle2nix = pkgs.callPackage ./default.nix { };
inherit (scope) gradle2nix gradleSetupHook;
default = self.packages.${system}.gradle2nix;
};

apps = rec {
apps = {
gradle2nix = {
type = "app";
program = lib.getExe self.packages.${system}.default;
program = lib.getExe self.packages.${system}.gradle2nix;
};

default = gradle2nix;
default = self.apps.${system}.gradle2nix;
};

formatter = pkgs.writeShellScriptBin "gradle2nix-fmt" ''
Expand Down
43 changes: 10 additions & 33 deletions nix/build-gradle-package.nix
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
# This file is generated by gradle2nix.
#
# Example usage (e.g. in default.nix):
#
# with (import <nixpkgs> {});
# let
# buildGradle = callPackage ./gradle.nix {};
# in
# buildGradle {
# lockFile = ./gradle.lock;
#
# src = ./.;
#
# gradleFlags = [ "installDist" ];
#
# installPhase = ''
# mkdir -p $out
# cp -r app/build/install/myproject $out
# '';
# }

{
lib,
stdenv,
gradle,
buildMavenRepo,
gradleSetupHook,
writeText,
Expand All @@ -31,10 +9,8 @@
{
# Path to the lockfile generated by gradle2nix (e.g. gradle.lock).
lockFile ? null,
pname ? "project",
version ? null,
# The Gradle package to use. Default is 'pkgs.gradle'.
gradlePackage ? gradle,
gradle ? null,
# Override the default JDK used to run Gradle itself.
buildJdk ? null,
# Override functions which fetch dependency artifacts.
Expand Down Expand Up @@ -105,24 +81,25 @@
let
inherit (builtins) removeAttrs;

gradleSetupHook' = gradleSetupHook.overrideAttrs (_: {
propagatedBuildInputs = [ gradlePackage ];
});

offlineRepo =
if lockFile != null then buildMavenRepo { inherit lockFile fetchers overrides; } else null;

buildGradlePackage = stdenv.mkDerivation (
finalAttrs:
{

inherit buildJdk pname version;
inherit buildJdk gradle;

inherit (offlineRepo) gradleInitScript;

dontStrip = true;

nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ gradleSetupHook' ];
gradleSetupHook =
if (finalAttrs.gradle != null) then
gradleSetupHook.override { inherit (finalAttrs) gradle; }
else
gradleSetupHook;

nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ finalAttrs.gradleSetupHook ];

gradleFlags =
[ "--console=plain" ]
Expand All @@ -137,7 +114,7 @@ let
"lockFile"
"fetchers"
"nativeBuildInputs"
"overlays"
"overrides"
"passthru"
]
);
Expand Down
15 changes: 15 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
lib,
gradle,
newScope,
}:

lib.makeScope newScope (
self: with self; {
inherit gradle;
buildGradlePackage = callPackage ./build-gradle-package.nix { };
buildMavenRepo = callPackage ./build-maven-repo.nix { };
gradleSetupHook = callPackage ./gradle-setup-hook.nix { };
gradle2nix = callPackage ./gradle2nix.nix { };
}
)
7 changes: 7 additions & 0 deletions nix/gradle-setup-hook.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ makeSetupHook, gradle }:

makeSetupHook {
name = "gradle-setup-hook";
propagatedBuildInputs = [ gradle ];
passthru.gradle = gradle;
} ./setup-hook.sh
51 changes: 51 additions & 0 deletions nix/gradle2nix.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
lib,
buildGradlePackage,
buildMavenRepo,
gradle,
gradleSetupHook,
}:

buildGradlePackage {
pname = "gradle2nix";
version = "2.0.0";
lockFile = ../gradle.lock;

src = lib.cleanSourceWith {
filter = lib.cleanSourceFilter;
src = lib.cleanSourceWith {
filter =
path: type:
let
baseName = baseNameOf path;
in
!(
(type == "directory" && (baseName == "build" || baseName == ".idea" || baseName == ".gradle"))
|| (lib.hasSuffix ".iml" baseName)
);
src = ../.;
};
};

gradleInstallFlags = [ ":app:installDist" ];

postInstall = ''
mkdir -p $out/{bin,/lib/gradle2nix}
cp -r app/build/install/gradle2nix/* $out/lib/gradle2nix/
rm $out/lib/gradle2nix/bin/gradle2nix.bat
ln -sf $out/lib/gradle2nix/bin/gradle2nix $out/bin
'';

passthru = {
inherit buildGradlePackage buildMavenRepo gradleSetupHook;
};

meta = with lib; {
inherit (gradle.meta) platforms;
description = "Wrap Gradle builds with Nix";
homepage = "https://github.com/tadfisher/gradle2nix";
license = licenses.asl20;
maintainers = with maintainers; [ tadfisher ];
mainProgram = "gradle2nix";
};
}

0 comments on commit 0160c94

Please sign in to comment.