gradle-purifier full guide

Detailed setup, dependency management, build commands, and troubleshooting. Start with the overview for the initial commands; use the sections below for the complete setup and behavior. gradle-purifier is classified at maturity tier 2 (see tier assessment for details).

Contents

Setup and activation

Use the overview’s Quickstart for the initial command sequence. The setup details below apply to that workflow.

Commit the lockfiles that the lock command writes, including the build tool’s own lockfile when it has one. Keep using the project’s usual build, test and editor commands with this setup. Nix provides the locked dependencies; the development commands above run outside the Nix sandbox. No project Nix files are needed for this workflow.

Sandboxed builds are an optional next step.

Install these packages through your usual Nix configuration.

Version compatibility

Tool or language Packaged defaults Tested versions
Gradle Project-selected 5.6.4, 6.9.4, 7.6.6, 8.14, 8.14.3
Java (runs the tool) 21 11, 17, 21
Java (compiles the project) Project-selected 8, 21
Java (runs the project) Project-selected 8

Operating systems and architectures

OS CPU architecture Packaged toolchain Recorded workflow
macOS ARM64 Yes Tested
Linux ARM64 Yes Tested
Linux x86-64 Yes Tested

Choosing versions

Component Selected by
Build tool / package manager: Gradle The project’s Gradle wrapper selects the Gradle distribution.
gradlew, gradle/wrapper/gradle-wrapper.properties
Runtime running the tool: JDK For projects without gradle-purifier.sdks.json, JAVA_HOME selects the capture JVM when –java is absent; otherwise the packaged fallback is used.
JAVA_HOME
Runtime running the tool: JDK For projects without gradle-purifier.sdks.json, an explicit –java option selects the JDK major for capture.
--java
Runtime running the tool: JDK On x86_64-linux, the project SDK declaration selects a source-owned manager adapter with explicit executable and runtime home.
gradle-purifier.sdks.json
Project compiler: JDK On x86_64-linux, the project SDK declaration supplies immutable SDK candidates; original native Gradle compiler and Javadoc selections must match declared exact SDK identities.
gradle-purifier.sdks.json
Project runtime: JDK On x86_64-linux, the project SDK declaration supplies immutable SDK candidates; original native Test and JavaExec selections must match declared exact SDK identities.
gradle-purifier.sdks.json

Requirements

Development loop

After each lock, run prefetch at the build root:

nix shell 'github:nix-purifiers/gradle-purifier'
gradle-purifier prefetch

It realizes in the Nix store the locked downloads, the JDK, the Gradle distribution of the wrapper and, for a Kotlin/Native build, the patched toolchain. Their garbage-collector roots go in .gradle-purifier/ beside the lockfile. It also installs an init script in the init.d directory of the Gradle user home, which Gradle applies to every build. For the builds of this root, the script points every repository at the locked downloads. It leaves every other build alone. After that, the build tool works as usual:

./gradlew build

prefetch links the distribution where ./gradlew looks for it, so the wrapper downloads nothing. Gradle reads the locked files where they are, so its own download cache stays empty. A build that needs a download the lockfile does not hold fails, and the init script names gradle-purifier lock. When the lockfile changed since the last prefetch, the init script says so and the build resolves the usual way until prefetch runs again. prefetch warns when JAVA_HOME names another Java major version than the lock, and prints the JDK to use.

Gradle keeps a lockfile of its own only in a build that turns on its dependency locking. Without one, gradle-purifier.lock.json records the resolution as well as the downloads: it holds the version listings (maven-metadata.xml) that dynamic versions resolve through, and lock resolves from them again. In a build with dependency locking, lock keeps the resolution of Gradle’s lockfiles as they are.

Gradle’s dependency locking writes a lockfile for each project, for the settings and for each buildscript, rather than one file. Gradle’s lockfiles therefore stay in the tree: the builder takes no argument for a copy of them kept elsewhere, and the commands take no --tool-lockfile. A build kept external keeps only gradle-purifier.lock.json outside the tree.

These are the commands:

For a build kept external, each command takes --lockfile <path>, or the environment variable GRADLE_PURIFIER_LOCKFILE. The dev shell of the flake that passes lockFile to the builder can set the variable to the same path.

Publishing

nix shell 'github:nix-purifiers/gradle-purifier'
gradle-purifier publish

