Nvidia CUDA configuration on NixOS

Hello Nixos people,

I hope all who read this are doing well. I am new to NixOS and have been struggling to get other software (the SBCL compiler) to find the Nvidia CUDA drivers installed in my NixOS environment on WSL. As I try to configure SBCL packages that depend upon those CUDA drivers, they throw some permutation of cuda.h not found. It seems that this issue arises because the NixOS system structure deviates from the standard File System Hierarchy used by most other Linux systems.

Fortunately, this page on the Nixos Wiki provides several nice examples showcasing different ways to create a wrapper that points those packages that expect a standard FHS to the right place. I chose the flake option:

# flake.nix, run with `nix develop`
{
  description = "CUDA development environment";
  outputs = {
    self,
    nixpkgs,
  }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs {
      inherit system;
      config.allowUnfree = true;
      config.cudaSupport = true;
      config.cudaVersion = "12";
    };
    # Change according to the driver used: stable, beta
    nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable;
  in {
    # alejandra is a nix formatter with a beautiful output
    formatter."${system}" = nixpkgs.legacyPackages.${system}.alejandra;
    devShells.${system}.default = pkgs.mkShell {
      buildInputs = with pkgs; [
        ffmpeg
        fmt.dev
        cudaPackages.cuda_cudart
        cudatoolkit
        nvidiaPackage
        cudaPackages.cudnn
        libGLU
        libGL
        xorg.libXi
        xorg.libXmu
        freeglut
        xorg.libXext
        xorg.libX11
        xorg.libXv
        xorg.libXrandr
        zlib
        ncurses
        stdenv.cc
        binutils
        uv
      ];

      shellHook = ''
        export LD_LIBRARY_PATH="${nvidiaPackage}/lib:$LD_LIBRARY_PATH"
        export CUDA_PATH=${pkgs.cudatoolkit}
        export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib"
        export EXTRA_CCFLAGS="-I/usr/include"
        export CMAKE_PREFIX_PATH="${pkgs.fmt.dev}:$CMAKE_PREFIX_PATH"
        export PKG_CONFIG_PATH="${pkgs.fmt.dev}/lib/pkgconfig:$PKG_CONFIG_PATH"
      '';
    };
  };
}

Now, I would like to import this flake into my configuration.nix so that everything is available systemwide immediately after a nixos-rebuild switch. So, I tell Nix where it can find the flake that it should treat as a module:

#Other stuff ----
imports = [
   <nixos-wsl/modules>
   ./ml-toolchain/flake.nix
];
#Other stuff ----

However, this throws: error: The option 'description' does not exist. As I understand it, this is because the Nix evaluator tries to enter this file, thinking it is a module, but finds the structure of a flake and does not know what to do.

This is where all approaches seem to diverge, and I become very confused.

From what I read, only modules can be imported, not flakes – but, apparently, all modules are contained within a flake! I am not yet familiar enough with Nix syntax to recognize the differences between a module and a flake. Furthermore, the error messages thrown by the Nix evaluator/compiler(?) presuppose just a little bit more knowledge of NixOS than I currently have.

Is anyone more experienced willing to provide some insight and guidence?

Thank you for your time and consideration.

1 Like

A devShell won’t be installed system-wide when using nixos-rebuild switch. Instead, you’d have to cd into the Nix flake for your NixOS configuration and use nix develop. If you’re looking for a shell what will contain the dev tools you want, you can use writeShellApplication. See my example here: llama-cpp-cuda/pkgs/llama-cpp-shell/default.nix at 448b74fbc809933c4a784ac0f1b98585a10c8dce · emmanuelrosa/llama-cpp-cuda · GitHub

As for how to import the flake, if you’re not building NixOS as a flake you may have to use one of those wrappers that makes a flake look like an “old-style” import. If you are building NixOS as a flake, then you’ll need to import the ml flake using inputs, not as a module.


1 Like