Custom Python package 1

Let's package the cowsay server from before, so that others can use it in their environments. This is done using the stdenv.mkDerivation, a package is a function defined in a .nix file taking all the build inputs and outputting its results in a special $out location.

First try:

{ stdenv
, python3
}:

stdenv.mkDerivation rec {
  pname = "cowsayer";
  version = "0.0.1";

  src = [
    ./myapp.py
  ];

  dontUnpack = true;

  # can be used at build time only
  nativeBuildInputs = [ ];

  # can be used at build time and run time
  buildInputs = [
    (python3.withPackages (ps: [ ps.flask ]))
  ];

  buildPhase = ''
    sleep 1
  '';

  installPhase = ''
    install -m755 -D ${./myapp.py} $out/bin/cowsayer-server
  '';
}

Let's try building it:

nix-build myapp_v1.nix

Better:

nix-build -E 'with import <nixpkgs> {}; callPackage ./myapp_v1.nix {}'

Test:

./result/bin/cowsayer-server
nix-shell -p curl jq
curl 127.0.0.1:5000 | jq -r '.message'