-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1346 from dsalaza4/dsalazaratfluid
feat(back): #1345 make python poetry environment
- Loading branch information
Showing
5 changed files
with
152 additions
and
70 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,73 +1,21 @@ | ||
{ __nixpkgs__, listOptional, makeSearchPaths, ... }: | ||
{ __nixpkgs__, makePythonPoetryEnvironment, makeSearchPaths, ... }: | ||
{ pythonProjectDir, pythonVersion, preferWheels ? true | ||
, overrides ? (self: super: { }), }: | ||
let | ||
# Import poetry2nix | ||
poetry2nix = let | ||
commit = "528d500ea826383cc126a9be1e633fc92b19ce5d"; | ||
sha256 = "sha256:1q245v4q0bb30ncfj66gl6dl1k46am28x7kjj6d3y7r6l4fzppq8"; | ||
src = builtins.fetchTarball { | ||
inherit sha256; | ||
url = | ||
"https://api.github.com/repos/nix-community/poetry2nix/tarball/${commit}"; | ||
}; | ||
in import src { pkgs = __nixpkgs__; }; | ||
|
||
# Decide Python version | ||
is39 = pythonVersion == "3.9"; | ||
is310 = pythonVersion == "3.10"; | ||
is311 = pythonVersion == "3.11"; | ||
is312 = pythonVersion == "3.12"; | ||
python = { | ||
"3.9" = __nixpkgs__.python39; | ||
"3.10" = __nixpkgs__.python310; | ||
"3.11" = __nixpkgs__.python311; | ||
"3.12" = __nixpkgs__.python312; | ||
}.${pythonVersion}; | ||
|
||
# Override HOME directory for each package/derivation | ||
# and add any custom override on top of it | ||
overridenPackages = self: super: | ||
let | ||
overridePackageHome = pkg: super: | ||
super.${pkg}.overridePythonAttrs (old: { | ||
preUnpack = '' | ||
export HOME=$(mktemp -d) | ||
rm -rf /homeless-shelter | ||
'' + (old.preUnpack or ""); | ||
}); | ||
packages = let | ||
lock = "${pythonProjectDir}/poetry.lock"; | ||
data = builtins.fromTOML (builtins.readFile lock); | ||
main = builtins.map (pkg: pkg.name) data.package; | ||
extras = let | ||
list = builtins.concatLists (builtins.map (pkg: | ||
if builtins.hasAttr "extras" pkg then | ||
builtins.concatLists (builtins.attrValues pkg.extras) | ||
else | ||
[ ]) data.package); | ||
names = builtins.map (extra: | ||
builtins.toString (builtins.head | ||
(builtins.split " " (__nixpkgs__.lib.toLower extra)))) list; | ||
in names; | ||
in main ++ extras; | ||
overridenHomes = builtins.listToAttrs (builtins.map (pkg: { | ||
name = pkg; | ||
value = overridePackageHome pkg super; | ||
}) packages); | ||
in overridenHomes // (overrides self super); | ||
|
||
# Final python environment | ||
env = poetry2nix.mkPoetryEnv { | ||
overrides = poetry2nix.defaultPoetryOverrides.extend overridenPackages; | ||
env = makePythonPoetryEnvironment { | ||
inherit pythonProjectDir; | ||
inherit pythonVersion; | ||
inherit preferWheels; | ||
projectDir = pythonProjectDir; | ||
inherit python; | ||
inherit overrides; | ||
}; | ||
in makeSearchPaths { | ||
bin = [ env ]; | ||
pythonPackage39 = listOptional is39 env; | ||
pythonPackage310 = listOptional is310 env; | ||
pythonPackage311 = listOptional is311 env; | ||
pythonPackage312 = listOptional is312 env; | ||
pythonPackage39 = | ||
__nixpkgs__.lib.lists.optional (env.pythonVersion == "3.9") env; | ||
pythonPackage310 = | ||
__nixpkgs__.lib.lists.optional (env.pythonVersion == "3.10") env; | ||
pythonPackage311 = | ||
__nixpkgs__.lib.lists.optional (env.pythonVersion == "3.11") env; | ||
pythonPackage312 = | ||
__nixpkgs__.lib.lists.optional (env.pythonVersion == "3.12") env; | ||
} |
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,64 @@ | ||
{ __nixpkgs__, makeSearchPaths, ... }: | ||
{ pythonProjectDir, pythonVersion, preferWheels ? true | ||
, overrides ? (self: super: { }), }: | ||
let | ||
# Import poetry2nix | ||
poetry2nix = let | ||
commit = "528d500ea826383cc126a9be1e633fc92b19ce5d"; | ||
sha256 = "sha256:1q245v4q0bb30ncfj66gl6dl1k46am28x7kjj6d3y7r6l4fzppq8"; | ||
src = builtins.fetchTarball { | ||
inherit sha256; | ||
url = | ||
"https://api.github.com/repos/nix-community/poetry2nix/tarball/${commit}"; | ||
}; | ||
in import src { pkgs = __nixpkgs__; }; | ||
|
||
# Decide Python version | ||
is39 = pythonVersion == "3.9"; | ||
is310 = pythonVersion == "3.10"; | ||
is311 = pythonVersion == "3.11"; | ||
is312 = pythonVersion == "3.12"; | ||
python = { | ||
"3.9" = __nixpkgs__.python39; | ||
"3.10" = __nixpkgs__.python310; | ||
"3.11" = __nixpkgs__.python311; | ||
"3.12" = __nixpkgs__.python312; | ||
}.${pythonVersion}; | ||
|
||
# Override HOME directory for each package/derivation | ||
# and add any custom override on top of it | ||
overridenPackages = self: super: | ||
let | ||
overridePackageHome = pkg: super: | ||
super.${pkg}.overridePythonAttrs (old: { | ||
preUnpack = '' | ||
export HOME=$(mktemp -d) | ||
rm -rf /homeless-shelter | ||
'' + (old.preUnpack or ""); | ||
}); | ||
packages = let | ||
lock = "${pythonProjectDir}/poetry.lock"; | ||
data = builtins.fromTOML (builtins.readFile lock); | ||
main = builtins.map (pkg: pkg.name) data.package; | ||
extras = let | ||
list = builtins.concatLists (builtins.map (pkg: | ||
if builtins.hasAttr "extras" pkg then | ||
builtins.concatLists (builtins.attrValues pkg.extras) | ||
else | ||
[ ]) data.package); | ||
names = builtins.map (extra: | ||
builtins.toString (builtins.head | ||
(builtins.split " " (__nixpkgs__.lib.toLower extra)))) list; | ||
in names; | ||
in main ++ extras; | ||
overridenHomes = builtins.listToAttrs (builtins.map (pkg: { | ||
name = pkg; | ||
value = overridePackageHome pkg super; | ||
}) packages); | ||
in overridenHomes // (overrides self super); | ||
in poetry2nix.mkPoetryEnv { | ||
overrides = poetry2nix.defaultPoetryOverrides.extend overridenPackages; | ||
inherit preferWheels; | ||
projectDir = pythonProjectDir; | ||
inherit python; | ||
} |
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