The command builds the release with Nix, from the lockfile, then runs the publish task of Gradle (or the tasks given after publish) with the artifacts and the Gradle module metadata of every Maven publication replaced by the files Nix built. The publishing repositories and credentials of the build apply, and nothing is compiled outside the sandbox. Gradle generates the POM from the build, as it does for any publish.

Using locked dependencies

gradle-purifier keeps Gradle’s development workflow on locked, Nix-resolved dependencies. Use the normal build and test commands in your working tree; building the project itself in the Nix sandbox is an optional next step.

Working offline

After prefetch, ./gradlew, a Gradle on the PATH and an IDE build with the network cut off. Gradle reads the locked files from the Nix store, so its own download cache stays empty.

Language tooling integration

An IDE imports a Gradle build with its own Gradle run, through the Gradle tooling API: IntelliJ IDEA, and Eclipse and VS Code through Buildship and jdtls. The init script that prefetch installs applies to those runs too, so the import reads the locked downloads. The lock run resolves the sources and the javadoc of every library, so an IDE that attaches them finds them in the lock. An IDE set to use the Gradle wrapper finds the distribution that prefetch linked. One set to a local distribution can use .gradle-purifier/distribution beside the lockfile.

Nix builds

Once the native development workflow is working, you can also build and test the whole project in the Nix sandbox. For this step, keep a flake or another Nix build definition in the project’s repository, or maintain an external packaging repository that points at its sources and lockfiles. The builder consumes the same lockfiles as the development workflow.

A flake is optional and can live in the project or in another repository:

# flake.nix
{
  inputs.gradle-purifier.url =
    "github:nix-purifiers/gradle-purifier";
  inputs.nixpkgs.follows = "gradle-purifier/nixpkgs";

  outputs =
    { nixpkgs, gradle-purifier, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ gradle-purifier.overlays.default ];
      };
    in
    {
      packages.${system}.app =
        (pkgs.gradle-purifier.buildGradleProject { src =
          ./.; })
        .packages.app;
    };
}

gradle-purifier.overrideScope replaces what the scope shares. The builder takes what it needs from the package set by name, such as javaPackages and stdenvNoCC, and gradle-purifier.buildGradleProject.override replaces any of them. The builder’s result has these attributes:

The builder takes these arguments besides src, the root of the build:

lib.gradle-purifier.readLock { src, lockFile } reads the lock into the shape that every purifier shares: entries, outputs and problems.

One-off builds without a project Nix file

For an ad hoc build, the builder can also be evaluated directly from the project root. Replace the example package name with one from your project:

nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/gradle-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.gradle-purifier.buildGradleProject { src = ./.; })
.packages.app'

This uses the purifier’s pinned nixpkgs, matching the lock command above. To build with ambient <nixpkgs> from your Nix search path instead, replace import purifier.inputs.nixpkgs with import <nixpkgs>. Both forms need no Nix files in your project. The build tool and language versions must match the lockfile; with ambient nixpkgs, lock with the purifier from that package set or pass a compatible toolchain to the builder.

Project projections

Pass a producer’s flake package to lock:

gradle-purifier lock \
  --input 'github:owner/library/<rev>#library'
gradle-purifier prefetch

The producer must export a Maven view through projections.maven. Its catalog selects the supplied modules automatically, including their Nix-derived versions. Keep ordinary dependency declarations in the project; no version rewrite table is needed. Repeat --input for additional producers.

The lock records the producer’s pinned Nix source, rather than listing its artifacts as downloads. Relocking retains that pin; update refreshes it. Conflicting providers of the same module are rejected.

For a Nix build definition, the corresponding builder argument is projectDependencies = [ producer.packages.${system}.library ];. By default, the builder reads the inputs recorded in the lock.

Projected repositories use immutable, Nix-derived versions. The purifier’s publish command deploys those coordinates and supplied dependencies, with POM references rewritten so ordinary Maven consumers can resolve them.

Publications also include dependencies from Gradle’s included builds. When a Nix input changes a transitive edge, the affected intermediate descriptors get immutable versions too.

Supported scope

Supported language repositories

The lock records every download through Gradle’s repositories: Maven and Ivy repositories of settings scripts, of pluginManagement, of dependencyResolutionManagement, of buildscripts and of projects, in the root build, in buildSrc and in included builds, and the Gradle plugin portal. To do that, the lock run copies the build to .gradle-purifier/lock-run and runs it through an init script that points every repository at a recorder on the loopback address, which fetches each file and records its URL and hash. The lock run builds, tests and publishes (into a scratch directory) every project, and resolves every resolvable configuration with sources and javadoc, so the lockfile holds what any package build or editor needs.

