Skip to content

Commit 0cb6510

Browse files
committed
Nix development environment using direnv and a flake.nix (pyproject.nix and uv2nix)
1 parent f233497 commit 0cb6510

File tree

3 files changed

+289
-0
lines changed

3 files changed

+289
-0
lines changed

.envrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# use flake .
2+
use flake .#uv2nix

flake.lock

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Sources:
2+
# https://pyproject-nix.github.io/pyproject.nix/use-cases/pyproject.html
3+
# https://pyproject-nix.github.io/uv2nix/usage/hello-world.html
4+
5+
{
6+
description = "PyFracVAL flake devenv";
7+
8+
inputs = {
9+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
10+
11+
pyproject-nix = {
12+
url = "github:pyproject-nix/pyproject.nix";
13+
inputs.nixpkgs.follows = "nixpkgs";
14+
};
15+
16+
uv2nix = {
17+
url = "github:pyproject-nix/uv2nix";
18+
inputs = {
19+
pyproject-nix.follows = "pyproject-nix";
20+
nixpkgs.follows = "nixpkgs";
21+
};
22+
};
23+
24+
pyproject-build-systems = {
25+
url = "github:pyproject-nix/build-system-pkgs";
26+
inputs = {
27+
pyproject-nix.follows = "pyproject-nix";
28+
uv2nix.follows = "uv2nix";
29+
nixpkgs.follows = "nixpkgs";
30+
};
31+
};
32+
33+
flake-parts = {
34+
url = "github:hercules-ci/flake-parts";
35+
};
36+
};
37+
38+
outputs =
39+
inputs@{
40+
flake-parts,
41+
nixpkgs,
42+
uv2nix,
43+
pyproject-nix,
44+
pyproject-build-systems,
45+
...
46+
}:
47+
flake-parts.lib.mkFlake { inherit inputs; } {
48+
systems = [
49+
"x86_64-linux"
50+
];
51+
perSystem =
52+
{
53+
self',
54+
pkgs,
55+
lib,
56+
...
57+
}:
58+
let
59+
project = pyproject-nix.lib.project.loadPyproject { projectRoot = ./.; };
60+
workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; };
61+
62+
overlay = workspace.mkPyprojectOverlay {
63+
sourcePreference = "wheel";
64+
};
65+
66+
pyprojectOverrides = final: prev: {
67+
numba = prev.numba.overrideAttrs (old: {
68+
buildInputs = (old.buildInputs or [ ]) ++ [ pkgs.tbb_2021_11 ];
69+
});
70+
};
71+
72+
python = pkgs.python3;
73+
74+
pythonSet =
75+
(pkgs.callPackage pyproject-nix.build.packages {
76+
inherit python;
77+
}).overrideScope
78+
(
79+
lib.composeManyExtensions [
80+
pyproject-build-systems.overlays.default
81+
overlay
82+
pyprojectOverrides
83+
]
84+
);
85+
86+
in
87+
{
88+
packages = {
89+
default = self'.packages.pyfracval;
90+
pyfracval = python.pkgs.buildPythonPackage (
91+
(project.renderers.buildPythonPackage { inherit python; }) // { env.CUSTOM_ENVVAR = "hello"; }
92+
);
93+
pyfracval-env = pythonSet.mkVirtualEnv "pyfracval-env" workspace.deps.default;
94+
};
95+
96+
devShells = {
97+
default =
98+
let
99+
arg = project.renderers.withPackages {
100+
inherit python;
101+
extras = [
102+
# "test"
103+
];
104+
};
105+
106+
pythonEnv = python.withPackages arg;
107+
in
108+
pkgs.mkShell {
109+
packages = [
110+
pythonEnv
111+
self'.packages.pyfracval
112+
];
113+
};
114+
115+
impure = pkgs.mkShell {
116+
packages = [
117+
python
118+
pkgs.uv
119+
];
120+
shellHook = ''
121+
unset PYTHONPATH
122+
'';
123+
};
124+
125+
uv2nix =
126+
let
127+
editableOverlay = workspace.mkEditablePyprojectOverlay {
128+
root = "$REPO_ROOT";
129+
};
130+
131+
editablePythonSet = pythonSet.overrideScope editableOverlay;
132+
133+
virtualenv = editablePythonSet.mkVirtualEnv "pyfracval-dev-env" (
134+
workspace.deps.default
135+
// {
136+
pyfracval = [
137+
# "explore"
138+
# "test"
139+
];
140+
}
141+
);
142+
143+
in
144+
pkgs.mkShell {
145+
packages = [
146+
virtualenv
147+
pkgs.uv
148+
];
149+
shellHook = ''
150+
unset PYTHONPATH
151+
export REPO_ROOT=$(git rev-parse --show-toplevel)
152+
'';
153+
};
154+
};
155+
};
156+
};
157+
}

0 commit comments

Comments
 (0)