Skip to content

Commit

Permalink
python bindings: Make IFD work by default
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Feb 7, 2023
1 parent 27993f4 commit f079036
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@
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}
Expand Down
5 changes: 5 additions & 0 deletions python/src/python-module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ static struct PyModuleDef nixmodule = {
NixMethods};

extern "C" _public_ PyObject *PyInit_nix(void) {
// By default, Nix sets the build-hook to be "$(readlink /proc/self/exe) __build-remote", expecting the current binary to be Nix itself.
// But when we call the Nix library from Python this isn't the case, the current binary is Python then
// So we need to change this default, pointing it to the Nix binary instead
nix::settings.buildHook = nix::settings.nixBinDir + "/nix __build-remote";
// And by setting buildHook before calling initNix, we can override the defaults without overriding the user-provided options from the config files
nix::initNix();
nix::initGC();

Expand Down
11 changes: 11 additions & 0 deletions python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,16 @@ def test_bool(self):
def test_none(self):
self.assertEqual(nix.eval("a", vars=dict(a=None)), None)

def test_ifd(self):
expression = """
builtins.readFile (derivation {
name = "test";
args = [ "-c" "echo -n test > $out" ];
builder = "/bin/sh";
system = builtins.currentSystem;
})
"""
self.assertEqual(nix.eval(expression, vars=dict()), "test")

if __name__ == '__main__':
unittest.main()

0 comments on commit f079036

Please sign in to comment.