|
1 | 1 | { |
2 | | - description = "A Nix flake-based development environment for working with stats_can"; |
| 2 | + description = "Stats Can flake using uv2nix"; |
3 | 3 |
|
4 | | - inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; |
| 6 | + |
| 7 | + pyproject-nix = { |
| 8 | + url = "github:pyproject-nix/pyproject.nix"; |
| 9 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 10 | + }; |
| 11 | + |
| 12 | + uv2nix = { |
| 13 | + url = "github:pyproject-nix/uv2nix"; |
| 14 | + inputs.pyproject-nix.follows = "pyproject-nix"; |
| 15 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 16 | + }; |
| 17 | + |
| 18 | + pyproject-build-systems = { |
| 19 | + url = "github:pyproject-nix/build-system-pkgs"; |
| 20 | + inputs.pyproject-nix.follows = "pyproject-nix"; |
| 21 | + inputs.uv2nix.follows = "uv2nix"; |
| 22 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 23 | + }; |
| 24 | + }; |
5 | 25 |
|
6 | 26 | outputs = |
7 | | - { self, nixpkgs }: |
| 27 | + { |
| 28 | + self, |
| 29 | + nixpkgs, |
| 30 | + uv2nix, |
| 31 | + pyproject-nix, |
| 32 | + pyproject-build-systems, |
| 33 | + ... |
| 34 | + }: |
8 | 35 | let |
9 | | - supportedSystems = [ |
10 | | - "x86_64-linux" |
11 | | - "aarch64-linux" |
12 | | - "x86_64-darwin" |
13 | | - "aarch64-darwin" |
14 | | - ]; |
15 | | - forEachSupportedSystem = |
16 | | - f: |
17 | | - nixpkgs.lib.genAttrs supportedSystems ( |
18 | | - system: |
19 | | - f { |
20 | | - pkgs = import nixpkgs { |
21 | | - inherit system; |
22 | | - config.allowUnfree = true; |
23 | | - }; |
24 | | - } |
25 | | - ); |
| 36 | + inherit (nixpkgs) lib; |
| 37 | + |
| 38 | + workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; }; |
| 39 | + |
| 40 | + overlay = workspace.mkPyprojectOverlay { |
| 41 | + sourcePreference = "wheel"; |
| 42 | + }; |
| 43 | + |
| 44 | + pyprojectOverrides = _final: _prev: { |
| 45 | + # Implement build fixups here. |
| 46 | + }; |
| 47 | + |
| 48 | + pkgs = nixpkgs.legacyPackages.x86_64-linux; |
| 49 | + |
| 50 | + pythonVersions = { |
| 51 | + python310 = pkgs.python310; |
| 52 | + python311 = pkgs.python311; |
| 53 | + python312 = pkgs.python312; |
| 54 | + python313 = pkgs.python313; |
| 55 | + default = pkgs.python313; # This will create a 'default' shell |
| 56 | + }; |
| 57 | + |
| 58 | + # --- CHANGED: A function to generate a dev shell for a given Python version --- |
| 59 | + # This function encapsulates all the logic that was previously hardcoded for a single Python version. |
| 60 | + mkPythonDevShell = |
| 61 | + python: |
| 62 | + let |
| 63 | + # 'pythonSet' is now defined inside this function, using the 'python' argument. |
| 64 | + pythonSet = |
| 65 | + (pkgs.callPackage pyproject-nix.build.packages { |
| 66 | + inherit python; |
| 67 | + }).overrideScope |
| 68 | + ( |
| 69 | + lib.composeManyExtensions [ |
| 70 | + pyproject-build-systems.overlays.default |
| 71 | + overlay |
| 72 | + pyprojectOverrides |
| 73 | + ] |
| 74 | + ); |
| 75 | + |
| 76 | + # The rest of the shell-building logic is also moved inside. |
| 77 | + editableOverlay = workspace.mkEditablePyprojectOverlay { |
| 78 | + root = "$REPO_ROOT"; |
| 79 | + }; |
| 80 | + |
| 81 | + editablePythonSet = pythonSet.overrideScope ( |
| 82 | + lib.composeManyExtensions [ |
| 83 | + editableOverlay |
| 84 | + (final: prev: { |
| 85 | + stats-can = prev.stats-can.overrideAttrs (old: { |
| 86 | + src = lib.fileset.toSource { |
| 87 | + root = old.src; |
| 88 | + fileset = lib.fileset.unions [ |
| 89 | + (old.src + "/pyproject.toml") |
| 90 | + (old.src + "/README.md") |
| 91 | + (old.src + "/src/stats_can/__init__.py") |
| 92 | + ]; |
| 93 | + }; |
| 94 | + nativeBuildInputs = |
| 95 | + old.nativeBuildInputs |
| 96 | + ++ final.resolveBuildSystem { |
| 97 | + editables = [ ]; |
| 98 | + }; |
| 99 | + }); |
| 100 | + }) |
| 101 | + ] |
| 102 | + ); |
| 103 | + |
| 104 | + virtualenv = editablePythonSet.mkVirtualEnv "stats-can-dev-env" workspace.deps.all; |
| 105 | + |
| 106 | + in |
| 107 | + pkgs.mkShell { |
| 108 | + packages = [ |
| 109 | + virtualenv |
| 110 | + pkgs.uv |
| 111 | + ]; |
| 112 | + |
| 113 | + env = { |
| 114 | + UV_NO_SYNC = "1"; |
| 115 | + # Use the 'python' argument to set the interpreter path. |
| 116 | + UV_PYTHON = python.interpreter; |
| 117 | + UV_PYTHON_DOWNLOADS = "never"; |
| 118 | + }; |
| 119 | + |
| 120 | + shellHook = '' |
| 121 | + unset PYTHONPATH |
| 122 | + export REPO_ROOT=$(git rev-parse --show-toplevel) |
| 123 | + ''; |
| 124 | + }; |
| 125 | + |
26 | 126 | in |
27 | 127 | { |
28 | | - devShells = forEachSupportedSystem ( |
29 | | - { pkgs }: |
30 | | - { |
31 | | - default = pkgs.mkShell { |
32 | | - packages = with pkgs; [ |
33 | | - uv |
34 | | - migrate-to-uv |
35 | | - ]; |
36 | | - }; |
37 | | - } |
38 | | - ); |
| 128 | + # --- CHANGED: Generate multiple dev shells using lib.mapAttrs --- |
| 129 | + # We map over the 'pythonVersions' set. For each entry, we call our 'mkPythonDevShell' function. |
| 130 | + # The key of the entry (e.g., "python312") becomes the name of the shell. |
| 131 | + devShells.x86_64-linux = lib.mapAttrs (_name: pythonPkg: mkPythonDevShell pythonPkg) pythonVersions; |
39 | 132 | }; |
40 | 133 | } |
0 commit comments