1
0
Fork 0
mirror of https://github.com/NixOS/nix.dev.git synced 2024-10-18 14:32:43 -04:00
nix.dev/source/tutorials/dev-environment.rst

84 lines
1.9 KiB
ReStructuredText
Raw Normal View History

2020-05-18 04:52:41 -04:00
Setup a development environment
===============================
As an exercise, let us build a Python web application using the Flask
web framework.
Create a new file ``default.nix``. This file is conventionally used for
specifying packages:
2020-05-18 05:41:12 -04:00
.. code:: nix
2020-05-18 04:52:41 -04:00
{ pkgs ? import <nixpkgs> {} }:
pkgs.python3Packages.buildPythonApplication {
pname = "myapp";
src = ./.;
version = "0.1";
propagatedBuildInputs = [ pkgs.python3Packages.flask ];
}
2020-07-01 12:19:00 -04:00
You will also need a simple Flask app as ``myapp.py``:
2020-05-18 04:52:41 -04:00
2020-05-18 05:41:12 -04:00
.. code:: python
2020-05-18 04:52:41 -04:00
#! /usr/bin/env python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, Nix!"
def run():
2020-07-01 12:19:00 -04:00
app.run(host="0.0.0.0")
if __name__ == "__main__":
run()
2020-05-18 04:52:41 -04:00
and a ``setup.py`` script:
2020-05-18 05:41:12 -04:00
.. code:: python
2020-05-18 04:52:41 -04:00
from setuptools import setup
setup(
name='myapp',
version='0.1',
py_modules=['myapp'],
entry_points={
'console_scripts': ['myapp = myapp:run']
},
)
Now build the package with:
2020-05-18 05:41:12 -04:00
.. code:: bash
2020-05-18 04:52:41 -04:00
nix-build
This will create a symbolic link ``result`` to our package's path in the
Nix store, which looks like
``/nix/store/6i4l781jwk5vbia8as32637207kgkllj-myapp-0.1``. Look around
to see what is inside.
You may notice we can run the application from the package like
2020-07-01 12:19:00 -04:00
``./result/bin/myapp.py``. We can still use the ``default.nix`` as a
2020-05-18 04:52:41 -04:00
shell environment to get the same result:
2020-05-18 05:41:12 -04:00
.. code:: bash
2020-05-18 04:52:41 -04:00
nix-shell default.nix
2020-07-01 12:19:00 -04:00
python3 myapp.py
2020-05-18 04:52:41 -04:00
In this context, Nix takes on the role that you would otherwise use pip
or virtualenv for. Nix installs required dependencies and separates the
environment from others on your system.
You can check this Nix configuration into version control and share it
with others to make sure you are all running the same software.
Especially with many dependencies this is a great way to prevent
configuration drift between different team members & contributors.