rework python module

nixos
Moritz Böhme 2023-01-26 10:55:47 +01:00
parent eb101fbcf2
commit c78d9e9e24
2 changed files with 41 additions and 27 deletions

View File

@ -78,7 +78,7 @@ with lib; {
hub.enable = true; hub.enable = true;
ledger.enable = true; ledger.enable = true;
logseq.enable = true; logseq.enable = true;
python.enable = true; python.versions."311".enable = true;
ssh = { ssh = {
enable = true; enable = true;
includeSecrets = [ ../../secrets/ssh-home.age ]; includeSecrets = [ ../../secrets/ssh-home.age ];

View File

@ -8,41 +8,55 @@ with lib;
let let
cfg = config.my.programs.python; cfg = config.my.programs.python;
mkPython = packages: version: pkgs.${version}.withPackages (ps: map (flip getAttr ps) packages); pythonVersions = map (version: "3${toString version}") (range 8 11);
enabledVersions = filterAttrs (name: value: value.enable) cfg.versions;
pythonVersions = [ pythonPackages = version: attrNames pkgs."python${version}Packages";
"python311"
"python310"
"python39"
"python38"
];
packageLists = map (version: attrNames pkgs."${version}Packages") cfg.versions; commonPackages =
let
packageLists = map pythonPackages (attrNames enabledVersions);
in
foldl' intersectLists (head packageLists) (tail packageLists);
commonPackages = foldl' intersectLists (head packageLists) (tail packageLists); versionOpts = version: {
enable = mkEnableOption (toString version);
pythonPackages = mkOption {
default = [ ];
type = with types; listOf (enum (pythonPackages version));
};
};
in in
{ {
options.my.programs.python = { options.my.programs.python = {
enable = mkEnableOption "python"; versions = genAttrs pythonVersions versionOpts;
versions = mkOption { defaultPackages = mkOption {
default = [ "python310" ]; default = [ ];
type = with types; nonEmptyListOf (enum pythonVersions);
example = [ "python39" ];
};
packages = mkOption {
default = [
"flake8"
"isort"
"mypy"
"poetry"
"pytest"
"python-lsp-server"
];
type = with types; listOf (enum commonPackages); type = with types; listOf (enum commonPackages);
}; };
extraPackages = mkOption {
default = with pkgs; [
poetry
ruff
python310Packages.python-lsp-server
];
type = with types; listOf package;
};
}; };
config = mkIf cfg.enable { config = {
users.users.moritz.packages = map (mkPython cfg.packages) cfg.versions; users.users.moritz.packages =
(
let
mkPython = version:
let
package = pkgs."python${version}";
finalPythonPackages = cfg.versions.${version}.pythonPackages ++ cfg.defaultPackages;
getPythonPackages = ps: map (flip getAttr ps) finalPythonPackages;
in
package.withPackages getPythonPackages;
in
map mkPython (attrNames enabledVersions)
) ++ cfg.extraPackages;
}; };
} }