{ lib }: url: let # strip ?query/#fragment and trailing / u0 = builtins.head (builtins.split "[?#]" url); u = lib.removeSuffix "/" u0; # GitHub: https://github.com/owner/repo/(tree|tag|commit)/rev/(optional/subdir) gh = builtins.match "https://github.com/([^/]+)/([^/]+)(/(tree|tag|commit)/([^/]+)(/(.*))?)?" u; # PyPI project page: https://pypi.org/project/name/version pypiProject = builtins.match "https://pypi.org/project/([^/]+)(/([^/]+))?" u; # PyPI short form: # pypi:requests@2.32.3 # pypi:requests==2.32.3 # pypi:requests/2.32.3 pypiShort = builtins.match "^pypi:([^@=/]+)(==|@|/)?([^/]+)?$" u; in if gh != null then let owner = builtins.elemAt gh 0; repo = builtins.elemAt gh 1; kind = builtins.elemAt gh 3; # null if absent rev = builtins.elemAt gh 4; # null if absent subdir = builtins.elemAt gh 6; # null or "a/b" version = if rev == null then null else let vm = builtins.match "^.*([0-9]+(\\.[0-9]+)+).*$" rev; in if vm == null then null else builtins.elemAt vm 0; in { type = "github"; pname = repo; homepage = "https://github.com/${owner}/${repo}"; inherit owner repo kind rev subdir version ; } else if pypiShort != null then let pname = builtins.elemAt pypiShort 0; version = builtins.elemAt pypiShort 2; # may be null in { type = "pypi"; homepage = "https://pypi.org/project/${pname}/"; inherit pname version; subdir = null; } else if pypiProject != null then let pname = builtins.elemAt pypiProject 0; version = builtins.elemAt pypiProject 2; # may be null in { type = "pypi"; homepage = "https://pypi.org/project/${pname}/"; inherit pname version; subdir = null; } else throw "parse-source-ref: unsupported url: ${url}"