This works well.
test = pkgs.buildGoModule {
name = "test";
src = gitignore.lib.gitignoreSource ./.;
doCheck = false;
ldFlags = "-mod=mod";
vendorHash = "sha256-ZKp4eeZRFWbogDadB8ZJfBXhSYyVYq1uPpz2WekvtPg=";
CGO_ENABLED = 0;
overrideModAttrs = (oldAttrs: {
impureEnvVars = oldAttrs.impureEnvVars or [ ] ++ [
"NIX_GITLAB_TOKEN"
];
preBuild = /* bash */ ''
export HOME=$(mktemp -d)
cat > ~/.netrc <<EOF
machine YOUR_PRIVATE_REPO
login oauth2
password $NIX_GITLAB_TOKEN
EOF
export GOPRIVATE=YOUR_PRIVATE_REPO/*
export GONOSUMDB=YOUR_PRIVATE_REPO
'';
preferLocalBuild = true;
});
};