Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Add new build infrastructure #154

Merged
merged 22 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .envrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ __pycache__
__pypackages__
.direnv
.pdm-python
.envrc
62 changes: 62 additions & 0 deletions build/bootstrap.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
stdenv,
python,
pyprojectInstallHook,
pyprojectBytecodeHook,
pyprojectOutputSetupHook,
python3Packages,
}:

let

buildBootstrapPackage =
base: attrs:
stdenv.mkDerivation (
{
inherit (base)
pname
src
patches
version
meta
;
dontConfigure = true;
nativeBuildInputs = [
python
pyprojectInstallHook
pyprojectBytecodeHook
pyprojectOutputSetupHook
];
buildPhase = ''
runHook preBuild

PYTHONPATH="${bootstrap.flit-core}/${python.sitePackages}" \
${python.interpreter} -m flit_core.wheel

runHook postBuild
'';
}
// attrs
);

bootstrap = {
flit-core = buildBootstrapPackage python3Packages.flit-core {
sourceRoot = "${python3Packages.flit-core.src.name}/flit_core";
buildPhase = ''
runHook preBuild
${python.interpreter} -m flit_core.wheel
runHook postBuild
'';
};
pyproject-hooks = buildBootstrapPackage python3Packages.pyproject-hooks { };
packaging = buildBootstrapPackage python3Packages.packaging { };
build = buildBootstrapPackage python3Packages.build {
passthru.dependencies = {
packaging = [ ];
pyproject-hooks = [ ];
};
};
};

in
bootstrap
220 changes: 220 additions & 0 deletions build/checks/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
{
pyproject-nix,
lib,
pkgs,
}:

let
inherit (pyproject-nix.build.lib) isBootstrapPackage renderers;
inherit (lib) filter attrValues isDerivation;

python = pkgs.python312;

# Inject your own packages on top with overrideScope
pythonSet = pkgs.callPackage pyproject-nix.build.packages {
inherit python;
};

testVenv = pythonSet.pythonPkgsHostHost.mkVirtualEnv "test-venv" {
build = [ ];
};

# Test fixture
myapp = pyproject-nix.lib.project.loadPyproject {
projectRoot = ./fixtures/myapp;
};

testEnviron = pyproject-nix.lib.pep508.mkEnviron python;

in

{
make-venv =
pkgs.runCommand "venv-run-build-test"
{
nativeBuildInputs = [ testVenv ];
}
''
pyproject-build --help > /dev/null
touch $out
'';

make-venv-cross =
let
pkgs' = pkgs.pkgsCross.aarch64-multiplatform;
python = pkgs'.python312;
crossSet = pkgs'.callPackage pyproject-nix.build.packages {
inherit python;
};
in
crossSet.pythonPkgsHostHost.mkVirtualEnv "cross-venv" {
build = [ ];
};

prebuilt-wheel = pythonSet.pythonPkgsHostHost.callPackage (
{
stdenv,
fetchurl,
pyprojectWheelHook,
}:
stdenv.mkDerivation {
pname = "arpeggio";
version = "2.0.2";

src = fetchurl {
url = "https://files.pythonhosted.org/packages/f7/4f/d28bf30a19d4649b40b501d531b44e73afada99044df100380fd9567e92f/Arpeggio-2.0.2-py2.py3-none-any.whl";
hash = "sha256-98iuT0BWqJ4CDCTHICrI3z4ryE5BZ0byCw2jW7HeAlA=";
};

nativeBuildInputs = [ pyprojectWheelHook ];
}
) { };

# Bootstrap dependencies need to use pyprojectBootstrapHook
overriden-bootstrap-dep =
let
overlay = final: _prev: {
packaging = final.stdenv.mkDerivation {
pname = "packaging";
version = "24.1";

src = pkgs.fetchurl {
url = "https://files.pythonhosted.org/packages/51/65/50db4dda066951078f0a96cf12f4b9ada6e4b811516bf0262c0f4f7064d4/packaging-24.1.tar.gz";
hash = "sha256-Am7XLI7T/M5b+JUFciWGmJJ/0dvaEKXpgc3wrDf08AI=";
};

nativeBuildInputs =
assert isBootstrapPackage "packaging";
[
final.pyprojectBootstrapHook
]
++ final.resolveBuildSystem {
flit-core = [ ];
};
};
};

