No description
Find a file
2020-10-01 16:00:13 +02:00
.github/workflows ci: Bump cachix action to v6 2020-07-01 10:36:07 +02:00
bin Use overridePythonAttrs in CLI code generation 2020-03-27 23:11:54 +00:00
hooks overrides: Remove condition from wheel override 2020-08-17 05:02:19 +02:00
nix Format with nixpkgs-fmt 0.9 2020-05-19 21:06:02 +01:00
pkgs/poetry poetry: Manually touch up poetry.lock to circumvent https://github.com/python-poetry/poetry/issues/2520 2020-07-22 16:12:43 +02:00
tests feat: test tag/rev/branch 2020-09-08 16:15:09 +02:00
tools Format with nixpkgs-fmt 0.9 2020-05-19 21:06:02 +01:00
.envrc Add .envrc for direnv 2020-02-27 08:44:43 +00:00
.gitignore Add flamegraph to gitignore 2020-02-29 14:09:40 +00:00
check-fmt Add path to shell.nix in check-fmt script 2020-05-19 21:10:01 +01:00
cli.nix nixpkgs-fmt broke it's format yet again 2020-04-29 14:12:59 +01:00
default.nix Null out poetry in pythonPackages set when bootstrapping 2020-10-01 14:34:58 +02:00
editable.nix Install an egg for editablePackageSources 2020-05-28 16:18:57 +02:00
extensions.json Add support for eggs 2020-01-08 14:03:01 +00:00
fetch-wheel.sh fetchWheelFromPypi: Map closer to nixpkgs fetchurl 2020-05-03 10:10:19 +01:00
flake.nix flake: remove the deprecated 'edition' entry 2020-09-01 11:39:21 +02:00
generate.py Add support for eggs 2020-01-08 14:03:01 +00:00
lib.nix throw an error when no build-backend is specified 2020-07-14 23:09:10 +02:00
Makefile Properly fetch sdists with extension 2019-06-24 17:01:27 +01:00
mk-poetry-dep.nix mk-poetry-dep: Remove redundant pythonPackages argument 2020-10-01 14:35:49 +02:00
overlay.nix Add overlay files 2019-11-19 16:34:50 +00:00
overrides.nix overrides: Add version override for keyring 2020-10-01 16:00:13 +02:00
pep425.nix Fix pep425 matching when modifiers are present 2020-07-23 17:08:41 +02:00
pep508.nix Format with nixpkgs-fmt 0.9 2020-05-19 21:06:02 +01:00
plugins.nix Add experimental support for plugin closures 2020-07-14 14:35:28 +02:00
README.md fix anchor in readme 2020-09-05 18:35:18 +02:00
semver.nix Format with nixpkgs-fmt 0.9 2020-05-19 21:06:02 +01:00
shell-scripts.nix mkPoetryEnv: Add scripts from pyproject.toml (if they exist) 2020-08-17 11:06:13 +02:00
shell.nix Pin NIX_PATH in nix-shell 2020-05-19 22:23:52 +01:00

poetry2nix

poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions. It does so by parsing pyproject.toml and poetry.lock and converting them to Nix derivations on the fly.

API

The poetry2nix public API consists of the following attributes:

mkPoetryApplication

Creates a Python application using the Python interpreter specified based on the designated poetry project and lock files. mkPoetryApplication takes an attribute set with the following attributes (attributes without default are mandatory):

  • projectDir: path to the root of the project.
  • src: project source (default: cleanPythonSources { src = projectDir; }).
  • pyproject: path to pyproject.toml (default: projectDir + "/pyproject.toml").
  • poetrylock: poetry.lock file path (default: projectDir + "/poetry.lock").
  • overrides: Python overrides to apply (default: [defaultPoetryOverrides]).
  • meta: application meta data (default: {}).
  • python: The Python interpreter to use (default: pkgs.python3).

Other attributes are passed through to buildPythonApplication.

Example

poetry2nix.mkPoetryApplication {
    projectDir = ./.;
}

See ./pkgs/poetry/default.nix for a working example.

Dependency environment

The resulting derivation also has the passthru attribute dependencyEnv, which is an environment with a python interpreter, all non-development dependencies and your application. This can be used if your application doesn't provide any binaries on its own and instead relies on dependencies binaries to call its modules (as is often the case with celery or gunicorn). For example, if your application defines a CLI for the module admin and a gunicorn app for the module web, a working default.nix would contain

let
    app = poetry2nix.mkPoetryApplication {
        projectDir = ./.;
    };
in app.dependencyEnv

After building this expression, your CLI and app can be called with these commands

$ result/bin/python -m admin
$ result/bin/gunicorn web:app

Note: If you need to perform overrides on the application, use app.dependencyEnv.override { app = app.override { ... }; }. See ./tests/dependency-environment/default.nix for a full example.

