Nix shell for building OpenJDK on macOS

Magnus Ihse Bursie magnus.ihse.bursie at oracle.com
Fri Mar 7 13:53:50 UTC 2025


Thank you for your report!

I have only touched nix briefly, but it's good to know that they seem to 
have an official jdk package. Maybe we should mention this in the build 
readme as an alternative way to setup a dev environment for the JDK?

/Magnus

On 2025-03-07 14:29, Galder Zamarreno wrote:
> Hi,
>
> The dependencies required to build JDK vary depending on which JDK 
> version you are building. I've been building my own set of scripts 
> that allows me to easily manage this, to the extent that I can do it 
> on the given base OS I run. E.g. if I'm building a mainline I'd use X 
> as boot JDK but if I'm building a JDK 21 backport I'd use Y as boot 
> JDK. These scripts are cumbersome and they can't control all tooling 
> dependencies, so their use is limited.
>
> In my spare time I've been experimenting with Nix, which offers a 
> declarative way for package management and system configuration. Its 
> package manager can be installed on either Linux or Mac and enables 
> you to start shell instances with a set of isolated dependencies.
>
> I've been wanting to move from my cumbersome scripts to a setup that 
> allows me to build my local JDKs within a Nix isolated shell. However, 
> although Nix packages exist that build OpenJDK [1], these only support 
> Linux and I'm often developing on a mac. After some exploration, I've 
> been able to craft a Nix descriptor file that allows me to build JDK 
> in an isolated Nix shell on a mac. I wanted to share this in this 
> mailing list in case it is useful to other JDK developers.
>
> The starting point is having a mac devkit. Since I'm not a Nix expert 
> I built it outside of Nix, e.g.
>
> ```
> $ cd jdk/make/devkit
> $ bash ./createMacosxDevkit.sh /Applications/Xcode.app
> ```
>
> Then I added the devkit manually to the Nix store:
>
> ```
> $ cd jdk/build/devkit
> $ nix-store --add-fixed --recursive sha256 Xcode16.2-MacOSX15
> /nix/store/vhsix1jn849mpxggwbw2zh1nbxpy0grc-Xcode16.2-MacOSX15
> ```
>
> Then I created a shell.nix file that declares the dependencies and 
> sets environment variables to build the JDK successfully. Something 
> that is odd is that if you depend on pkgs.jdk23 on mac, it doesn't 
> throw an error because [1] is not supported there, but it downloads 
> Azul's Zulu JDK. I've asked how this happens [2] but didn't get a 
> satisfactory answer but this is ok for now for a boot jdk. The DEVKIT 
> env variable points to the devkit I added to the nix store and MIGCC 
> is set so that the JDK build uses the clang from the package dependency.
>
> ```
> { pkgs ? import <nixpkgs> {} }:
>
> let
>   devkit = 
> "/nix/store/vhsix1jn849mpxggwbw2zh1nbxpy0grc-Xcode16.2-MacOSX15";
> in
> pkgs.mkShell {
>   packages = [
>     pkgs.autoconf
>     pkgs.jdk23
>     pkgs.clang
>
>     devkit
>   ];
>
>   shellHook = ''
>     echo "Setting DEVKIT_ROOT to path of the devkit in the Nix store."
>     export DEVKIT_ROOT=${devkit}
>
>     echo "Setting MIGCC to clang compiler cc binary."
>     export MIGCC="${pkgs.clang}/bin/cc"
>   '' ;
> }
> ```
>
> From the directory where the shell.nix file is located you can start a 
> Nix shell that will start the isolated environment:
>
> ```
> nix-shell
> ```
>
> In that nix-shell you can now configure the JDK pointing the boot JDK 
> and the devkit:
>
> ```
> [nix-shell: jdk] $ bash configure \
>     --with-boot-jdk=$(dirname $(dirname $(readlink -f $(which java)))) \
>     --with-devkit=$DEVKIT_ROOT
> ```
>
> The configure output looks like this:
>
> ```
> A new configuration has been successfully created in
> /Users/galder/1/colata/nix-darwin/jdk/build/macosx-aarch64-server-release
> using configure arguments 
> '--with-boot-jdk=/nix/store/wm5rma6x2527qmypzj7rwml8vf9vprgj-zulu-ca-jdk-23.0.0/zulu-23.jdk/Contents/Home 
> --with-devkit=/nix/store/vhsix1jn849mpxggwbw2zh1nbxpy0grc-Xcode16.2-MacOSX15'.
>
> Configuration summary:
> * Name:           macosx-aarch64-server-release
> * Debug level:    release
> * HS debug level: product
> * JVM variants:   server
> * JVM features:   server: 'cds compiler1 compiler2 dtrace epsilongc 
> g1gc jfr jni-check jvmci jvmti management parallelgc serialgc services 
> shenandoahgc vm-structs zgc'
> * OpenJDK target: OS: macosx, CPU architecture: aarch64, address 
> length: 64
> * Version string: 25-internal-adhoc.galder.jdk (25-internal)
> * Source date:    315532800 (1980-01-01T00:00:00Z)
>
> Tools summary:
> * Boot JDK:       openjdk version "23" 2024-09-17 OpenJDK Runtime 
> Environment Zulu23.28+85-CA (build 23+37) OpenJDK 64-Bit Server VM 
> Zulu23.28+85-CA (build 23+37, mixed mode, sharing) (at 
> /nix/store/wm5rma6x2527qmypzj7rwml8vf9vprgj-zulu-ca-jdk-23.0.0/zulu-23.jdk/Contents/Home)
> * Toolchain:      clang (clang/LLVM from Xcode 16.2)
> * Devkit:         Xcode 16.2 (devkit) 
> (/nix/store/vhsix1jn849mpxggwbw2zh1nbxpy0grc-Xcode16.2-MacOSX15)
> * C Compiler:     Version 16.0.0 (at 
> /nix/store/vhsix1jn849mpxggwbw2zh1nbxpy0grc-Xcode16.2-MacOSX15/Xcode/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang)
> * C++ Compiler:   Version 16.0.0 (at 
> /nix/store/vhsix1jn849mpxggwbw2zh1nbxpy0grc-Xcode16.2-MacOSX15/Xcode/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++)
>
> Build performance summary:
> * Build jobs:     14
> * Memory limit:   49152 MB
> ```
>
> Then you can just run make and verify the JDK version:
>
> ```
> [nix-shell: jdk] $ make
> ...
> Finished building target 'default (exploded-image)' in configuration 
> 'macosx-aarch64-server-release'
>
> [nix-shell: jdk] $ ./build/macosx-aarch64-server-release/jdk/bin/java 
> --version
> openjdk 25-internal 2025-09-16
> OpenJDK Runtime Environment (build 25-internal-adhoc.galder.jdk)
> OpenJDK 64-Bit Server VM (build 25-internal-adhoc.galder.jdk, mixed mode)
> ```
>
> The above is sufficient for my own use case, but it can be enhanced 
> further to add capstone dependency...etc.
>
> I don't know who is taking care of [1] but while working on this a 
> member in Nix macos discord asked me to create an issue in Nix 
> packages so that others can maybe take this further and enhance [1] so 
> that it also supports darwin-aarch64. So I created [3].
>
> XCode is tricky to handle, but it would be great if eventually there 
> would be Nix packages for JDK devkits for macos. If those were 
> available, it would enable OpenJDK developers to build earlier JDK 
> versions even of the very latest macos environments, by depending on 
> earlier XCode version based JDK devkits. You could also pick whichever 
> clang, capstone...etc version that matches that...etc.
>
> Thanks
> Galder
>
> [1] 
> https://github.com/NixOS/nixpkgs/blob/nixos-24.11/pkgs/development/compilers/openjdk/generic.nix
> [2] https://github.com/NixOS/nixpkgs/issues/377908
> [3] https://github.com/NixOS/nixpkgs/issues/387516
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/build-dev/attachments/20250307/65f5169d/attachment-0001.htm>


More information about the build-dev mailing list