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.