made it modular
This commit is contained in:
parent
c2300d813b
commit
d8a3a9f8a1
126
flake.nix
126
flake.nix
@ -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";
|
||||||
@ -8,67 +8,121 @@
|
|||||||
|
|
||||||
outputs = { self, nixpkgs, flake-utils }:
|
outputs = { self, nixpkgs, flake-utils }:
|
||||||
flake-utils.lib.eachDefaultSystem (system:
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
|
let
|
||||||
|
# Function to create a Python environment with a given version
|
||||||
|
mkPython = {
|
||||||
|
pythonVersion ? "python313", # Default Python version
|
||||||
|
extraPackages ? [], # Additional packages to include
|
||||||
|
extraSystemPackages ? [] # Additional system packages
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { inherit system; };
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
|
||||||
# Python version configuration - easy to change in one place
|
# Select Python version based on the parameter
|
||||||
pythonVersion = pkgs.python311;
|
python = pkgs.${pythonVersion};
|
||||||
|
|
||||||
# Define Python packages in a separate variable for clarity
|
# Base Python packages that are always included
|
||||||
pythonPackages = ps: with ps; [
|
basePackages = ps: with ps; [
|
||||||
# Data science
|
# Data science
|
||||||
numpy
|
numpy
|
||||||
pandas
|
pandas
|
||||||
matplotlib
|
matplotlib
|
||||||
scipy
|
|
||||||
|
|
||||||
# Web and networking
|
|
||||||
requests
|
|
||||||
httpx
|
|
||||||
beautifulsoup4
|
|
||||||
|
|
||||||
# Development tools
|
# Development tools
|
||||||
black
|
black
|
||||||
pylint
|
|
||||||
pytest
|
pytest
|
||||||
ipython
|
ipython
|
||||||
|
|
||||||
# Add more packages as needed
|
# Add more default packages as needed
|
||||||
];
|
];
|
||||||
|
|
||||||
# Create the Python environment with the specified packages
|
# Combine base packages with extra packages
|
||||||
pythonEnv = pythonVersion.withPackages pythonPackages;
|
allPackages = ps: (basePackages ps) ++ (extraPackages ps);
|
||||||
|
|
||||||
|
# Create the Python environment
|
||||||
|
pythonEnv = python.withPackages allPackages;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
# Development shell for interactive use
|
# The Python environment
|
||||||
devShells.default = pkgs.mkShell {
|
package = pythonEnv;
|
||||||
|
|
||||||
|
# Make the chosen Python version accessible
|
||||||
|
inherit python pythonVersion;
|
||||||
|
|
||||||
|
# Development shell with the Python environment
|
||||||
|
devShell = pkgs.mkShell {
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
pythonEnv
|
pythonEnv
|
||||||
pkgs.poetry
|
] ++ extraSystemPackages;
|
||||||
pkgs.git
|
|
||||||
];
|
|
||||||
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
echo "Python $(${python}/bin/python --version)"
|
||||||
echo "🐍 Python $(${pythonVersion}/bin/python --version)"
|
echo "Custom Python environment activated!"
|
||||||
echo "📦 Poetry $(${pkgs.poetry}/bin/poetry --version)"
|
|
||||||
echo "📂 Working directory: $(pwd)"
|
|
||||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
# Make the Python environment available as a package
|
|
||||||
packages.default = pythonEnv;
|
|
||||||
|
|
||||||
# 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 Python configuration
|
||||||
formatter = pkgs.nixpkgs-fmt;
|
defaultPython = mkPython {};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
# Expose the function to create custom Python environments
|
||||||
|
lib = {
|
||||||
|
inherit mkPython;
|
||||||
|
};
|
||||||
|
|
||||||
|
# Default packages using default Python version
|
||||||
|
packages = {
|
||||||
|
default = defaultPython.package;
|
||||||
|
};
|
||||||
|
|
||||||
|
# Default development shell
|
||||||
|
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
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user