Nix language-specific dependencies
The following Nix shell declaration has a common bug:
let
pkgs = import (builtins.fetchTarball {
name = "nixos-unstable-2024-06-05";
url = "https://github.com/nixos/nixpkgs/archive/57610d2f8f0937f39dbd72251e9614b1561942d8.tar.gz";
sha256 = "0k8az8vmfdk1n8xlza252sqk0hm1hfc7g67adin6jxqaab2s34n9";
}) { };
in
pkgs.mkShell {
packages = [
pkgs.bashInteractive
pkgs.python3Packages.pytest
pkgs.python310
];
}
Running python
within the shell works, and running pytest
works, but the two
are not connected:
$ python -c "import pytest"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'pytest'
The fix is to explicitly include the Python packages with Python itself:
let
pkgs = import (builtins.fetchTarball {
name = "nixos-unstable-2024-06-05";
url = "https://github.com/nixos/nixpkgs/archive/57610d2f8f0937f39dbd72251e9614b1561942d8.tar.gz";
sha256 = "0k8az8vmfdk1n8xlza252sqk0hm1hfc7g67adin6jxqaab2s34n9";
}) { };
in
pkgs.mkShell {
packages = [
pkgs.bashInteractive
(pkgs.python310.withPackages (pythonPackages: [ pythonPackages.pytest ]))
];
}
Many interpreters support this pattern. Have a look for “withPackages
” in the
nixpkgs manual.
No webmentions were found.