How to get Cuda working in Blender?

What do you mean by “cudatoolkit properly on system”? I have the nvidia driver set up properly, but I don’t know about CUDA.

2 Likes

Is there any reason CUDA is not integrated by default? Given how GPU rendering is famous in blender, I guess many people would expect it to be present by default, especially if it’s packed with the official distribution.

2 Likes

It’s because CUDA is not free software so it’s not enabled by default in Nixpkgs.

5 Likes

I tried this method, and I get ./blender: No such file or directory even though the file is there and executable.

[nix-shell:~/path/to/blender-3.0.0-linux-x64]$ ./blender
bash: ./blender: No such file or directory
$ ll
total 140M
drwxr-xr-x 5 aidan users    5 Dec  3 23:01 3.0
-rwxr-xr-x 1 aidan users 276M Dec  3 13:47 blender
-rw-r--r-- 1 aidan users 5.6K Oct 28 08:20 blender.desktop
-rwxr-xr-x 1 aidan users  713 Oct 28 08:20 blender-softwaregl
-rw-r--r-- 1 aidan users 1.7K Oct 28 08:20 blender.svg
-rw-r--r-- 1 aidan users 3.8K Oct 28 08:20 blender-symbolic.svg
-rwxr-xr-x 1 aidan users 2.1M Dec  3 04:53 blender-thumbnailer
-rw-r--r-- 1 aidan users 4.7K Oct 28 08:20 copyright.txt
drwxr-xr-x 2 aidan users   11 Dec  3 23:01 lib
drwxr-xr-x 2 aidan users   17 Dec  3 23:01 license
-rw-r--r-- 1 aidan users 5.1K Dec  3 13:35 readme.html
-rw-r--r-- 1 aidan users  318 Jan 13 18:21 shell.nix
$ ldd blender
        linux-vdso.so.1 (0x00007ffeda6bf000)
        librt.so.1 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/librt.so.1 (0x00007f1775ab7000)
        libutil.so.1 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libutil.so.1 (0x00007f1775ab2000)
        libpthread.so.0 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libpthread.so.0 (0x00007f1775a92000)
        libdl.so.2 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libdl.so.2 (0x00007f1775a8d000)
        libX11.so.6 => not found
        libXi.so.6 => not found
        libXxf86vm.so.1 => not found
        libXfixes.so.3 => not found
        libXrender.so.1 => not found
        libm.so.6 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libm.so.6 (0x00007f177594a000)
        libGL.so.1 => not found
        libc.so.6 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libc.so.6 (0x00007f1775783000)
        /lib64/ld-linux-x86-64.so.2 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib64/ld-linux-x86-64.so.2 (0x00007f1775ac4000)
        libgcc_s.so.1 => /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libgcc_s.so.1 (0x00007f1775769000)
$ 

Although this may be a separate problem to getting CUDA to run, because I am trying to run Blender from upstream.

How would I use this with flake-compat?

After much spelunking last year, I was able to get cuda support in blender simply by adding the following in configuration.nix:

nixpkgs.config = {
    allowUnfree = true;
    cudaSupport = true;
  };

The best I could figure was that a bunch of Blender’s dependencies also needed cuda enabled, and enabling it for the blender package alone didn’t propagate to the dependencies.

n.b.: Enabling this will cause a whole bunch of packges to be rebuilt locally from scratch, ignoring the binary cache. blender-bin is easier unless you think you might benefit from whichever other packages on your system end up being gpu-accelerated by this change.

8 Likes

Just use the blender from edolstra’s nix-warez

Cuda works out of the box if you have the cuda toolkit and stuff installed

Works even as a container running inside a cuda enabled docker on ubuntu

You shouldn’t ever need to manually set CUDA_PATH, EXTRA_LDFLAGS, or really any other environment variables in order to use CUDA-enabled packages. If they are configured to use cudatoolkit but can’t find it, then that’s a bug with that package.

2 Likes

cuda-blender-shell.nix


{ pkgs ? import <nixpkgs> {config.cudaSupport = true;} }:
pkgs.stdenv.mkDerivation {
  name = "cuda-env-shell";
  buildInputs = with pkgs; [
    cudatoolkit
    blender
  ];
  shellHook = ''
    export CUDA_PATH=${pkgs.cudatoolkit}
    export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
    export EXTRA_CCFLAGS="-I/usr/include"
  '';
}
1 Like

Will this work with blender 4.0.1? Asking because I see “pkgs.blender_2_93”…

Also, might I ask, should I just append the flake output you have given?

Just a question, if after the rebuilt, lets say someday I remove the cudaSupport = true;, will this rebuild the packages from scratch again? Or will I have to take some other steps to revert the packages back without the cuda?

