1
{
  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;
        f = ps: with ps;[
          ipython
          matplotlib
          pandas
        ];        
        pip_python_packages= python.withPackages(f);

        myDevTools = [
          pip_python_packages
          pkgs.streamlit
        ];
        outputName = builtins.attrNames self.outputs self.outputs;
      in rec {
        devShells.default = pkgs.mkShell {
          buildInputs = myDevTools;
        };

        packages.default = with pkgs; stdenv.mkDerivation {
          pname = "zoBEremaned";
          name = "my-package";  # Add the 'name' attribute

          #version = "3.3.1";
          src =self;
          nativeBuildInputs = myDevTools;
          buildPhase = " mkdir -p '$out/bin' ";
          installPhase = "echo '#!/usr/bin/bash' > $out/bin/a.out ; echo 'python main.py' >> $out/bin/a.out "; # "python main.py";
        };
        apps.default = flake-utils.lib.mkApp {drv =packages.default;};

      });
}

It still doesn't work

error: builder for '/nix/store/zd7bscfnp7bqnb70p3pwkz8ib7acpaly-my-package.drv' failed with exit code 1; last 10 log lines: > unpacking sources > unpacking source archive /nix/store/288rgfvjxpgpcypmr3lh6qr2ljy0d9bl-source > source root is source > patching sources > configuring > no configure script, doing nothing > building > It failed > installing > /nix/store/j0xzghjmynydvw37zihdbw598j0iwly9-stdenv-linux/setup: line 1596: /nix/store/ikp9pqfzgb1rxbz78ajvgj2h8ilhlfn2-my-package/bin/a.out: No such file or directory For full logs, run 'nix log /nix/store/zd7bscfnp7bqnb70p3pwkz8ib7acpaly-my-package.drv'.

Even after modifying it so that it work, it will work only on a computer with a shell located there "usr/bin/bash" if it has a shell.

How could I for example launch the executable in the default devshell

updated

I tried something similar as in this flake.

I gave the the package definition ito buildInputs of devshell instead of the list of package.

Now I have one more problem. nix develop doesn't work either

{
  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;
        f = ps: with ps;[
          ipython
          matplotlib
          pandas
        ];        
        pip_python_packages= python.withPackages(f);

        myDevTools = [
          pip_python_packages
          pkgs.streamlit
        ];
        outputName = builtins.attrNames self.outputs self.outputs;

        derivationToBeRenamed=with pkgs; stdenv.mkDerivation {
          pname = "zoBEremaned";
          name = "my-package";  # Add the 'name' attribute

          #version = "3.3.1";
          src =self;
          nativeBuildInputs = myDevTools;
          buildPhase = "echo '#!/usr/bin/bash' > a.out ; echo 'python main.py' >> a.out ;  ";
          installPhase = ''
            mkdir -p '$out/bin'
            mv a.out $out/bin 
            '';
        };
      in rec {
        devShells.default = pkgs.mkShell {
          buildInputs = derivationToBeRenamed;
        };

        packages.default = derivationToBeRenamed;
        apps.default = flake-utils.lib.mkApp {drv =derivationToBeRenamed;};

      });
}

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;

   error: value is a set while a list was expected

1 Answers1

0

For more information : How to package a single Python script with nix?

main.py

import pandas as pd

# Create a dictionary with dummy data
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'London', 'Paris']
}

# Create a DataFrame from the dictionary
df = pd.DataFrame(data)

# Print the DataFrame
print(df)

flake.nix

{
  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;
        f = ps: with ps;[
          ipython
          matplotlib
          pandas
        ];        
        pip_python_packages= python.withPackages(f);

        myDevTools = [
          pip_python_packages
          pkgs.streamlit
        ];
        outputName = builtins.attrNames self.outputs self.outputs;

        derivationToBeRenamed=with pkgs; stdenv.mkDerivation {
          #pname = "zoBEremaned";
          name = "my-package";  # Add the 'name' attribute

          #version = "3.3.1";
          #src =self;
          propagatedBuildInputs  = myDevTools;
          dontUnpack = true;
          buildPhase = "
            echo '#!/usr/bin/env python' > ./temp.txt && cat ${./main.py} >> ./temp.txt && mv ./temp.txt ${./main.py}
          ";
          installPhase = "install -Dm755 ${./main.py} $out/bin/my-package";
        };
      in rec {
        devShells.default = pkgs.mkShell {
          buildInputs = myDevTools;
        };

        packages.default = derivationToBeRenamed;
        apps.default = flake-utils.lib.mkApp {drv =derivationToBeRenamed;};

      });
}

it works:

  Name  Age      City 

0 Alice 25 New York 1 Bob 30 London 2 Charlie 35 Paris