Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for buildGoModule #3

Closed
Mic92 opened this issue May 31, 2019 · 10 comments
Closed

Support for buildGoModule #3

Mic92 opened this issue May 31, 2019 · 10 comments

Comments

@Mic92
Copy link
Contributor

Mic92 commented May 31, 2019

Thanks for writing this tool. It would be create if it would also support modSha256 of our new buildGoModule function.
cc @kalbasit

@msteen
Copy link
Owner

msteen commented May 31, 2019

I have not tested that function myself yet, but from scanning the source code of the function, it seems like you should be able to do something similar as for Rust crates, where there is an attribute containing the fixed-output derivation that uses the sha attribute: nix-prefetch hello_rs.cargoDeps, which for the go modules seems to be: nix-prefetch <name>.go-modules. Can you verify this?

@Mic92
Copy link
Contributor Author

Mic92 commented May 31, 2019

Yes. This is the way it works:

$ nix repl '<nixpkgs>'
nix-repl> pet.go-modules.out
«derivation /nix/store/pbwbkk0y9rl4cjzwkrlpp677ixr0jsqc-pet-0.3.4-go-modules.drv»
nix-repl> :b pet.go-modules.out
[1 copied (5.9 MiB), 5.2 MiB DL]

this derivation produced the following outputs:
  out -> /nix/store/hvjhl6fs576jdb6rfnxrf0zmdm36574s-pet-0.3.4-go-modules

/nix/store/hvjhl6fs576jdb6rfnxrf0zmdm36574s-pet-0.3.4-go-modules in that case contains all go modules required for the build.

@msteen
Copy link
Owner

msteen commented May 31, 2019

I looked into it, but unlike Rust's cargo dependencies, the way the buildGoModule function is implemented, makes it impossible for the current implementation of nix-prefetch to handle it, because nix-prefetch replaces function calls, and expects that function call to have a standard (e.g. sha256, not modSha256) hash argument. I guess I will have to add an option to provide a custom hash argument name to handle these kind of cases. I will add it to my TODO and think some more about it. Thanks for reporting this!

@Mic92
Copy link
Contributor Author

Mic92 commented May 31, 2019

Would it help to export an sha256 attribute?

@msteen
Copy link
Owner

msteen commented May 31, 2019

If go-modules was defined via some fetcher function rather than directly as part of the package itself, then nix-prefetch would be able to handle it. Rust's cargoDeps is the most similar to go-modules and nix-prefetch works for them because they make a fetcher call for them, which nix-prefetch then can hijack.

I have an idea on how to make nix-prefetch more flexible to be able to handle buildGoModule as it is implemented at the moment, but I don't know when I have to time to attempt to implement this.

If it has high priority for you and you don't mind changing the code behind buildGoModule, then you will have to restructure it such that there is a way for nix-prefetch to hijack the call to the function building the fixed-output derivation for go-modules, which at the moment simply does not exist. The buildGoModule function creates both the package and the fixed-output derivation for the modules. To make it work, the fixed-output derivation should be build by some function (accepting a hash argument, i.e. a fetcher function) that can be hijacked by nix-prefetch (i.e. is put it in some file, like fetchcargo.nix, is introduced as an argument, like fetchurl).

@kalbasit
Copy link

kalbasit commented Jun 1, 2019

@msteen that does not sound too difficult of a task. @Mic92 would you be able to take a stab at it? Otherwise, I might have some capacity to get a PR together tomorrow/Sunday.

@Mic92
Copy link
Contributor Author

Mic92 commented Jun 1, 2019

@kalbasit Sorry. I am busy on the weekend.

@msteen
Copy link
Owner

msteen commented Jun 1, 2019

I did found a way to make nix-prefetch work for go-modules without changing a thing in either buildGoModule or nix-prefetch:

> nix-prefetch --file 'fetchTarball "channel:nixos-unstable"' '{ sha256 }: pet.go-modules.overrideAttrs (_: { modSha256 = sha256; })'
06ham8lsx5c1vk5jkwp1aa9g4q4g7sfq7gxz2gkffa98x2vlawyf

I verified the result with:

> nix eval '(with import (fetchTarball "channel:nixos-unstable") { }; pet.go-modules.outputHash)'
"06ham8lsx5c1vk5jkwp1aa9g4q4g7sfq7gxz2gkffa98x2vlawyf"

This works because nix-prefetch support any fetcher function, even those defined on-the-fly, as long as it takes the hash as an argument and produces a fixed-output derivation. So I leveraged that to create a fetcher function specialized to just fetch pet.go-modules.

@msteen
Copy link
Owner

msteen commented Jun 5, 2019

I am closing this issue, because I don't think nix-prefetch can do any better than nix-prefetch '{ sha256 }: pet.go-modules.overrideAttrs (_: { modSha256 = sha256; })' without hard coding specialized support for it, in which case it would be better to just modify buildGoModule instead, as described before.

@xvello
Copy link

xvello commented Jul 8, 2021

It might be obvious to initiated people, but for those trying to run this on a private package (with a default.nix holding the derivation), you can run:

On nixpkgs 23.11 and later

nix-prefetch "{ sha256 }: (callPackage (import ./default.nix) { }).goModules.overrideAttrs (_: { modSha256 = sha256; })"

On nixpkgs 23.05 and earlier

nix-prefetch '{ sha256 }: (callPackage (import ./default.nix) { }).go-modules.overrideAttrs (_: { modSha256 = sha256; })'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants