Has anyone managed to get CUDA working with poetry2nix? I’ve seen a number of questions related to CUDA and python but can’t find a solution with poetry.
I have set up nvidia globally and nvidia-smi
successfully shows my GPU. Additionally, I followed this nixos + cuda + docker guide and can successfully use my GPU and it can be used inside docker (podman).
Running an official pytorch docker with docker run -ti --rm --gpus all pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime
, pytorch finds the GPU with import torch; print(torch.cuda.is_available())
. And if I install jax with cuda inside the docker, it also finds the GPU fine.
I cannot get jax or pytorch to find it with a flake using poetry2nix. Trying jax.devices()
gives the error
RuntimeError: Unable to initialize backend 'cuda': FAILED_PRECONDITION: No visible GPU devices. (you may need to uninstall the failing plugin package, or set JAX_PLATFORMS=cpu to skip this backend.)
Using a flake.nix:
{
description = "Test CUDA and poetry2nix";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, poetry2nix }:
let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
overlays = [ poetry2nix.overlays.default ];
config.allowUnfreePredicate = pkg:
builtins.elem (pkgs.lib.getName pkg) [
"cuda-merged"
"cuda_cuobjdump"
"cudnn"
"cuda_gdb"
"cuda_nvcc"
"cuda_nvdisasm"
"cuda_nvprune"
"cuda_cccl"
"cuda_cudart"
"cuda_cupti"
"cuda_cuxxfilt"
"cuda_nvml_dev"
"cuda_nvrtc"
"cuda_nvtx"
"cuda_profiler_api"
"cuda_sanitizer_api"
"libcublas"
"libcufft"
"libcurand"
"libcusolver"
"libnvjitlink"
"libcusparse"
"libnpp"
"nvidia-settings"
"nvidia-x11"
];
};
pyEnv = pkgs.poetry2nix.mkPoetryEnv {
projectDir = ./.;
editablePackageSources = { foo = ./.; };
preferWheels = true;
};
in {
devShells.${system}.default = pkgs.mkShell {
# Not sure what build inputs I need but seen these in various places
# related to CUDA + python.
buildInputs = with pkgs; [
cudatoolkit
linuxPackages.nvidia_x11
cudaPackages.cudnn
libGLU
libGL
xorg.libXi
xorg.libXmu
freeglut
xorg.libXext
xorg.libX11
xorg.libXv
xorg.libXrandr
zlib
ncurses5
stdenv.cc
binutils
];
packages = [ pyEnv pkgs.poetry ];
# Similarly to the build inputs, seen various env variables that
# might need to be set.
shellHook = ''
# export LD_LIBRARY_PATH=$"LD_LIBRARY_PATH:${pkgs.cudatoolkit}/targets/x86_64-linux/lib"
# export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
# export CUDA_PATH=${pkgs.cudatoolkit}
export LD_LIBRARY_PATH="${pkgs.linuxPackages.nvidia_x11}/lib"
# export EXTRA_CCFLAGS="-I/usr/include"
'';
};
};
}
And a pyproject.toml:
[tool.poetry]
name = "foo"
version = "0.1.0"
description = "Use CUDA"
authors = ["me"]
packages = [{ include = "foo" }]
[tool.poetry.dependencies]
python = "^3.10,<3.13"
jax = {version = "^0.4.35", extras = ["cuda12"]}
torch = "^2.5.1"
[tool.poetry.group.dev.dependencies]
ipython = "^8.16.1"
And the relavent lines of in my global config:
services.xserver = {
videoDrivers = [ "nvidia" ];
};
hardware = {
graphics.enable = true;
nvidia = {
modesetting.enable = true;
powerManagement.enable = false;
powerManagement.finegrained = true;
open = true;
nvidiaSettings = true;
package = config.boot.kernelPackages.nvidiaPackages.stable;
prime = {
offload.enable = true;
intelBusId = "PCI:0:2:0";
nvidiaBusId = "PCI:1:0:0";
};
};
nvidia-container-toolkit.enable = true;
};
In the flake, running poetry lock
to generate the poetry.lock
file, building, and running ipython -c 'import jax; print(jax.devices())'
gives the error.