Allow unfree in a shell.nix
I recently made a switch to NixOS on my primary laptop, and have been exploring the use of nix-shell to build and run declarative, ephemeral environments. Each environment comes with different packages installed.
For those unfamiliar with NixOS, it's a Linux distribution that takes a unique approach to package and configuration management. And nix-shell is a tool that lets you create environments isolated from each other. However, I hit a roadblock when I attempted to use an "unfree" package, CUDA, in one of my experiments.
"Unfree" refers to software that doesn't meet "free software" criteria. By default, NixOS restricts the installation of such packages. The solutions I've found online suggested setting an environment variable to allow the installation of unfree packages, but I wanted a solution within the nix shell file itself. After a bit of tinkering, I managed to find a way. Here it is:
let
# Configure Nix to allow unfree packages.
config = {
allowUnfree = true;
};
pkgs = import <nixpkgs> { inherit config; };
in
pkgs.mkShell {
buildInputs = with pkgs; [
python3
python3Packages.tensorflowWithCuda # this is the problematic line
python3Packages.keras
python3Packages.pandas
python3Packages.numpy
python3Packages.matplotlib
python3Packages.jupyterlab
python3Packages.ipywidgets
python3Packages.ipydatawidgets
python3Packages.ipython
];
# Automatically run jupyter when entering the shell.
shellHook = "jupyter-lab";
}
Here, the let section is importing nixpkgs with the config.allowUnfree option set to true.
After that, running nix-shell succeeded with installing the cuda packages.