dotfiles/modules/programs/python.nix

61 lines
1.6 KiB
Nix
Raw Normal View History

2022-08-31 12:01:07 +02:00
{ config
, lib
, pkgs
, ...
}:
let
2024-06-17 09:59:39 +02:00
inherit (lib) range filterAttrs attrNames foldl' head tail mkOption mkEnableOption intersectLists genAttrs flip getAttr;
inherit (lib.types) listOf enum package;
2022-08-31 12:01:07 +02:00
cfg = config.my.programs.python;
2022-09-01 11:11:11 +02:00
2024-11-22 19:47:22 +01:00
pythonVersions = map (version: "3${toString version}") (range 9 13);
enabledVersions = filterAttrs (_: value: value.enable) cfg.versions;
2022-09-01 11:11:11 +02:00
2023-01-26 10:55:47 +01:00
pythonPackages = version: attrNames pkgs."python${version}Packages";
2022-09-01 11:11:11 +02:00
2023-01-26 10:55:47 +01:00
commonPackages =
let
packageLists = map pythonPackages (attrNames enabledVersions);
in
foldl' intersectLists (head packageLists) (tail packageLists);
2022-09-01 11:11:11 +02:00
2023-01-26 10:55:47 +01:00
versionOpts = version: {
enable = mkEnableOption (toString version);
pythonPackages = mkOption {
default = [ ];
2024-06-17 09:59:39 +02:00
type = listOf (enum (pythonPackages version));
2023-01-26 10:55:47 +01:00
};
};
2022-08-31 12:01:07 +02:00
in
{
options.my.programs.python = {
2023-01-26 10:55:47 +01:00
versions = genAttrs pythonVersions versionOpts;
defaultPackages = mkOption {
default = [ ];
2024-06-17 09:59:39 +02:00
type = listOf (enum commonPackages);
2022-09-01 11:11:11 +02:00
};
2023-01-26 10:55:47 +01:00
extraPackages = mkOption {
default = [ ];
2024-06-17 09:59:39 +02:00
type = listOf package;
2022-08-31 12:01:07 +02:00
};
};
2023-01-26 10:55:47 +01:00
config = {
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;
2022-08-31 12:01:07 +02:00
};
}