Home Explore Blog Models CI



nixpkgs

3rd chunk of `doc/languages-frameworks/cuda.section.md`
cf7cbd1219b65f9d3d300ab62cd57397ead94668182adfa60000000100000fe5
As an example, you can change the fixup function used for cuDNN for only the default CUDA package set with this overlay:

```nix
final: prev: {
  cudaPackages = prev.cudaPackages.override (prevArgs: {
    _cuda = prevArgs._cuda.extend (
      _: prevAttrs: {
        fixups = prevAttrs.fixups // {
          cudnn = <your-fixup-function>;
        };
      }
    );
  });
}
```

### Extending CUDA package sets {#cuda-extending-cuda-package-sets}

CUDA package sets are scopes and provide the usual `overrideScope` attribute for overriding package attributes (see the note about `cudaMajorMinorVersion` and `_cuda` in [Configuring CUDA package sets](#cuda-modifying-cuda-package-sets)).

Inspired by `pythonPackagesExtensions`, the `_cuda.extensions` attribute is a list of extensions applied to every version of the CUDA package set, allowing modification of all versions of the CUDA package set without needing to know their names or explicitly enumerate and modify them. As an example, disabling `cuda_compat` across all CUDA package sets can be accomplished with this overlay:

```nix
final: prev: {
  _cuda = prev._cuda.extend (
    _: prevAttrs: {
      extensions = prevAttrs.extensions ++ [ (_: _: { cuda_compat = null; }) ];
    }
  );
}
```

### Using `cudaPackages` {#cuda-using-cudapackages}

::: {.caution}
A non-trivial amount of CUDA package discoverability and usability relies on the various setup hooks used by a CUDA package set. As a result, users will likely encounter issues trying to perform builds within a `devShell` without manually invoking phases.
:::

To use one or more CUDA packages in an expression, give the expression a `cudaPackages` parameter, and in case CUDA support is optional, add a `config` and `cudaSupport` parameter:

```nix
{
  config,
  cudaSupport ? config.cudaSupport,
  cudaPackages,
}:
<package-expression>
```

In your package's derivation arguments, it is _strongly_ recommended that the following are set:

```nix
{
  __structuredAttrs = true;
  strictDeps = true;
}
```

These settings ensure that the CUDA setup hooks function as intended.

When using `callPackage`, you can choose to pass in a different variant, e.g. when a package requires a specific version of CUDA:

```nix
{ mypkg = callPackage { cudaPackages = cudaPackages_12_6; }; }
```

::: {.caution}
Overriding the CUDA package set for a package may cause inconsistencies, because the override does not affect its direct or transitive dependencies. As a result, it is easy to end up with a package that use a different CUDA package set than its dependencies. If possible, it is recommended that you change the default CUDA package set globally, to ensure a consistent environment.
:::

### Nixpkgs CUDA variants {#cuda-nixpkgs-cuda-variants}

Nixpkgs CUDA variants are provided primarily for the convenience of selecting CUDA-enabled packages by attribute path. As an example, the `pkgsForCudaArch` collection of CUDA Nixpkgs variants allows you to access an instantiation of OpenCV with CUDA support for an Ada Lovelace GPU with the attribute path `pkgsForCudaArch.sm_89.opencv`, without needing to modify the `config` provided when importing Nixpkgs.

::: {.caution}
Nixpkgs variants are not free: they require re-evaluating Nixpkgs. Where possible, import Nixpkgs once, with the desired configuration.
:::

#### Using `cudaPackages.pkgs` {#cuda-using-cudapackages-pkgs}

Each CUDA package set has a `pkgs` attribute, which is a variant of Nixpkgs in which the enclosing CUDA package set becomes the default. This was done primarily to avoid package set leakage, wherein a member of a non-default CUDA package set has a (potentially transitive) dependency on a member of the default CUDA package set.

::: {.note}
Package set leakage is a common problem in Nixpkgs and is not limited to CUDA package sets.
:::

As an added benefit of `pkgs` being configured this way, building a package with a non-default version of CUDA is as simple as accessing an attribute. As an example, `cudaPackages_12_8.pkgs.opencv` provides OpenCV built against CUDA 12.8.

Title: Nixpkgs CUDA: Extending, Using, and Managing Package Set Variants
Summary
This section details how to extend and utilize CUDA package sets within Nixpkgs. It explains that the `_cuda.extensions` attribute allows for global modifications across all CUDA package sets. For using CUDA packages, expressions should accept a `cudaPackages` parameter, and it's highly recommended to set `__structuredAttrs = true;` and `strictDeps = true;`. Users can specify different CUDA package set variants with `callPackage`, but caution is advised against inconsistencies with dependencies; global overrides are preferred. The text also introduces Nixpkgs CUDA variants like `pkgsForCudaArch` for convenient selection of CUDA-enabled packages by architecture, noting that variants incur re-evaluation costs. Finally, it describes the `cudaPackages.pkgs` attribute, which provides a Nixpkgs variant where a specific CUDA package set is default, helping to prevent package set leakage and simplifying the use of non-default CUDA versions for packages.