Skip to content

Commit 505e07c

Browse files
authored
refactor(nix): clean flake (amperser#1464)
- Split logic across multiple files in the `nix/` directory - Split helpers into a separate file - Use lib.getExe{,'} over `{pkgs,derivation}/bin/{name}`
1 parent f6b9f69 commit 505e07c

File tree

7 files changed

+209
-202
lines changed

7 files changed

+209
-202
lines changed

flake.nix

Lines changed: 11 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
inputs = {
33
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
4+
45
hooks = {
56
url = "github:cachix/git-hooks.nix";
67
inputs.nixpkgs.follows = "nixpkgs";
@@ -32,217 +33,25 @@
3233
outputs = {
3334
uv,
3435
self,
35-
hooks,
3636
nixpkgs,
37-
pyproject,
38-
build-systems,
3937
...
4038
}: let
4139
inherit (nixpkgs) lib;
42-
43-
getPythonVersion = let
44-
val = builtins.getEnv "PYTHON_VERSION";
45-
result =
46-
if val == ""
47-
then "3.13"
48-
else val;
49-
in
50-
builtins.replaceStrings ["."] [""] result;
40+
inherit (import ./nix/helpers.nix {inherit self nixpkgs overlay;}) forAllSystems;
5141

5242
workspace = uv.lib.workspace.loadWorkspace {workspaceRoot = ./.;};
5343
overlay = workspace.mkPyprojectOverlay {sourcePreference = "wheel";};
5444

55-
forAllSystems = f:
56-
lib.genAttrs [
57-
"x86_64-linux"
58-
"aarch64-linux"
59-
"x86_64-darwin"
60-
"aarch64-darwin"
61-
] (system:
62-
f rec {
63-
inherit system;
64-
65-
pkgs = nixpkgs.legacyPackages.${system};
66-
python = pkgs."python${getPythonVersion}";
67-
68-
pythonSet =
69-
(pkgs.callPackage pyproject.build.packages {inherit python;}).overrideScope (
70-
lib.composeManyExtensions [
71-
build-systems.overlays.default
72-
overlay
73-
]
74-
);
45+
importWithAttrs = path:
46+
forAllSystems (attrs:
47+
import path {
48+
inherit workspace self lib;
49+
inherit (attrs) pkgs system python pythonSet;
7550
});
7651
in {
77-
devShells =
78-
forAllSystems ({
79-
pkgs,
80-
system,
81-
python,
82-
pythonSet,
83-
...
84-
}: let
85-
check = self.checks.${system}.pre-commit;
86-
in {
87-
default = let
88-
editableOverlay =
89-
workspace.mkEditablePyprojectOverlay {
90-
root = "$REPO_ROOT";
91-
};
92-
93-
editablePythonSet =
94-
pythonSet.overrideScope (
95-
lib.composeManyExtensions [
96-
editableOverlay
97-
98-
(final: prev: {
99-
proselint =
100-
prev.proselint.overrideAttrs (old: {
101-
nativeBuildInputs =
102-
old.nativeBuildInputs
103-
++ final.resolveBuildSystem {
104-
editables = [];
105-
};
106-
});
107-
})
108-
109-
(final: prev: {
110-
google-re2 =
111-
prev.google-re2.overrideAttrs (old: {
112-
nativeBuildInputs =
113-
(old.nativeBuildInputs or [])
114-
++ (with final; [setuptools pybind11])
115-
++ (with pkgs; [re2 abseil-cpp]);
116-
});
117-
})
118-
]
119-
);
120-
121-
virtualenv = editablePythonSet.mkVirtualEnv "proselint-env" {proselint = ["test" "dev" "web"];};
122-
in
123-
pkgs.mkShell {
124-
buildInputs = check.enabledPackages;
125-
126-
packages = [
127-
virtualenv
128-
pkgs.git-cliff
129-
pkgs.typos
130-
pkgs.uv
131-
];
132-
133-
env = {
134-
UV_NO_SYNC = "1";
135-
UV_PYTHON = python.interpreter;
136-
UV_PYTHON_DOWNLOADS = "never";
137-
LD_LIBRARY_PATH = "${pkgs.stdenv.cc.cc.lib}/lib";
138-
};
139-
140-
shellHook =
141-
''
142-
export REPO_ROOT=$(git rev-parse --show-toplevel)
143-
unset PYTHONPATH
144-
''
145-
+ check.shellHook;
146-
};
147-
});
148-
149-
packages =
150-
forAllSystems ({
151-
pythonSet,
152-
pkgs,
153-
...
154-
}: {
155-
default = pythonSet.mkVirtualEnv "proselint-env" workspace.deps.default;
156-
157-
wheel =
158-
pythonSet.proselint.override {
159-
pyprojectHook = pythonSet.pyprojectDistHook;
160-
};
161-
162-
sdist =
163-
(pythonSet.proselint.override {
164-
pyprojectHook = pythonSet.pyprojectDistHook;
165-
}).overrideAttrs (old: {
166-
env.uvBuildType = "sdist";
167-
});
168-
169-
api = let
170-
env =
171-
pythonSet.mkVirtualEnv "proselint-api-env" {
172-
proselint = ["web"];
173-
};
174-
in
175-
pkgs.stdenv.mkDerivation {
176-
name = "proselint-api";
177-
src = ./.;
178-
179-
dontBuild = true;
180-
dontConfigure = true;
181-
182-
installPhase = ''
183-
mkdir -p $out/bin $out/share/proselint-api
184-
185-
cp $src/app.py $out/share/proselint-api
186-
187-
cat > $out/bin/proselint-api-run <<-EOF
188-
#!${pkgs.bash}/bin/bash
189-
cd $out/share/proselint-api
190-
exec ${env}/bin/uvicorn app:app "\$@"
191-
EOF
192-
193-
chmod +x $out/bin/proselint-api-run
194-
'';
195-
};
196-
});
197-
198-
apps =
199-
forAllSystems ({system, ...}: {
200-
default = {
201-
type = "app";
202-
program = "${self.packages.${system}.default}/bin/proselint";
203-
};
204-
205-
api = {
206-
type = "app";
207-
program = "${self.packages.${system}.api}/bin/proselint-api-run";
208-
};
209-
});
210-
211-
checks =
212-
forAllSystems ({
213-
system,
214-
pkgs,
215-
...
216-
}: {
217-
pre-commit =
218-
hooks.lib.${system}.run {
219-
src = ./.;
220-
package = pkgs.prek;
221-
222-
hooks = {
223-
trim-trailing-whitespace.enable = true;
224-
end-of-file-fixer.enable = true;
225-
mixed-line-endings.enable = true;
226-
markdownlint.enable = true;
227-
228-
ruff.enable = true;
229-
pyright = let
230-
pyright = pkgs.basedpyright;
231-
in {
232-
enable = true;
233-
package = pyright;
234-
entry = "${pyright}/bin/basedpyright";
235-
};
236-
237-
convco.enable = true;
238-
239-
alejandra.enable = true;
240-
statix = {
241-
enable = true;
242-
settings.ignore = ["/.direnv"];
243-
};
244-
};
245-
};
246-
});
52+
devShells = importWithAttrs ./nix/dev-shells.nix;
53+
packages = importWithAttrs ./nix/packages.nix;
54+
checks = importWithAttrs ./nix/checks.nix;
55+
apps = importWithAttrs ./nix/apps.nix;
24756
};
24857
}

nix/apps.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
lib,
3+
self,
4+
system,
5+
...
6+
}: {
7+
default = {
8+
type = "app";
9+
program = lib.getExe' self.packages.${system}.default "proselint";
10+
};
11+
12+
api = {
13+
type = "app";
14+
program = lib.getExe' self.packages.${system}.api "proselint-api-run";
15+
};
16+
}

nix/checks.nix

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
lib,
3+
self,
4+
pkgs,
5+
system,
6+
...
7+
}: {
8+
pre-commit =
9+
self.inputs.hooks.lib.${system}.run {
10+
src = ./.;
11+
package = pkgs.prek;
12+
13+
hooks = {
14+
trim-trailing-whitespace.enable = true;
15+
end-of-file-fixer.enable = true;
16+
mixed-line-endings.enable = true;
17+
markdownlint.enable = true;
18+
alejandra.enable = true;
19+
convco.enable = true;
20+
ruff.enable = true;
21+
22+
statix = {
23+
enable = true;
24+
settings.ignore = ["/.direnv"];
25+
};
26+
27+
pyright = {
28+
enable = true;
29+
package = pkgs.basedpyright;
30+
entry = lib.getExe pkgs.basedpyright;
31+
};
32+
};
33+
};
34+
}

nix/dev-shells.nix

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
workspace,
3+
self,
4+
lib,
5+
system,
6+
pkgs,
7+
python,
8+
pythonSet,
9+
}: let
10+
check = self.checks.${system}.pre-commit;
11+
12+
editablePythonSet =
13+
pythonSet.overrideScope (
14+
lib.composeManyExtensions [
15+
(workspace.mkEditablePyprojectOverlay {root = "$REPO_ROOT";})
16+
17+
(final: prev: {
18+
proselint =
19+
prev.proselint.overrideAttrs (old: {
20+
nativeBuildInputs =
21+
old.nativeBuildInputs
22+
++ final.resolveBuildSystem {
23+
editables = [];
24+
};
25+
});
26+
})
27+
28+
(final: prev: {
29+
google-re2 =
30+
prev.google-re2.overrideAttrs (old: {
31+
nativeBuildInputs =
32+
(old.nativeBuildInputs or [])
33+
++ (with final; [setuptools pybind11])
34+
++ (with pkgs; [re2 abseil-cpp]);
35+
});
36+
})
37+
]
38+
);
39+
in {
40+
default =
41+
pkgs.mkShell {
42+
buildInputs = check.enabledPackages;
43+
44+
packages = [
45+
(editablePythonSet.mkVirtualEnv "proselint-env" {proselint = ["test" "dev" "web"];})
46+
47+
pkgs.git-cliff
48+
pkgs.typos
49+
pkgs.uv
50+
];
51+
52+
env = {
53+
UV_NO_SYNC = "1";
54+
UV_PYTHON = python.interpreter;
55+
UV_PYTHON_DOWNLOADS = "never";
56+
LD_LIBRARY_PATH = "${pkgs.stdenv.cc.cc.lib}/lib";
57+
};
58+
59+
shellHook =
60+
''
61+
export REPO_ROOT=$(git rev-parse --show-toplevel)
62+
unset PYTHONPATH
63+
''
64+
+ check.shellHook;
65+
};
66+
}

nix/helpers.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
self,
3+
nixpkgs,
4+
overlay,
5+
}: let
6+
getPythonVersion = let
7+
val = builtins.getEnv "PYTHON_VERSION";
8+
9+
result =
10+
if val == ""
11+
then "3.13"
12+
else val;
13+
in
14+
builtins.replaceStrings ["."] [""] result;
15+
in {
16+
forAllSystems = f:
17+
nixpkgs.lib.genAttrs [
18+
"x86_64-linux"
19+
"aarch64-linux"
20+
"x86_64-darwin"
21+
"aarch64-darwin"
22+
] (system:
23+
f rec {
24+
inherit system;
25+
26+
pkgs = nixpkgs.legacyPackages.${system};
27+
python = pkgs."python${getPythonVersion}";
28+
pythonSet =
29+
(pkgs.callPackage self.inputs.pyproject.build.packages {inherit python;}).overrideScope (
30+
nixpkgs.lib.composeManyExtensions [
31+
self.inputs.build-systems.overlays.default
32+
overlay
33+
]
34+
);
35+
});
36+
}

0 commit comments

Comments
 (0)