pythonstreamlitnixnix-flake

How to create a nix project with the python package streamlit


{
  description = "virtual environment with python and streamlit";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        python=pkgs.python311;
        python_packages= python.withPackages(ps: with ps;[
          ipython
          matplotlib
          pandas
          streamlit
        ]);


        myDevTools = [
          python_packages
        ];
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = myDevTools;
        };
      });
}

the command nix develop works fine until I add the package streamlit

error: … while calling the 'derivationStrict' builtin

     at /builtin/derivation.nix:9:12: (source not available)

   … while evaluating derivation 'nix-shell'
     whose name attribute is located at /nix/store/s1z7nb9n6r5n0r34fabp6yybwkbr8mjk-source/pkgs/stdenv/generic/make-derivation.nix:303:7

   … while evaluating attribute 'buildInputs' of derivation 'nix-shell'

     at /nix/store/s1z7nb9n6r5n0r34fabp6yybwkbr8mjk-source/pkgs/stdenv/generic/make-derivation.nix:350:7:

      349|       depsHostHost                = lib.elemAt (lib.elemAt dependencies 1) 0;
      350|       buildInputs                 = lib.elemAt (lib.elemAt dependencies 1) 1;
         |       ^
      351|       depsTargetTarget            = lib.elemAt (lib.elemAt dependencies 2) 0;

   (stack trace truncated; use '--show-trace' to show the full trace)

   error: undefined variable 'streamlit'

   at /nix/store/zxv0741pn2r7h0vk1f1i9knvybfh3yff-source/flake.nix:15:11:

       14|           pandas
       15|           streamlit

Solution

  • Streamlit isn't a python package if you search through Nix packages: https://search.nixos.org/packages?channel=23.05&show=streamlit&from=0&size=50&sort=relevance&type=packages&query=streamlit

    So you have to add it here:

    myDevTools = [
        python_packages pkgs.streamlit
    ];
    

    The streamlit pip packages don't seem to be in Nix, but this page tells you how to add anything from the cheeseshop.

    my-python-packages = ps: with ps; [
      # ...
      (
        buildPythonPackage rec {
          pname = "deserialize";
          version = "1.8.3";
          src = fetchPypi {
            inherit pname version;
            sha256 = "sha256-0aozmQ4Eb5zL4rtNHSFjEynfObUkYlid1PgMDVmRkwY=";
          };
          doCheck = false;
          propagatedBuildInputs = [
            # Specify dependencies
            pkgs.python3Packages.numpy
          ];
        }
      )
    ];