-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake-module.nix
175 lines (142 loc) · 5.72 KB
/
flake-module.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{pydev, ...}: { inputs, lib, config, self, ... }@top: {
imports = [
pydev.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 (self + /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:
pydev.inputs.poetry2nix.legacyPackages.${system}.mkPoetryApplication (poetryArgs python)))
//
(forAllPythons "testEnv-" (python:
pydev.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}"
'';
}));
};
}