Python: Difference between revisions
Improve the how to package Python application section about projects that use setup.py |
Include a section about pyproject.nix. A framework to package pyproject.toml application |
||
Line 256: | Line 256: | ||
== Package a Python application == | == Package a Python application == | ||
=== With setup.py === | === With <code>setup.py</code> === | ||
To package a Python application that uses <code>setup.py</code> you can use <code>buildPythonApplication</code>. More details about this and similar functions can be found in [https://nixos.org/manual/nixpkgs/stable/#building-packages-and-applications the nixpkgs manual]. | To package a Python application that uses <code>setup.py</code> you can use <code>buildPythonApplication</code>. More details about this and similar functions can be found in [https://nixos.org/manual/nixpkgs/stable/#building-packages-and-applications the nixpkgs manual]. | ||
Line 303: | Line 303: | ||
} | } | ||
</syntaxhighlight>Finally, build your project using <code>nix-build</code>. The result will be executable in <code>./result/bin/app.py</code>. | </syntaxhighlight>Finally, build your project using <code>nix-build</code>. The result will be executable in <code>./result/bin/app.py</code>. | ||
=== With <code>pyproject.toml</code> === | |||
When your project is using <code>pyproject.toml</code>you can use [https://github.com/nix-community/pyproject.nix pyproject.nix] to package your application. | |||
First, a simple file structure could look like this:<syntaxhighlight> | |||
├── app/ | |||
└── main.py | |||
├── flake.nix | |||
├── pyproject.toml | |||
└── README.md | |||
</syntaxhighlight>To reuse the example from above, we use the same flask application:<syntaxhighlight lang="python"> | |||
from flask import Flask | |||
app = Flask(__name__) | |||
@app.route('/') | |||
def hello_world(): | |||
return 'Hello, World!' | |||
if __name__ == '__main__': | |||
app.run(host="0.0.0.0", port=8080) | |||
</syntaxhighlight>Also, you need to define the <code>pyproject.toml</code>. Here, we only show some of the important parts. Please refer to <code>pyproject.nix</code> [https://nix-community.github.io/pyproject.nix/use-cases/pyproject.html documentation] for a full example.<syntaxhighlight lang="toml"> | |||
[project] | |||
name = "my-app" | |||
version = "0.1.0" | |||
description = "Simple app" | |||
# define any Python dependencies | |||
dependencies = [ | |||
"flask>3", | |||
] | |||
# define the CLI executable | |||
# Here, we define the entry point to be the 'main()' function in the module 'app/main.py' | |||
[project.scripts] | |||
cli = "app.main:main" | |||
</syntaxhighlight>We package the application by calling the <code>loadPyproject</code> function from <code>pyproject.nix</code>. Again, we only show a minimal example. More information can be found in the [https://nix-community.github.io/pyproject.nix/use-cases/pyproject.html documentation].<syntaxhighlight lang="nix"> | |||
{ | |||
description = "A basic flake using pyproject.toml project metadata"; | |||
inputs = { | |||
pyproject-nix = { | |||
url = "github:nix-community/pyproject.nix"; | |||
inputs.nixpkgs.follows = "nixpkgs"; | |||
}; | |||
}; | |||
outputs = { nixpkgs, pyproject-nix, ... }: | |||
let | |||
inherit (nixpkgs) lib; | |||
project = pyproject-nix.lib.project.loadPyproject { | |||
# Read & unmarshal pyproject.toml relative to this project root. | |||
# projectRoot is also used to set `src` for renderers such as buildPythonPackage. | |||
projectRoot = ./.; | |||
}; | |||
# This example is only using x86_64-linux | |||
pkgs = nixpkgs.legacyPackages.x86_64-linux; | |||
python = pkgs.python3; | |||
in | |||
{ | |||
# Build our package using `buildPythonPackage | |||
packages.x86_64-linux.default = | |||
let | |||
# Returns an attribute set that can be passed to `buildPythonPackage`. | |||
attrs = project.renderers.buildPythonPackage { inherit python; }; | |||
in | |||
# Pass attributes to buildPythonPackage. | |||
# Here is a good spot to add on any missing or custom attributes. | |||
python.pkgs.buildPythonPackage (attrs // { | |||
env.CUSTOM_ENVVAR = "hello"; | |||
}); | |||
}; | |||
} | |||
</syntaxhighlight>To run the application, call <code>nix run</code>. | |||
== Nixpkgs Python contribution guidelines == | == Nixpkgs Python contribution guidelines == |