Skip to content

Commit d944765

Browse files
committed
migrate from poetry2nix to uv2nix
1 parent 3aad7cc commit d944765

File tree

3 files changed

+133
-26
lines changed

3 files changed

+133
-26
lines changed

{{cookiecutter.project_slug}}/flake.nix

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,80 @@
33
inputs = {
44
nixpkgs.url = "nixpkgs/nixos-unstable";
55
flake-utils.url = "github:numtide/flake-utils";
6-
poetry2nix.url = "github:nix-community/poetry2nix";
6+
pyproject-nix = {
7+
url = "github:pyproject-nix/pyproject.nix";
8+
inputs.nixpkgs.follows = "uv2nix/nixpkgs";
9+
# inputs.uv2nix.follows = "nixpkgs";
10+
};
11+
pyproject-build-systems = {
12+
url = "github:pyproject-nix/build-system-pkgs";
13+
inputs.pyproject-nix.follows = "pyproject-nix";
14+
inputs.uv2nix.follows = "uv2nix";
15+
inputs.nixpkgs.follows = "uv2nix/nixpkgs";
16+
# inputs.uv2nix.follows = "nixpkgs";
17+
};
18+
uv2nix = {
19+
url = "github:pyproject-nix/uv2nix";
20+
inputs.pyproject-nix.follows = "pyproject-nix";
21+
# stale nixpkgs is missing the alias `lib.match` -> `builtins.match`
22+
# therefore point uv2nix to a patched nixpkgs, which introduces this alias
23+
# this is a temporary solution until nixpkgs us up-to-date again
24+
inputs.nixpkgs.url = "github:runtimeverification/nixpkgs/libmatch";
25+
# inputs.uv2nix.follows = "nixpkgs";
26+
};
727
};
8-
outputs = { self, nixpkgs, flake-utils, poetry2nix }:
9-
let
10-
allOverlays = [
11-
poetry2nix.overlay
12-
(final: prev: {
13-
{{ cookiecutter.project_slug }} = prev.poetry2nix.mkPoetryApplication {
14-
python = prev.python310;
15-
projectDir = ./.;
16-
groups = [];
17-
# We remove `dev` from `checkGroups`, so that poetry2nix does not try to resolve dev dependencies.
18-
checkGroups = [];
19-
};
20-
})
21-
];
22-
in flake-utils.lib.eachSystem [
28+
outputs = { self, nixpkgs, flake-utils, pyproject-nix, pyproject-build-systems, uv2nix }:
29+
let
30+
pythonVer = "310";
31+
in flake-utils.lib.eachSystem [
2332
"x86_64-linux"
2433
"x86_64-darwin"
2534
"aarch64-linux"
2635
"aarch64-darwin"
2736
] (system:
28-
let
29-
pkgs = import nixpkgs {
30-
inherit system;
31-
overlays = allOverlays;
37+
let
38+
# due to the nixpkgs that we use in this flake being outdated, uv is also heavily outdated
39+
# we can instead use the binary release of uv provided by uv2nix for now
40+
uvOverlay = final: prev: {
41+
uv = uv2nix.packages.${final.system}.uv-bin;
42+
};
43+
{{ cookiecutter.project_slug }}Overlay = final: prev: {
44+
{{ cookiecutter.project_slug }} = final.callPackage ./nix/{{ cookiecutter.project_slug }} {
45+
inherit pyproject-nix pyproject-build-systems uv2nix;
46+
python = final."python${pythonVer}";
3247
};
33-
in {
34-
packages = rec {
35-
inherit (pkgs) {{ cookiecutter.project_slug }};
36-
default = {{ cookiecutter.project_slug }};
48+
};
49+
pkgs = import nixpkgs {
50+
inherit system;
51+
overlays = [
52+
uvOverlay
53+
{{ cookiecutter.project_slug }}Overlay
54+
];
55+
};
56+
python = pkgs."python${pythonVer}";
57+
in {
58+
devShells.default = pkgs.mkShell {
59+
name = "uv develop shell";
60+
buildInputs = [
61+
python
62+
pkgs.uv
63+
];
64+
env = {
65+
# prevent uv from managing Python downloads and force use of specific
66+
UV_PYTHON_DOWNLOADS = "never";
67+
UV_PYTHON = python.interpreter;
3768
};
38-
}) // {
39-
overlay = nixpkgs.lib.composeManyExtensions allOverlays;
69+
shellHook = ''
70+
unset PYTHONPATH
71+
'';
72+
};
73+
packages = rec {
74+
inherit (pkgs) {{ cookiecutter.project_slug }};
75+
default = {{ cookiecutter.project_slug }};
76+
};
77+
}) // {
78+
overlays.default = final: prev: {
79+
inherit (self.packages.${final.system}) {{ cookiecutter.project_slug }};
4080
};
81+
};
4182
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
final: prev:
2+
let
3+
inherit (final) resolveBuildSystem;
4+
inherit (builtins) mapAttrs;
5+
6+
# Build system dependencies specified in the shape expected by resolveBuildSystem
7+
# The empty lists below are lists of optional dependencies.
8+
#
9+
# A package `foo` with specification written as:
10+
# `setuptools-scm[toml]` in pyproject.toml would be written as
11+
# `foo.setuptools-scm = [ "toml" ]` in Nix
12+
buildSystemOverrides = {
13+
# add dependencies here, e.g.:
14+
# pyperclip.setuptools = [ ];
15+
};
16+
in
17+
mapAttrs (
18+
name: spec:
19+
prev.${name}.overrideAttrs (old: {
20+
nativeBuildInputs = old.nativeBuildInputs ++ resolveBuildSystem spec;
21+
})
22+
) buildSystemOverrides
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
lib,
3+
callPackage,
4+
5+
pyproject-nix,
6+
pyproject-build-systems,
7+
uv2nix,
8+
9+
python
10+
}:
11+
let
12+
pyproject-util = callPackage pyproject-nix.build.util {};
13+
pyproject-packages = callPackage pyproject-nix.build.packages {
14+
inherit python;
15+
};
16+
17+
# load a uv workspace from a workspace root
18+
workspace = uv2nix.lib.workspace.loadWorkspace {
19+
workspaceRoot = ../..;
20+
};
21+
22+
# create overlay
23+
lockFileOverlay = workspace.mkPyprojectOverlay {
24+
# prefer "wheel" over "sdist" due to maintance overhead
25+
# there is no bundled set of overlays for "sdist" in uv2nix, in contrast to poetry2nix
26+
sourcePreference = "wheel";
27+
};
28+
29+
buildSystemsOverlay = import ./build-systems-overlay.nix;
30+
31+
# construct package set
32+
pythonSet = pyproject-packages.overrideScope (lib.composeManyExtensions [
33+
# make build tools available by default as these are not necessarily specified in python lock files
34+
pyproject-build-systems.overlays.default
35+
# include all packages from the python lock file
36+
lockFileOverlay
37+
# add build system overrides to certain python packages
38+
buildSystemsOverlay
39+
]);
40+
in pyproject-util.mkApplication {
41+
# default dependancy group enables no optional dependencies and no dependency-groups
42+
venv = pythonSet.mkVirtualEnv "{{ cookiecutter.project_slug }}-env" workspace.deps.default;
43+
package = pythonSet.{{ cookiecutter.project_slug }};
44+
}

0 commit comments

Comments
 (0)