This commit is contained in:
Arthur Wambst 2025-05-06 13:54:27 +02:00
parent 227a920266
commit 17ddcada9a

View File

@ -10,67 +10,78 @@
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
# Function to create a Python environment with a given version # Function to create a Python environment with a given version
mkPython = { mkPython = {
pythonVersion ? "python313", # Default Python version pythonVersion ? "python313", # Default Python version
extraPackages ? [], # Additional packages to include extraPackages ? [], # Additional packages to include
extraSystemPackages ? [] # Additional system packages extraSystemPackages ? [] # Additional system packages
}: }:
let let
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
# Select Python version based on the parameter # Select Python version based on the parameter
python = pkgs.${pythonVersion}; python = pkgs.${pythonVersion};
handlePackageCompatibility = pythonPackages:
let
fixSphinx = ps:
if ps ? sphinx
then ps.sphinx.overridePythonAttrs (old: { version = "7.2.6"; })
else ps.sphinx;
in
pythonPackages;
# Define package overrides for specific Python versions
packageOverrides = self: super: {
# Fix Sphinx for Python 3.10
sphinx = if pythonVersion == "python310" then
super.sphinx.overridePythonAttrs (old: {
version = "7.2.6"; # Use a version known to work with Python 3.10
doCheck = false; # Skip tests to avoid compatibility issues
})
else
super.sphinx;
# Add more package overrides as needed
};
# Create Python with package overrides
pythonWithOverrides = python.override {
packageOverrides = packageOverrides;
};
# Base Python packages that are always included # Base Python packages that are always included
basePackages = ps: with ps; [ basePackages = ps: with ps; [
# Data science # Data science
numpy numpy
pandas pandas
matplotlib matplotlib
# Development tools # Development tools
black black
pytest pytest
ipython ipython
# Add more default packages as needed # Add more default packages as needed
]; ];
# Combine base packages with extra packages # Combine base packages with extra packages
allPackages = ps: (basePackages ps) ++ (extraPackages ps); allPackages = ps: (basePackages ps) ++ (extraPackages ps);
# Create the Python environment # Create the Python environment using the Python with overrides
pythonEnv = python.withPackages allPackages; pythonEnv = pythonWithOverrides.withPackages allPackages;
in in
{ {
# The Python environment # The Python environment
package = pythonEnv; package = pythonEnv;
# Make the chosen Python version accessible # Make the chosen Python version accessible
inherit python pythonVersion; python = pythonWithOverrides;
inherit pythonVersion;
# Development shell with the Python environment # Development shell with the Python environment
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
buildInputs = [ buildInputs = [
pythonEnv pythonEnv
] ++ extraSystemPackages; ] ++ extraSystemPackages;
shellHook = '' shellHook = ''
echo "Python $(${python}/bin/python --version)" echo "Python $(${pythonWithOverrides}/bin/python --version)"
echo "Custom Python environment activated!" echo "Custom Python environment activated!"
''; '';
}; };
}; };
# Default Python configuration # Default Python configuration
defaultPython = mkPython {}; defaultPython = mkPython {};
in in
@ -79,49 +90,49 @@
lib = { lib = {
inherit mkPython; inherit mkPython;
}; };
# Default packages using default Python version # Default packages using default Python version
packages = { packages = {
default = defaultPython.package; default = defaultPython.package;
}; };
# Default development shell # Default development shell
devShells.default = defaultPython.devShell; devShells.default = defaultPython.devShell;
# NixOS module for system-wide integration # NixOS module for system-wide integration
nixosModules.default = { config, lib, pkgs, ... }: nixosModules.default = { config, lib, pkgs, ... }:
with lib; with lib;
let let
cfg = config.services.customPython; cfg = config.services.customPython;
in { in {
options.services.customPython = { options.services.customPython = {
enable = mkEnableOption "Enable the custom Python environment"; enable = mkEnableOption "Enable the custom Python environment";
pythonVersion = mkOption { pythonVersion = mkOption {
type = types.str; type = types.str;
default = "python311"; default = "python311";
description = "Python version to use (e.g., python39, python310, python311)"; description = "Python version to use (e.g., python39, python310, python311)";
}; };
extraPackages = mkOption { extraPackages = mkOption {
type = types.functionTo (types.listOf types.anything); type = types.functionTo (types.listOf types.anything);
default = _: []; default = _: [];
description = "Extra Python packages to include"; description = "Extra Python packages to include";
}; };
extraSystemPackages = mkOption { extraSystemPackages = mkOption {
type = types.listOf types.package; type = types.listOf types.package;
default = []; default = [];
description = "Extra system packages to include"; description = "Extra system packages to include";
}; };
includeInSystemPackages = mkOption { includeInSystemPackages = mkOption {
type = types.bool; type = types.bool;
default = true; default = true;
description = "Whether to include the Python environment in system packages"; description = "Whether to include the Python environment in system packages";
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.systemPackages = mkIf cfg.includeInSystemPackages [ environment.systemPackages = mkIf cfg.includeInSystemPackages [
(mkPython { (mkPython {