-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
190 lines (187 loc) · 7.22 KB
/
flake.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
176
177
178
179
180
181
182
183
184
185
186
187
188
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
pre-commit-hooks = {
url = "github:cachix/git-hooks.nix";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = { self, nixpkgs, flake-utils, poetry2nix, pre-commit-hooks, ... }:
flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ] (system:
let
projectConfig = {
python = pkgs.python311;
dependencyOverrides = (final: prev: {
docutils = prev.docutils.overridePythonAttrs (old: {
buildInputs = (old.buildInputs or [ ]) ++ [ final.flit-core ];
});
mypy = prev.mypy.override {
preferWheel = true;
};
});
};
pkgs = nixpkgs.legacyPackages.${system};
inherit (poetry2nix.lib.mkPoetry2Nix { pkgs = pkgs; }) mkPoetryApplication mkPoetryEnv defaultPoetryOverrides cleanPythonSources cli;
pyProject = builtins.fromTOML (builtins.readFile (./. + "/pyproject.toml"));
mkPoetryArgs = {
overrides = [ projectConfig.dependencyOverrides defaultPoetryOverrides ];
python = projectConfig.python;
projectDir = ./.;
};
nixpkgsAttrMap = attrnames: builtins.map (p: pkgs.${p}) attrnames;
pyProjectNixpkgsDeps = nixpkgsAttrMap (pyProject.tool.nixpkgs.dependencies or [ ]);
pyProjectNixpkgsDevDeps = pyProjectNixpkgsDeps ++ nixpkgsAttrMap (pyProject.tool.nixpkgs.dev-dependencies or [ ]);
# HACK work around a bug in poetry2nix where the .egg-info is named incorrectly
# https://github.com/nix-community/poetry2nix/issues/616
pkgInfoFields = {
Metadata-Version = "2.1";
Name = pyProject.tool.poetry.name;
Version = pyProject.tool.poetry.version;
Summary = pyProject.tool.poetry.description;
};
pkgInfoFile = with pkgs.lib.generators; (
builtins.toFile "${pyProject.tool.poetry.name}-PKG-INFO"
(toKeyValue { mkKeyValue = mkKeyValueDefault { } ": "; } pkgInfoFields)
);
moduleNames = (
pkgs.lib.attrNames
(
pkgs.lib.filterAttrs
(n: v: v == "directory")
(builtins.readDir srcDir)
)
);
editableEggInfoFix = ps:
(ps.toPythonModule (
pkgs.runCommand "editable-egg-info-fix" { } ''
mkdir -p "$out/${ps.python.sitePackages}"
cd "$out/${ps.python.sitePackages}"
${
pkgs.lib.concatMapStringsSep
"\n"
(pkg: (
if (pkg != pyProject.tool.poetry.name)
then ''mkdir "${pkg}.egg-info"; ln -s "${pkgInfoFile}" "${pkg}.egg-info/PKG-INFO"''
else ""
))
moduleNames
}
''));
# use impure flake in direnv to get live editing for mkPoetryEnv
envProjectDir = builtins.getEnv "PROJECT_DIR";
srcDir = (if envProjectDir == "" then ./src else "${envProjectDir}/src");
mkPoetryEnvEditableArgs = {
editablePackageSources.${pyProject.tool.poetry.name} = srcDir;
extraPackages = (ps: [ (editableEggInfoFix ps) ]);
};
in
rec {
packages = {
default = (mkPoetryApplication mkPoetryArgs).overrideAttrs (old: {
propagatedBuildInputs = (old.propagatedBuildInputs or [ ]) ++ pyProjectNixpkgsDeps;
});
containerImage = (pkgs.dockerTools.streamLayeredImage {
name = pyProject.tool.poetry.name;
# add deps to contents so they can be invoked directly if needed (e.g. /bin/someprogram)
contents = [ packages.default ] ++ pyProjectNixpkgsDeps;
config.Cmd = [ "/bin/${pyProject.tool.poetry.name}" ];
}).overrideAttrs (old: { passthru.exePath = ""; });
};
apps = pkgs.lib.mapAttrs (k: v: flake-utils.lib.mkApp { drv = v; }) packages;
devShells =
let
baseShellPackages = [
pkgs.act
(pkgs.poetry.withPlugins (ps: with ps; [ poetry-plugin-up ]))
cli
];
in
{
default = (
(mkPoetryEnv (mkPoetryArgs // mkPoetryEnvEditableArgs)).env.overrideAttrs (
oldAttrs: {
buildInputs = baseShellPackages ++ pyProjectNixpkgsDevDeps;
shellHook = ''
${checks.pre-commit-hooks.shellHook}
'';
"SHELL_LOADED_${builtins.hashString "sha256" envProjectDir}" = "1";
}
)
);
poetryFallback = pkgs.mkShell {
packages = baseShellPackages ++ [ projectConfig.python ];
};
};
checks.pre-commit-hooks = pre-commit-hooks.lib.${pkgs.system}.run {
src = ./.;
hooks =
let
poetryPreCommit = name: text: (pkgs.writeShellApplication
{
name = "pre-commit-${name}";
runtimeInputs = with devShells.default; (nativeBuildInputs ++ buildInputs);
text = text;
} + "/bin/pre-commit-${name}");
in
{
shellcheck.enable = true;
black = {
enable = true;
entry = pkgs.lib.mkForce (poetryPreCommit "black" ''black "$@"'');
};
nixpkgs-fmt.enable = true;
prettier = {
enable = true;
types_or = [ "markdown" "json" "yaml" ];
excludes = [ "^\\.template/.+/\\.cruft\\.json$" ];
};
isort = {
enable = true;
entry = pkgs.lib.mkForce (poetryPreCommit "isort" ''isort "$@"'');
};
mypy = {
enable = true;
name = "mypy";
entry = pkgs.lib.mkForce (poetryPreCommit "mypy" ''mypy "$@"'');
pass_filenames = false;
};
pytest = {
enable = true;
name = "pytest";
entry = poetryPreCommit "pytest" ''
# HACK force path to be in scope for flake evaluation
# ${ cleanPythonSources { src = ./.; } + "/src" }
pytest "$@"
'';
pass_filenames = false;
};
sphinx = {
enable = true;
name = "sphinx";
entry = poetryPreCommit "sphinx" "sphinx-build docs/ docs/generated/";
pass_filenames = false;
};
no-merge-rejects = {
enable = true;
name = "no-merge-rejects";
entry = poetryPreCommit "no-merge-rejects" ''
for filename in "$@"; do
echo "Rejected merge file exists: $filename"
done
exit 1
'';
files = "\\.rej$";
};
};
};
});
}