fork
Configure Feed
Select the types of activity you want to include in your feed.
lol
fork
Configure Feed
Select the types of activity you want to include in your feed.
1{ go, govers, parallel, lib, fetchgit, fetchhg, fetchbzr, rsync
2, removeReferencesTo, fetchFromGitHub }:
3
4{ name, buildInputs ? [], nativeBuildInputs ? [], passthru ? {}, preFixup ? ""
5
6# We want parallel builds by default
7, enableParallelBuilding ? true
8
9# Disabled flag
10, disabled ? false
11
12# Go import path of the package
13, goPackagePath
14
15# Go package aliases
16, goPackageAliases ? [ ]
17
18# Extra sources to include in the gopath
19, extraSrcs ? [ ]
20
21# Extra gopaths containing src subfolder
22# with sources to include in the gopath
23, extraSrcPaths ? [ ]
24
25# go2nix dependency file
26, goDeps ? null
27
28, dontRenameImports ? false
29
30# Do not enable this without good reason
31# IE: programs coupled with the compiler
32, allowGoReference ? false
33
34, meta ? {}, ... } @ args':
35
36if disabled then throw "${name} not supported for go ${go.meta.branch}" else
37
38with builtins;
39
40let
41 args = lib.filterAttrs (name: _: name != "extraSrcs") args';
42
43 removeReferences = [ ] ++ lib.optional (!allowGoReference) go;
44
45 removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}'';
46
47 dep2src = goDep:
48 {
49 inherit (goDep) goPackagePath;
50 src = if goDep.fetch.type == "git" then
51 fetchgit {
52 inherit (goDep.fetch) url rev sha256;
53 }
54 else if goDep.fetch.type == "hg" then
55 fetchhg {
56 inherit (goDep.fetch) url rev sha256;
57 }
58 else if goDep.fetch.type == "bzr" then
59 fetchbzr {
60 inherit (goDep.fetch) url rev sha256;
61 }
62 else if goDep.fetch.type == "FromGitHub" then
63 fetchFromGitHub {
64 inherit (goDep.fetch) owner repo rev sha256;
65 }
66 else abort "Unrecognized package fetch type: ${goDep.fetch.type}";
67 };
68
69 importGodeps = { depsFile }:
70 map dep2src (import depsFile);
71
72 goPath = if goDeps != null then importGodeps { depsFile = goDeps; } ++ extraSrcs
73 else extraSrcs;
74in
75
76go.stdenv.mkDerivation (
77 (builtins.removeAttrs args [ "goPackageAliases" "disabled" ]) // {
78
79 inherit name;
80 nativeBuildInputs = [ removeReferencesTo go parallel ]
81 ++ (lib.optional (!dontRenameImports) govers) ++ nativeBuildInputs;
82 buildInputs = [ go ] ++ buildInputs;
83
84 configurePhase = args.configurePhase or ''
85 runHook preConfigure
86
87 # Extract the source
88 cd "$NIX_BUILD_TOP"
89 mkdir -p "go/src/$(dirname "$goPackagePath")"
90 mv "$sourceRoot" "go/src/$goPackagePath"
91
92 '' + lib.flip lib.concatMapStrings goPath ({ src, goPackagePath }: ''
93 mkdir goPath
94 (cd goPath; unpackFile "${src}")
95 mkdir -p "go/src/$(dirname "${goPackagePath}")"
96 chmod -R u+w goPath/*
97 mv goPath/* "go/src/${goPackagePath}"
98 rmdir goPath
99
100 '') + (lib.optionalString (extraSrcPaths != []) ''
101 ${rsync}/bin/rsync -a ${lib.concatMapStrings (p: "${p}/src") extraSrcPaths} go
102
103 '') + ''
104 export GOPATH=$NIX_BUILD_TOP/go:$GOPATH
105
106 runHook postConfigure
107 '';
108
109 renameImports = args.renameImports or (
110 let
111 inputsWithAliases = lib.filter (x: x ? goPackageAliases)
112 (buildInputs ++ (args.propagatedBuildInputs or [ ]));
113 rename = to: from: "echo Renaming '${from}' to '${to}'; govers -d -m ${from} ${to}";
114 renames = p: lib.concatMapStringsSep "\n" (rename p.goPackagePath) p.goPackageAliases;
115 in lib.concatMapStringsSep "\n" renames inputsWithAliases);
116
117 buildPhase = args.buildPhase or ''
118 runHook preBuild
119
120 runHook renameImports
121
122 buildGoDir() {
123 local d; local cmd;
124 cmd="$1"
125 d="$2"
126 . $TMPDIR/buildFlagsArray
127 echo "$d" | grep -q "\(/_\|examples\|Godeps\)" && return 0
128 [ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0
129 local OUT
130 if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" -v $d 2>&1)"; then
131 if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
132 echo "$OUT" >&2
133 return 1
134 fi
135 fi
136 if [ -n "$OUT" ]; then
137 echo "$OUT" >&2
138 fi
139 return 0
140 }
141
142 getGoDirs() {
143 local type;
144 type="$1"
145 if [ -n "$subPackages" ]; then
146 echo "$subPackages" | sed "s,\(^\| \),\1$goPackagePath/,g"
147 else
148 pushd "$NIX_BUILD_TOP/go/src" >/dev/null
149 find "$goPackagePath" -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort | uniq
150 popd >/dev/null
151 fi
152 }
153
154 if [ ''${#buildFlagsArray[@]} -ne 0 ]; then
155 declare -p buildFlagsArray > $TMPDIR/buildFlagsArray
156 else
157 touch $TMPDIR/buildFlagsArray
158 fi
159 export -f buildGoDir # parallel needs to see the function
160 if [ -z "$enableParallelBuilding" ]; then
161 export NIX_BUILD_CORES=1
162 fi
163 getGoDirs "" | parallel -j $NIX_BUILD_CORES buildGoDir install
164
165 runHook postBuild
166 '';
167
168 checkPhase = args.checkPhase or ''
169 runHook preCheck
170
171 getGoDirs test | parallel -j $NIX_BUILD_CORES buildGoDir test
172
173 runHook postCheck
174 '';
175
176 installPhase = args.installPhase or ''
177 runHook preInstall
178
179 mkdir -p $out
180 pushd "$NIX_BUILD_TOP/go"
181 while read f; do
182 echo "$f" | grep -q '^./\(src\|pkg/[^/]*\)/${goPackagePath}' || continue
183 mkdir -p "$(dirname "$out/share/go/$f")"
184 cp "$NIX_BUILD_TOP/go/$f" "$out/share/go/$f"
185 done < <(find . -type f)
186 popd
187
188 mkdir -p $bin
189 dir="$NIX_BUILD_TOP/go/bin"
190 [ -e "$dir" ] && cp -r $dir $bin
191
192 runHook postInstall
193 '';
194
195 preFixup = preFixup + ''
196 find $bin/bin -type f -exec ${removeExpr removeReferences} '{}' + || true
197 '';
198
199 # Disable go cache, which is not reused in nix anyway
200 GOCACHE = "off";
201
202 shellHook = ''
203 d=$(mktemp -d "--suffix=-$name")
204 '' + toString (map (dep: ''
205 mkdir -p "$d/src/$(dirname "${dep.goPackagePath}")"
206 ln -s "${dep.src}" "$d/src/${dep.goPackagePath}"
207 ''
208 ) goPath) + ''
209 export GOPATH=${lib.concatStringsSep ":" ( ["$d"] ++ ["$GOPATH"] ++ ["$PWD"] ++ extraSrcPaths)}
210 '';
211
212 disallowedReferences = lib.optional (!allowGoReference) go
213 ++ lib.optional (!dontRenameImports) govers;
214
215 passthru = passthru //
216 { inherit go; } //
217 lib.optionalAttrs (goPackageAliases != []) { inherit goPackageAliases; };
218
219 enableParallelBuilding = enableParallelBuilding;
220
221 # I prefer to call this dev but propagatedBuildInputs expects $out to exist
222 outputs = args.outputs or [ "bin" "out" ];
223
224 meta = {
225 # Add default meta information
226 homepage = "https://${goPackagePath}";
227 platforms = go.meta.platforms or lib.platforms.all;
228 } // meta // {
229 # add an extra maintainer to every package
230 maintainers = (meta.maintainers or []) ++
231 [ lib.maintainers.ehmry lib.maintainers.lethalman ];
232 };
233})