poetry-netrc/lib.nix

36 lines
1.2 KiB
Nix

{ pkgs ? import <nixpkgs> { }
, lib ? pkgs.lib
}:
{
genNetrc =
{ projectDir ? null
, poetry ? projectDir + "/poetry.toml"
, pyproject ? projectDir + "/pyproject.toml"
}:
let
poetryToml = builtins.fromTOML (builtins.readFile poetry);
pyprojectToml = builtins.fromTOML (builtins.readFile pyproject);
sourceNames = builtins.attrNames poetryToml.http-basic;
authSources =
let
sourceNeedsAuth = { name, ... }: builtins.elem name sourceNames;
in
builtins.filter sourceNeedsAuth pyprojectToml.tool.poetry.source;
mergedSources = map (source: source // (builtins.getAttr source.name poetryToml.http-basic)) authSources;
mkMachine = url:
let
matches = builtins.split "(https?://)([^/]*)(.*)" url;
mainMatch = builtins.elemAt matches 1;
domain = builtins.elemAt mainMatch 1;
in
domain;
mkEntry = { url, username, password, ... }:
"machine ${mkMachine url} login ${username} password ${password}";
entries = map mkEntry mergedSources;
contents = lib.concatLines entries;
in
pkgs.writeText "netrc" contents;
}