Automatic installation of Oracle Java

written by Christian Zagrodnick on 2016-05-12

Our customers at times require Oracle Java for their applications. Our new platform is based on NixOS. As with most Linux distributions, Oracle Java cannot be installed just like that. Oracle’s license prevents redistribution or direct downloading from their servers. NixOS is no exception there.

While manual installation is pretty straightforward on NixOS, ultimately an automated process is what makes operators happy. We use Batou for this.

Manual installation

First thing, Oracle Java is unfree and you need to explicitly allow unfree packages:

# nix-env -iA nixos.oraclejre8 
error: Package ‘oraclejre-8u65’ in ‘/nix/store/gbgmy7cdwrbniilfh7z2n4z7vnnv2dls-nixos-15.09.1586.593703e/nixos/pkgs/development/compilers/oraclejdk/jdk-linux-base.nix:64’ has an unfree license (‘unfree’), refusing to evaluate.
For `nixos-rebuild` you can set
 { nixpkgs.config.allowUnfree = true; }
in configuration.nix to override this.
For `nix-env` you can add
 { allowUnfree = true; }
to ~/.nixpkgs/config.nix.
So let's enable unfree packages:

# mkdir ~/.nixpkgs

echo “{ allowUnfree = true; }” > ~/.nixpkgs/config.nix</pre>

Okay. Next try.

# nix-env -iA nixos.oraclejre8 
building path(s) ‘/nix/store/8qzdxq7kki8dv4rv8yyy5gjw3y1acahb-jdk-8u65-linux-x64.tar.gz’


Unfortunately, we may not download file jdk-8u65-linux-x64.tar.gz automatically. Please, go to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, download it yourself, and add it to the Nix store using either nix-store —add-fixed sha256 jdk-8u65-linux-x64.tar.gz or nix-prefetch-url —type sha256 file://path/to/jdk-8u65-linux-x64.tar.gz


builder for ‘/nix/store/qphkbfaxx4x7nrgy6id13qv53finfijd-jdk-8u65-linux-x64.tar.gz.drv’ failed to produce output path ‘/nix/store/8qzdxq7kki8dv4rv8yyy5gjw3y1acahb-jdk-8u65-linux-x64.tar.gz’ cannot build derivation ‘/nix/store/dka7h74rl7h4d0sccjmqcsayzgd70n77-oraclejre-8u65.drv’: 1 dependencies couldn’t be built error: build of ‘/nix/store/dka7h74rl7h4d0sccjmqcsayzgd70n77-oraclejre-8u65.drv’ failed</pre> Now the pain starts. Java 8u65 is not the latest release. You need an Oracle account to download old releases. You have to log in and download the exact file, inject it into the store and re-run nix-env.

Automatic installation

batouFor automatic installation you still need to download the archive. But for internal projects or deployments, it would be useful to download Oracle Java once and put it onto an internal file server. I would not consider this a redistribution which is denied by the license.

If you happen to deploy your application with Batou, the following code snippet comes handy. It injects the local download URL to the store and installs Java to the service users' environment.

import batou
import batou.component
import batou.lib.download

class Java(batou.component.Component):

# NIXOS will do checksumming!
url = 'http://your-server/jdk-8u65-linux-x64.tar.gz'
package = 'oraclejre-8u65'
attribute = 'nixos.oraclejre8'

def configure(self):
    self.provide('java', self)
    self += batou.lib.file.File(
        '~/.nixpkgs/config.nix',
        leading=True, source='config.nix')

def verify(self):
    stdout, stderr = self.cmd('nix-env —query')
    if self.package not in stdout.splitlines():
        raise batou.UpdateNeeded()

def update(self):
    self.cmd('nix-prefetch-url —type sha256 {}'.format(
        self.url))
    self.cmd('nix-env -iA {}'.format(self.attribute))</pre>

Note that for this to work you must download the exact version required by NixOS. Also you must use the archive as it is provided by Oracle – otherwise the checksum will not match and NixOS will not find the archive.

Components which require Java can declare it via:

self.require('java', host=self.host)
This makes the configuration fail if there is no Java, and makes sure Java is built before the component requiring it is built. And with our Managed Operations option we will do all that for you.

Cover photo by Nicolas Nova, © 2008 CC-BY-2.0

Get in touch

Call us or send us an email.

Contact
mail: mail@flyingcircus.io
fon: +49 345 219 401 0
fax: +49 345 219 401 28

Address
Flying Circus Internet Operations GmbH
Leipziger Str. 70/71
06108 Halle (Saale)
GERMANY


Commercial register
AG Stendal as HRB 21169
VAT ID: DE297423633

Managing Directors:
Christian Theune, Christian Zagrodnick

flyingcircus.io — 2016-2021Privacy