justinwoo / spago2nix

Generate a derivation of (old) Spago dependencies, and use them to install them into the directory structure used by Spago.
MIT License
42 stars 23 forks source link

Private packages cannot be fetched using `pkgs.fetchgit` #54

Open aciceri opened 1 year ago

aciceri commented 1 year ago

I've some private packages in my packages.dhall with the following repo:

{
  # ...
  , repo = "ssh://git@github.com/my-org/my-repo.git"
  , version = "<rev>";
}

spago2nix generate gives me derivations with srcs looking like:

src = pkgs.fetchgit {
  url = "https://github.com/my-org/my-repo.git";
  rev = "<rev>";
  sha256 = "<hash>";
}

But pkgs.fetchgit cannot download private repos. My solution was to create a wrapped-spago-packages.nix that wraps the automatically generated spago-pacakges.nix using builtins.fetchGit instead of pkgs.fetchgit for predefined list of repos i.e. the private repos.

# wrapped-spago-packages.nix
{ pkgs }:
let
  patchedPkgs = pkgs // {
    fetchgit = args:
      let
        regex = ".*/(.*)/([^.]*).*";
        matches = builtins.match regex args.url;
        owner = builtins.elemAt matches 0;
        repo = builtins.elemAt matches 1;
        condition = owner == "my-org" && builtins.elem repo [
          "my-repo"
        ];
      in
      if condition then
        builtins.fetchGit
          {
            inherit (args) rev;
            url = "ssh://git@github.com/${owner}/${repo}.git";
          }
      else pkgs.fetchgit args;
  };
in
import ./spago-packages.nix { pkgs = patchedPkgs; }

And then, wherever I was using ./spago-packages.nix I use ./wrapped-spago-packages.nix instead.

Said this, one obvious solution would be directly generating ./spago-packages.nix using builtins.fetchGit instead of pkgs.fetchgit but I'm not sure about consequences. As far as I know builtins.fetchGit doesn't return a derivation but directly a path, that means that the fetching is done at eval time and fetching all of this stuff at eval time could be a not good thing. Perhaps there should be an spago2nix's argument to let people choose what they want to do?

@justinwoo If you agree about this and you want to give me clear directives i would be happy to open a PR.

justinwoo commented 1 year ago

seems like at least a first step would be to optionally generate with fetchGit with a flag then. i think your point of difference of when the fetch happens is a good point also, but maybe that's the preferred behavior with this.