Nix Notes

1 Cheat sheet

1.1 Install/upgrade nix

Date: 2017-05-06

Sources:

  1. https://www.domenkozar.com/2014/01/02/getting-started-with-nix-package-manager/
  2. https://nixos.org/nix/
bash <(curl https://nixos.org/nix/install)

OR

$ curl https://nixos.org/nix/install | sh

1.2 Activate a nix user profile

source ~/.nix-profile/etc/profile.d/nix.sh

1.3 Search for packages

nix-env -qaP | grep python3-3

1.4 List all installed packages

nix-env -q

1.5 Garbage collect

nix-collect-garbage

1.6 Create a shell environment with the jdk package

nix-shell -p jdk

The -p argument specifies the packages to include in the environment.


2 Java

2.1 Packaging Maven projects

Date May 12, 2017

Sources

  1. https://ww.telent.net/2017/5/10/building_maven_packages_with_nix
  2. https://github.com/NixOS/mvn2nix-maven-plugin

IMPORTANT Unfortunately, the workaround for authenticated flag problem in mvn2nix does not work for me. The directions in this section might work for you since others have reported success with it. I decided to install pre-built .jars using nix instead of building the source.

To use the mvn2nix plugin, you need to have Maven >= 3.2.5. On May 12, 2017, this required installing from jessie-backports on Debian:

sudo apt-get -t jessie-backports install maven

Next, generate the project-info.json file that build-maven.nix will use the Nix expression for your package. To do this, run the following command from inside the Maven project's root directory.

mvn -Dmaven.repo.local=$(mktemp -d -t mavenXXX) org.nixos.mvn2nix:mvn2nix-maven-plugin:mvn2nix

Important note: on my Debian Jessie system, mktemp requires at least three X's appended to the end of the output file, hence the $(mktemp -d -t mavenXXX) above. Alternatively, you can just drop the template argument altogether.

The output of the above command will be a file named project-info.json. Move this some convenient directory inside your project's hierarchy, such as app/.


2.2 Installing pre-built .jars

Instead of building Java source code, you could get away with simply installing prebuilt .jars using Nix. Here is a short expression I used to copy the ImageJ 1.51j .jar file into the Nix store.

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "ij-1.51j";
  src = (builtins.toPath ./ij-1.51j.jar);
  phases = "installPhase";

  installPhase = ''
    mkdir -p $out/java/share
    cp -v $src $out/java/share/
  '';

  meta = {
    description = "Open source scientific multidimensional image processing";
    homepage = https://imagej.net;
  };
}

It's important to specify the phases attribute because stdenv.mkDerivation will attempt to run a build phase by default. This means it will try to run configure, make, make install, etc. on contents of the src attribute. By specifying that we only want an install phase, we can simply copy the .jar to the store.


3 Glossary

The official glossary is located in the Nix documentation here: http://nixos.org/nix/manual/#idm140737315919056