├── .gitignore ├── Makefile ├── README.md ├── conf.py ├── index.rst ├── make.bat ├── requirements.txt ├── shell.nix └── tutorial.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " applehelp to make an Apple Help Book" 34 | @echo " devhelp to make HTML files and a Devhelp project" 35 | @echo " epub to make an epub" 36 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 37 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 38 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 39 | @echo " text to make text files" 40 | @echo " man to make manual pages" 41 | @echo " texinfo to make Texinfo files" 42 | @echo " info to make Texinfo files and run them through makeinfo" 43 | @echo " gettext to make PO message catalogs" 44 | @echo " changes to make an overview of all changed/added/deprecated items" 45 | @echo " xml to make Docutils-native XML files" 46 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 47 | @echo " linkcheck to check all external links for integrity" 48 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 49 | @echo " coverage to run coverage check of the documentation (if enabled)" 50 | 51 | clean: 52 | rm -rf $(BUILDDIR)/* 53 | 54 | html: 55 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 56 | @echo 57 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 58 | 59 | dirhtml: 60 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 61 | @echo 62 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 63 | 64 | singlehtml: 65 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 66 | @echo 67 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 68 | 69 | pickle: 70 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 71 | @echo 72 | @echo "Build finished; now you can process the pickle files." 73 | 74 | json: 75 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 76 | @echo 77 | @echo "Build finished; now you can process the JSON files." 78 | 79 | htmlhelp: 80 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 81 | @echo 82 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 83 | ".hhp project file in $(BUILDDIR)/htmlhelp." 84 | 85 | qthelp: 86 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 87 | @echo 88 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 89 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 90 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/PythononNixtutorial.qhcp" 91 | @echo "To view the help file:" 92 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/PythononNixtutorial.qhc" 93 | 94 | applehelp: 95 | $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp 96 | @echo 97 | @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." 98 | @echo "N.B. You won't be able to view it unless you put it in" \ 99 | "~/Library/Documentation/Help or install it in your application" \ 100 | "bundle." 101 | 102 | devhelp: 103 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 104 | @echo 105 | @echo "Build finished." 106 | @echo "To view the help file:" 107 | @echo "# mkdir -p $$HOME/.local/share/devhelp/PythononNixtutorial" 108 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/PythononNixtutorial" 109 | @echo "# devhelp" 110 | 111 | epub: 112 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 113 | @echo 114 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 115 | 116 | latex: 117 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 118 | @echo 119 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 120 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 121 | "(use \`make latexpdf' here to do that automatically)." 122 | 123 | latexpdf: 124 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 125 | @echo "Running LaTeX files through pdflatex..." 126 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 127 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 128 | 129 | latexpdfja: 130 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 131 | @echo "Running LaTeX files through platex and dvipdfmx..." 132 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 133 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 134 | 135 | text: 136 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 137 | @echo 138 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 139 | 140 | man: 141 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 142 | @echo 143 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 144 | 145 | texinfo: 146 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 147 | @echo 148 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 149 | @echo "Run \`make' in that directory to run these through makeinfo" \ 150 | "(use \`make info' here to do that automatically)." 151 | 152 | info: 153 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 154 | @echo "Running Texinfo files through makeinfo..." 155 | make -C $(BUILDDIR)/texinfo info 156 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 157 | 158 | gettext: 159 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 160 | @echo 161 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 162 | 163 | changes: 164 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 165 | @echo 166 | @echo "The overview file is in $(BUILDDIR)/changes." 167 | 168 | linkcheck: 169 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 170 | @echo 171 | @echo "Link check complete; look for any errors in the above output " \ 172 | "or in $(BUILDDIR)/linkcheck/output.txt." 173 | 174 | doctest: 175 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 176 | @echo "Testing of doctests in the sources finished, look at the " \ 177 | "results in $(BUILDDIR)/doctest/output.txt." 178 | 179 | coverage: 180 | $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage 181 | @echo "Testing of coverage in the sources finished, look at the " \ 182 | "results in $(BUILDDIR)/coverage/python.txt." 183 | 184 | xml: 185 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 186 | @echo 187 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 188 | 189 | pseudoxml: 190 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 191 | @echo 192 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 193 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-on-nix 2 | 3 | This repository contains a [tutorial](http://python-on-nix.readthedocs.org/en/latest/) to [Python](https://www.python.org/) on [Nix](http://nixos.org/nix/). 4 | 5 | ## Building the docs 6 | 7 | Documentation is build from CommonMark/Markdown and ReStructuredText using [Sphinx](http://sphinx-doc.org/). 8 | When using Nix/NixOS, just run `nix-shell` in this directory to obtain a shell with all dependencies. 9 | To build html docs, just run `make html`. 10 | 11 | ## Merged into Nixpkgs documentation 12 | 13 | These docs are not maintained anymore since they we're merged into the Nixpkgs manual. 14 | See http://hydra.nixos.org/job/nixpkgs/trunk/manual/latest 15 | 16 | -------------------------------------------------------------------------------- /conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Python on Nix tutorial documentation build configuration file, created by 5 | # sphinx-quickstart on Tue Dec 1 10:52:40 2015. 6 | # 7 | # This file is execfile()d with the current directory set to its 8 | # containing dir. 9 | # 10 | # Note that not all possible configuration values are present in this 11 | # autogenerated file. 12 | # 13 | # All configuration values have a default; values that are commented out 14 | # serve to show the default. 15 | 16 | import sys 17 | import os 18 | import shlex 19 | 20 | # If extensions (or modules to document with autodoc) are in another directory, 21 | # add these directories to sys.path here. If the directory is relative to the 22 | # documentation root, use os.path.abspath to make it absolute, like shown here. 23 | #sys.path.insert(0, os.path.abspath('.')) 24 | 25 | # -- General configuration ------------------------------------------------ 26 | 27 | # If your documentation needs a minimal Sphinx version, state it here. 28 | #needs_sphinx = '1.0' 29 | 30 | # Add any Sphinx extension module names here, as strings. They can be 31 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 32 | # ones. 33 | extensions = [] 34 | 35 | # Add any paths that contain templates here, relative to this directory. 36 | templates_path = ['_templates'] 37 | 38 | # The suffix(es) of source filenames. 39 | # You can specify multiple suffix as a list of string: 40 | # source_suffix = ['.rst', '.md'] 41 | source_suffix = ['.rst', '.md' ] 42 | 43 | # The encoding of source files. 44 | #source_encoding = 'utf-8-sig' 45 | 46 | # Parsers. 47 | from recommonmark.parser import CommonMarkParser 48 | 49 | source_parsers = { 50 | '.md' : CommonMarkParser, 51 | } 52 | 53 | # The master toctree document. 54 | master_doc = 'index' 55 | 56 | # General information about the project. 57 | project = 'Python on Nix tutorial' 58 | copyright = '2015, Frederik Rietdijk' 59 | author = 'Frederik Rietdijk' 60 | 61 | # The version info for the project you're documenting, acts as replacement for 62 | # |version| and |release|, also used in various other places throughout the 63 | # built documents. 64 | # 65 | # The short X.Y version. 66 | version = '0.0.1' 67 | # The full version, including alpha/beta/rc tags. 68 | release = '0.0.1' 69 | 70 | # The language for content autogenerated by Sphinx. Refer to documentation 71 | # for a list of supported languages. 72 | # 73 | # This is also used if you do content translation via gettext catalogs. 74 | # Usually you set "language" from the command line for these cases. 75 | language = None 76 | 77 | # There are two options for replacing |today|: either, you set today to some 78 | # non-false value, then it is used: 79 | #today = '' 80 | # Else, today_fmt is used as the format for a strftime call. 81 | #today_fmt = '%B %d, %Y' 82 | 83 | # List of patterns, relative to source directory, that match files and 84 | # directories to ignore when looking for source files. 85 | exclude_patterns = ['_build'] 86 | 87 | # The reST default role (used for this markup: `text`) to use for all 88 | # documents. 89 | #default_role = None 90 | 91 | # If true, '()' will be appended to :func: etc. cross-reference text. 92 | #add_function_parentheses = True 93 | 94 | # If true, the current module name will be prepended to all description 95 | # unit titles (such as .. function::). 96 | #add_module_names = True 97 | 98 | # If true, sectionauthor and moduleauthor directives will be shown in the 99 | # output. They are ignored by default. 100 | #show_authors = False 101 | 102 | # The name of the Pygments (syntax highlighting) style to use. 103 | pygments_style = 'sphinx' 104 | 105 | highlight_language = 'nix' 106 | 107 | # A list of ignored prefixes for module index sorting. 108 | #modindex_common_prefix = [] 109 | 110 | # If true, keep warnings as "system message" paragraphs in the built documents. 111 | #keep_warnings = False 112 | 113 | # If true, `todo` and `todoList` produce output, else they produce nothing. 114 | todo_include_todos = False 115 | 116 | 117 | # -- Options for HTML output ---------------------------------------------- 118 | 119 | # The theme to use for HTML and HTML Help pages. See the documentation for 120 | # a list of builtin themes. 121 | html_theme = 'alabaster' 122 | 123 | # Theme options are theme-specific and customize the look and feel of a theme 124 | # further. For a list of options available for each theme, see the 125 | # documentation. 126 | #html_theme_options = {} 127 | 128 | # Add any paths that contain custom themes here, relative to this directory. 129 | #html_theme_path = [] 130 | 131 | # The name for this set of Sphinx documents. If None, it defaults to 132 | # " v documentation". 133 | #html_title = None 134 | 135 | # A shorter title for the navigation bar. Default is the same as html_title. 136 | #html_short_title = None 137 | 138 | # The name of an image file (relative to this directory) to place at the top 139 | # of the sidebar. 140 | #html_logo = None 141 | 142 | # The name of an image file (within the static path) to use as favicon of the 143 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 144 | # pixels large. 145 | #html_favicon = None 146 | 147 | # Add any paths that contain custom static files (such as style sheets) here, 148 | # relative to this directory. They are copied after the builtin static files, 149 | # so a file named "default.css" will overwrite the builtin "default.css". 150 | html_static_path = ['_static'] 151 | 152 | # Add any extra paths that contain custom files (such as robots.txt or 153 | # .htaccess) here, relative to this directory. These files are copied 154 | # directly to the root of the documentation. 155 | #html_extra_path = [] 156 | 157 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 158 | # using the given strftime format. 159 | #html_last_updated_fmt = '%b %d, %Y' 160 | 161 | # If true, SmartyPants will be used to convert quotes and dashes to 162 | # typographically correct entities. 163 | #html_use_smartypants = True 164 | 165 | # Custom sidebar templates, maps document names to template names. 166 | #html_sidebars = {} 167 | 168 | # Additional templates that should be rendered to pages, maps page names to 169 | # template names. 170 | #html_additional_pages = {} 171 | 172 | # If false, no module index is generated. 173 | #html_domain_indices = True 174 | 175 | # If false, no index is generated. 176 | #html_use_index = True 177 | 178 | # If true, the index is split into individual pages for each letter. 179 | #html_split_index = False 180 | 181 | # If true, links to the reST sources are added to the pages. 182 | #html_show_sourcelink = True 183 | 184 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 185 | #html_show_sphinx = True 186 | 187 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 188 | #html_show_copyright = True 189 | 190 | # If true, an OpenSearch description file will be output, and all pages will 191 | # contain a tag referring to it. The value of this option must be the 192 | # base URL from which the finished HTML is served. 193 | #html_use_opensearch = '' 194 | 195 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 196 | #html_file_suffix = None 197 | 198 | # Language to be used for generating the HTML full-text search index. 199 | # Sphinx supports the following languages: 200 | # 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' 201 | # 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr' 202 | #html_search_language = 'en' 203 | 204 | # A dictionary with options for the search language support, empty by default. 205 | # Now only 'ja' uses this config value 206 | #html_search_options = {'type': 'default'} 207 | 208 | # The name of a javascript file (relative to the configuration directory) that 209 | # implements a search results scorer. If empty, the default will be used. 210 | #html_search_scorer = 'scorer.js' 211 | 212 | # Output file base name for HTML help builder. 213 | htmlhelp_basename = 'PythononNixtutorialdoc' 214 | 215 | # -- Options for LaTeX output --------------------------------------------- 216 | 217 | latex_elements = { 218 | # The paper size ('letterpaper' or 'a4paper'). 219 | #'papersize': 'letterpaper', 220 | 221 | # The font size ('10pt', '11pt' or '12pt'). 222 | #'pointsize': '10pt', 223 | 224 | # Additional stuff for the LaTeX preamble. 225 | #'preamble': '', 226 | 227 | # Latex figure (float) alignment 228 | #'figure_align': 'htbp', 229 | } 230 | 231 | # Grouping the document tree into LaTeX files. List of tuples 232 | # (source start file, target name, title, 233 | # author, documentclass [howto, manual, or own class]). 234 | latex_documents = [ 235 | (master_doc, 'PythononNixtutorial.tex', 'Python on Nix tutorial Documentation', 236 | 'Frederik Rietdijk', 'manual'), 237 | ] 238 | 239 | # The name of an image file (relative to this directory) to place at the top of 240 | # the title page. 241 | #latex_logo = None 242 | 243 | # For "manual" documents, if this is true, then toplevel headings are parts, 244 | # not chapters. 245 | #latex_use_parts = False 246 | 247 | # If true, show page references after internal links. 248 | #latex_show_pagerefs = False 249 | 250 | # If true, show URL addresses after external links. 251 | #latex_show_urls = False 252 | 253 | # Documents to append as an appendix to all manuals. 254 | #latex_appendices = [] 255 | 256 | # If false, no module index is generated. 257 | #latex_domain_indices = True 258 | 259 | 260 | # -- Options for manual page output --------------------------------------- 261 | 262 | # One entry per manual page. List of tuples 263 | # (source start file, name, description, authors, manual section). 264 | man_pages = [ 265 | (master_doc, 'pythononnixtutorial', 'Python on Nix tutorial Documentation', 266 | [author], 1) 267 | ] 268 | 269 | # If true, show URL addresses after external links. 270 | #man_show_urls = False 271 | 272 | 273 | # -- Options for Texinfo output ------------------------------------------- 274 | 275 | # Grouping the document tree into Texinfo files. List of tuples 276 | # (source start file, target name, title, author, 277 | # dir menu entry, description, category) 278 | texinfo_documents = [ 279 | (master_doc, 'PythononNixtutorial', 'Python on Nix tutorial Documentation', 280 | author, 'PythononNixtutorial', 'One line description of project.', 281 | 'Miscellaneous'), 282 | ] 283 | 284 | # Documents to append as an appendix to all manuals. 285 | #texinfo_appendices = [] 286 | 287 | # If false, no module index is generated. 288 | #texinfo_domain_indices = True 289 | 290 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 291 | #texinfo_show_urls = 'footnote' 292 | 293 | # If true, do not generate a @detailmenu in the "Top" node's menu. 294 | #texinfo_no_detailmenu = False 295 | -------------------------------------------------------------------------------- /index.rst: -------------------------------------------------------------------------------- 1 | .. Python on Nix tutorial documentation master file, created by 2 | sphinx-quickstart on Tue Dec 1 10:52:40 2015. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to the Python on Nix tutorial! 7 | ================================================== 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | tutorial 15 | 16 | 17 | Indices and tables 18 | ================== 19 | 20 | * :ref:`genindex` 21 | * :ref:`modindex` 22 | * :ref:`search` 23 | 24 | -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | echo. coverage to run coverage check of the documentation if enabled 41 | goto end 42 | ) 43 | 44 | if "%1" == "clean" ( 45 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 46 | del /q /s %BUILDDIR%\* 47 | goto end 48 | ) 49 | 50 | 51 | REM Check if sphinx-build is available and fallback to Python version if any 52 | %SPHINXBUILD% 2> nul 53 | if errorlevel 9009 goto sphinx_python 54 | goto sphinx_ok 55 | 56 | :sphinx_python 57 | 58 | set SPHINXBUILD=python -m sphinx.__init__ 59 | %SPHINXBUILD% 2> nul 60 | if errorlevel 9009 ( 61 | echo. 62 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 63 | echo.installed, then set the SPHINXBUILD environment variable to point 64 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 65 | echo.may add the Sphinx directory to PATH. 66 | echo. 67 | echo.If you don't have Sphinx installed, grab it from 68 | echo.http://sphinx-doc.org/ 69 | exit /b 1 70 | ) 71 | 72 | :sphinx_ok 73 | 74 | 75 | if "%1" == "html" ( 76 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 77 | if errorlevel 1 exit /b 1 78 | echo. 79 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 80 | goto end 81 | ) 82 | 83 | if "%1" == "dirhtml" ( 84 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 85 | if errorlevel 1 exit /b 1 86 | echo. 87 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 88 | goto end 89 | ) 90 | 91 | if "%1" == "singlehtml" ( 92 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 93 | if errorlevel 1 exit /b 1 94 | echo. 95 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 96 | goto end 97 | ) 98 | 99 | if "%1" == "pickle" ( 100 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 101 | if errorlevel 1 exit /b 1 102 | echo. 103 | echo.Build finished; now you can process the pickle files. 104 | goto end 105 | ) 106 | 107 | if "%1" == "json" ( 108 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 109 | if errorlevel 1 exit /b 1 110 | echo. 111 | echo.Build finished; now you can process the JSON files. 112 | goto end 113 | ) 114 | 115 | if "%1" == "htmlhelp" ( 116 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 117 | if errorlevel 1 exit /b 1 118 | echo. 119 | echo.Build finished; now you can run HTML Help Workshop with the ^ 120 | .hhp project file in %BUILDDIR%/htmlhelp. 121 | goto end 122 | ) 123 | 124 | if "%1" == "qthelp" ( 125 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 129 | .qhcp project file in %BUILDDIR%/qthelp, like this: 130 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\PythononNixtutorial.qhcp 131 | echo.To view the help file: 132 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\PythononNixtutorial.ghc 133 | goto end 134 | ) 135 | 136 | if "%1" == "devhelp" ( 137 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 138 | if errorlevel 1 exit /b 1 139 | echo. 140 | echo.Build finished. 141 | goto end 142 | ) 143 | 144 | if "%1" == "epub" ( 145 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 146 | if errorlevel 1 exit /b 1 147 | echo. 148 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 149 | goto end 150 | ) 151 | 152 | if "%1" == "latex" ( 153 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 154 | if errorlevel 1 exit /b 1 155 | echo. 156 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 157 | goto end 158 | ) 159 | 160 | if "%1" == "latexpdf" ( 161 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 162 | cd %BUILDDIR%/latex 163 | make all-pdf 164 | cd %~dp0 165 | echo. 166 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 167 | goto end 168 | ) 169 | 170 | if "%1" == "latexpdfja" ( 171 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 172 | cd %BUILDDIR%/latex 173 | make all-pdf-ja 174 | cd %~dp0 175 | echo. 176 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 177 | goto end 178 | ) 179 | 180 | if "%1" == "text" ( 181 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 182 | if errorlevel 1 exit /b 1 183 | echo. 184 | echo.Build finished. The text files are in %BUILDDIR%/text. 185 | goto end 186 | ) 187 | 188 | if "%1" == "man" ( 189 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 190 | if errorlevel 1 exit /b 1 191 | echo. 192 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 193 | goto end 194 | ) 195 | 196 | if "%1" == "texinfo" ( 197 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 198 | if errorlevel 1 exit /b 1 199 | echo. 200 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 201 | goto end 202 | ) 203 | 204 | if "%1" == "gettext" ( 205 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 206 | if errorlevel 1 exit /b 1 207 | echo. 208 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 209 | goto end 210 | ) 211 | 212 | if "%1" == "changes" ( 213 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 214 | if errorlevel 1 exit /b 1 215 | echo. 216 | echo.The overview file is in %BUILDDIR%/changes. 217 | goto end 218 | ) 219 | 220 | if "%1" == "linkcheck" ( 221 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 222 | if errorlevel 1 exit /b 1 223 | echo. 224 | echo.Link check complete; look for any errors in the above output ^ 225 | or in %BUILDDIR%/linkcheck/output.txt. 226 | goto end 227 | ) 228 | 229 | if "%1" == "doctest" ( 230 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 231 | if errorlevel 1 exit /b 1 232 | echo. 233 | echo.Testing of doctests in the sources finished, look at the ^ 234 | results in %BUILDDIR%/doctest/output.txt. 235 | goto end 236 | ) 237 | 238 | if "%1" == "coverage" ( 239 | %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage 240 | if errorlevel 1 exit /b 1 241 | echo. 242 | echo.Testing of coverage in the sources finished, look at the ^ 243 | results in %BUILDDIR%/coverage/python.txt. 244 | goto end 245 | ) 246 | 247 | if "%1" == "xml" ( 248 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 249 | if errorlevel 1 exit /b 1 250 | echo. 251 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 252 | goto end 253 | ) 254 | 255 | if "%1" == "pseudoxml" ( 256 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 257 | if errorlevel 1 exit /b 1 258 | echo. 259 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 260 | goto end 261 | ) 262 | 263 | :end 264 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pygments 2 | sphinx 3 | recommonmark 4 | -------------------------------------------------------------------------------- /shell.nix: -------------------------------------------------------------------------------- 1 | with import {}; 2 | 3 | ( pkgs.python35.buildEnv.override { 4 | extraLibs = with pkgs.python35Packages; [ sphinx recommonmark pkgs.gnumake ]; 5 | }).env -------------------------------------------------------------------------------- /tutorial.md: -------------------------------------------------------------------------------- 1 | # Python on Nix 2 | 3 | You're a Python dev, and recently you've heard about [Nix](http://nixos.org/nix/). 4 | Immediately convinced by sane package management you decide you want to start 5 | using Nix. But before you do so, there's still an important question that you 6 | want an answer to. **How does Nix affect my Python development workflow?** 7 | In this tutorial we show you how you use and develop with Python on Nix. 8 | 9 | ## Introduction to Nix 10 | 11 | While we assume you've already read a bit on Nix before going through this 12 | tutorial, we recap a couple of things first. [Nix](http://nixos.org/nix/) 13 | is a purely functional package manager for *Nix systems. Packages are specified 14 | using the [Nix expression language](http://nixos.org/nix/manual/#chap-writing-nix-expressions). 15 | [NixOS](http://nixos.org) is an operating system built around the Nix package 16 | manager. On NixOS not only packages but also services (called 17 | [modules](http://nixos.org/nixos/manual/index.html#sec-writing-modules)) are 18 | described using the Nix expression language. 19 | [Nixpkgs](http://nixos.org/nixpkgs/) is a collection of packages (and NixOS 20 | modules). 21 | 22 | 23 | ## Getting started 24 | 25 | If you don't have Nix yet, and you would like to install it on your *Nix system, 26 | then head on to the [Nix Quick 27 | Start](http://nixos.org/nix/manual/#chap-quick-start). 28 | if instead you want to install NixOS, then please continue now with the [NixOS 29 | installation guide](http://nixos.org/nixos/manual/index.html#ch-installation). 30 | 31 | ## Using Python 32 | 33 | 34 | 35 | 36 | ### Installing software 37 | 38 | Now that you've installed Nix you're ready to start installing software. 39 | With the Nix package manager you can [ad hoc install](http://nixos.org/nixos/manual/index.html#sec-ad-hoc-packages) software in your profile 40 | using `nix-env -iA`. e.g. 41 | 42 | ```sh 43 | $ nix-env -iA nixpkgs.pkgs.pandoc 44 | ``` 45 | 46 | would install the [Pandoc](http://pandoc.org/) tool in your profile. From then on you will have the 47 | possibility to run `pandoc` from within a shell. 48 | 49 | If you are running NixOS and you have `root`/`sudo` access, then you can specify 50 | [declaratively](http://nixos.org/nixos/manual/index.html#sec-declarative-package-mgmt) which packages need to be installed by appending those packages to 51 | `environment.systemPackages` in the `/etc/nixos/configuration.nix` file 52 | 53 | ```nix 54 | ... 55 | 56 | environment.systemPackages = with pkgs; [ 57 | busybox 58 | chromium 59 | pandoc 60 | ]; 61 | 62 | ... 63 | ``` 64 | 65 | A common method on Nix is however not to install all the software you need, but 66 | instead using the [Nix shell](https://nixos.org/nix/manual/#sec-nix-shell) 67 | (`nix-shell`) to open a shell with exactly those packages that you need when you 68 | need them. E.g., say you want to convert some files and so you want to use 69 | Pandoc, then you could run 70 | 71 | ```sh 72 | $ nix-shell -p pandoc 73 | ``` 74 | 75 | which opens a shell from which you can run pandoc 76 | 77 | ```sh 78 | [nix-shell:~] pandoc tutorial.md -o tutorial.pdf 79 | ``` 80 | 81 | ### Installing Python? 82 | 83 | You might be wondering now why, if this tutorial is about Python, we are using 84 | Pandoc as an example and not Python? Well, that's because with Python you're 85 | generally interested in not just the interpreter, but also Python packages. On 86 | Nix most software can be installed in a profile, either ad hoc or declaratively. 87 | However, this is not possibly with Python and packages. Actually, to be precise, 88 | the tools allow you to install to your profile e.g. Python 3.5 using 89 | 90 | ```sh 91 | $ nix-env -iA nixpkgs.pkgs.python35 92 | ``` 93 | 94 | Most likely, now running 95 | 96 | ```sh 97 | $ python35 98 | ``` 99 | 100 | will actually open the interpreter. Nothing wrong, right? Well, not entirely. 101 | There is a problem though with installing Python modules/packages in this way; 102 | generally they cannot be accessed from the interpreter. 103 | Obviously you do not want this. What's the solution to that, you might ask? 104 | 105 | Installing Python declaratively perhaps? How about the following in your 106 | `/etc/nixos/configuration.nix` in case you're running NixOS? 107 | 108 | ```nix 109 | ... 110 | 111 | environment.systemPackages = with pkgs; [ 112 | busybox 113 | chromium 114 | pandoc 115 | python35 116 | python35Packages.numpy 117 | python35Packages.toolz 118 | ]; 119 | 120 | ... 121 | ``` 122 | Nope, it might work, but likely not always. But *why* does it not work then? 123 | 124 | With Nix you install only applications. Because with Nix you can have multiple 125 | versions of a library/application at the same time, an application needs to know 126 | which exact libraries to use, that is, which exact entries in the Nix store 127 | (`/nix/store`). Libraries that are needed by an application are defined as 128 | `buildInputs` in the Nix expression of the application. When building/installing 129 | the application, the libraries are also built/installed. You won't ever manually 130 | install libraries using `nix-env -iA` or in `environment.systemPackages`. 131 | 132 | But now let's consider Python. You might want to install the interpreter 133 | system-wide, along with maybe some Python packages. As user, you realise you 134 | want to have some additional packages so you install them using `nix-env -iA`. 135 | Remember, with Nix, you can have multiple versions of libraries because 136 | different applications might require different versions. How now, would the 137 | interpreter decide which version of say [`numpy`](http://www.numpy.org/) to use, when multiple are 138 | installed? 139 | 140 | The bottomline is that **installing Python and packages is not supported**. 141 | The way to go though is environments... 142 | 143 | 144 | ### Python using nix-shell 145 | 146 | Perhaps the easiest way to get a functional Python environment is by using 147 | [`nix-shell`](http://nixos.org/nix/manual/#sec-nix-shell). 148 | 149 | Executing 150 | 151 | ```sh 152 | $ nix-shell -p python35Packages.numpy python35Packages.toolz 153 | ``` 154 | 155 | opens a Nix shell which has available the requested packages and dependencies. 156 | Now you can launch the Python interpreter (which is itself a dependency) 157 | 158 | ```sh 159 | [nix-shell:~] python3 160 | ``` 161 | 162 | If the packages were not available yet in the Nix store, Nix would download or 163 | compile them automatically. 164 | A convenient option with `nix-shell` is the `--run` option, with which you can 165 | execute a command in the `nix-shell`. Let's say we want the above environment 166 | and directly run the Python interpreter 167 | 168 | ```sh 169 | $ nix-shell -p python35Packages.numpy python35Packages.toolz --run "python3" 170 | ``` 171 | 172 | You can also use the `--run` option to directly execute a script 173 | 174 | ```sh 175 | $ nix-shell -p python35Packages.numpy python35Packages.toolz --run "python3 myscript.py" 176 | ``` 177 | 178 | For this specific case there is another convenient method; you can add a shebang 179 | to your script specifying which dependencies Nix shell needs. With the following 180 | shebang, you can use `nix-shell myscript.py` and it will make available all 181 | dependencies and run the script in the `python3` shell. 182 | 183 | ```sh 184 | #! /usr/bin/env nix-shell 185 | #! nix-shell -i python3 -p python35Packages.numpy python35Packages.toolz 186 | ``` 187 | 188 | The first line here is a standard shebang. We say we want to use `nix-shell`. 189 | With the Nix shell you are however not limited to only a single line, but you 190 | can have multiple. The second line instructs Nix shell to create an environment 191 | with the `-p` packages in it, as we did before, and then run the `-i` 192 | interpreter. Note that the `-i` option can only be used as part of a shebang. In 193 | other cases you will have to use the `--run` option as shown above. 194 | 195 | By default all installed applications are still accessible from the Nix shell. 196 | If you do not want this, you can use the `--pure` option. 197 | 198 | ```sh 199 | $ nix-env -iA nixpkgs.pkgs.pandoc 200 | $ nix-shell -p python35Packages.numpy python35Packages.toolz --pure 201 | [nix-shell:~] pandoc 202 | The program ‘pandoc’ is currently not installed. You can install it by typing: 203 | nix-env -iA nixos.pandoc 204 | ``` 205 | 206 | Likely you do not want to type your dependencies each and every time. What you 207 | can do is write a simple Nix expression which sets up an environment for you, 208 | requiring you only to type `nix-shell`. Say we want to have Python 3.5, `numpy` 209 | and `toolz`, like before, in an environment. With a `shell.nix` file 210 | containing 211 | 212 | ```nix 213 | with import {}; 214 | 215 | ( pkgs.python35.buildEnv.override { 216 | extraLibs = with pkgs.python35Packages; [ numpy toolz ]; 217 | }).env 218 | ``` 219 | 220 | executing `nix-shell` gives you again a Nix shell from which you can run Python. 221 | So what do those lines here mean? Let's consider line by line: 222 | 223 | 1. We begin with importing the Nix Packages collections. `import {}` does the actual import and the `with` statement brings all attributes of `nixpkgs` in the local scope. Therefore we can now use `pkgs`. 224 | 2. Then we say we want a Python 3.5 environment, so we use the derivation [`pkgs.python35.buildEnv`](http://nixos.org/nixpkgs/manual/#ssec-python-build-env). Because we want to use it with a custom set of Python packages, we override it. 225 | 3. The `extraLibs` argument of the original `buildEnv` function can be used to specify which packages you want. We want `numpy` and `toolz`. Again, we use the `with` statement to bring a set of attributes into the local scope. 226 | 4. **EXPLAIN** 227 | 228 | 229 | ### Declarative environment using myEnvFun 230 | 231 | Using Nix shell means you either need to add a bunch of arguments to the 232 | `nix-shell` invocation, or executing a specific file with Nix shell. Another 233 | option is to instead define your environments [declaratively in your user 234 | profile](http://nixos.org/nixpkgs/manual/#chap-packageconfig). As user you have 235 | a `~/.nixpkgs/config.nix` file in which you can include 236 | [overrides](http://nixos.org/nixpkgs/manual/#sec-modify-via-packageOverrides) 237 | specifically for yourself. Here we can add our declarative environments as well. 238 | Let's say we already have the following `config.nix`. 239 | 240 | ```nix 241 | with {}; 242 | { 243 | allowUnfree = true; 244 | } 245 | ``` 246 | 247 | This expression imports the Nix packages collections, and says that we allow 248 | unfree software. Let's extend this now with two environments. We add one 249 | environment that we use for development, and another for blogging with 250 | [Pelican](http://blog.getpelican.com/). 251 | 252 | ```nix 253 | with {}; 254 | { 255 | allowUnfree = true; 256 | allowBroken = true; 257 | 258 | packageOverrides = pkgs: with pkgs; { 259 | devEnv = pkgs.myEnvFun { 260 | name = "work"; 261 | buildInputs = with python34Packages; [ 262 | python34 263 | numpy 264 | toolz 265 | ]; 266 | }; 267 | 268 | blogEnv = pkgs.myEnvFun { 269 | name = "blog"; 270 | buildInputs = with python27Packages; [ 271 | python27 272 | pelican 273 | markdown 274 | ]; 275 | }; 276 | }; 277 | } 278 | ``` 279 | 280 | For the first environment we want Python 3.4, and for the second Python 2.7. 281 | Note that we have to explicitly include the interpreter when using `myEnvFun`! 282 | We can install these environments with `nix-env -i env-` and use them by 283 | calling `load-env-`. In both cases `` is the argument `name` of the 284 | function `myEnvFun`. 285 | 286 | ```sh 287 | $ nix-env -i env-work 288 | installing ‘env-work’ 289 | 290 | $ load-env-work 291 | env-work loaded 292 | work:[~]$ 293 | ``` 294 | You can now start the interpreter, `python3`. 295 | 296 | ### Missing Python modules? 297 | 298 | At this point you might have gone ahead using the Nix shell or `myEnvFun` to create Python environments, and got some very surprising import errors, unrelated to those explained before. 299 | If you haven't encountered these yet, try running 300 | 301 | ```sh 302 | $ nix-shell -p python27 --run python 303 | ``` 304 | 305 | and then 306 | 307 | ```py 308 | >>> import sqlite3 309 | ``` 310 | 311 | You will notice that you get an `ImportError`. 312 | 313 | ```py 314 | >>> import sqlite3 315 | Traceback (most recent call last): 316 | File "", line 1, in 317 | File "/nix/store/v9pq6f0s1r5fdybqc7pbv7mkb33lx9yy-python-2.7.10/lib/python2.7/sqlite3/__init__.py", line 24, in 318 | from dbapi2 import * 319 | File "/nix/store/v9pq6f0s1r5fdybqc7pbv7mkb33lx9yy-python-2.7.10/lib/python2.7/sqlite3/dbapi2.py", line 28, in 320 | from _sqlite3 import * 321 | ImportError: No module named _sqlite3 322 | ``` 323 | 324 | What's going on here? In Nixpkgs the Python 2.x interpreters were sent on a diet. To [reduce dependency bloat](http://nixos.org/nixpkgs/manual/#sec-python) some modules were removed from the 2.x interpreters. 325 | If you do want some of these modules, then you have to include them explicitly, e.g. 326 | 327 | ```sh 328 | $ nix-shell -p python27 python27.modules.sqlite3 --run python 329 | ``` 330 | 331 | to include the `sqlite3` module. For convenience, there is also a `python27Full` package which includes all these modules 332 | 333 | ```sh 334 | $ nix-shell -p python27Full python27Packages.numpy python27Packages.toolz --run python 335 | ``` 336 | 337 | ### How to find Python packages? 338 | 339 | So far we only considered two python packages, `numpy` and `toolz`. At this point you might be wondering how to find packages. 340 | You can search for packages with `nix-env -qa`. The `-q` stands for query and `-a` for available derivations. Let's search for `numpy` 341 | 342 | ```sh 343 | $ nix-env -qa '.*numpy.*' 344 | pypy2.6-numpydoc-0.5 345 | python2.7-numpy-1.10.1 346 | python2.7-numpydoc-0.5 347 | python3.4-numpy-1.10.1 348 | python3.4-numpydoc-0.5 349 | python3.5-numpy-1.10.1 350 | python3.5-numpydoc-0.5 351 | ``` 352 | 353 | A tool that is generally easier to begin with is [Nox](https://github.com/madjar/nox). You can install Nox with `nix-env -i nox` or try it with `nix-shell -p nox`. 354 | Let's search for `numpy` with Nox. 355 | 356 | ```sh 357 | $ nox numpy 358 | Refreshing cache 359 | 1 pypy2.6-numpydoc-0.5 (nixos.pypyPackages.numpydoc) 360 | Sphinx extension to support docstrings in Numpy format 361 | 2 python2.7-numpy-1.10.1 (nixos.python27Packages.numpy) 362 | Scientific tools for Python 363 | 3 python2.7-numpydoc-0.5 (nixos.python27Packages.numpydoc) 364 | Sphinx extension to support docstrings in Numpy format 365 | 4 python3.4-numpy-1.10.1 (nixos.python34Packages.numpy) 366 | Scientific tools for Python 367 | 5 python3.4-numpydoc-0.5 (nixos.python34Packages.numpydoc) 368 | Sphinx extension to support docstrings in Numpy format 369 | 6 python3.5-numpy-1.10.1 (nixos.python35Packages.numpy) 370 | Scientific tools for Python 371 | 7 python3.5-numpydoc-0.5 (nixos.python35Packages.numpydoc) 372 | Sphinx extension to support docstrings in Numpy format 373 | Packages to install: 374 | ``` 375 | 376 | Nox provides, among other things, an easier interface to `nix-env` for querying and installing packages. 377 | Nox shows you the name with version of packages, along with the Nix attribute, e.g. `nixos.python34Packages.numpy`. 378 | The first part is the identifier of the channel, in this case `nixos`, since 379 | 380 | ```sh 381 | $ nix-channel --list 382 | nixos https://nixos.org/channels/nixos-unstable 383 | ``` 384 | 385 | Another example 386 | 387 | ```sh 388 | $ nox pandas 389 | 1 pypy2.6-pandas-0.17.0 (nixos.pypyPackages.pandas) 390 | Python Data Analysis Library 391 | 2 python2.7-pandas-0.17.0 (nixos.python27Packages.pandas) 392 | Python Data Analysis Library 393 | 3 python3.4-pandas-0.17.0 (nixos.python34Packages.pandas) 394 | Python Data Analysis Library 395 | 4 python3.5-pandas-0.17.0 (nixos.python35Packages.pandas) 396 | Python Data Analysis Library 397 | Packages to install: 398 | ``` 399 | 400 | ### Alternative interpreters and shells 401 | 402 | So far we considered only the CPython interpreter, but in the examples shown just before, you could see that packages for the PyPy interpreter also show up. 403 | Indeed, you can use the PyPy interpreter on Nix as well 404 | 405 | ```sh 406 | $ nix-shell -p pypy --run pypy 407 | Python 2.7.9 (295ee98b69288471b0fcf2e0ede82ce5209eb90b, Sep 21 2015, 22:02:02) 408 | [PyPy 2.6.0 with GCC 4.9.3] on linux2 409 | Type "help", "copyright", "credits" or "license" for more information. 410 | >>>> 411 | ``` 412 | 413 | We can get an environment with PyPy just like we did before. 414 | 415 | ```sh 416 | $ nix-shell -p pypyPackages.numpy pypyPackages.toolz 417 | ``` 418 | 419 | but this results in an error 420 | 421 | ```sh 422 | error: numpy-1.10.1 not supported for interpreter pypy 423 | (use ‘--show-trace’ to show detailed location information) 424 | ``` 425 | 426 | Why is that? As the message explains, `numpy` is 427 | [not supported](http://pypy.org/numpydonate.html) for `pypy`, just like many 428 | other packages that include extension types. 429 | This is however not a Nix issue, but a PyPy issue. Even so, you will encounter 430 | these kind of errors more often, since also with CPython certain packages are 431 | supported on certain versions, but not all. 432 | 433 | Included in the Nix packages collection are also alternative Python shells, like Jupyter/IPython. 434 | Say we want to use `numpy` and `toolz` again but now using the [IPython](http://ipython.org/) interpreter 435 | 436 | ```sh 437 | $ nix-shell -p python34Packages.ipython python34Packages.numpy python34Packages.toolz --run ipython 438 | Python 3.4.3 (default, Jan 01 1970, 00:00:01) 439 | Type "copyright", "credits" or "license" for more information. 440 | 441 | IPython 4.0.0 -- An enhanced Interactive Python. 442 | ? -> Introduction and overview of IPython's features. 443 | %quickref -> Quick reference. 444 | help -> Python's own help system. 445 | object? -> Details about 'object', use 'object??' for extra details. 446 | 447 | In [1]: 448 | ``` 449 | 450 | We can also use the [Jupyter QtConsole](http://jupyter.org/qtconsole/stable/) 451 | 452 | ```sh 453 | nix-shell -p python34Packages.qtconsole --run "jupyter qtconsole" 454 | ``` 455 | 456 | or the [Jupyter Notebook](http://jupyter.org/) 457 | 458 | ```sh 459 | nix-shell -p python34Packages.notebook --run "jupyter notebook" 460 | ``` 461 | 462 | ## Developing a Python package 463 | 464 | Now that you know how to get a working Python environment on Nix, it is time to go forward and start actually developing with Python. 465 | We will first have a look at how Python packages are packaged on Nix. Then, we will look how you can use development mode with your code. 466 | 467 | ### Python packaging on Nix 468 | 469 | On Nix all packages are built by functions. The main function in Nix for building Python packages is [`buildPythonPackage`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix). 470 | Let's see how we would build the `toolz` package. According to [`python-packages.nix`](https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/top-level/python-packages.nix) `toolz` is build using 471 | 472 | ```nix 473 | toolz = buildPythonPackage rec{ 474 | name = "toolz-${version}"; 475 | version = "0.7.4"; 476 | 477 | src = pkgs.fetchurl{ 478 | url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz"; 479 | sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd"; 480 | }; 481 | 482 | meta = { 483 | homepage = "http://github.com/pytoolz/toolz/"; 484 | description = "List processing tools and functional utilities"; 485 | license = licenses.bsd3; 486 | maintainers = with maintainers; [ fridh ]; 487 | }; 488 | }; 489 | ``` 490 | 491 | What happens here? The function `buildPythonPackage` is called and as argument 492 | it accepts a set. In this case the set is a recursive set ([`rec`](http://nixos.org/nix/manual/#sec-constructs)). One of the 493 | arguments is the name of the package, which consists of a basename (generally 494 | following the name on PyPi) and a version. Another argument, `src` specifies the 495 | source, which in this case is fetched from an url. `fetchurl` not only downloads 496 | the target file, but also validates its hash. Furthermore, we specify some 497 | (optional) [meta information](http://nixos.org/nixpkgs/manual/#chap-meta). 498 | 499 | The output of the function is a derivation, which is an attribute with the name 500 | `toolz` of the set `pythonPackages`. Actually, sets are created for all interpreter versions, 501 | so `python27Packages`, `python34Packages`, `python35Packages` and `pypyPackages`. 502 | 503 | The above example works when you're directly adding or modifying packages to `python-packages.nix`. 504 | Often though, you will want to test a Nix expression outside of the Nixpkgs tree. If you create a `shell.nix` file with the following contents 505 | 506 | ```nix 507 | with import {}; 508 | 509 | pkgs.python35Packages.buildPythonPackage rec { 510 | name = "toolz-${version}"; 511 | version = "0.7.4"; 512 | 513 | src = pkgs.fetchurl{ 514 | url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz"; 515 | sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd"; 516 | }; 517 | 518 | meta = { 519 | homepage = "http://github.com/pytoolz/toolz/"; 520 | description = "List processing tools and functional utilities"; 521 | license = licenses.bsd3; 522 | maintainers = with maintainers; [ fridh ]; 523 | }; 524 | } 525 | ``` 526 | 527 | and then execute `nix-shell` will result in an environment in which you can use 528 | Python 3.5 and the `toolz` package. As you can see we had to explicitly mention 529 | for which Python version we want to build a package. 530 | 531 | Often though, you will want to use a package in environments together with other packages. 532 | If we create a `shell.nix` file with the following contents 533 | 534 | ```nix 535 | with import {}; 536 | 537 | ( let 538 | toolz = pkgs.python35Packages.buildPythonPackage rec { 539 | name = "toolz-${version}"; 540 | version = "0.7.4"; 541 | 542 | src = pkgs.fetchurl{ 543 | url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz"; 544 | sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd"; 545 | }; 546 | 547 | meta = { 548 | homepage = "http://github.com/pytoolz/toolz/"; 549 | description = "List processing tools and functional utilities"; 550 | license = licenses.bsd3; 551 | maintainers = with maintainers; [ fridh ]; 552 | }; 553 | }; 554 | 555 | in pkgs.python35.buildEnv.override rec { 556 | 557 | extraLibs = [ pkgs.python35Packages.numpy toolz ]; 558 | } 559 | ).env 560 | ``` 561 | 562 | and again execute `nix-shell`, then we get a Python 3.5 environment with our 563 | locally defined package as well as `numpy` which is build according to the 564 | definition in Nixpkgs. What did we do here? Well, we took the Nix expression 565 | that we used earlier to build a Python environment, and said that we wanted to 566 | include our own version of `toolz`. To introduce our own package in the scope of 567 | `buildEnv.override` we used a 568 | [`let`](http://nixos.org/nix/manual/#sec-constructs) expression. 569 | 570 | ### Handling dependencies 571 | 572 | So far our example, `toolz`, didn't have any dependencies on other Python 573 | packages or system libraries. According to the manual, the `buildPythonPackage` 574 | uses the arguments `buildInputs` and `propagatedBuildInputs`. If something is 575 | exclusively a build-time dependency, then the dependency should be included as a 576 | `buildInput`, but if it is (also) a runtime dependency, then it should be added 577 | to `propagatedBuildInputs`. 578 | 579 | The following example shows which arguments are given to `buildPythonPackage` in order to be build [`datashape`](https://github.com/blaze/datashape). 580 | 581 | ```nix 582 | datashape = buildPythonPackage rec { 583 | name = "datashape-${version}"; 584 | version = "0.4.7"; 585 | 586 | src = pkgs.fetchurl { 587 | url = "https://pypi.python.org/packages/source/D/DataShape/${name}.tar.gz"; 588 | sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278"; 589 | }; 590 | 591 | buildInputs = with self; [ pytest ]; 592 | propagatedBuildInputs = with self; [ numpy multipledispatch dateutil ]; 593 | 594 | meta = { 595 | homepage = https://github.com/ContinuumIO/datashape; 596 | description = "A data description language"; 597 | license = licenses.bsd2; 598 | maintainers = with maintainers; [ fridh ]; 599 | }; 600 | }; 601 | ``` 602 | 603 | We can see several runtime dependencies, `numpy`, `multipledispatch`, and 604 | `dateutil`. Furthermore, we have one `buildInput`, i.e. `pytest`. `pytest` is a 605 | test runner and is only used during the `checkPhase` and is therefore not added 606 | to `propagatedBuildInputs`. 607 | 608 | In the previous case we had only dependencies on other packages to consider. 609 | Occasionally you have also system libraries to consider. E.g., `lxml` provides 610 | Python bindings to `libxml2` and `libxslt`. These libraries are only required 611 | when building the bindings and are therefore added as `buildInputs`. 612 | 613 | ```nix 614 | lxml = buildPythonPackage rec { 615 | name = "lxml-3.4.4"; 616 | 617 | src = pkgs.fetchurl { 618 | url = "http://pypi.python.org/packages/source/l/lxml/${name}.tar.gz"; 619 | sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk"; 620 | }; 621 | 622 | buildInputs = with self; [ pkgs.libxml2 pkgs.libxslt ]; 623 | 624 | meta = { 625 | description = "Pythonic binding for the libxml2 and libxslt libraries"; 626 | homepage = http://lxml.de; 627 | license = licenses.bsd3; 628 | maintainers = with maintainers; [ sjourdois ]; 629 | }; 630 | }; 631 | ``` 632 | 633 | In this example `lxml` and Nix are able to work out exactly where the relevant 634 | files of the dependencies are. This is not always the case. 635 | 636 | The example below shows bindings to The Fastest Fourier Transform in the West, commonly known as 637 | FFTW. On Nix we have separate packages of FFTW for the different types of floats 638 | (`"single"`, `"double"`, `"long-double"`). The bindings need all three types, 639 | and therefore we add all three as `buildInputs`. The bindings don't expect to 640 | find each of them in a different folder, and therefore we have to set `LDFLAGS` 641 | and [`CFLAGS`](https://en.wikipedia.org/wiki/CFLAGS). 642 | 643 | ```nix 644 | pyfftw = buildPythonPackage rec { 645 | name = "pyfftw-${version}"; 646 | version = "0.9.2"; 647 | 648 | src = pkgs.fetchurl { 649 | url = "https://pypi.python.org/packages/source/p/pyFFTW/pyFFTW-${version}.tar.gz"; 650 | sha256 = "f6bbb6afa93085409ab24885a1a3cdb8909f095a142f4d49e346f2bd1b789074"; 651 | }; 652 | 653 | buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble]; 654 | 655 | propagatedBuildInputs = with self; [ numpy scipy ]; 656 | 657 | # Tests cannot import pyfftw. pyfftw works fine though. 658 | doCheck = false; 659 | 660 | preConfigure = '' 661 | export LDFLAGS="-L${pkgs.fftw}/lib -L${pkgs.fftwFloat}/lib -L${pkgs.fftwLongDouble}/lib" 662 | export CFLAGS="-I${pkgs.fftw}/include -I${pkgs.fftwFloat}/include -I${pkgs.fftwLongDouble}/include" 663 | ''; 664 | 665 | meta = { 666 | description = "A pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms"; 667 | homepage = http://hgomersall.github.com/pyFFTW/; 668 | license = with licenses; [ bsd2 bsd3 ]; 669 | maintainer = with maintainers; [ fridh ]; 670 | }; 671 | }; 672 | ``` 673 | 674 | Note also the line `doCheck = false;`, we explicitly disabled running the test-suite. 675 | 676 | ### Developing with Nix shell 677 | 678 | When building a package, Nix sequentially goes through a series of 679 | [phases](http://nixos.org/nixpkgs/manual/#sec-stdenv-phases). Separate phases 680 | exist for unpacking the source code, patching it, building it and 681 | installing it. `buildPythonPackage` 682 | [modifies](http://nixos.org/nixpkgs/manual/#ssec-build-python-package) these 683 | generic phases slightly in order to use the Python infrastructure. 684 | 685 | If you tested the above examples, then hopefully everything ran without 686 | problems. Unfortunately, that is not always the case. Sometimes you encounter 687 | problems when building a package. When this happens Nix aborts the build, and 688 | removes all build data. This can be annoying, especially when after a long time 689 | of compiling just a single test fails and Nix decides to abort (example: `scikitlearn`). 690 | 691 | Luckily there is the `-K` option to 692 | 693 | 694 | 695 | 696 | 697 | meaning sometimes there's maybe a single test failing, and youE.g., if you're building a Python package which took a long time to compile (e.g. `scikitlearn`) In such cases you might want to quickly change 698 | 699 | 700 | ### Develop local package 701 | 702 | As a Python developer you're likely aware of [development mode](http://pythonhosted.org/setuptools/setuptools.html#development-mode) (`python setup.py develop`); 703 | instead of installing the package this command creates a special link to the project code. 704 | That way, you can run updated code without having to reinstall after each and every change you make. 705 | Development mode is also available on Nix as [explained](http://nixos.org/nixpkgs/manual/#ssec-python-development) in the Nixpkgs manual. 706 | Let's see how you can use it. 707 | 708 | In the previous Nix expression the source was fetched from an url. We can also refer to a local source instead using 709 | 710 | ```nix 711 | src = ./path/to/source/tree; 712 | ``` 713 | 714 | Now, if we create a `shell.nix` file which calls `buildPythonPackage`, and if `src` 715 | is a local source, and if the local source has a `setup.py`, then development 716 | mode is activated. 717 | 718 | In the following example we create a simple environment that 719 | has a Python 3.5 version of our package in it, as well as its dependencies and 720 | other packages we like to have in the environment, all specified with `propagatedBuildInputs`. 721 | Indeed, we can just add any package we like to have in our environment to `propagatedBuildInputs`. 722 | 723 | ```nix 724 | with import ; 725 | with pkgs.python35Packages; 726 | 727 | buildPythonPackage rec { 728 | name = "mypackage"; 729 | src = ./path/to/package/source; 730 | propagatedBuildInputs = [ pytest numpy pkgs.libsndfile ]; 731 | }; 732 | ``` 733 | 734 | It is important to note that due to how development mode is implemented on Nix it is not possible to have multiple packages simultaneously in development mode. 735 | 736 | 737 | ## Organising your packages 738 | 739 | So far we discussed how you can use Python on Nix, and how you can develop with 740 | it. We've looked at how you write expressions to package Python packages, and we 741 | looked at how you can create environments in which specified packages are 742 | available. 743 | 744 | At some point you'll likely have multiple packages which you would 745 | like to be able to use in different projects. In order to minimise unnecessary 746 | duplication we now look at how you can maintain yourself a repository with your 747 | own packages. The important functions here are `import` and `callPackage`. 748 | 749 | In previous examples we used `import` generally in combination with the `with` 750 | statement, thereby introducing the attributes of the imported attribute set into 751 | the local scope. We can also simply assign the imported attribute set using a `let` expression. 752 | 753 | ### Including a derivation using `callPackage` 754 | Earlier we created a Python environment using `buildEnv`, and included the `toolz` package via a `let` expression. 755 | Let's split the package definition from the environment definition. 756 | 757 | We first create a function that builds `toolz` in `~/path/to/toolz/release.nix` 758 | 759 | ```nix 760 | { pkgs, buildPythonPackage }: 761 | 762 | buildPythonPackage rec { 763 | name = "toolz-${version}"; 764 | version = "0.7.4"; 765 | 766 | src = pkgs.fetchurl{ 767 | url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz"; 768 | sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd"; 769 | }; 770 | 771 | meta = { 772 | homepage = "http://github.com/pytoolz/toolz/"; 773 | description = "List processing tools and functional utilities"; 774 | license = licenses.bsd3; 775 | maintainers = with maintainers; [ fridh ]; 776 | }; 777 | }; 778 | ``` 779 | 780 | It takes two arguments, `pkgs` and `buildPythonPackage`. 781 | We now call this function using `callPackage` in the definition of our environment 782 | 783 | ```nix 784 | with import {}; 785 | 786 | ( let 787 | toolz = pkgs.callPackage ~/path/to/toolz/release.nix { pkgs=pkgs; buildPythonPackage=pkgs.python35Packages.buildPythonPackage; }; 788 | in pkgs.python35.buildEnv.override rec { 789 | extraLibs = [ pkgs.python35Packages.numpy toolz ]; 790 | } 791 | ).env 792 | ``` 793 | 794 | Important to remember is that the Python version for which the package is made 795 | depends on the `python` derivation that is passed to `buildPythonPackage`. Nix 796 | tries to automatically pass arguments when possible, which is why generally you 797 | don't explicitly define which `python` derivation should be used. In the above 798 | example we use `buildPythonPackage` that is part of the set `python35Packages`, 799 | and in this case the `python35` interpreter is automatically used. 800 | 801 | 802 | ### Creating a set of packages 803 | 804 | With the previously explained method you can split packages from environments. and you can call as many packages as you want from whichever environment. 805 | Likely you will have at some point developed several packages with dependencies between each other. 806 | In such case it might make sense to make a set of packages which you can then import in your environments, just like we do with the `nixpkgs` repository. 807 | 808 | 809 | 810 | 811 | ## Python on Nix internals 812 | 813 | 814 | ### Important files and folders 815 | 816 | - [`buildPythonPackage`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix) is a function to build Python packages. 817 | - [`python-packages.nix`](https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/top-level/python-packages.nix) is a Nix expression listing most of Python packages that are available in Nix. 818 | - Folder with [expressions for Python interpreters](https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/interpreters/python). 819 | - ['wrapper.nix'](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/wrapper.nix) wraps Python binaries. 820 | 821 | ## FAQ 822 | 823 | ### How can I prevent my packages from being garbage-collected? 824 | 825 | So you're using `nix-shell` and whenever you run `nix-collect-garbage -d` your packages are gone? 826 | 827 | With `nix-shell`... 828 | 829 | On NixOS the `system.extraDependencies` option for `configuration.nix` exists. 830 | Packages added here are added to the Nix store, but not made available to users. 831 | 832 | ```nix 833 | ... 834 | system.extraDependencies = with pkgs.python35Packages; [ 835 | numpy 836 | toolz 837 | ]; 838 | ... 839 | ``` 840 | 841 | ### Why should I use buildEnv for creating environments instead of buildPythonPackage or mkDerivation? 842 | --------------------------------------------------------------------------------