made it modular

This commit is contained in:
Arthur Wambst 2025-05-06 13:05:22 +02:00
parent c2300d813b
commit d8a3a9f8a1

160
flake.nix
View File

@ -1,5 +1,5 @@
{ {
description = "Reusable Python development environment"; description = "Customizable Python development environment";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
@ -9,66 +9,120 @@
outputs = { self, nixpkgs, flake-utils }: outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = import nixpkgs { inherit system; }; # Function to create a Python environment with a given version
mkPython = {
# Python version configuration - easy to change in one place pythonVersion ? "python313", # Default Python version
pythonVersion = pkgs.python311; extraPackages ? [], # Additional packages to include
extraSystemPackages ? [] # Additional system packages
# Define Python packages in a separate variable for clarity }:
pythonPackages = ps: with ps; [ let
# Data science pkgs = import nixpkgs { inherit system; };
numpy
pandas
matplotlib
scipy
# Web and networking # Select Python version based on the parameter
requests python = pkgs.${pythonVersion};
httpx
beautifulsoup4
# Development tools # Base Python packages that are always included
black basePackages = ps: with ps; [
pylint # Data science
pytest numpy
ipython pandas
matplotlib
# Add more packages as needed
]; # Development tools
black
# Create the Python environment with the specified packages pytest
pythonEnv = pythonVersion.withPackages pythonPackages; ipython
in
{ # Add more default packages as needed
# Development shell for interactive use
devShells.default = pkgs.mkShell {
buildInputs = [
pythonEnv
pkgs.poetry
pkgs.git
]; ];
shellHook = '' # Combine base packages with extra packages
echo "" allPackages = ps: (basePackages ps) ++ (extraPackages ps);
echo "🐍 Python $(${pythonVersion}/bin/python --version)"
echo "📦 Poetry $(${pkgs.poetry}/bin/poetry --version)" # Create the Python environment
echo "📂 Working directory: $(pwd)" pythonEnv = python.withPackages allPackages;
echo "" in
''; {
# The Python environment
package = pythonEnv;
# Make the chosen Python version accessible
inherit python pythonVersion;
# Development shell with the Python environment
devShell = pkgs.mkShell {
buildInputs = [
pythonEnv
] ++ extraSystemPackages;
shellHook = ''
echo "Python $(${python}/bin/python --version)"
echo "Custom Python environment activated!"
'';
};
};
# Default Python configuration
defaultPython = mkPython {};
in
{
# Expose the function to create custom Python environments
lib = {
inherit mkPython;
}; };
# Make the Python environment available as a package # Default packages using default Python version
packages.default = pythonEnv; packages = {
default = defaultPython.package;
# Add an app that runs Python directly
apps.default = flake-utils.lib.mkApp {
drv = pythonEnv;
name = "python";
exePath = "/bin/python";
}; };
# Add formatter for consistent Nix code formatting # Default development shell
formatter = pkgs.nixpkgs-fmt; devShells.default = defaultPython.devShell;
# NixOS module for system-wide integration
nixosModules.default = { config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.customPython;
in {
options.services.customPython = {
enable = mkEnableOption "Enable the custom Python environment";
pythonVersion = mkOption {
type = types.str;
default = "python311";
description = "Python version to use (e.g., python39, python310, python311)";
};
extraPackages = mkOption {
type = types.functionTo (types.listOf types.anything);
default = _: [];
description = "Extra Python packages to include";
};
extraSystemPackages = mkOption {
type = types.listOf types.package;
default = [];
description = "Extra system packages to include";
};
includeInSystemPackages = mkOption {
type = types.bool;
default = true;
description = "Whether to include the Python environment in system packages";
};
};
config = mkIf cfg.enable {
environment.systemPackages = mkIf cfg.includeInSystemPackages [
(mkPython {
pythonVersion = cfg.pythonVersion;
extraPackages = cfg.extraPackages;
extraSystemPackages = cfg.extraSystemPackages;
}).package
];
};
};
} }
); );
} }