CUDA setup on NixOS


#1

I’m using ambrop72’s fix on 19.03pre154487.0a7e258012b (Koi).

hardware.nvidia.modesetting.enable = true;                                    
hardware.nvidia.optimus_prime = {                                             
  enable = true;                                                              
  intelBusId = "PCI:0:2:0";                                                   
  nvidiaBusId = "PCI:9:0:0";                                                  
};

with

nixpkgs.config.allowUnfree = true;  

(...)

services.xserver.videoDrivers = [ "nvidia" ];

nvidia-smi seems to work:

$ nvidia-smi
Wed Oct 10 09:49:31 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.87                 Driver Version: 390.87                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GT 740M     Off  | 00000000:09:00.0 N/A |                  N/A |
| N/A   47C    P5    N/A /  N/A |     80MiB /  2004MiB |     N/A      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0                    Not Supported                                       |
+-----------------------------------------------------------------------------+

What I’m trying to do is set up CUDA & nvcc, but I’m finding some issues due to lack of info, like:

  • It seems that cudatoolkit isn’t listed as package in nixos packages? But works fine if added in environment.systemPackages.
  • The wiki mentions that an additional rule needs to created:

Vulkan, CUDA and OpenCL work, though CUDA needs an additional device creation rule https://github.com/NixOS/nixpkgs/blob/05e375d7103ac51e2da917965c37246c99f1ae4f/nixos/modules/hardware/video/nvidia.nix#L72

Should I add this on my configuration.nix? nvidia.nix already seems to do that.

  # CUDA
  systemd.services.nvidia-control-devices = {
    wantedBy = [
      "multi-user.target"
    ];
    serviceConfig.ExecStart = "${pkgs.linuxPackages.nvidia_x11}/bin/nvidia-smi";
  };

However, while trying to build the examples:

$ cd cuda-samples
$ nix-build default.nix -A examplecuda

error: Package ‘cudatoolkit-9.1.85.1’ in /nix/store/wly35vrz5p3vhbjpf2xsr4zgqqyhsjvm-nixos-19.03pre154487.0a7e258012b/nixos/pkgs/development/compilers/cudatoolkit/default.nix:145 has an unfree license (‘unfree’), refusing to evaluate.

a) For `nixos-rebuild` you can set
  { nixpkgs.config.allowUnfree = true; }
in configuration.nix to override this.

b) For `nix-env`, `nix-build`, `nix-shell` or any other Nix command you can add
  { allowUnfree = true; }
to ~/.config/nixpkgs/config.nix.

Edit: nvidia-control-devices seems to fail to start.

sudo systemctl status nvidia-control-devices.service 
● nvidia-control-devices.service
   Loaded: loaded (/nix/store/7wc1p30n6ysi44jz9dcsi06f9hzwprzw-unit-nvidia-control-devices.service/nvidia-control-devices.service; enabled; vendor>
   Active: failed (Result: exit-code) since Wed 2018-10-10 10:11:59 -03; 1s ago
  Process: 2362 ExecStart=/nix/store/wkbhrvlnngd1r3mgc7075qcl3ql3splb-nvidia-x11-390.87-4.14.74/bin/nvidia-smi (code=exited, status=203/EXEC)
 Main PID: 2362 (code=exited, status=203/EXEC)

#2

Nevermind, I’m really stupid. It seems nix-env, nix-shell and nix-build don’t use the nixpkgs config from /etc/nixos/configuration.nix.

Just had to create a .config/nixpkgs/config.nix for my user and add:


{
    allowUnfree = true;
}

To run Grahan’s examples.


#3

Quoting Marcos Benevides (2018-10-13 22:41:27)

Nevermind, I’m really stupid. It seems nix-env, nix-shell and nix-build don’t use the nixpkgs config from /etc/nixos/configuration.nix.

Yes. This is intentional, I guess, to make users be able to decide on
their own if they want unfree packages in their user profile.