• Malgas@beehaw.org
    link
    fedilink
    English
    arrow-up
    1
    ·
    22 hours ago

    Do you have any tips on how to set up a python environment using nix?

    I also use NixOS (btw) but have so far sidestepped this by the expedient of giving up on any project not in nixpkgs whose install process is a pypi script.

    • ultimate_worrier@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      1
      ·
      15 hours ago

      Here’s an example dev environment for a web scraper provisioned in a flake:

      
      {
        description = "Generic Web Scraper";
      
        inputs = {
          nixpkgs.url = "github:NixOS/nixpkgs?ref=nixpkgs-unstable";
          utils.url = "github:numtide/flake-utils";
        };
      
        outputs = { self, nixpkgs, utils }: utils.lib.eachSystem ["x86_64-linux"] (system: let
          pkgs = import nixpkgs { system = system; };
        in rec {
          packages = {
            pythonEnv =
              pkgs.python3.withPackages (ps: with ps; [ webdriver-manager openpyxl pandas requests beautifulsoup4 websocket-client selenium keyboard ]);
          };
      
          devShell = pkgs.mkShell {
            buildInputs = [
              pkgs.chromium
              pkgs.undetected-chromedriver
              packages.pythonEnv
            ];
      
            shellHook = ''
              export PATH=${pkgs.chromium}/bin:${pkgs.undetected-chromedriver}/bin:$PATH
            '';
          };
        });
      }
      
      

      Not sure if it’s helpful but each Pypi package needs to be declared in that array. There are other ways to do it but this is an example I had laying around.