diff --git a/README.org b/README.org index b3c0ca0..8d06e4c 100644 --- a/README.org +++ b/README.org @@ -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 diff --git a/default.nix b/default.nix index f5a1b7e..0aa801d 100644 --- a/default.nix +++ b/default.nix @@ -2,62 +2,11 @@ pkgs ? import { }, }: -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; + }; +}) diff --git a/flake.nix b/flake.nix index c3b78bf..52e7bf5 100644 --- a/flake.nix +++ b/flake.nix @@ -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" '' diff --git a/nix/build-gradle-package.nix b/nix/build-gradle-package.nix index a2e0ce0..b963c45 100644 --- a/nix/build-gradle-package.nix +++ b/nix/build-gradle-package.nix @@ -1,28 +1,6 @@ -# This file is generated by gradle2nix. -# -# Example usage (e.g. in default.nix): -# -# with (import {}); -# 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, @@ -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. @@ -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" ] @@ -137,7 +114,7 @@ let "lockFile" "fetchers" "nativeBuildInputs" - "overlays" + "overrides" "passthru" ] ); diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..a49ce70 --- /dev/null +++ b/nix/default.nix @@ -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 { }; + } +) diff --git a/nix/gradle-setup-hook.nix b/nix/gradle-setup-hook.nix new file mode 100644 index 0000000..948d72b --- /dev/null +++ b/nix/gradle-setup-hook.nix @@ -0,0 +1,7 @@ +{ makeSetupHook, gradle }: + +makeSetupHook { + name = "gradle-setup-hook"; + propagatedBuildInputs = [ gradle ]; + passthru.gradle = gradle; +} ./setup-hook.sh diff --git a/nix/gradle2nix.nix b/nix/gradle2nix.nix new file mode 100644 index 0000000..360ad9b --- /dev/null +++ b/nix/gradle2nix.nix @@ -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"; + }; +}