mkPoetryEnv

Creates an environment that provides a Python interpreter along with all dependencies declared by the designated poetry project and lock files. Also allows package sources of an application to be installed in editable mode for fast development. mkPoetryEnv takes an attribute set with the following attributes (attributes without default are mandatory):

  • projectDir: path to the root of the project.
  • pyproject: path to pyproject.toml (default: projectDir + "/pyproject.toml").
  • poetrylock: poetry.lock file path (default: projectDir + "/poetry.lock").
  • overrides: Python overrides to apply (default: [defaultPoetryOverrides]).
  • python: The Python interpreter to use (default: pkgs.python3).
  • editablePackageSources: A mapping from package name to source directory, these will be installed in editable mode (default: {}).

Example

poetry2nix.mkPoetryEnv {
    projectDir = ./.;
}

See ./tests/env/default.nix for a working example.

poetry2nix.mkPoetryEnv {
    projectDir = ./.;
    editablePackageSources = {
        my-app = ./src;
    };
}

See ./tests/editable/default.nix for a working example of an editable package.

mkPoetryPackages

Creates an attribute set of the shape { python, poetryPackages, pyProject, poetryLock }. Where python is the interpreter specified, poetryPackages is a list of all generated python packages, pyProject is the parsed pyproject.toml and poetryLock is the parsed poetry.lock file. mkPoetryPackages takes an attribute set with the following attributes (attributes without default are mandatory):

  • projectDir: path to the root of the project.
  • pyproject: path to pyproject.toml (default: projectDir + "/pyproject.toml").
  • poetrylock: poetry.lock file path (default: projectDir + "/poetry.lock").
  • overrides: Python overrides to apply (default: [defaultPoetryOverrides]).
  • python: The Python interpreter to use (default: pkgs.python3).

Example

poetry2nix.mkPoetryPackages {
    projectDir = ./.;
    python = python35;
}

defaultPoetryOverrides

poetry2nix bundles a set of default overrides that fix problems with various Python packages. These overrides are implemented in overrides.nix.

overrides.withDefaults

Returns a list containing the specified overlay and defaultPoetryOverrides.

Takes an attribute set with the following attributes (attributes without default are mandatory):

  • src: project source directory

Example

poetry2nix.mkPoetryEnv {
    projectDir = ./.;
    overrides = poetry2nix.overrides.withDefaults (self: super: { foo = null; });
}

See ./tests/override-support/default.nix for a working example.

overrides.withoutDefaults

Returns a list containing just the specified overlay, ignoring defaultPoetryOverrides.

Example

poetry2nix.mkPoetryEnv {
    projectDir = ./.;
    overrides = poetry2nix.overrides.withoutDefaults (self: super: { foo = null; });
}

cleanPythonSources

Provides a source filtering mechanism that:

  • Filters gitignore's
  • Filters pycache/pyc files
  • Uses cleanSourceFilter to filter out .git/.hg, .o/.so, editor backup files & nix result symlinks

Example

poetry2nix.cleanPythonSources {
    src = ./.;
}

Creating a custom Poetry2nix instance

Sometimes when it can be convenient to create a custom instance of poetry2nix with a different set of default overrides.

Example

let
  # self & super refers to poetry2nix
  p2nix = poetry2nix.overrideScope' (self: super: {

    # pyself & pysuper refers to python packages
    defaultPoetryOverrides = super.defaultPoetryOverrides.extend (pyself: pysuper: {

      my-custom-pkg = super.my-custom-pkg.overridePythonAttrs (oldAttrs: { });

    });

  });

in
p2nix.mkPoetryApplication {
  projectDir = ./.;
}

or as a nixpkgs overlay:

let
  pkgs = import <nixpkgs> {
    overlays = [
      # self & super refers to nixpkgs
      (self: super: {

        # p2self & p2super refers to poetry2nix
        poetry2nix = super.poetry2nix.overrideScope' (p2nixself: p2nixsuper: {

          # pyself & pysuper refers to python packages
          defaultPoetryOverrides = p2nixsuper.defaultPoetryOverrides.extend (pyself: pysuper: {

            my-custom-pkg = super.my-custom-pkg.overridePythonAttrs (oldAttrs: { });

          });

        });
      })

    ];
  };

in pkgs.poetry2nix.mkPoetryApplication {
  projectDir = ./.;
}

Contributing

Contributions to this project are welcome in the form of GitHub PRs. Please consider the following before creating PRs:

  • This project uses nixpkgs-fmt for formatting the Nix code. You can use nix-shell --run "nixpkgs-fmt . to format everything.
  • If you are planning to make any considerable changes, you should first present your plans in a GitHub issue so it can be discussed.
  • If you add new features please consider adding tests.

License

poetry2nix is released under the terms of the MIT license.