Jump to content

Python: Difference between revisions

Poetry fix was not working... it should work now :)
(Added a sample python / nix-ld patch to allow compiled libraries to run with poetry or venv.)
(Poetry fix was not working... it should work now :))
Line 202: Line 202:
The following configuration automatically fix the dependencies :<syntaxhighlight lang="nixos" line="1">
The following configuration automatically fix the dependencies :<syntaxhighlight lang="nixos" line="1">
let
let
   python = pkgs.python311; # or another version
   python = pkgs.python311;
  # https://github.com/NixOS/nixpkgs/blob/c339c066b893e5683830ba870b1ccd3bbea88ece/nixos/modules/programs/nix-ld.nix#L44
  # > We currently take all libraries from systemd and nix as the default.
  pythonldlibpath = lib.makeLibraryPath (with pkgs; [
    zlib
    zstd
    stdenv.cc.cc
    curl
    openssl
    attr
    libssh
    bzip2
    libxml2
    acl
    libsodium
    util-linux
    xz
    systemd
  ]);
   patchedpython = (python.overrideAttrs (
   patchedpython = (python.overrideAttrs (
     previousAttrs: {
     previousAttrs: {
       # Add the nix-ld libraries to the LD_LIBRARY_PATH.
       # Add the nix-ld libraries to the LD_LIBRARY_PATH.
       # creating a new library path from all desired libraries
       # creating a new library path from all desired libraries
      # https://github.com/NixOS/nixpkgs/blob/c339c066b893e5683830ba870b1ccd3bbea88ece/nixos/modules/programs/nix-ld.nix#L44
      # > We currently take all libraries from systemd and nix as the default.
       postInstall = previousAttrs.postInstall + ''
       postInstall = previousAttrs.postInstall + ''
         mv  "$out/bin/python3.11" "$out/bin/unpatched_python3.11"
         mv  "$out/bin/python3.11" "$out/bin/unpatched_python3.11"
         cat << EOF >> "$out/bin/python3.11"
         cat << EOF >> "$out/bin/python3.11"
         #!/run/current-system/sw/bin/bash
         #!/run/current-system/sw/bin/bash
         export LD_LIBRARY_PATH="${lib.makeLibraryPath (with pkgs; [
         export LD_LIBRARY_PATH="${pythonldlibpath}"
          zlib
          zstd
          stdenv.cc.cc
          curl
          openssl
          attr
          libssh
          bzip2
          libxml2
          acl
          libsodium
          util-linux
          xz
          systemd
        ])}"
         exec "$out/bin/unpatched_python3.11" "\$@"
         exec "$out/bin/unpatched_python3.11" "\$@"
         EOF
         EOF
         chmod +x "$out/bin/python3.11"
         chmod +x "$out/bin/python3.11"
      '';
    }
  ));
  # if you want poetry
  patchedpoetry =  ((pkgs.poetry.override { python3 = patchedpython; }).overrideAttrs (
    previousAttrs: {
      # same as above, but for poetry
      # not that if you dont keep the blank line bellow, it crashes :(
      postInstall = previousAttrs.postInstall + ''
        mv "$out/bin/poetry" "$out/bin/unpatched_poetry"
        cat << EOF >> "$out/bin/poetry"
        #!/run/current-system/sw/bin/bash
        export LD_LIBRARY_PATH="${pythonldlibpath}"
        exec "$out/bin/unpatched_poetry" "\$@"
        EOF
        chmod +x "$out/bin/poetry"
       '';
       '';
     }
     }
Line 242: Line 260:
     patchedpython
     patchedpython


     # or maybe if you want poetry
     # if you want poetry
     (poetry.override { python3 = patchedpython; })
     patchedpoetry
   ];
   ];
}
}
3

edits