Building and running Blender from source (using official doc)

Hi!

I am trying to build and run Blender from source using the instructions from the official docs. Overall, it’s pretty straightforward – requiring only a few dependencies and then calling make. All needed packages were easy to find in nixpkgs and I’ve installed them in a shell with:

{ pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
  packages = with pkgs; [
    python3
    git
    git-lfs

    cmake
    subversion
    pkg-config

    libx11
    libxkbcommon
    libxxf86vm
    libxcursor
    libxi
    libxrandr
    libxinerama
    libGL

    wayland
    dbus
  ];
}

Executing make update (to install a few pre-compiled libs) and then make to build Blender itself succeeds in producing an executable without issues. However, running it throws the following error:

../build_linux/bin/blender: error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or directory

From what I understood, dynamic libs should be avoided in NixOS and so tried setting the CMake option WITH_STATIC_LIBS to true and rebuilding, but it leads to exactly the same error.

I’ve had similar issues previously which led me to enable nix-ld and, checking my config, I have libSM there. But it doesn’t seem to resolve the present problem.

I think it might have something to do with the pre-compiled libraries installed (as the official nix derivation for Blender doesn’t use libSM directly it seems), but I don’t know how to verify this without recompiling everything. And I’d like to avoid shooting in the dark as each new attempt takes a good 30-40 min where I can’t use my computer because the compilation uses all my RAM.

So I’d be very grateful if someone more knowledgable could point me in the right direction regarding this. And another more general question, how do you usually go on hacking on projects already in nixpkgs? It seems natural to go look there first – as all dependencies and fixes needed are already there – but from what I understood derivations cannot use cached compilation results by design. And so everything is recompiled each time which is less than ideal for quickly iterating on code.

Nixpkgs has a ‘patchelf’ phase which fixes this by hard coding the library paths in the binary using RPATH, see Nixpkgs Reference Manual and autoPatchelfHook | nixpkgs

You could do that manually yourself. Alternatively you can set the LD_LIBRARY_PATH environment variable to include all the libraries paths you need, then running the binary should work without patching. This is similar to using nix-ld.

The ‘nix’ way would be to build blender in nix as well, then the patchelf will be done for you.

As for iterating from nixpkgs, you can use nix-shell against a package from nixpkgs, you’ll then get a shell with all the build dependencies available, and the right environment variables set. Something like ‘nix-shell -A blender /path/to/nixpkgs’

1 Like

This is exactly what I needed, it is very helpful, thank you! I will read more about it and experiment with it.

As for the nix way, I see why it would be better to build blender in nix, but is it possible to re-use previous compilation results for each new evaluation? And say only compile the source files that changed since last time (as it would be if using directly cmake)? From what I understood, it compiles everything all over each time which is what makes the whole thing reproducible and deterministic.

And that last command is handy as well, will try to use it in the future, thanks for that as well.

It’s possible to set up a cache for incremental builds, but it’s not super straightforward: CCache - NixOS Wiki

There are some other techniques, like building the cache as a separate derivation in nix, there was a ‘nix hour’ about it: https://www.youtube.com/watch?v=1tEur9Tzv9c

It was also recently discussed: Nix *could* be a great build system

So doing it in the shell with nix-shell is still the easiest, you can actually run the build as defined in nix but manually by calling functions: The Standard Environment | nixpkgs

1 Like

This is very interesting, I will take a look at your links, thanks a lot for your thorough answers. I think I will do as you suggest and learn about all intermediate commands and run them together.

1 Like