Merge pull request #150 from nix-community/env-scripts

mkPoetryEnv: Add scripts from pyproject.toml (if they exist)
This commit is contained in:
adisbladis 2020-08-17 11:27:57 +02:00 committed by GitHub
commit a0122fd930
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View file

@ -159,13 +159,28 @@ lib.makeScope pkgs.newScope (self: {
}
);
inherit (py) pyProject;
# Add executables from tool.poetry.scripts
scripts = pyProject.tool.poetry.scripts or { };
hasScripts = scripts != { };
scriptsPackage = import ./shell-scripts.nix {
inherit scripts lib;
inherit (py) python;
};
hasEditable = editablePackageSources != { };
editablePackage = import ./editable.nix {
inherit pkgs lib poetryLib editablePackageSources;
inherit (py) pyProject python;
};
in
py.python.withPackages (_: py.poetryPackages ++ lib.optional (editablePackageSources != { }) editablePackage);
py.python.withPackages (
_: py.poetryPackages
++ lib.optional hasEditable editablePackage
++ lib.optional hasScripts scriptsPackage
);
/* Creates a Python application from pyproject.toml and poetry.lock

41
shell-scripts.nix Normal file
View file

@ -0,0 +1,41 @@
{ lib
, scripts
, python
}:
let
mkScript = bin: entrypoint:
let
elem = builtins.elemAt (builtins.split ":" entrypoint);
module = elem 0;
fn = elem 2;
in
''
cat << EOF >> $out/bin/${bin}
#!${python.interpreter}
import sys
import re
# Insert "" to add CWD to import path
sys.path.insert(0, "")
from ${module} import ${fn}
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', "", sys.argv[0])
sys.exit(${fn}())
EOF
chmod +x $out/bin/${bin}
'';
in
python.pkgs.buildPythonPackage {
name = "poetry2nix-env-scripts";
dontUnpack = true;
dontUseSetuptoolsBuild = true;
dontConfigure = true;
dontUseSetuptoolsCheck = true;
installPhase = ''
mkdir -p $out/bin
${lib.concatStringsSep "\n" (lib.mapAttrsToList mkScript scripts)}
'';
}