How to install a Python environnement with nix flakes?

I would like to install a Python environnement with a few packages (numpy, scipy, etc.) into my profile. How can this be done with flakes?

2 Likes

Here is an example defining an environment.

{
  description = "My flake";

  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: rec {

    packages = {
      pythonEnv = nixpkgs.legacyPackages.${system}.python3.withPackages(ps: with ps; [ numpy pandas ]);
    };

    defaultPackage = packages.pythonEnv; # If you want to juist build the environment
    devShell = packages.pythonEnv.env; # We need .env in order to use `nix develop`
  }));
}

You can now install into the default package into your profile using nix profile install or the specific package under packages using nix profile install .#pythonEnv. Note you can see what is installed with nix profile info.

5 Likes