Skip to content

Commit

Permalink
python-bindings: Incremental building
Browse files Browse the repository at this point in the history
Can now do this:

$ nix develop .#python
$ cd python
$ make

Also reuses the common.sh/init.sh testing scripts
  • Loading branch information
infinisil committed Feb 7, 2023
1 parent f079036 commit 098a31b
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 52 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ makefiles = \
misc/upstart/local.mk \
doc/manual/local.mk \
tests/local.mk \
tests/plugins/local.mk
tests/plugins/local.mk \
python/local.mk

-include Makefile.config

Expand Down
57 changes: 13 additions & 44 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -388,49 +388,10 @@
postUnpack = "sourceRoot=$sourceRoot/perl";
});

passthru.python-bindings =
let
python = final.python3;
in python.pkgs.buildPythonPackage {
name = "nix";
format = "other";

src = self;
postUnpack = "sourceRoot=$sourceRoot/python";

nativeBuildInputs = [
final.buildPackages.ninja
(final.buildPackages.meson.override { python3 = python; })
final.buildPackages.pkgconfig
final.buildPackages.nix
];

strictDeps = true;

buildInputs = [
final.nix
final.boost
];

installCheckPhase = ''
export TEST_ROOT=$(mktemp -d)
export NIX_STATE_DIR=$TEST_ROOT/var/nix
export NIX_STORE_DIR=$TEST_ROOT/store
export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
export NIX_CONF_DIR=$TEST_ROOT/etc
mkdir "$NIX_CONF_DIR"
cat > "$NIX_CONF_DIR"/nix.conf <<EOF
sandbox = false
substituters =
EOF
export PYTHONPATH=$out/${python.sitePackages}
ninja test
'';
};
passthru.python-bindings = final.callPackage ./python {
inherit self;
python = final.python3;
};

meta.platforms = systems;
};
Expand Down Expand Up @@ -768,7 +729,15 @@
'';
}
)
// { default = self.devShells.${system}.stdenv; }
// {
default = self.devShells.${system}.stdenv;
python = nixpkgsFor.${system}.mkShell {
inputsFrom = [
self.devShells.${system}.stdenv
(self.packages.${system}.nix.python-bindings.override { nix = null; })
];
};
}
);

};
Expand Down
1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.cache
build
9 changes: 9 additions & 0 deletions python/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test:
$(MAKE) -C .. python/build tests/common.sh
meson test -C python/build -v

build:
$(MAKE) -C .. python/build
meson compile -C python/build

.PHONY: build test
24 changes: 24 additions & 0 deletions python/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ self, python, ninja, meson, pkg-config, boost, nix }:
python.pkgs.buildPythonPackage {
name = "nix";
format = "other";

src = self;
postUnpack = "sourceRoot=$sourceRoot/python";

nativeBuildInputs = [
ninja
(meson.override { python3 = python; })
pkg-config
nix
];

strictDeps = true;

buildInputs = [
nix
boost
];

installCheckPhase = "meson test -v";
}
8 changes: 8 additions & 0 deletions python/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pythonbuild=$(d)/python/build

clean-files += $(pythonbuild)

python/build: src/libexpr/nix-expr.pc $(libdir)/libnixexpr.so src/libmain/nix-main.pc $(libdir)/libnixmain.so
rm -rf $(pythonbuild)
PKG_CONFIG_PATH=$(libdir)/pkgconfig:$$PKG_CONFIG_PATH meson setup $(pythonbuild) python
touch python/build
11 changes: 8 additions & 3 deletions python/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ python_dep = dependency('python3', required : true)
nix_expr_dep = dependency('nix-expr', required: true)
nix_main_dep = dependency('nix-main', required: true)

python = python_mod.find_python()
test('python test', python, args : files('tests.py'))

subdir('src')

fs = import('fs')

env = environment()
env.prepend('PYTHONPATH', fs.parent(pythonix.full_path()))
bash = find_program('bash')
test('python test', bash, args : files('test.sh'), env : env)

2 changes: 1 addition & 1 deletion python/src/internal/nix-to-python.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Python.h>

#include <nix/config.h>
#include <config.h>

#include <eval.hh>

Expand Down
2 changes: 1 addition & 1 deletion python/src/internal/python-to-nix.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Python.h>
#include <nix/config.h>
#include <config.h>

#include <eval.hh>
#include <optional>
Expand Down
2 changes: 1 addition & 1 deletion python/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ src = [
'python-module.cc',
]

python_mod.extension_module('nix', src,
pythonix = python_mod.extension_module('nix', src,
dependencies : [python_dep, nix_expr_dep, nix_main_dep],
install: true,
install_dir: python_mod.sysconfig_path('platlib'),
Expand Down
2 changes: 1 addition & 1 deletion python/src/python-module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "internal/eval.hh"
#include "internal/ptr.hh"

#include <nix/config.h>
#include <config.h>

#include <eval.hh>
#include <globals.hh>
Expand Down
7 changes: 7 additions & 0 deletions python/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

cd "$SCRIPT_DIR"/../tests

source init.sh

python "$SCRIPT_DIR"/tests.py

0 comments on commit 098a31b

Please sign in to comment.