How to get Cuda working in Blender?

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.