How to get Cuda working in Blender?

I am having trouble getting cuda support in blender. I have a nvidia gtx970 gpu and have configured my system the following way after looking at various online information.

Can anyone spot where my issue lies?

nixos-version:
19.09.2079.8731aaaf8b3 (Loris)

nvidia-smi:

Sun Feb 16 21:40:40 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.36       Driver Version: 440.36       CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 970     Off  | 00000000:01:00.0 Off |                  N/A |
| 30%   30C    P8     9W / 180W |    990MiB /  4039MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1037      G   ...bppvdxn86lf8rv-xorg-server-1.20.5/bin/X   420MiB |
|    0      1617      G   /run/current-system/sw/bin/kwin_x11           90MiB |
|    0      1619      G   /run/current-system/sw/bin/krunner             2MiB |
|    0      1621      G   /run/current-system/sw/bin/plasmashell       130MiB |
|    0      2340      G   ...AAAAAAAAAAAAAAgAAAAAAAAA --shared-files    72MiB |
|    0      3929      G   ...cker-5.16.5/libexec/kscreenlocker_greet   261MiB |
+-----------------------------------------------------------------------------+

configuration.nix:

{ config, pkgs, ... }:

let 
  unstableTarball =
    fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz;
in

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];
  boot = {
    loader = {
      systemd-boot.enable = true;
      efi.canTouchEfiVariables = true;
    };
    
    kernelParams = [
      "amd_iommu=on"
    ];

    kernelModules = [
      "kvm-amd"
      "vfio-pci"
      "nvidia-uvm"
    ];    
    
    extraModprobeConfig = ''
      options kvm   ignore_msrs=1
      options vfio_pci ids=1002:67df,1002:aaf0
    '';
    
    blacklistedKernelModules = [
      "radeon" "amdgpu"
    ];
  };
  networking.hostName = "thorium"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # The global useDHCP flag is deprecated, therefore explicitly set to false here.
  # Per-interface useDHCP will be mandatory in the future, so this generated config
  # replicates the default behaviour.
  networking.useDHCP = false;
  networking.bridges = {
    br0 = { interfaces = [ "enp5s0" ];
    };
  };
  # networking.interfaces.enp5s0.useDHCP = true;
  networking.interfaces.br0.useDHCP = true;

  ## Internationalisation properties.
  # i18n = {
  #   consoleFont = "Lat2-Terminus16";
  #   consoleKeyMap = "us";
  #   defaultLocale = "en_US.UTF-8";
  # };

  time.timeZone = "Europe/Oslo";

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  nixpkgs.config = { 
    allowUnfree = true;
    packageOverrides = pkgs: {
      unstable = import unstableTarball {
        config = config.nixpkgs.config;
      };
    };
    blender = {
      cudaSupport = true;
    };
    unstable.blender = {
      cudaSupport = true;
    };
  };

  environment.systemPackages = with pkgs; [
    pciutils
    wget
    # vim
    (import ../../modules/vim.nix)
    wireguard
    google-chrome
    virtmanager-qt
    barrier
    git
    steam
    ## From unstable
    unstable.blender
  ];

  programs = {
    vim.defaultEditor = true;
  };

  ## SERVICES ## 
  nix.gc = {
    automatic = true;
    dates = "weekly";
    options = "--delete-older-than 30d";
  };

  services.openssh = {
    enable = true;
    forwardX11 = true;
  };

  # Security config.
  security.sudo.wheelNeedsPassword = false;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  virtualisation.libvirtd = {
    enable = true;
    qemuOvmf = true;
    qemuRunAsRoot = false;
    onBoot = "ignore";
  };
  # Enable CUPS to print documents.
  # services.printing.enable = true;

  # Enable sound.
  sound.enable = true;
  hardware.pulseaudio.enable = true;

  # Enable the X11 windowing system.
  services.xserver.enable = true;
  services.xserver.videoDrivers = [ "nvidia" ];
  hardware.opengl.driSupport32Bit = true;
  hardware.opengl.extraPackages32 = with pkgs.pkgsi686Linux; [ libva ];
  hardware.pulseaudio.support32Bit = true;

  # Enable the KDE Desktop Environment.
  services.xserver.displayManager.sddm.enable = true;
  services.xserver.desktopManager.plasma5.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users = {
    sjakkmarius = {
      isNormalUser = true;
      extraGroups = [ "wheel" "libvirtd" ]; 
      openssh.authorizedKeys.keys = [ "ssh-rsa ..." ];
    };
  };

  # This value determines the NixOS release with which your system is to be
  # compatible, in order to avoid breaking some software such as database
  # servers. You should change this only after NixOS release notes say you
  # should.
  system.stateVersion = "19.09"; # Did you read the comment?

}

3 Likes

Solved by adding the cudatoolkit package to my configuration.nix

edit:
Might have been to quick to celebrate, when I try to render using Cycles and Cuda GPU I get an error and the console shows something missing:

CUDA version 10.2 detected, build may succeed but only CUDA 10.1 is officially supported.
Compiling CUDA kernel …
“nvcc” -arch=sm_52 --cubin “/nix/store/nzci6fzx4v01blhiyfpgqvs8a39kd525-blender-2.82/share/blender/2.82/scripts/addons/cycles/source/kernel/kernels/cuda/kernel.cu” -o “/home/marius/.cache/cycles/kernels/cycles_kernel_sm52_F80EBC91C56BBBB785E84E2EFB89D785.cubin” -m64 --ptxas-options="-v" --use_fast_math -DNVCC -I"/nix/store/nzci6fzx4v01blhiyfpgqvs8a39kd525-blender-2.82/share/blender/2.82/scripts/addons/cycles/source"
cc1plus: fatal error: cuda_runtime.h: No such file or directory

My thought is that this is some directory or environment not exported due to some misconfiguration in my configuration.nix

Anyone know a fix?

2 Likes

I’m able to get it working by using a nix-shell with this:

{ pkgs ? import <nixpkgs> {} }:

pkgs.stdenv.mkDerivation {
  name = "cuda-env-shell";
  buildInputs = with pkgs; [
    cudatoolkit
  ];
  shellHook = ''
    export CUDA_PATH=${pkgs.cudatoolkit}
    export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
    export EXTRA_CCFLAGS="-I/usr/include"
  '';
}

Not sure if it’ll work for you tho…

1 Like

I made it work by applying Overlays instead of packageOverride.
Look:

in configuration.nix:

  nixpkgs = {
    config = { allowUnfree = true; };
    overlays = [
      (final: prev: {
        blender = prev.blender.override { cudaSupport = true; };
      })
    ];
  };

With NVIDIA drivers and cudatoolkit properly on system.

1 Like

If you’re using flakes, you can also use the blender-bin flake, which just wraps the upstream Blender binaries, so it has CUDA support. You can run the most recent stable version using nix run blender-bin, or enable it in a NixOS configuration:

{
  outputs = { self, nixpkgs, blender-bin }: {
    nixosConfigurations.bla = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules =
        [ ({ config, pkgs, ... }:
          { nixpkgs.overlays = [ blender-bin.overlay ];
            environment.systemPackages = [ pkgs.blender_2_93 ];
          })
          ...
        ];
    };
  };
}
1 Like

Thanks, this helped me discover nix registry list.

It would be nice if blender-bin showed up under search.nixos.org/flakes

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

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.

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

1 Like

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?

Hosted by Flying Circus.