Thanks to the purity of nix, the configuration of your system solely depends on what is written in your configuration. For instance, the blender derivation contains cudaSupport ? config.cudaSupport, so that’s why enabling nixpkgs.config.cudaSupport = true; will give you cuda support. So if you remove that line, it will revert the system back to use the non-cuda version of blender (since blender is in the cache, it should not really build it, it will just download the package from the cache).

1 Like

To check the packages available in the blender-bin registry, just check in https://github.com/edolstra/nix-warez/blob/4ec7bfb219eab0c3ecf7c3bfba3aa8582a6cd832/blender/flake.nix. You can see that it provides now a blender_4_0 package, targeting, as of today, blender 4.0.2 as you can read in the flake.nix.

1 Like

Thank you for the explanations!

I think I will go the other route with nixpkgs.config.cudaSupport = true; (as I still am not well versed in flakes even though it’s my current system. So I don’t know how to implement blender-bin flake yet :sneezing_face:)

Seems like a good solution as well, but if you want to use the binary package, you can add in your input

inputs.blender-bin.url = "github:edolstra/nix-warez?dir=blender";

and in the list of modules add

modules = [
  # Add the overlay of blender-bin
  ({config, pkgs, ...}: { nixpkgs.overlays = [ blender-bin.overlays.default ]; })
  # In configuration.nix, you can now refer to blender_4_0 in environment.systemPackages
  ./configuration.nix
]

Full version:

{
  description = "The configuration of my system";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
    blender-bin.url = "github:edolstra/nix-warez?dir=blender";
  };
  
  outputs = { self, nixpkgs, blender-bin, ... }@inputs: {
    nixosConfigurations.foo = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        ({config, pkgs, ...}: {
          nixpkgs.overlays = [ blender-bin.overlays.default ];
          # This line can either be here or in configuration.nix
          environment.systemPackages = with pkgs; [ blender_4_0 ];
        })
        ./configuration.nix
      ];
    };
  };
}
1 Like

Thank you for this! I think I will try blender-bin way for now then as I am facing a strange issue the other way around. Though maybe I can ask you here about it?

If I could trouble you a little bit more, I am facing an issue where after inserting nixpkgs.config.cudaSupport = true and updating the flake, the rebuild cranks the cpu to full utilization and the temperatures go all the way up to 89-90C while rebuilding. This isn’t an issue as I think the system is just updating but then the system hangs in between the rebuild. Is there a way for me to slow down the rebuild and tell the system to do it slowly so as to prevent the system hang?

I guess what you observe is because blender with cuda support might not be in the Hydra cache, so you are compiling them yourself. To avoid system hangs, I personally tweak the CPU performance policy (I hink under the hood this is made thanks to services.power-profiles-daemon.enable enabled by default in Plasma and Gnome I think). In Plasma, I can just go to the systray in the battery icon, and I have a line “Energy gestion profile” that I can put to the minimal position when the temperature goes too high.

image

If the hang is due to memory being full, I can maybe recommend a swap file, but this will be quite slow. You can use htop/dmesg/journalctl to see the memory consumption in real time/the reason of a crash.

1 Like

Thank you for the helpful suggestion! I have checked out journalctl and dmesg. Will be keeping my eye on them as I try the rebuild again.

By the way, do I have to clear any caches before trying the rebuild again? (considering the rebuild was stopped inbetween)

Strangely, after making the changes accordingly, updating flake, and rebuilding it, blender still can’t detect the GPU. I am running blender through terminal with the command nvidia-offload blender. Should I run the blender in some other way? What can be the issue here?..

This is the output of nvidia-smi and it shows the blender app in processes.

Wed Jan 10 05:32:53 2024       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 545.29.06              Driver Version: 545.29.06    CUDA Version: 12.3     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce RTX 3050 ...    Off | 00000000:01:00.0 Off |                  N/A |
| N/A   53C    P8               3W /  35W |    357MiB /  4096MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      2398      G   ...mn-gnome-shell-45.2/bin/gnome-shell        1MiB |
|    0   N/A  N/A      3908    C+G   /run/current-system/sw/bin/blender          349MiB |
+---------------------------------------------------------------------------------------+

My apologies ^^ I had forgotten to remove the blender package from the configuration.nix. so my system had blender packages called in both the flakes.nix and configuration.nix.

Removing the blender from configuration.nix and rebuilding solved the issue. Thank you for your help @tobiasBora !

I want to add that for those who aren’t using flake, maybe this will help:
https://www.reddit.com/r/NixOS/comments/10f9a2f/comment/j4vqf3l/?utm_source=share&utm_medium=web2x&context=3

basically, replace blender in your configuration.nix with:

 environment.systemPackages = with pkgs; [
    ...
    vim
    firefox
    # replace blender with this line
    (blender.override {
        cudaSupport = true;
    })
    ...
];

i haven’t tried it out myself but it seemed to work for some people.