Skip to content

Commit

Permalink
polishing flakes (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
zupo authored Oct 28, 2023
1 parent db3e84c commit b7b1854
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 194 deletions.
49 changes: 12 additions & 37 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,20 @@ concurrency:

jobs:

tests_linux:
name: Tests on Linux
tests:
name: Tests

strategy:
matrix:
python: ["python3.9", "python3.10", "python3.11"]

runs-on: ubuntu-latest
steps:
- name: Convert matrix python version to nix python version
run: echo "PYTHON=$(echo ${{ matrix.python }} | tr -d .)" >> $GITHUB_ENV

- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
- uses: cachix/cachix-action@v12
with:
name: oceansprint
authToken: '${{ secrets.CACHIX_AUTH_TOKEN_PUBLIC }}'

- name: Run linters and unit tests the Nix way
run: |
nix build .#checks.x86_64-linux.pre-commit
nix build .#devShells.x86_64-linux.$(echo $PYTHON)
nix build .#packages.x86_64-linux.tesh-$(echo $PYTHON)
nix build .#packages.x86_64-linux.testEnv-$(echo $PYTHON)
nix build .#checks.x86_64-linux.tests-$(echo $PYTHON)
tests_macos:
name: Tests on macOS

strategy:
matrix:
python: ["python3.9", "python3.10", "python3.11"]

runs-on: macos-13
runner: ["ubuntu-latest", "macos-13"]
include:
- runner: "ubuntu-latest"
arch: "x86_64-linux"
- runner: "macos-13"
arch: "x86_64-darwin"

runs-on: ${{ matrix.runner }}
steps:
- name: Convert matrix python version to nix python version
run: echo "PYTHON=$(echo ${{ matrix.python }} | tr -d .)" >> $GITHUB_ENV
Expand All @@ -74,11 +53,7 @@ jobs:

- name: Run linters and unit tests the Nix way
run: |
nix build .#checks.x86_64-darwin.pre-commit
nix build .#devShells.x86_64-darwin.$(echo $PYTHON)
nix build .#packages.x86_64-darwin.tesh-$(echo $PYTHON)
nix build .#packages.x86_64-darwin.testEnv-$(echo $PYTHON)
nix build .#checks.x86_64-darwin.tests-$(echo $PYTHON)
nix build .#checks.${{ matrix.arch }}.ci-$(echo $PYTHON)
docker_linux:
name: Docker on Linux
Expand Down Expand Up @@ -125,7 +100,7 @@ jobs:

release:
name: Release
needs: [tests_linux, tests_macos]
needs: tests

# To test publishing to testpypi:
# * comment out "verify git tag matches pyproject.toml version"
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ tesh:
.PHONY: test
test: tests

skip_lint = false
.PHONY: tests
tests:
# In `nix flake check` we already have pre-commit as a separate check so we don't need to run it here
ifeq ($(skip_lint),false)
@make lint all=true
endif
@make types
@make unit
@make tesh
175 changes: 175 additions & 0 deletions flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{ inputs, lib, config, self, ... }@top: {
imports = [
inputs.pre-commit-hooks-nix.flakeModule
./interface.nix
];
perSystem = { config, self', inputs', pkgs, system, lib, ... }:

let
autoflake = pkgs.python3Packages.autoflake.overrideAttrs (old: {
propagatedBuildInputs = old.propagatedBuildInputs ++ [ pkgs.python3Packages.tomli ];
});

supportedPythons = top.config.pyDev.supportedPythons;
forAllPythons = name: f:
let
attrNames = (map (py: "${name}${py}") supportedPythons);
outputs = lib.genAttrs supportedPythons
(python: f python);
in
lib.mapAttrs' (py: value: { name = "${name}${py}"; inherit value; }) outputs;

poetryArgs = python: {
projectDir = self;
preferWheels = true;
python = pkgs.${python};
};

pyproject = lib.importTOML ./pyproject.toml;
name = pyproject.tool.poetry.name;

in
{
# Per-system attributes can be defined here. The self' and inputs'
# module parameters provide easy access to attributes of the same
# system.

formatter = pkgs.nixpkgs-fmt;

pre-commit.settings.hooks.black.enable = true;
pre-commit.settings.hooks.isort.enable = true;
pre-commit.settings.hooks.yamllint.enable = true;
pre-commit.settings.hooks.autoflake.enable = true;
pre-commit.settings.hooks.flake8.enable = true;

pre-commit.settings.settings.autoflake.binPath = "${autoflake}/bin/autoflake";
pre-commit.settings.hooks.check-merge-conflict = {
enable = true;

name = "check-merge-conflict";
description = "Check for files that contain merge conflict strings.";
entry = "${pkgs.python3Packages.pre-commit-hooks}/bin/check-merge-conflict";
types = [ "text" ];
};

pre-commit.settings.hooks.end-of-file-fixer = {
enable = true;

name = "end-of-file-fixer";
description = "Ensures that a file is either empty, or ends with one newline.";
entry = "${pkgs.python3Packages.pre-commit-hooks}/bin/end-of-file-fixer";
types = [ "text" ];
};

pre-commit.settings.hooks.trailing-whitespace = {
enable = true;

name = "trailing-whitespace";
description = "This hook trims trailing whitespace.";
entry = "${pkgs.python3Packages.pre-commit-hooks}/bin/trailing-whitespace-fixer";
types = [ "text" ];
};

pre-commit.settings.hooks.codespell = {
enable = true;

name = "codespell";
description = "Checks for common misspellings in text files.";
entry = "${pkgs.codespell}/bin/codespell --ignore-words .aspell.en.pws";
types = [ "text" ];
};

packages =
(forAllPythons "${name}-" (python:
inputs.poetry2nix.legacyPackages.${system}.mkPoetryApplication (poetryArgs python)))

//

(forAllPythons "testEnv-" (python:
inputs.poetry2nix.legacyPackages.${system}.mkPoetryEnv (poetryArgs python)));

checks =
(forAllPythons "tests-" (python:
pkgs.runCommand "tests"
{
nativeBuildInputs = self'.devShells."${python}".nativeBuildInputs;
} ''
cp -r ${self} ./source
chmod +w -R ./source
cd ./source
export PYTHONPATH="$(realpath ./src)"
make tests skip_lint=true
cp -r htmlcov $out/
''))
//

(forAllPythons "ci-" (python:
pkgs.runCommand "ci" {
jobs = [
self'.packages."${name}-${python}"
self'.packages."testEnv-${python}"
self'.devShells."${python}"
self'.checks."tests-${python}"
self'.checks."pre-commit"
];
} ''touch $out''
));

devShells =
{ default = self'.devShells.python311; }

//

(forAllPythons "" (python:
let
testEnv = self'.packages."testEnv-${python}";
in

pkgs.mkShell
{
name = "dev-shell";

nativeBuildInputs = with pkgs; [
poetry
testEnv

# remove when https://github.com/cachix/pre-commit-hooks.nix/issues/356 is merged
autoflake
black
codespell
pkgs.python3Packages.flake8
isort
yamllint

]
# extra test dependencies
++ (map (name: pkgs.${name}) top.config.pyDev.extraTestDependencies);

inputsFrom = [ config.pre-commit.devShell ];

shellHook = ''
tmp_path="$(realpath ./.direnv)"
mkdir -p "$tmp_path"
source="$(realpath .)"
mkdir -p "$tmp_path/python/${testEnv.sitePackages}"
# Install the package in editable mode
# This allows executing the project scripts from within the dev-shell using the current
# version of the code and its dependencies.
PYTHONPATH="${pkgs."${python}".pkgs.poetry-core}/${testEnv.sitePackages}:${testEnv}/${testEnv.sitePackages}" \
${pkgs."${python}".pkgs.pip}/bin/pip install \
--no-deps \
--disable-pip-version-check \
--no-index \
--no-build-isolation \
--prefix "$tmp_path/python" \
--editable $source
export PATH="$tmp_path/python/bin:$PATH"
export PYTHONPATH="$source/src:$tmp_path/python/${testEnv.sitePackages}:${testEnv}/${testEnv.sitePackages}"
'';

}));
};
}
Loading

0 comments on commit b7b1854

Please sign in to comment.