templates: add uv-python

This commit is contained in:
2026-06-19 17:52:34 +02:00
parent 66c7fc1382
commit 78441fb2d5
3 changed files with 131 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# ^ make editor happy
#
# Use https://direnv.net/ to automatically load the dev shell.
#
if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
fi
watch_file nix/**
watch_file -- **/*.nix
watch_file uv.lock
# Adding files to git includes them in a flake
# But it is also a bit much reloading.
# watch_file .git/index .git/HEAD
use flake . --show-trace
+95
View File
@@ -0,0 +1,95 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
pyproject-nix.url = "github:pyproject-nix/pyproject.nix";
pyproject-nix.inputs.nixpkgs.follows = "nixpkgs";
uv2nix.url = "github:pyproject-nix/uv2nix";
uv2nix.inputs.pyproject-nix.follows = "pyproject-nix";
uv2nix.inputs.nixpkgs.follows = "nixpkgs";
pyproject-build-systems.url = "github:pyproject-nix/build-system-pkgs";
pyproject-build-systems.inputs.pyproject-nix.follows = "pyproject-nix";
pyproject-build-systems.inputs.uv2nix.follows = "uv2nix";
pyproject-build-systems.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs @ {
flake-parts,
pyproject-nix,
uv2nix,
pyproject-build-systems,
...
}: let
projectName = "YOUR_PROJECT_NAME"; # TODO: Fill in your project name
in
flake-parts.lib.mkFlake {inherit inputs;} {
systems = ["x86_64-linux"];
perSystem = {
pkgs,
self',
...
}: let
workspace = uv2nix.lib.workspace.loadWorkspace {workspaceRoot = ./.;};
python = pkgs.lib.head (pyproject-nix.lib.util.filterPythonInterpreters {
inherit (workspace) requires-python;
inherit (pkgs) pythonInterpreters;
});
overlay = workspace.mkPyprojectOverlay {
sourcePreference = "wheel";
};
pythonBase = pkgs.callPackage pyproject-nix.build.packages {
inherit python;
};
pythonSet = pythonBase.overrideScope (
pkgs.lib.composeManyExtensions [
pyproject-build-systems.overlays.wheel
overlay
]
);
editableOverlay = workspace.mkEditablePyprojectOverlay {
root = "$REPO_ROOT";
};
editablePythonSet = pythonSet.overrideScope editableOverlay;
devvenv = editablePythonSet.mkVirtualEnv "${projectName}-dev-env" workspace.deps.all;
venv = pythonSet.mkVirtualEnv "${projectName}-env" workspace.deps.default;
in {
packages.default = self'.packages.${projectName};
packages.${projectName} = (pkgs.callPackage pyproject-nix.build.util {}).mkApplication {
inherit venv;
package = pythonSet.${projectName};
};
apps.default = self'.apps.${projectName};
apps.${projectName} = {
type = "app";
program = "${self'.packages.default}/bin/${projectName}";
};
devShells.default = pkgs.mkShell {
packages = [
devvenv
pkgs.uv
];
env = {
UV_NO_SYNC = "1";
UV_PYTHON = editablePythonSet.python.interpreter;
UV_PYTHON_DOWNLOADS = "never";
};
shellHook = ''
unset PYTHONPATH
export REPO_ROOT=$(git rev-parse --show-toplevel)
'';
};
devShells.uv = pkgs.mkShell {
packages = with pkgs; [python3 uv];
};
};
};
}
+17
View File
@@ -0,0 +1,17 @@
_: {
flake.templates.uv-python = {
path = ./_uv-python;
description = "A simple uv2nix python project";
welcomeText = ''
# Setup your uv project first
- Run `nix develop .#uv`
- Run `uv init --app --packages`
- Run `uv lock`
- Edit `projectName` in flake.nix
# Start developing
- Run `direnv allow` or `nix develop`
- Add packages with `uv add --dev black pyright`
'';
};
}