Some files are recorded in a particular way:

  • When Gradle compares a file it holds from one repository with the checksum another repository serves, the recorder fetches the whole file, so every repository’s copy is in the lockfile.
  • A maven-metadata.xml, which Gradle reads for dynamic versions and snapshots, changes whenever a new version is published, so the lockfile holds its contents.
  • A file of Maven Central gets Google’s mirror of Maven Central as a mirror, which Nix tries when Maven Central fails. With GRADLE_PURIFIER_CENTRAL_MIRROR=1, the lock run downloads from the mirror. The lockfile names Maven Central either way.
  • Kotlin/Native downloads its toolchain (LLVM, a GCC toolchain with a glibc sysroot, libffi) from download.jetbrains.com with its own downloader. The recorder gives that downloader an HTTPS tunnel to the download hosts of JetBrains only, and the command records each archive the downloader names. In the sandbox, the builder unpacks the archives, patches their programs with autoPatchelf, and links programs with the dynamic linker of nixpkgs. A toolchain runs on the machine it was made for, so the lockfile records the host, and a build with Kotlin/Native targets builds on the system its lock was made on, x86_64-linux or aarch64-linux. Lock it on each system that builds it, with a lockfile per system kept outside the tree. Kotlin/Native 2.2 crashes the JVM on JDK 21, so lock such a build with --java 17. On NixOS, the lock run needs a conventional dynamic loader, such as nix-ld provides, because it runs the toolchain before anything can patch it.

The lock refuses these:

  • Downloads outside the repositories. Gradle’s own HTTP client gets a proxy that refuses every connection, so a plugin that downloads with its own client fails in the lock run rather than later in the sandbox. The file needs a repository that the build declares.
  • Kotlin/JS targets. Yarn installs npm packages from the npm registry outside Gradle, so a Kotlin/JS target fails at its first download.
  • The configuration cache is off in the lock run and in the sandbox, because the tasks of the init script read the project model.

A lock run sends the Authorization header of a repository on to it, so a lock can be made from a repository that needs credentials. The Nix build then needs a fetchers.fetchurl that supplies them.

Target systems

  • x86_64-linux
  • aarch64-linux
  • aarch64-darwin

Platform limitations and validation

  • Apple Silicon macOS support is contributor-reported from unsandboxed builds; no macOS CI is configured
  • Sandboxed Darwin –check rebuilds have not been verified
  • Reported Darwin validation excludes Kotlin/Native fixtures on ARM64 and the manual lock-real-world job

Troubleshooting

Every problem with a lockfile fails in the command and at Nix evaluation, with a message that names the fix.

Problem Message Fix
The build definition changed since the lock “the build definition … changed since … was made” gradle-purifier lock
The lockfile was edited by hand “does not match its contentHash, so it was edited” gradle-purifier lock --relock, or restore it from version control
The lockfile is not JSON, has another format version or lacks a field “not valid JSON”, “has lock format version …”, “lacks a field” gradle-purifier lock --relock
A download does not match its hash Nix reports a hash mismatch restore the lockfile, or gradle-purifier lock --relock if the remote changed the file
The build needs a file the lockfile does not hold “the build needs a file that … does not hold” gradle-purifier lock --relock
A dynamic version asks for a version the lock never saw “a dynamic version asks for a version that the version listings of the lockfile do not have” gradle-purifier update <group>:<artifact>
The lockfile changed since the last prefetch “changed since ‘gradle-purifier prefetch’ ran” gradle-purifier prefetch

Examples

For day-to-day work on these projects, use the native-tool setup and commands in Quickstart and Development loop. The Nix expressions below show optional sandboxed packaging of those projects.

Spring PetClinic is a Spring Boot application with a Gradle wrapper and a Java 17 toolchain. In a clone of it, lock it on Java 17:

nix shell 'github:nix-purifiers/gradle-purifier'
gradle-purifier lock --java 17
git add gradle-purifier.lock.json

Build the Spring Boot jar from the clone’s directory, with no Nix files added to it, then run it on Java 17:

nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/gradle-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.gradle-purifier.buildGradleProject { src = ./.; })
.packages.spring-petclinic'
java -jar result/share/archives/spring-petclinic.jar

For the development loop, prefetch sets up the wrapper, and the usual commands work offline:

nix shell 'github:nix-purifiers/gradle-purifier'
gradle-purifier prefetch
./gradlew bootRun