Skip to content

Commit

Permalink
Build the application's docker image 🐳 (#14)
Browse files Browse the repository at this point in the history
Run "nix-build" or "nix-build -A docker" to build a docker image. Run "nix-build -A app" to build Poretitioner locally without Docker.

#14
  • Loading branch information
thequicksort authored Apr 12, 2020
1 parent a2f9144 commit 4f15406
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 134 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ jobs:
with:
name: poretitioner_for_docker
path: /tmp/artifacts/poretitioner_docker.tar.gz

3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
rev: 18.9b0
hooks:
- id: black
args: [--verbose, --line-length=99]
args: [--fast, --line-length=99]
name: Prettify code
always_run: true

Expand All @@ -18,6 +18,7 @@ repos:
hooks:
- id: isort
name: Sort imports
always_run: true
args: [-]

#####################################
Expand Down
1 change: 0 additions & 1 deletion bootstrap_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ configure_nix_env_if_necessary () {
source $fn
fi
added=1
break
fi
done
fi
Expand Down
70 changes: 48 additions & 22 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,62 @@
###########################################################################################
#
# This expression builds the Poretitioner application.
# It runs the test suite before building and will fail if any tests fail.
# - To see how this application is built, see the `App` section.
# - To see how this application is packaged for Docker, see the `Docker` section.
#
###########################################################################################

with import <nixpkgs> { };

{ pkgs ? import <nixpkgs> { }, python ? pkgs.python37 }:
with pkgs;
let
python = python37;
run_pkgs = callPackage ./nix/runDependencies.nix { inherit python; };
dev_pkgs = callPackage ./nix/devDependencies.nix { inherit python; };
test_pkgs = callPackage ./nix/testingDependencies.nix { inherit python; };
name = "poretitioner";

############################################################
#
# App - Builds the actual poretitioner application.
#
############################################################
dependencies = (callPackage ./nix/dependencies.nix { inherit python; });
run_pkgs = dependencies.run;
test_pkgs = dependencies.test;

# To understand how `buildPythonApplication` works, check out https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/mk-python-derivation.nix
in python.pkgs.buildPythonApplication {
pname = "poretitioner";
version = "0.0.1";
poretitioner = python.pkgs.buildPythonApplication {
pname = name;
version = "0.0.1";

src = ./.;

checkInputs = test_pkgs;
doCheck = true;
checkPhase = ''pytest tests'';

# Run-time dependencies
propagatedBuildInputs = run_pkgs;
};

####################################################################
#
# Docker - Builds the Docker image for the poretitioner application.
#
####################################################################

src = ./.;
binPath = builtins.concatStringsSep ''/'' [ poretitioner.outPath ''bin'' poretitioner.pname ];

# Build-time exclusive dependencies
nativeBuildInputs = dev_pkgs;
dockerImage = dockerTools.buildImage {
name = name;
tag = "latest";

# Test Dependencies
doCheck = true;
checkInputs = test_pkgs;
checkPhase = ''
py.test tests
'';
# Setting 'created' to 'now' will correctly set the file's creation date
# (instead of setting it to Unix epoch + 1). This is impure, but fine for our purposes.
created = "now";
config = {
# Runs 'poretitioner' by default.
Cmd = [ ''${binPath}'' ];
};
};

# Run-time dependencies
buildInputs = run_pkgs;
propagatedBuildInputs = run_pkgs;
in {
app = poretitioner;
docker = dockerImage;
}
97 changes: 97 additions & 0 deletions nix/dependencies.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
###########################################################################################
#
# dependencies.nix
#
###########################################################################################
#
# This Nix expression hosts the project's dependencies. There are three sections:
#
# run - Anything that needs to be available at runtime (e.g. you need to import it in python).
#
# build - Packages you don't want packaged in the final build, but still need to build
# the project (e.g. pre-commit hooks linters)
#
# test - Test-only dependencies (e.g code coverage packages)
#
# It takes in Nixpkgs and the Python package corresponding to a Python version.
#
###########################################################################################


{ pkgs, python, lib ? pkgs.lib, stdenv ? pkgs.stdenv }:
let precommit = (import ./pkgs/pre-commit/pre-commit.nix) { inherit python; };
in
with python.pkgs; rec {

###########################################################################################
#
# run - This list hosts the project's runtime dependencies (basically, anything you
# need to explicitly import in Python)
#
###########################################################################################

run = [
# Numerical computation library
numpy
# Data manipulation and analysis
pandas
# Hierarchical Data Format utilities
h5py
# Parallel computing library
dask
# Charts and plotting library
matplotlib
# Data visualization
seaborn
# Interactive computing
notebook
# For interactive builds
jupyter
] ++ lib.optional (stdenv.isLinux) pkgs.qt5.full; # Needed for certain graphical packages like matplotlib.

###########################################################################################
#
# build - This list hosts dependencies that shouldn't be packaged for distribution,
# but are still needed for developers. This includes testing frameworks, as well as
# tools like linters, git hooks, and static analyzers.
#
###########################################################################################

build = [
# Git hooks
precommit
# Import sorter
isort
# Highly opinionated code-formatter
black
# Style-guide enforcer
flake8
# Docstring static analyzer
pydocstyle
];


###########################################################################################
#
# test- This list hosts the project's test-only dependencies (e.g. test runners).
# It should not include any packages that aren't part of the testing infrastructure.
#
###########################################################################################

test = [
# Testing suite
pytest
# Test runner
pytestrunner
# Test code coverage generator
pytestcov
];

###########################################################################################
#
# all - A list containing all of the above packages.
#
###########################################################################################

all = run ++ test ++ build;
}
40 changes: 0 additions & 40 deletions nix/devDependencies.nix

This file was deleted.

5 changes: 3 additions & 2 deletions nix/env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
with import <nixpkgs> { };
let
python = pkgs.python37;
dev_pkgs = callPackage ./devDependencies.nix { inherit python; };
testing_pkgs = callPackage ./testingDependencies.nix { inherit python; };
dependencies = (callPackage ./dependencies.nix { inherit python; });
dev_pkgs = dependencies.build;
testing_pkgs = dependencies.test;
in dev_pkgs ++ testing_pkgs
32 changes: 0 additions & 32 deletions nix/runDependencies.nix

This file was deleted.

6 changes: 2 additions & 4 deletions nix/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ with import <nixpkgs> { };

let
python = pkgs.python37;
run_pkgs = callPackage ./runDependencies.nix { inherit python; };
dev_pkgs = callPackage ./devDependencies.nix { inherit python; };
test_pkgs = callPackage ./testingDependencies.nix { inherit python; };
in mkShell { buildInputs = run_pkgs ++ dev_pkgs ++ test_pkgs; }
dependencies = callPackage ./dependencies.nix { inherit python; };
in mkShell { buildInputs = dependencies.all; }
22 changes: 0 additions & 22 deletions nix/testingDependencies.nix

This file was deleted.

61 changes: 52 additions & 9 deletions poretitioner/poretitioner.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
# The following imports are simply a code-review proof-of-concept to illustrate that
# dependency management works.
import dask # noqa: F401
import jupyter # noqa: F401
import numpy # noqa: F401
import pandas # noqa: F401
def main():
# The following imports are simply a code-review proof-of-concept to illustrate that
# dependency management works.

print("Importing 'numpy'...")
import numpy # noqa: F401

def main():
# Stub for the command line
print("This print statement is a placeholder.")
print("\tImported 'numpy'!")

print("Importing 'pandas'...")
import pandas # noqa: F401

print("\tImported 'pandas'!")

print("Importing 'h5py'...")
import h5py # noqa: F401

print("\tImported 'h5py'!")

print("Importing 'dask'...")
import dask # noqa: F401

print("\tImported 'dask'!")

print("Importing 'matplotlib'...")
import matplotlib # noqa: F401

print("\tImported 'matplotlib'!")

print("Importing 'seaborn'...")
import seaborn # noqa: F401

print("\tImported 'seaborn'!")

print("Importing 'iPython notebook'...")
import notebook # noqa: F401

print("\tImported 'iPython notebook'!")

print("Importing 'jupyter'...")
import jupyter # noqa: F401

print("\tImported 'jupyter'!")

from matplotlib import pyplot as plt

print(
"As proof that these libraries are properly packaged and callable, drawing a basic plot..."
)
plt.plot([0, 1], [1, 0])
plt.title("matplotlib is properly packaged and importable.")
plt.show()

print("\tPlotted!")


if __name__ == "__main__":
Expand Down

0 comments on commit 4f15406

Please sign in to comment.