CUDA shows different memory size in docker and nixos

I have a 4090 and i want to start packaging some cuda applications. But i am encountering some weird behaviours during this simple flake.

This is the simple cuda kernel:

#include <unistd.h>
#include <iostream>
int main(){
    size_t mf, ma;
    cudaMemGetInfo(&mf, &ma);
    std::cout << "Free memory (mb): " << mf/1024/1024 << std::endl;
    std::cout << "Total memory (mb): " << ma/1024/1024 << std::endl;
    return 0;
}

I am using nixos with these relevant configurations:

{
  hardware.nvidia-container-toolkit.enable = true;
  hardware.graphics = {
    enable = true;
  };
  services.xserver.videoDrivers = ["nvidia"];
  hardware.nvidia = {
    modesetting.enable = true;
    powerManagement.enable = false;
    powerManagement.finegrained = false;
    open = false;
    nvidiaSettings = true;
    package = config.boot.kernelPackages.nvidiaPackages.stable;
  };
  virtualisation.docker.enable = true;
}

Which i am compiling in this flake:

{
  description = "cuda kernel compilation with nix";

  nixConfig = {
    extra-substituters = [
      "https://nix-community.cachix.org"
    ];
    extra-trusted-public-keys = [
      "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
    ];
  };

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
  };
  outputs = {
    self,
    nixpkgs,
    ...
  }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs {
      inherit system;
      config.allowUnfree = true;
      config.cudaSupport = true;
      config.nvidia.acceptLicense = true;
    };
  in {
    packages."${system}" = {
      # just one main.cu file for now
      # compile it and place it to $out/bin/cuda-kernel
      cuda-kernel = pkgs.stdenv.mkDerivation {
        name = "cuda-kernel";
        src = ./.;
        buildInputs = with pkgs; [
          cudatoolkit
          cudaPackages.cuda_cudart
          linuxPackages.nvidia_x11
        ];
        LD_LIBRARY_PATH = "${pkgs.linuxPackages.nvidia_x11}/lib";
        buildPhase = ''
          nvcc -o main main.cu
          mkdir -p $out/bin
          mv main $out/bin/cuda-kernel
        '';
      };
      default = self.packages."${system}".cuda-kernel;
    };
  };
}

But it does give me this as output:

Free memory (mb): 0
Total memory (mb): 553

But when i try to compile this inside the nvidia/cuda:12.6.3-devel-ubuntu24.04 with nvcc it works and shows me correct output:

Free memory (mb): 22083
Total memory (mb): 24109

Any tip help or even blogposts are appreciated. Thank you for reading and have a nice day. :slight_smile:

I have some additional information. When i enter the nix shell with nix develop i learned that there is a mismatch between the driver/library versions.

$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module  560.35.03  Fri Aug 16 21:39:15 UTC 2024
GCC version:  gcc version 13.3.0 (GCC)


$ nvidia-smi
Failed to initialize NVML: Driver/library version mismatch
NVML library version: 565.77


$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Tue_Feb_27_16:19:38_PST_2024
Cuda compilation tools, release 12.4, V12.4.99
Build cuda_12.4.r12.4/compiler.33961263_0

How can i resolve this? Can i make this flake use its own cuda drivers?