pythonSet' = pythonSet.overrideScope (
_final: prev: {
pythonPkgsBuildHost = prev.pythonPkgsBuildHost.overrideScope overlay;
}
);

in
pythonSet'.pythonPkgsHostHost.mkVirtualEnv "overriden-bootstrap-venv" {
build = [ ];
};

full-set =
let
pythonSetDrvs = filter isDerivation (attrValues pythonSet.pythonPkgsHostHost);
hooks = attrValues pythonSet.pythonPkgsHostHost.hooks;
pythonDrvs = filter (
drv:
!lib.elem drv hooks
&& !lib.elem (drv.pname or drv.name) [
"pyproject-hook"
"python3"
"stdenv-linux"
]
) pythonSetDrvs;

full-set-venv = pythonSet.pythonPkgsHostHost.mkVirtualEnv "test-venv" (
lib.listToAttrs (map (drv: lib.nameValuePair (drv.pname or drv.name) [ ]) pythonDrvs)
);
in
full-set-venv;

mkderivation =
let
testSet = pythonSet.pythonPkgsHostHost.overrideScope (
final: _prev: {
myapp = final.callPackage (
{
stdenv,
pyprojectHook,
resolveBuildSystem,
}:
stdenv.mkDerivation (
renderers.mkDerivation
{
project = myapp;
environ = testEnviron;
}
{
inherit pyprojectHook resolveBuildSystem;
}
)
) { };
}
);

venv = testSet.mkVirtualEnv "render-mkderivation-env" {
myapp = [ "toml" ];
};
in
pkgs.runCommand "render-mkderivation-test" { nativeBuildInputs = [ venv ]; } ''
# Assert that extra was enabled
python -c "import tomli_w"

# Script from myapp
hello

touch $out
'';

mkderivation-editable =
let
testSet = pythonSet.pythonPkgsHostHost.overrideScope (
final: _prev: {
myapp = final.callPackage (
{
python,
stdenv,
pyprojectHook,
resolveBuildSystem,
pythonPkgsBuildHost,
}:
stdenv.mkDerivation (
renderers.mkDerivationEditable
{
project = myapp;
environ = testEnviron;
root = "$NIX_BUILD_TOP/src";
}
{
inherit
python
pyprojectHook
resolveBuildSystem
pythonPkgsBuildHost
;
}
)
) { };
}
);

venv = testSet.mkVirtualEnv "render-mkderivation-editable-env" {
myapp = [ ];
};

in
pkgs.runCommand "render-mkeditable" { nativeBuildInputs = [ venv ]; } ''
# Unpack sources into build
cp -r ${./fixtures/myapp}/* .
chmod +w -R src

hello | grep "Hello from myapp"

cat > src/myapp/__init__.py <<EOF
def hello() -> None:
print("Hello from editable!")
EOF

hello | grep "Hello from editable"

touch $out
'';

}
1 change: 1 addition & 0 deletions build/checks/fixtures/myapp/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
3 changes: 3 additions & 0 deletions build/checks/fixtures/myapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# myapp

A simple application used to integration test build renderers
19 changes: 19 additions & 0 deletions build/checks/fixtures/myapp/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[project]
name = "myapp"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"zipp"
]

[project.optional-dependencies]
toml = ["tomli-w"]

[project.scripts]
hello = "myapp:hello"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
2 changes: 2 additions & 0 deletions build/checks/fixtures/myapp/src/myapp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello() -> None:
print("Hello from myapp!")
9 changes: 9 additions & 0 deletions build/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ lib, pyproject-nix }:

lib.fix (self: {
packages = import ./packages.nix {
inherit (self.lib) resolvers;
inherit pyproject-nix lib;
};
lib = import ./lib { inherit lib pyproject-nix; };
})
Loading