Skip to content

Commit

Permalink
feat: expose python flake to all systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Swarsel committed Dec 31, 2024
1 parent 5915a28 commit fbc1343
Showing 1 changed file with 74 additions and 61 deletions.
135 changes: 74 additions & 61 deletions templates/python/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}:
let
inherit (nixpkgs) lib;
forAllSystems = lib.genAttrs lib.systems.flakeExposed;

# Load a uv workspace from a workspace root.
# Uv2nix treats all uv projects as workspace projects.
Expand All @@ -55,80 +56,92 @@
# This is an additional overlay implementing build fixups.
# See:
# - https://pyproject-nix.github.io/uv2nix/FAQ.html
pyprojectOverrides = _final: _prev: {
# Implement build fixups here.
};

pkgs = nixpkgs.legacyPackages.x86_64-linux;
python = pkgs.python312;

# Construct package set
pythonSet =
# Use base package set from pyproject.nix builders
(pkgs.callPackage pyproject-nix.build.packages {
inherit python;
}).overrideScope
(
lib.composeManyExtensions [
pyproject-build-systems.overlays.default
overlay
pyprojectOverrides
]
);
pythonSets = forAllSystems
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
pyprojectOverrides = _final: _prev: {
# Implement build fixups here.
};
baseSet = pkgs.callPackage pyproject-nix.build.packages {
python = pkgs.python312;
};
in
# Use base package set from pyproject.nix builders
baseSet.overrideScope
(
lib.composeManyExtensions [
pyproject-build-systems.overlays.default
overlay
pyprojectOverrides
]
));

in
{
# Package a virtual environment as our main application.
#
# Enable no optional dependencies for production build.
packages.x86_64-linux.default = pythonSet.mkVirtualEnv "name-env" workspace.deps.default;
packages = forAllSystems (system:
let
pythonSet = pythonSets.${system};
in
{ default = pythonSet.mkVirtualEnv "name-env" workspace.deps.default; });

# This example provides two different modes of development:
# - Impurely using uv to manage virtual environments
# - Pure development using uv2nix to manage virtual environments
devShells.x86_64-linux = {
# This devShell uses uv2nix to construct a virtual environment purely from Nix, using the same dependency specification as the application.
# The notable difference is that we also apply another overlay here enabling editable mode ( https://setuptools.pypa.io/en/latest/userguide/development_mode.html ).
#
# This means that any changes done to your local files do not require a rebuild.
default =
devShells = forAllSystems
(system:
let
# Create an overlay enabling editable mode for all local dependencies.
editableOverlay = workspace.mkEditablePyprojectOverlay {
# Use environment variable
root = "$REPO_ROOT";
# Optional: Only enable editable for these packages
# members = [ "hello-world" ];
};

# Override previous set with our overrideable overlay.
editablePythonSet = pythonSet.overrideScope editableOverlay;

# Build virtual environment, with local packages being editable.
#
# Enable all optional dependencies for development.
virtualenv = editablePythonSet.mkVirtualEnv "name-dev-env" workspace.deps.all;

pythonSet = pythonSets.${system};
pkgs = nixpkgs.legacyPackages.${system};
in
pkgs.mkShell {
packages = [
virtualenv
pkgs.uv
];
shellHook = ''
# Undo dependency propagation by nixpkgs.
unset PYTHONPATH
# Don't create venv using uv
export UV_NO_SYNC=1
# Prevent uv from downloading managed Python's
export UV_PYTHON_DOWNLOADS=never
# Get repository root using git. This is expanded at runtime by the editable `.pth` machinery.
export REPO_ROOT=$(git rev-parse --show-toplevel)
'';
};
};
{
# This devShell uses uv2nix to construct a virtual environment purely from Nix, using the same dependency specification as the application.
# The notable difference is that we also apply another overlay here enabling editable mode ( https://setuptools.pypa.io/en/latest/userguide/development_mode.html ).
#
# This means that any changes done to your local files do not require a rebuild.
default =
let
# Create an overlay enabling editable mode for all local dependencies.
editableOverlay = workspace.mkEditablePyprojectOverlay {
# Use environment variable
root = "$REPO_ROOT";
# Optional: Only enable editable for these packages
# members = [ "hello-world" ];
};

# Override previous set with our overrideable overlay.
editablePythonSet = pythonSet.overrideScope editableOverlay;

# Build virtual environment, with local packages being editable.
#
# Enable all optional dependencies for development.
virtualenv = editablePythonSet.mkVirtualEnv "name-dev-env" workspace.deps.all;

in
pkgs.mkShell {
packages = [
virtualenv
pkgs.uv
];
shellHook = ''
# Undo dependency propagation by nixpkgs.
unset PYTHONPATH
# Don't create venv using uv
export UV_NO_SYNC=1
# Prevent uv from downloading managed Python's
export UV_PYTHON_DOWNLOADS=never
# Get repository root using git. This is expanded at runtime by the editable `.pth` machinery.
export REPO_ROOT=$(git rev-parse --show-toplevel)
'';
};
});
};
}

0 comments on commit fbc1343

Please sign in to comment.