mkdir sample_python_nix_docker; cd sample_python_nix_docker
echo "flask" > requirements.txt
cat <<EOF > hello.py
#!/usr/bin/env python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run("0.0.0.0") EOF
<<EOF > setup.py
cat from pip.req import parse_requirements
from setuptools import setup
= parse_requirements('requirements.txt', session='dev')
install_reqs = [str(ir.req) for ir in install_reqs]
reqs
setup(='hello',
name=['hello.py'],
scripts=reqs,
install_requires
) EOF
nix-shell -p pypi2nix --run "pypi2nix -r ./requirements.txt -V3"
nix-build requirements.nix -A interpreter -o .env
Now, .env can be used with pycharm, emacs …
cat <<EOF > default.nix
{ }:
let
pkgs = import <nixpkgs> {};
python = import ./requirements.nix { inherit pkgs; };
in python.mkDerivation {
name = "hello-1.0.0";
src = ./.;
buildInputs = [
python.packages."Flask"
];
propagatedBuildInputs = [
python.packages."Flask"
];
} EOF
./result/bin/hello.py
cat <<EOF > docker.nix
with import <nixpkgs> {};
let
hello = import ./default.nix {};
in {
helloImage = dockerTools.buildImage {
name = "hello";
contents = [
hello
];
config = {
EntryPoint = ["hello.py"];
ExposedPorts = {
"5000/tcp" = {};
};
};
};
} EOF
nix-build docker.nix -o docker-img
cat docker-img | docker load
docker run -it --rm -p 5000:5000 hello
Sources : https://github.com/apeyroux/sample_python_nix_docker