mirror of
https://github.com/vale981/poetry2nix
synced 2025-03-06 01:31:39 -05:00
Merge pull request #150 from nix-community/env-scripts
mkPoetryEnv: Add scripts from pyproject.toml (if they exist)
This commit is contained in:
commit
a0122fd930
2 changed files with 57 additions and 1 deletions
17
default.nix
17
default.nix
|
@ -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
41
shell-scripts.nix
Normal 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)}
|
||||
'';
|
||||
}
|
Loading…
Add table
Reference in a new issue