This commit is contained in:
Amy de Buitléir 2025-10-12 15:58:37 +01:00
parent 3d8f0e2b8b
commit d0be400f85
9 changed files with 109 additions and 33 deletions

View file

@ -65,22 +65,20 @@ $ python hello.py
== Configuring setuptools
Next, create a Python script to build the package. We'll use Python's
Next, configure the package. We'll use Python's
setuptools, but you can use other build tools. For more information on
setuptools, see the
https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/[Python
Packaging User Guide], especially the section on
https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#setup-args[setup
args].
https://setuptools.pypa.io/en/latest/index.html[Setuptools documentation], especially the section on
https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html[pyproject.toml].
////
$ curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/setup.py --silent --output setup.py
$ curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/pyproject.toml --silent --output pyproject.toml
////
[source,python,linenums]
.setup.py
[source,toml,linenums]
.pyproject.toml
....
$# cat setup.py
$# cat pyproject.toml
....
== Building the program manually (optional)
@ -140,11 +138,15 @@ Let's start with the development shell. It seems logical to write
something like the following.
....
devShells = rec {
devShells = forAllSupportedSystems (system:
let
pkgs = nixpkgsFor.${system};
pythonEnv = pkgs.python3.withPackages (ps: [ ps.build ]);
in {
default = pkgs.mkShell {
packages = [ (python.withPackages (ps: with ps; [ build ])) ];
packages = [ pythonEnv ];
};
};
});
....
Note that we need the parentheses to prevent `python.withPackages` and
@ -153,31 +155,30 @@ wanted to work with `virtualenv` and `pip` instead of `build`. We could
write something like the following.
....
devShells = rec {
devShells = forAllSupportedSystems (system:
let
pkgs = nixpkgsFor.${system};
pythonEnv = pkgs.python3.withPackages (ps: [ ps.virtualenv ps.pip ]);
in {
default = pkgs.mkShell {
packages = [
# Python plus helper tools
(python.withPackages (ps: with ps; [
virtualenv # Virtualenv
pip # The pip installer
]))
];
packages = [ pythonEnv ];
};
};
});
....
For the package builder, we can use the `buildPythonApplication`
function.
....
packages = rec {
hello = python.pkgs.buildPythonApplication {
packages = forAllSupportedSystems (system:
let pkgs = nixpkgsFor.${system}; in rec {
hello-flake-python = pkgs.python3Packages.buildPythonApplication {
name = "hello-flake-python";
buildInputs = with python.pkgs; [ pip ];
pyproject = true;
src = ./.;
build-system = with pkgs.python3Packages; [ setuptools ];
};
default = hello;
};
});
....
If you put all the pieces together, your `flake.nix` should look