-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/python: how to use pytorch from nixpkgs
- Loading branch information
Showing
6 changed files
with
701 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
examples/packages/languages/python-local-development-pdm-pytorch-from-nixpkgs/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.pdm-python |
58 changes: 58 additions & 0 deletions
58
examples/packages/languages/python-local-development-pdm-pytorch-from-nixpkgs/default.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# An example package with dependencies defined via pyproject.toml | ||
{ | ||
config, | ||
lib, | ||
dream2nix, | ||
... | ||
}: let | ||
python = config.deps.python; | ||
nixpkgsTorch = python.pkgs.torch; | ||
torchWheel = config.deps.runCommand "torch-wheel" {} '' | ||
file="$(ls "${nixpkgsTorch.dist}")" | ||
mkdir "$out" | ||
cp "${nixpkgsTorch.dist}/$file" "$out/$file" | ||
''; | ||
in { | ||
imports = [ | ||
dream2nix.modules.dream2nix.WIP-python-pdm | ||
]; | ||
|
||
mkDerivation = { | ||
src = lib.cleanSourceWith { | ||
src = lib.cleanSource ./.; | ||
filter = name: type: | ||
!(builtins.any (x: x) [ | ||
(lib.hasSuffix ".nix" name) | ||
(lib.hasPrefix "." (builtins.baseNameOf name)) | ||
(lib.hasSuffix "flake.lock" name) | ||
]); | ||
}; | ||
}; | ||
pdm.lockfile = ./pdm.lock; | ||
pdm.pyproject = ./pyproject.toml; | ||
|
||
buildPythonPackage = { | ||
pythonImportsCheck = [ | ||
"my_tool" | ||
]; | ||
}; | ||
|
||
# Override for torch to pick the wheel from nixpkgs instead of pypi | ||
overrides.torch = { | ||
mkDerivation.src = torchWheel; | ||
# This hack is needed to put the right filename into the src attribute. | ||
# We cannot know the exact wheel filename upfront, as it is system dependent. | ||
mkDerivation.prePhases = ["selectWheelFile"]; | ||
env.selectWheelFile = '' | ||
export src="$src/$(ls $src)" | ||
''; | ||
mkDerivation.buildInputs = [ | ||
# The original build inputs of torch are required for the autoPatchelf phase | ||
nixpkgsTorch.inputDerivation | ||
]; | ||
buildPythonPackage = { | ||
format = "wheel"; | ||
pyproject = null; | ||
}; | ||
}; | ||
} |
60 changes: 60 additions & 0 deletions
60
examples/packages/languages/python-local-development-pdm-pytorch-from-nixpkgs/flake.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
# This example flake.nix is pretty generic and the same for all | ||
# examples, except when they define devShells or extra packages. | ||
description = "Dream2nix example flake"; | ||
|
||
# We import the latest commit of dream2nix main branch and instruct nix to | ||
# re-use the nixpkgs revision referenced by dream2nix. | ||
# This is what we test in CI with, but you can generally refer to any | ||
# recent nixpkgs commit here. | ||
inputs = { | ||
dream2nix.url = "github:nix-community/dream2nix"; | ||
nixpkgs.follows = "dream2nix/nixpkgs"; | ||
}; | ||
|
||
outputs = { | ||
self, | ||
dream2nix, | ||
nixpkgs, | ||
}: let | ||
# A helper that helps us define the attributes below for | ||
# all systems we care about. | ||
eachSystem = nixpkgs.lib.genAttrs [ | ||
"aarch64-darwin" | ||
"aarch64-linux" | ||
"x86_64-darwin" | ||
"x86_64-linux" | ||
]; | ||
in { | ||
packages = eachSystem (system: { | ||
# For each system, we define our default package | ||
# by passing in our desired nixpkgs revision plus | ||
# any dream2nix modules needed by it. | ||
default = dream2nix.lib.evalModules { | ||
packageSets.nixpkgs = nixpkgs.legacyPackages.${system}; | ||
modules = [ | ||
# Import our actual package definiton as a dream2nix module from ./default.nix | ||
./default.nix | ||
{ | ||
# Aid dream2nix to find the project root. This setup should also works for mono | ||
# repos. If you only have a single project, the defaults should be good enough. | ||
paths.projectRoot = ./.; | ||
# can be changed to ".git" or "flake.nix" to get rid of .project-root | ||
paths.projectRootFile = "flake.nix"; | ||
paths.package = ./.; | ||
} | ||
]; | ||
}; | ||
}); | ||
devShells = eachSystem (system: { | ||
default = nixpkgs.legacyPackages.${system}.mkShell { | ||
# inherit from the dream2nix generated dev shell | ||
inputsFrom = [self.packages.${system}.default.devShell]; | ||
# add extra packages | ||
packages = [ | ||
nixpkgs.legacyPackages.${system}.hello | ||
]; | ||
}; | ||
}); | ||
}; | ||
} |
6 changes: 6 additions & 0 deletions
6
.../packages/languages/python-local-development-pdm-pytorch-from-nixpkgs/my_tool/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import requests | ||
import torch | ||
|
||
|
||
def main(): | ||
print("Hello World!") |
Oops, something went wrong.