# Build Julia on NixOS

**URL:** <https://discourse.julialang.org/t/build-julia-on-nixos/35129>\
**Category:** General Usage\
**Tags:** question, nixos\
**Created:** [February 25, 2020, 6:14pm UTC](https://discourse.julialang.org/t/build-julia-on-nixos/35129 "2020-02-25T18:14:16Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![RCHG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rchg/32/10104_2.png) [@RCHG](https://discourse.julialang.org/u/RCHG)\
**Post date:** [February 25, 2020, 6:14pm UTC](https://discourse.julialang.org/t/build-julia-on-nixos/35129/1 "2020-02-25T18:14:16Z")

</div>

Hello!

Here I would like to create a topic about the use of Julia on _NixOS_, and open the contact with other Julia users under NixOS or those using the _Nix package manager_. First, just a short introduction about why the use of Julia with this operating system might not be so trivial:

[NixOS](https://nixos.org/) is a distribution that has a different way to install packages to have a kind of purely reproducible working environment. This means that it doesn’t follow the _FHS_ (file hierarchical system) of unix/linux.

To give an example of what it implies in the case of python, a typical installation with _pip_ or _conda_ of a python package might not work, since it is usually looking for files on specific directories (on a prescribed _FHS_ like /usr/lib, /usr/share/ … or similar) to resolve dependencies. Actually, it happens that these dependencies are (or can be) in the system but not there (when conda/pip are looking for them).

Julia compiler itself can be easily installed (as simple as: `nix-env -iA nixos.julia_11` for version 1.1) but the Julia’s built-in package manager usually has difficulties to build the packages and organize their dependencies. The NixOS people provided a solution for those cases, as in this operating system it is possible to run a program on a virtual environment that fulfil the _FHS_. This is useful to use, for example, conda on NixOS on a smooth way, or installations depending on it (for example [here](https://github.com/ESMValGroup/ESMValCore/issues/354).

More and less this is the idea or direction to overcome the problem also in Julia, but still the users may need some support, so I have open this topic to share our experiences or difficulties. Here I give some links to discussion about Julia within the NixOS community:

- [Main Discussion (sometimes very technical)](https://github.com/NixOS/nixpkgs/issues/20649)
- [Typical problems to overcome](https://github.com/NixOS/nixpkgs/issues/50415)
- [Also this one](https://github.com/NixOS/nixpkgs/issues/70536)
- [Kind of solution at the end of this thread](https://discourse.nixos.org/t/system-with-nixos-how-to-add-another-extra-distribution/5767/11)

Inside _Julia world_ we had discussions about support of Nix package manager:

- [Nix and Julia](https://github.com/JuliaPackaging/BinDeps.jl/pull/115)

The current solution, based on create an FHD environment, that aims to solve our problems is close to this piece of code to be added in our NixOS configuration of overlays (kindly provided by [other user](https://discourse.nixos.org/u/cstich)):

```julia-auto
{pkgs, stdenv, ...}:

with pkgs;
let
  julia = julia_11;
  d = version: "v${lib.concatStringsSep "." (lib.take 2 (lib.splitString "." version))}";
  extraLibs = [
    # 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 which runs without Xrender & Xext =
    # but cannot save files ===================
    xorg.libXt xorg.libX11 xorg.libXrender xorg.libXext glfw freetype
    # Flux.jl =================================
    cudatoolkit linuxPackages.nvidia_x11 git gitRepo gnupg autoconf curl
    procps gnumake utillinux m4 gperf unzip libGLU_combined ncurses5 stdenv.cc binutils
    xorg.libXi xorg.libXmu freeglut xorg.libXext xorg.libX11 xorg.libXv xorg.libXrandr zlib
    # Arpack.jl ===============================
    arpack gfortran.cc
    (pkgs.runCommand "openblas64_" {} ''
      mkdir -p "$out"/lib/
      ln -s ${openblasCompat}/lib/libopenblas.so "$out"/lib/libopenblas64_.so.0
    '')
  ];
in
stdenv.mkDerivation rec {
  name = "julia-env";
  version = julia.version;
  nativeBuildInputs = [makeWrapper cacert git pkgconfig which];
  buildInputs = [
    julia
    /* jupyterEnv # my custom jupyter */
  ] ++ extraLibs;
  phases = ["installPhase"];
  installPhase = ''
    export CUDA_PATH="${cudatoolkit}"
    export LD_LIBRARY_PATH=${lib.makeLibraryPath extraLibs}
    # pushd $JULIA_PKGDIR/${d version}
    makeWrapper ${julia}/bin/julia $out/bin/julia \
        --prefix LD_LIBRARY_PATH : "$LD_LIBRARY_PATH" \
        --prefix LD_LIBRARY_PATH ":" "${linuxPackages.nvidia_x11}/lib" \
        --set CUDA_PATH "${cudatoolkit}" \
        --set JULIA_PKGDIR $JULIA_PKGDIR
  '';
}

```

Kind regards!

---

_[View the full topic](https://discourse.julialang.org/t/build-julia-on-nixos/35129)._
