NixOps - Configure Nginx proxy pass with Python Flask

721 Views Asked by At

I am new to Nix and trying to implement a service which passes Python Flask web services though Nginx proxy_pass. This is what I have tried so far.

with import <nixpkgs> {};


    let
        buildInputs = [
            nginx
            python35Packages.python
            python35Packages.flask
            python35Packages.pyyaml
        ];

        installPhase = ''
            mkdir -p $out/pynix
            cp -rv src config.yml $out/pynix
            cd $out/pynix && nohup python src/main.py &> log.txt
        '';


    in {
        network.description = "Local machine";

        webserver = {
            deployment = {
                targetEnv = "virtualbox";
                virtualbox.memorySize = 1024;
            };

            services = {
                nginx = {
                    enable = true;
                    config = '';
                        http {
                            include         ${nginx}/conf/mime.types;
                            server_name     localhost;

                            location / {
                                proxy_pass http://localhost:5000;
                            }
                        }
                    '';
                };
            };
        };
    }

src/main.py is a Python Flask service running on port 5000. How can I achieve this web service up and running when I do nixops deploy -d DEPLOYMENT_NAME? Please help.

1

There are 1 best solutions below

0
On BEST ANSWER

I think you've confused a package and a service. The package is the static output of the build while the service manages the run time activation of the package. I think your configuration currently attempts to describe a python app that is run at build time without any service to activate it at run time. This is pretty much the opposite of what you want! Especially as with nixops your are likely running your service in a different environment to where it was built.
You should be able to get an idea of what I mean by looking at the nix expressions for the nginx package and the nginx service - specifically the section services.systemd.nginx. From here you can see that the nginx service manages the running of the nginx package. I think you will want to write similar expressions for your python app. You could also see if there are expressions specifically for python based NixOS services that you could use as a base, but the nginx expressions should be a sufficient guide too.