Hi,
If I use this slightly obtuse command line, I get a Python with Torch:
nix develop --impure --expr "with import <nixpkgs> {}; pkgs.mkShell { packages = [python3 python3Packages.torch ];}"
If I wanted to package this with Python in a proper way that should integrate nicely with Nix, I think I should use Poetry. So I make a directory, do nix shell nixpkgs/23.05#poetry
and then do poetry init
. It interviews me, but the only thing I do of interest is add torch==2.0.1
when it asks me for project dependencies. Then I run poetry lock
to pin the dependency. Then I ran nix flake init -t poetry2nix#app
and modify the generated flake to get this:
{
description = "Application packaged using poetry2nix";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/23.05";
inputs.poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, flake-utils, poetry2nix }:
flake-utils.lib.eachDefaultSystem (system:
let
# see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples.
inherit (poetry2nix.legacyPackages.${system}) mkPoetryApplication;
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = {
myapp = mkPoetryApplication { projectDir = self; };
default = self.packages.${system}.myapp;
};
devShells.default = pkgs.mkShell {
packages = [ poetry2nix.packages.${system}.poetry ];
};
});
}
Seems innocuous. However, nix shell
fails with the following error:
python3.10-torch-test> File "/nix/store/r6vfjn6isavbqvnnmfvwdfy9anv8cgd2-python3.10-poetry-core-1.4.0/lib/python3.10/site-packages/poetry/core/masonry/utils/package_include.py", line 66, in check_elements
python3.10-torch-test> raise ValueError(
python3.10-torch-test> ValueError: /private/tmp/nix-build-python3.10-torch-test-0.1.0.drv-0/source/torch_test does not contain any element
python3.10-torch-test> error: subprocess-exited-with-error
python3.10-torch-test> × Preparing metadata (pyproject.toml) did not run successfully.
python3.10-torch-test> │ exit code: 1
python3.10-torch-test> ╰─> See above for output.
python3.10-torch-test> note: This error originates from a subprocess, and is likely not a problem with pip.
python3.10-torch-test> full command: /nix/store/xmxc0xc03fqq8b19sxfxwyd8k71kaz5m-python3-3.10.11/bin/python3.10 /nix/store/9zqasc9xrs5nlgv6wqfvkbah7gdfcvlg-python3.10-pip-23.0.1/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py prepare_metadata_for_build_wheel /private/tmp/nix-build-python3.10-torch-test-0.1.0.drv-0/tmp74sy6atb
python3.10-torch-test> cwd: /private/tmp/nix-build-python3.10-torch-test-0.1.0.drv-0/source
python3.10-torch-test> Preparing metadata (pyproject.toml) ... error
python3.10-torch-test> error: metadata-generation-failed
python3.10-torch-test> × Encountered error while generating package metadata.
python3.10-torch-test> ╰─> See above for output.
python3.10-torch-test> note: This is an issue with the package mentioned above, not pip.
python3.10-torch-test> hint: See above for details.
error: builder for '/nix/store/003ysq6zxk7jmvvgs6id7lrhhly63x5m-python3.10-torch-test-0.1.0.drv' failed with exit code 1;
My sense is that if I could tell Nix to use the NixPkgs version of Torch, all would be well. But I am confused as to why I need to. What is the right way to proceed here, assuming I want a Python development environment and a Poetry package, and I’d rather the two not differ meaningfully?
Thanks!