Add a Go module to Nixpkgs
Intro
I recently had to make bob installable via Nix so that meant I had to write an expression file for it and add it to Nixpkgs collection repository.
The result
{ lib, buildGoModule, fetchFromGitHub }:
buildGoModule rec {
pname = "bob";
version = "0.5.3";
src = fetchFromGitHub {
owner = "benchkram";
repo = pname;
rev = "${version}";
hash = "sha256-JG1fysCqqd/MwpNhKJwLr4cTGq4/88f9OMMapb+r3bc=";
};
CGO_ENABLED = 0;
ldflags = [ "-s" "-w" "-X main.Version=${version}" ];
vendorHash = "sha256-R+zXGR5isoo76oc4lsFf9uCM0Kyi8dQiKEg4BUxtv+k=";
excludedPackages = [ "example/server-db" "test/e2e" "tui-example" ];
# tests require network access
doCheck = false;
meta = with lib; {
description = "A build system for microservices";
homepage = "https://bob.build";
license = licenses.asl20;
platforms = platforms.unix;
maintainers = with maintainers; [ zuzuleinen ];
};
}
How to get the hash/sha256 value
fetchFromGitHub
expects a sha256
attribute, or you can also call it hash
when the hash is prefixed with
the hashing algorithm, in our case sha256-
.
I installed nix-prefetch and then run:
nix-prefetch fetchFromGitHub --owner benchkram --repo bob --rev 0.5.3
which had the output:
The fetcher will be called as follows:
> fetchFromGitHub {
> owner = "benchkram";
> repo = "bob";
> rev = "0.5.3";
> sha256 = "sha256:0000000000000000000000000000000000000000000000000000";
> }
sha256-JG1fysCqqd/MwpNhKJwLr4cTGq4/88f9OMMapb+r3bc=
You can also download the source code of that particular release,
un-archive it on your local and then run, where bob-0.5.3
is the unzipped archive:
$ nix hash path --base64 --type sha256 bob-0.5.3
JG1fysCqqd/MwpNhKJwLr4cTGq4/88f9OMMapb+r3bc=
How to get the value of vendorSha256
I explained more on Stack Overflow, but essentially it's enough to try and build the expression with a wrong hash, and then you will get a validation message with the correct one.
You can build an expression inside a file using:
nix-build -E 'with import <nixpkgs> { }; callPackage ./your-file.nix { }'
You can also run assuming you are inside the local Nixpkgs repository:
nix-build -A bob
The validation message looks like this:
error: hash mismatch in fixed-output derivation '/nix/store/lcq0a950whcxnkik3kq0bblcxg38lf4v-bob-0.5.3-go-modules.drv':
specified: sha256-Y4WM+o+5jiwj8/99UyNHLpBNbtJkKteIGW2P1Jd9L6M=
got: sha256-R+zXGR5isoo76oc4lsFf9uCM0Kyi8dQiKEg4BUxtv+k=
error: 1 dependencies of derivation '/nix/store/r1wvs76xax157c0w296lkld47agk91s3-bob-0.5.3.drv' failed to build
Implementation of buildGoModule
In case you know how to read Nix, you can find the buildGoModule
implementation here.
Even if you don't know it, it might help you with knowing what to add as attributes when building your package.
You can read more here about the buildGoModule
function.
Finding the license type
The license of your Nixpkgs expression should match the upstream package. Notice the license I added for bob:
license = licenses.asl20;
You can find all the license types here.
Publish your changes to Nixpkgs
Now that you have your Nix expression working locally, you can publish it on Nixpkgs making it available for every user who uses Nix.
Here are some guides to do that: