CUDA 12.8 support in nixpkgs

So it has been a crazy night for me so far, the local cuda 12.8 built with success at last, this is the approach and shell.nix:


# shell.nix
let
  pkgs = import <nixpkgs> { config.allowUnfree = true; };
  cuda128 = pkgs.stdenv.mkDerivation rec {
    name = "cudatoolkit-12.8.0";
    version = "12.8.0";
    src = /home/alice7/.dev/test-cuda_with_C/cuda_12.8.0_570.86.10_linux.run;
    nativeBuildInputs = [ pkgs.autoPatchelfHook pkgs.makeWrapper pkgs.coreutils pkgs.bash ];
    buildInputs = [
      pkgs.stdenv.cc.cc.lib  # libgcc_s, libc
      pkgs.libxml2           # libxml2.so.2
      pkgs.cudaPackages.cuda_cupti  # libcupti.so.12 (from nixpkgs, might be 12.4, but should work)
      pkgs.rdma-core         # libibverbs.so.1, librdmacm.so.1
      # libmlx5.so.1 not directly in nixpkgs; part of Mellanox OFED, ignore for now
    ];
    autoPatchelfIgnoreMissingDeps = [
      "libmlx5.so.1"       # RDMA-specific, optional
      "libcuda.so.1"       # Driver stub, provided by NVIDIA driver at runtime
    ];
    unpackPhase = ''
      echo "Unpacking Makeself CUDA 12.8.0 archive from $src"
      cp $src cuda.run
      chmod +x cuda.run
      mkdir -p $out
      ./cuda.run --tar xvf -C $out
      echo "Extracted contents before rearrangement:"
      ls -lh $out/builds
      mkdir -p $out/bin $out/lib64 $out/include
      mv $out/builds/cuda_nvcc/bin/* $out/bin/ 2>/dev/null || true
      mv $out/builds/*/lib64/* $out/lib64/ 2>/dev/null || true
      mv $out/builds/*/include/* $out/include/ 2>/dev/null || true
      rm -rf $out/builds
      echo "Final extracted contents:"
      ls -lh $out/bin $out/lib64 $out/include
    '';
    installPhase = ''
      echo "Installing CUDA 12.8.0"
      ls -lh $out/bin
      for bin in $out/bin/*; do
        if [ -f "$bin" ] && [ -x "$bin" ]; then
          wrapProgram "$bin" --prefix LD_LIBRARY_PATH : "$out/lib64"
        fi
      done
    '';
    postFixup = ''
      echo "Patching libraries:"
      ls -lh $out/lib64
      for lib in $out/lib64/*.so; do
        patchelf --set-rpath "$out/lib64" $lib
      done
    '';
  };
in
pkgs.mkShell {
  buildInputs = [ cuda128 ];
}


and this is the running result for a quick testing within the shell:


alice7@nixos ~/.d/test-cuda_with_C> nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on Wed_Jan_15_19:20:09_PST_2025
Cuda compilation tools, release 12.8, V12.8.61
Build cuda_12.8.r12.8/compiler.35404655_0
alice7@nixos ~/.d/test-cuda_with_C> nvidia-smi
Sat Feb 22 11:15:52 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 570.86.16              Driver Version: 570.86.16      CUDA Version: 12.8     |
|-----------------------------------------+------------------------+----------------------+
| 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 4060 ...    Off |   00000000:01:00.0 Off |                  N/A |
| N/A   44C    P8              2W /   80W |      15MiB /   8188MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A            1877      G   ...me-shell-47.2/bin/gnome-shell          2MiB |
+-----------------------------------------------------------------------------------------+



I am very close to make this to work:


// hello.cu
#include <stdio.h>
__global__ void kernel() { printf("Hello from GPU!\n"); }
int main() {
  kernel<<<1,1>>>();
  cudaDeviceSynchronize();
  printf("Hello from CPU!\n");
  return 0;
}

// compile with: nvcc -I{$CUDA_PATH}/include -L{$CUDA_PATH}/lib64 -L{$CUDA_PATH}/lib hello.cu -o hello


it returns:


alice7@nixos ~/.d/test-cuda_with_C> nvcc -I{$CUDA_PATH}/include -L{$CUDA_PATH}/lib64 -L{$CUDA_PATH}/lib hello.cu -o hello
nvcc warning : Support for offline compilation for architectures prior to '<compute/sm/lto>_75' will be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
sh: line 1: /nix/store/1wvizkjikwvzdy8ni3gxqifflnlmjdw4-cudatoolkit-12.8.0/bin/../nvvm/bin/cicc: No such file or directory


1 Like