From de148e41b50e776dae242c9a00a96abd01af7a7c Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 17 Feb 2023 19:33:45 +0100 Subject: [PATCH] python: Fix for IFD --- python/src/python-module.cc | 5 +++++ python/tests.py | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/python/src/python-module.cc b/python/src/python-module.cc index 32940dff9af..25806929bdb 100644 --- a/python/src/python-module.cc +++ b/python/src/python-module.cc @@ -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(); diff --git a/python/tests.py b/python/tests.py index 9d909e98a73..d2c4b1301ab 100644 --- a/python/tests.py +++ b/python/tests.py @@ -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" "printf \\"%s\\" test > $out" ]; + builder = "/bin/sh"; + system = builtins.currentSystem; + }) + """ + self.assertEqual(nix.eval(expression, vars=dict()), "test") + if __name__ == '__main__': unittest.main()