Skip to content

Instantly share code, notes, and snippets.

@kinnala
Last active September 18, 2020 07:37
Show Gist options
  • Save kinnala/520bcd9f657eaadd3cdcd995e1a8a657 to your computer and use it in GitHub Desktop.
Save kinnala/520bcd9f657eaadd3cdcd995e1a8a657 to your computer and use it in GitHub Desktop.
Running Julia in NixOS

These instructions create a conda-shell type of environment for running Julia 1.1. (Derivation originally from https://gist.github.com/tbenst/c8247a1abcf318d231c396dcdd1f5304).

Note: Some Julia packages may fail to install due to missing binary dependencies. The built-in package manager of Julia will normally install these for you but will fail in case of NixOS. You need to "simply" figure out what is missing and add them to the .nix-file.

  1. Write the following expression to a file, e.g., julia-shell.nix:
{ pkgs ? import <nixpkgs> {}}:

let
jupyterPort = pkgs.config.jupyterPort;
fhs = pkgs.buildFHSUserEnv {
  name = "julia-fhs";
  targetPkgs = pkgs: with pkgs;
    [
      git
      gitRepo
      gnupg
      autoconf
      curl
      procps
      gnumake
      utillinux
      m4
      gperf
      unzip
      libGL libGLU
      xorg.libXi xorg.libXmu freeglut
      xorg.libXext xorg.libX11 xorg.libXv xorg.libXrandr zlib
      ncurses5
      stdenv.cc
      binutils


      julia_11

      # Arpack.jl
      arpack
      gfortran.cc
      (pkgs.runCommand "openblas64_" {} ''
        mkdir -p "$out"/lib/
        ln -s ${openblasCompat}/lib/libopenblas.so "$out"/lib/libopenblas64_.so.0
      '')

      # IJulia.jl
      mbedtls
      zeromq3
      python3Packages.jupyterlab
      # ImageMagick.jl
      imagemagickBig
      # HDF5.jl
      hdf5
      # Cairo.jl
      cairo
      gettext
      pango.out
      glib.out
      # Gtk.jl
      gtk3
      gdk_pixbuf
      # GZip.jl # Required by DataFrames.jl
      gzip
      zlib
      # GR.jl # Runs even without Xrender and Xext, but cannot save files, so those are required
      xorg.libXt
      xorg.libX11
      xorg.libXrender
      xorg.libXext
      glfw
      freetype
    ];
  multiPkgs = pkgs: with pkgs; [ zlib ];
  runScript = "bash";
  profile = with pkgs; ''
    export LD_LIBRARY_PATH="${glfw}/lib:${mesa}/lib:${freetype}/lib:${imagemagick}/lib:${portaudio}/lib:${libsndfile.out}/lib:${libxml2.out}/lib:${expat.out}/lib:${cairo.out}/lib:${pango.out}/lib:${gettext.out}/lib:${glib.out}/lib:${gtk3.out}/lib:${gdk_pixbuf.out}/lib:${cairo.out}:${tk.out}/lib:${tcl.out}/lib:${pkgs.sqlite.out}/lib:${pkgs.zlib}/lib"
    export EXTRA_LDFLAGS="-L/lib"
    export EXTRA_CCFLAGS="-I/usr/include"
    '';
  };
  shellHook = ''
    TEMPDIR=$(mktemp -d -p /tmp)
    mkdir -p $TEMPDIR
    cp -r ${pkgs.python3Packages.jupyterlab}/share/jupyter/lab/* $TEMPDIR
    chmod -R 755 $TEMPDIR
    echo "$TEMPDIR is the app directory"
    # start jupyterlab
    jupyter lab --app-dir=$TEMPDIR --port=${jupyterPort} --no-browser
  '';

in pkgs.stdenv.mkDerivation {
  name = "julia-shell";
  nativeBuildInputs = [fhs];
  shellHook = "exec julia-fhs";
}
  1. Run nix-shell --pure julia-shell.nix.

  2. After everything is downloaded, built and the new shell opened, run GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt julia. Explanation: Some SSL certificates are missing from the pure environment. (TODO: add these environment variables directly to the derivation above.)

  3. Try installing packages, e.g., using Pkg; Pkg.add("DataFrames")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment