Skip to content

Commit

Permalink
python: Fix for IFD
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil authored and yorickvP committed Apr 19, 2023
1 parent 5dc8bcb commit de148e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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" "printf \\"%s\\" 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 de148e4

Please sign in to comment.