···1+From c4f452ef6ae083ed21095313582f6d1bd775cbf3 Mon Sep 17 00:00:00 2001
2+From: Andreas Rammhold <andreas@rammhold.de>
3+Date: Thu, 2 Nov 2023 17:32:07 +0100
4+Subject: [PATCH] NIXOS: don't ignore PYTHONPATH
5+6+On NixOS or rather within nixpkgs we provide the runtime Python
7+packages via the PYTHONPATH environment variable. FreeCAD tries its
8+best to ignore Python environment variables that are being inherited
9+from the environment. For Python versions >=3.11 it also tries to
10+initialize the interpreter config without any environmental data. We
11+have to initialize the configuration *with* the information from the
12+environment for our packaging to work.
13+14+Upstream has purposely isolated the environments AFAIK and thus
15+shouldn't accept this patch (as is). What they might accept (once
16+support for older Python versions has been dropped) is removing the
17+PYTHONPATH specific putenv calls.
18+---
19+ src/Base/Interpreter.cpp | 2 +-
20+ src/Main/MainGui.cpp | 3 ---
21+ 2 files changed, 1 insertion(+), 4 deletions(-)
22+23+diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp
24+index 52c47168af..9966bd0013 100644
25+--- a/src/Base/Interpreter.cpp
26++++ b/src/Base/Interpreter.cpp
27+@@ -554,7 +554,7 @@ void initInterpreter(int argc,char *argv[])
28+ {
29+ PyStatus status;
30+ PyConfig config;
31+- PyConfig_InitIsolatedConfig(&config);
32++ PyConfig_InitPythonConfig(&config);
33+34+ status = PyConfig_SetBytesArgv(&config, argc, argv);
35+ if (PyStatus_Exception(status)) {
36+diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp
37+index 48ae847ef4..28813df383 100644
38+--- a/src/Main/MainGui.cpp
39++++ b/src/Main/MainGui.cpp
40+@@ -112,17 +112,14 @@ int main( int argc, char ** argv )
41+ // See https://forum.freecad.org/viewtopic.php?f=18&t=20600
42+ // See Gui::Application::runApplication()
43+ putenv("LC_NUMERIC=C");
44+- putenv("PYTHONPATH=");
45+ #elif defined(FC_OS_MACOSX)
46+ (void)QLocale::system();
47+- putenv("PYTHONPATH=");
48+ #elif defined(__MINGW32__)
49+ const char* mingw_prefix = getenv("MINGW_PREFIX");
50+ const char* py_home = getenv("PYTHONHOME");
51+ if (!py_home && mingw_prefix)
52+ _putenv_s("PYTHONHOME", mingw_prefix);
53+ #else
54+- _putenv("PYTHONPATH=");
55+ // https://forum.freecad.org/viewtopic.php?f=4&t=18288
56+ // https://forum.freecad.org/viewtopic.php?f=3&t=20515
57+ const char* fc_py_home = getenv("FC_PYTHONHOME");
58+--
59+2.42.0
60+
+4-3
pkgs/applications/graphics/freecad/default.nix
···108 qtx11extras
109 ];
1100000111 cmakeFlags = [
112 "-Wno-dev" # turns off warnings which otherwise makes it hard to see what is going on
113 "-DBUILD_FLAT_MESH:BOOL=ON"
···127 export NIX_LDFLAGS="-L${gfortran.cc}/lib64 -L${gfortran.cc}/lib $NIX_LDFLAGS";
128 '';
129130- # Their main() removes PYTHONPATH=, and we rely on it.
131 preConfigure = ''
132- sed '/putenv("PYTHONPATH/d' -i src/Main/MainGui.cpp
133-134 qtWrapperArgs+=(--prefix PYTHONPATH : "$PYTHONPATH")
135 '';
136
···108 qtx11extras
109 ];
110111+ patches = [
112+ ./0001-NIXOS-don-t-ignore-PYTHONPATH.patch
113+ ];
114+115 cmakeFlags = [
116 "-Wno-dev" # turns off warnings which otherwise makes it hard to see what is going on
117 "-DBUILD_FLAT_MESH:BOOL=ON"
···131 export NIX_LDFLAGS="-L${gfortran.cc}/lib64 -L${gfortran.cc}/lib $NIX_LDFLAGS";
132 '';
1330134 preConfigure = ''
00135 qtWrapperArgs+=(--prefix PYTHONPATH : "$PYTHONPATH")
136 '';
137