{ config , lib , pkgs , ... }: let inherit (lib) range filterAttrs attrNames foldl' head tail mkOption mkEnableOption intersectLists genAttrs flip getAttr; inherit (lib.types) listOf enum package; cfg = config.my.programs.python; pythonVersions = map (version: "3${toString version}") (range 9 13); enabledVersions = filterAttrs (_: value: value.enable) cfg.versions; pythonPackages = version: attrNames pkgs."python${version}Packages"; commonPackages = let packageLists = map pythonPackages (attrNames enabledVersions); in foldl' intersectLists (head packageLists) (tail packageLists); versionOpts = version: { enable = mkEnableOption (toString version); pythonPackages = mkOption { default = [ ]; type = listOf (enum (pythonPackages version)); }; }; in { options.my.programs.python = { versions = genAttrs pythonVersions versionOpts; defaultPackages = mkOption { default = [ ]; type = listOf (enum commonPackages); }; extraPackages = mkOption { default = [ ]; type = listOf package; }; }; 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; }; }