├── .gitignore ├── LICENSE.md ├── README.md ├── buildGoCache.nix ├── examples ├── buildGoApplication.nix ├── buildGoModule.nix ├── gomod2nix.toml ├── imported-packages └── imported-packages-go-mod2nix ├── flake.lock ├── flake.nix ├── get-external-imports └── get-external-imports.nix /.gitignore: -------------------------------------------------------------------------------- 1 | /result 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2023 Jörg Thalheim d/b/a Repl.it 4 | 5 | See `LICENSES` for additional licensing terms. 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # buildGoCache 2 | 3 | buildGoCache speeds up nix's buildGoModule by pre-compiling imported go modules 4 | 5 | ## Potential speed-up 6 | 7 | For telegraf we measured the following build times with and without the buildGoCache. 8 | 9 | Build machine: AMD Ryzen 9 7950X3D 16-Core Processor, 128G DDR4 RAM, zfs raid0 10 | 11 | without cache: 12 | 13 | ``` 14 | time nix build .#example-no-cache -L 15 | 0.28s user 0.20s system 0% cpu 59.539 total 16 | time nix build .#example-proxy-vendor-no-cachA 17 | 0.30s user 0.20s system 0% cpu 1:14.01 total 18 | ``` 19 | 20 | with cache: 21 | 22 | ``` 23 | time nix build .#example -L 24 | 0.23s user 0.18s system 1% cpu 25.872 total 25 | time nix build .#example-proxy-vendor -L 26 | 0.03s user 0.04s system 0% cpu 30.501 total 27 | ``` 28 | 29 | Speedup: 2.3x..~2.4x 30 | 31 | ## Usage 32 | 33 | First we generate a list of all imported packages: 34 | 35 | ``` 36 | nix run .#get-external-imports -- ./. imported-packages 37 | ``` 38 | 39 | Than we modify our `buildGoModule` to use your go build cache: 40 | 41 | ```nix 42 | let 43 | vendorHash = "sha256-aMO7nH68E1S5G1iWj29McK0VY0frfjNnJ6D6rJ29cqQ="; 44 | proxyVendor = true; # must be in sync for buildGoCache and buildGoModule 45 | src = ./.; # replace this with the source directory 46 | 47 | goCache = buildGoCache { 48 | importPackagesFile = ./imported-packages; 49 | inherit src vendorHash proxyVendor; 50 | }; 51 | in 52 | buildGoModule { 53 | name = "myproject"; 54 | 55 | buildInputs = [ goCache ]; 56 | 57 | inherit src; 58 | 59 | inherit vendorHash proxyVendor; 60 | } 61 | ``` 62 | 63 | See [./examples]() for real-world examples based on telegraf and gomod2nix 64 | 65 | Other [real-world example](https://github.com/replit/upm/pull/155) 66 | -------------------------------------------------------------------------------- /buildGoCache.nix: -------------------------------------------------------------------------------- 1 | { buildGoModule 2 | , lib 3 | , rsync 4 | }: 5 | { importPackagesFile 6 | , src 7 | , vendorHash ? null 8 | , vendorEnv ? null 9 | , proxyVendor ? false 10 | , buildInputs ? [ ] 11 | , nativeBuildInputs ? [ ] 12 | }: 13 | assert (!(vendorHash != null && vendorEnv != null)); # vendorHash and vendorEnv are mutually exclusive 14 | assert (vendorHash != null || vendorEnv != null); # one of vendorHash or vendorEnv must be set 15 | assert (proxyVendor -> vendorEnv == null); # proxyVendor is not compatible with vendorEnv 16 | let 17 | filteredSource = builtins.path { 18 | path = src; 19 | filter = path: type: type == "regular" && (baseNameOf path == "go.sum" || baseNameOf path == "go.mod" || baseNameOf path == "vendor"); 20 | name = "go-cache-src"; 21 | }; 22 | 23 | goModules = 24 | if vendorEnv == null then (buildGoModule { 25 | name = "deps"; 26 | src = src; 27 | inherit vendorHash proxyVendor; 28 | }).goModules 29 | else vendorEnv; 30 | 31 | in 32 | buildGoModule { 33 | name = "go-cache"; 34 | src = filteredSource; 35 | inherit buildInputs; 36 | nativeBuildInputs = [ rsync ] ++ nativeBuildInputs; 37 | vendorHash = null; 38 | unpackPhase = '' 39 | mkdir source 40 | cp -r $src/* source 41 | chmod -R +w source 42 | cd source 43 | ''; 44 | inherit proxyVendor; 45 | buildPhase = '' 46 | export HOME=$TMPDIR 47 | mkdir -p $out/ 48 | 49 | export GOFLAGS=-trimpath 50 | ${if proxyVendor then '' 51 | export GOPROXY="file://${goModules}" 52 | mkdir -p $out/go-mod-cache 53 | export GOMODCACHE=$out/go-mod-cache 54 | '' else '' 55 | export GOPROXY=off 56 | export GOSUMDB=off 57 | 58 | cp -r --reflink=auto ${goModules}/ vendor 59 | export GOFLAGS="''${GOFLAGS} -mod=vendor" 60 | ''} 61 | export GOCACHE=$out/go-cache 62 | export GO_NO_VENDOR_CHECKS="1" 63 | export GOPATH=$TMPDIR/go 64 | export GOBIN=$GOPATH/bin 65 | mkdir -p $GOPATH/src 66 | xargs go build <${importPackagesFile} 67 | mkdir -p $out/nix-support 68 | cat > $out/nix-support/setup-hook <<'EOF' 69 | mkdir -p "$TMPDIR" || true 70 | if [ -d "$TMPDIR" ]; then 71 | echo "copying ${placeholder "out"}/go-cache" "$TMPDIR/go-cache" 72 | cp --reflink=auto -r "${placeholder "out"}/go-cache" "$TMPDIR/go-cache" 73 | chmod -R +w "$TMPDIR/go-cache" 74 | else 75 | echo skipping build-go-cache copy 76 | fi 77 | ${lib.optionalString proxyVendor ''export GOMODCACHE="${placeholder "out"}/go-mod-cache"''} 78 | EOF 79 | ''; 80 | 81 | doCheck = false; 82 | allowGoReference = true; 83 | phases = [ "unpackPhase" "buildPhase" ]; 84 | } 85 | -------------------------------------------------------------------------------- /examples/buildGoApplication.nix: -------------------------------------------------------------------------------- 1 | { buildGoCache 2 | , useGoCache ? true 3 | , buildGoApplication 4 | , fetchFromGitHub 5 | , lib 6 | }: 7 | let 8 | 9 | version = "1.28.3"; 10 | src = fetchFromGitHub { 11 | owner = "nix-community"; 12 | repo = "gomod2nix"; 13 | rev = "f95720e89af6165c8c0aa77f180461fe786f3c21"; 14 | hash = "sha256-c49BVhQKw3XDRgt+y+uPAbArtgUlMXCET6VxEBmzHXE="; 15 | }; 16 | modules = ./gomod2nix.toml; 17 | 18 | # use gomod2nix to generate a vendor directory for our project for use in buildGoCache 19 | inherit (buildGoApplication { 20 | pname = "vendor-env"; 21 | inherit src version modules; 22 | doCheck = false; 23 | }) vendorEnv; 24 | 25 | goCache = buildGoCache { 26 | importPackagesFile = ./imported-packages-go-mod2nix; 27 | inherit src vendorEnv; 28 | }; 29 | in 30 | buildGoApplication { 31 | pname = "gomod2nix"; 32 | inherit version src modules; 33 | buildInputs = lib.optional useGoCache goCache; 34 | subPackages = [ "." ]; 35 | vendorHash = null; 36 | } 37 | -------------------------------------------------------------------------------- /examples/buildGoModule.nix: -------------------------------------------------------------------------------- 1 | { buildGoModule 2 | , buildGoCache 3 | , fetchFromGitHub 4 | , proxyVendor ? false 5 | , useGoCache ? true 6 | , lib 7 | }: 8 | let 9 | 10 | vendorHash = if proxyVendor then 11 | "sha256-aMO7nH68E1S5G1iWj29McK0VY0frfjNnJ6D6rJ29cqQ=" 12 | else 13 | "sha256-UuFPSw4G607GhAH3pf5+vDkJGjxeyUcs7SN0GiGm/h4="; 14 | 15 | version = "1.28.3"; 16 | src = fetchFromGitHub { 17 | owner = "influxdata"; 18 | repo = "telegraf"; 19 | rev = "321c5a4070cd46d699826432ab4858224f25001d"; 20 | hash = "sha256-Jel/XE3lPIymTUqDT0LPY/vmVodbdPFMomoDl2y4194="; 21 | #rev = "v${version}"; 22 | #hash = "sha256-9BwAsLk8pz1QharomkuQdsoNVQYzw+fSU3nDkw053JE="; 23 | }; 24 | 25 | goCache = buildGoCache { 26 | importPackagesFile = ./imported-packages; 27 | inherit src vendorHash proxyVendor; 28 | }; 29 | in 30 | buildGoModule { 31 | pname = "telegraf"; 32 | inherit version; 33 | 34 | subPackages = [ "cmd/telegraf" ]; 35 | buildInputs = lib.optional useGoCache goCache; 36 | 37 | inherit src; 38 | doCheck = false; 39 | 40 | inherit vendorHash proxyVendor; 41 | 42 | ldflags = [ 43 | "-s" 44 | "-w" 45 | "-X=github.com/influxdata/telegraf/internal.Commit=${src.rev}" 46 | "-X=github.com/influxdata/telegraf/internal.Version=${version}" 47 | ]; 48 | } 49 | -------------------------------------------------------------------------------- /examples/gomod2nix.toml: -------------------------------------------------------------------------------- 1 | schema = 3 2 | 3 | [mod] 4 | [mod."github.com/BurntSushi/toml"] 5 | version = "v1.1.0" 6 | hash = "sha256-1Mib3Aj/YhrlwA3o3x9orRRkVlYa5JRDQfNdtaUyeu0=" 7 | [mod."github.com/inconshreveable/mousetrap"] 8 | version = "v1.0.0" 9 | hash = "sha256-ogTuLrV40FwS4ueo4hh6hi1wPywOI+LyIqfNjsibwNY=" 10 | [mod."github.com/nix-community/go-nix"] 11 | version = "v0.0.0-20220612195009-5f5614f7ca47" 12 | hash = "sha256-eBPzib8STUZnDpLFpWZz+F00thbp1P/98oiEE/XWE+M=" 13 | [mod."github.com/sirupsen/logrus"] 14 | version = "v1.8.1" 15 | hash = "sha256-vUIDlLXYBD74+JqdoSx+W3J6r5cOk63heo0ElsHizoM=" 16 | [mod."github.com/spf13/cobra"] 17 | version = "v1.4.0" 18 | hash = "sha256-I6j9sD61Ztcc2W/WGeWo3ggYtnGTxNxZ2EFPdtO0UEY=" 19 | [mod."github.com/spf13/pflag"] 20 | version = "v1.0.5" 21 | hash = "sha256-w9LLYzxxP74WHT4ouBspH/iQZXjuAh2WQCHsuvyEjAw=" 22 | [mod."golang.org/x/mod"] 23 | version = "v0.5.1" 24 | hash = "sha256-+r/pAtwGHJiGISV/je8ZPXCFukTTGbZ8y0p3zb8wGcs=" 25 | [mod."golang.org/x/sys"] 26 | version = "v0.0.0-20220610221304-9f5ed59c137d" 27 | hash = "sha256-/qCatdMn+M1Lbu10CK0kQtLCmYDBXtIQ1kdy2F/2hlc=" 28 | [mod."golang.org/x/tools"] 29 | version = "v0.0.0-20210106214847-113979e3529a" 30 | hash = "sha256-VdoV0LIBWehslesNW1ETJ1WmO4KsGojC2HDdNAjGzsI=" 31 | [mod."golang.org/x/xerrors"] 32 | version = "v0.0.0-20220609144429-65e65417b02f" 33 | hash = "sha256-tl8pv3oddbz2+KoIp7PFDKsxjQF8ocjPF8XPsY3sw38=" 34 | -------------------------------------------------------------------------------- /examples/imported-packages: -------------------------------------------------------------------------------- 1 | bufio 2 | bytes 3 | cloud.google.com/go/bigquery 4 | cloud.google.com/go/monitoring/apiv3/v2 5 | cloud.google.com/go/monitoring/apiv3/v2/monitoringpb 6 | cloud.google.com/go/pubsub 7 | cloud.google.com/go/storage 8 | collectd.org/api 9 | collectd.org/network 10 | compress/gzip 11 | compress/zlib 12 | container/list 13 | context 14 | crypto/aes 15 | crypto/cipher 16 | crypto/ecdsa 17 | crypto/hmac 18 | crypto/md5 19 | crypto/rand 20 | crypto/rsa 21 | crypto/sha1 22 | crypto/sha256 23 | crypto/sha512 24 | crypto/subtle 25 | crypto/tls 26 | crypto/x509 27 | crypto/x509/pkix 28 | database/sql 29 | database/sql/driver 30 | embed 31 | encoding/base32 32 | encoding/base64 33 | encoding/binary 34 | encoding/csv 35 | encoding/hex 36 | encoding/json 37 | encoding/pem 38 | encoding/xml 39 | errors 40 | flag 41 | fmt 42 | github.com/99designs/keyring 43 | github.com/aerospike/aerospike-client-go/v5 44 | github.com/alecthomas/units 45 | github.com/aliyun/alibaba-cloud-sdk-go/sdk 46 | github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth 47 | github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/providers 48 | github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests 49 | github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses 50 | github.com/aliyun/alibaba-cloud-sdk-go/services/cms 51 | github.com/aliyun/alibaba-cloud-sdk-go/services/ecs 52 | github.com/aliyun/alibaba-cloud-sdk-go/services/rds 53 | github.com/aliyun/alibaba-cloud-sdk-go/services/slb 54 | github.com/aliyun/alibaba-cloud-sdk-go/services/vpc 55 | github.com/amir/raidman 56 | github.com/antchfx/jsonquery 57 | github.com/antchfx/xmlquery 58 | github.com/antchfx/xpath 59 | github.com/apache/arrow/go/v13/arrow/flight/flightsql/driver 60 | github.com/apache/iotdb-client-go/client 61 | github.com/apache/thrift/lib/go/thrift 62 | github.com/aristanetworks/goarista/lanz 63 | github.com/aristanetworks/goarista/lanz/proto 64 | github.com/awnumar/memguard 65 | github.com/aws/aws-sdk-go-v2/aws 66 | github.com/aws/aws-sdk-go-v2/aws/signer/v4 67 | github.com/aws/aws-sdk-go-v2/config 68 | github.com/aws/aws-sdk-go-v2/credentials 69 | github.com/aws/aws-sdk-go-v2/credentials/stscreds 70 | github.com/aws/aws-sdk-go-v2/feature/ec2/imds 71 | github.com/aws/aws-sdk-go-v2/service/cloudwatch 72 | github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs 73 | github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types 74 | github.com/aws/aws-sdk-go-v2/service/cloudwatch/types 75 | github.com/aws/aws-sdk-go-v2/service/dynamodb 76 | github.com/aws/aws-sdk-go-v2/service/ec2 77 | github.com/aws/aws-sdk-go-v2/service/ec2/types 78 | github.com/aws/aws-sdk-go-v2/service/kinesis 79 | github.com/aws/aws-sdk-go-v2/service/kinesis/types 80 | github.com/aws/aws-sdk-go-v2/service/sts 81 | github.com/aws/aws-sdk-go-v2/service/timestreamwrite 82 | github.com/aws/aws-sdk-go-v2/service/timestreamwrite/types 83 | github.com/aws/smithy-go 84 | github.com/Azure/azure-event-hubs-go/v3 85 | github.com/Azure/azure-event-hubs-go/v3/persist 86 | github.com/Azure/azure-kusto-go/kusto 87 | github.com/Azure/azure-kusto-go/kusto/data/errors 88 | github.com/Azure/azure-kusto-go/kusto/ingest 89 | github.com/Azure/azure-kusto-go/kusto/kql 90 | github.com/Azure/azure-storage-queue-go/azqueue 91 | github.com/Azure/go-autorest/autorest 92 | github.com/Azure/go-autorest/autorest/adal 93 | github.com/Azure/go-autorest/autorest/azure/auth 94 | github.com/benbjohnson/clock 95 | github.com/blues/jsonata-go 96 | github.com/bmatcuk/doublestar/v3 97 | github.com/boschrexroth/ctrlx-datalayer-golang/pkg/sseclient 98 | github.com/boschrexroth/ctrlx-datalayer-golang/pkg/token 99 | github.com/BurntSushi/toml 100 | github.com/caio/go-tdigest 101 | github.com/cisco-ie/nx-telemetry-proto/mdt_dialout 102 | github.com/cisco-ie/nx-telemetry-proto/telemetry_bis 103 | github.com/clarify/clarify-go 104 | github.com/clarify/clarify-go/fields 105 | github.com/clarify/clarify-go/views 106 | github.com/ClickHouse/clickhouse-go 107 | github.com/cloudevents/sdk-go/v2 108 | github.com/cloudevents/sdk-go/v2/event 109 | github.com/compose-spec/compose-go/template 110 | github.com/compose-spec/compose-go/utils 111 | github.com/coocood/freecache 112 | github.com/coreos/go-semver/semver 113 | github.com/coreos/go-systemd/daemon 114 | github.com/couchbase/go-couchbase 115 | github.com/digitalocean/go-libvirt 116 | github.com/dimchansky/utfbom 117 | github.com/djherbis/times 118 | github.com/docker/docker/api/types 119 | github.com/docker/docker/api/types/container 120 | github.com/docker/docker/api/types/filters 121 | github.com/docker/docker/api/types/registry 122 | github.com/docker/docker/api/types/swarm 123 | github.com/docker/docker/api/types/volume 124 | github.com/docker/docker/client 125 | github.com/docker/docker/pkg/stdcopy 126 | github.com/docker/go-connections/nat 127 | github.com/dynatrace-oss/dynatrace-metric-utils-go/metric 128 | github.com/dynatrace-oss/dynatrace-metric-utils-go/metric/apiconstants 129 | github.com/dynatrace-oss/dynatrace-metric-utils-go/metric/dimensions 130 | github.com/eclipse/paho.golang/autopaho 131 | github.com/eclipse/paho.golang/paho 132 | github.com/eclipse/paho.mqtt.golang 133 | github.com/fatih/color 134 | github.com/gobwas/glob 135 | github.com/gofrs/uuid/v5 136 | github.com/golang/geo/s2 137 | github.com/golang-jwt/jwt/v5 138 | github.com/golang/snappy 139 | github.com/go-ldap/ldap/v3 140 | github.com/go-logfmt/logfmt 141 | github.com/google/cel-go/cel 142 | github.com/google/cel-go/checker/decls 143 | github.com/google/cel-go/common/types 144 | github.com/google/cel-go/common/types/ref 145 | github.com/google/cel-go/ext 146 | github.com/google/gnxi/utils/xpath 147 | github.com/google/go-cmp/cmp 148 | github.com/google/go-cmp/cmp/cmpopts 149 | github.com/google/go-github/v32/github 150 | github.com/google/gopacket 151 | github.com/google/gopacket/layers 152 | github.com/google/licensecheck 153 | github.com/google/uuid 154 | github.com/gopcua/opcua 155 | github.com/gopcua/opcua/debug 156 | github.com/gopcua/opcua/ua 157 | github.com/gophercloud/gophercloud 158 | github.com/gophercloud/gophercloud/openstack 159 | github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/schedulerstats 160 | github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/services 161 | github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumetenants 162 | github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes 163 | github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/aggregates 164 | github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/diagnostics 165 | github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/hypervisors 166 | github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/services 167 | github.com/gophercloud/gophercloud/openstack/compute/v2/flavors 168 | github.com/gophercloud/gophercloud/openstack/compute/v2/servers 169 | github.com/gophercloud/gophercloud/openstack/identity/v3/projects 170 | github.com/gophercloud/gophercloud/openstack/identity/v3/services 171 | github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/agents 172 | github.com/gophercloud/gophercloud/openstack/networking/v2/networks 173 | github.com/gophercloud/gophercloud/openstack/networking/v2/ports 174 | github.com/gophercloud/gophercloud/openstack/networking/v2/subnets 175 | github.com/gophercloud/gophercloud/openstack/orchestration/v1/stacks 176 | github.com/go-redis/redis/v7 177 | github.com/go-redis/redis/v8 178 | github.com/gorilla/mux 179 | github.com/gorilla/websocket 180 | github.com/gosnmp/gosnmp 181 | github.com/go-sql-driver/mysql 182 | github.com/go-stomp/stomp 183 | github.com/grid-x/modbus 184 | github.com/gwos/tcg/sdk/clients 185 | github.com/gwos/tcg/sdk/logper 186 | github.com/gwos/tcg/sdk/transit 187 | github.com/harlow/kinesis-consumer 188 | github.com/harlow/kinesis-consumer/store/ddb 189 | github.com/hashicorp/consul/api 190 | github.com/hashicorp/go-uuid 191 | github.com/influxdata/go-syslog/v3 192 | github.com/influxdata/go-syslog/v3/nontransparent 193 | github.com/influxdata/go-syslog/v3/octetcounting 194 | github.com/influxdata/go-syslog/v3/rfc3164 195 | github.com/influxdata/go-syslog/v3/rfc5424 196 | github.com/influxdata/influxdb-observability/common 197 | github.com/influxdata/influxdb-observability/influx2otel 198 | github.com/influxdata/influxdb-observability/otel2influx 199 | github.com/influxdata/line-protocol/v2/lineprotocol 200 | github.com/influxdata/tail 201 | github.com/influxdata/tail/watch 202 | github.com/influxdata/toml 203 | github.com/influxdata/toml/ast 204 | github.com/influxdata/wlog 205 | github.com/intel/iaevents 206 | github.com/jackc/pgconn 207 | github.com/jackc/pgio 208 | github.com/jackc/pgtype 209 | github.com/jackc/pgx/v4 210 | github.com/jackc/pgx/v4/pgxpool 211 | github.com/jackc/pgx/v4/stdlib 212 | github.com/james4k/rcon 213 | github.com/jeremywohl/flatten/v2 214 | github.com/jhump/protoreflect/desc 215 | github.com/jhump/protoreflect/desc/protoparse 216 | github.com/jmespath/go-jmespath 217 | github.com/karrick/godirwalk 218 | github.com/kballard/go-shellquote 219 | github.com/klauspost/compress/gzip 220 | github.com/klauspost/compress/zlib 221 | github.com/klauspost/compress/zstd 222 | github.com/klauspost/pgzip 223 | github.com/kolo/xmlrpc 224 | github.com/linkedin/goavro/v2 225 | github.com/logzio/azure-monitor-metrics-receiver 226 | github.com/lxc/lxd/client 227 | github.com/lxc/lxd/shared/api 228 | github.com/Masterminds/semver/v3 229 | github.com/Masterminds/sprig 230 | github.com/Masterminds/sprig/v3 231 | github.com/matttproud/golang_protobuf_extensions/pbutil 232 | github.com/mdlayher/apcupsd 233 | github.com/mdlayher/vsock 234 | github.com/Mellanox/rdmamap 235 | github.com/microsoft/ApplicationInsights-Go/appinsights 236 | github.com/microsoft/go-mssqldb 237 | github.com/miekg/dns 238 | github.com/moby/ipvs 239 | github.com/multiplay/go-ts3 240 | github.com/nats-io/nats.go 241 | github.com/nats-io/nats-server/v2/server 242 | github.com/netsampler/goflow2/decoders/netflow 243 | github.com/netsampler/goflow2/decoders/netflowlegacy 244 | github.com/netsampler/goflow2/decoders/sflow 245 | github.com/newrelic/newrelic-telemetry-sdk-go/cumulative 246 | github.com/newrelic/newrelic-telemetry-sdk-go/telemetry 247 | github.com/nsqio/go-nsq 248 | github.com/nwaples/tacplus 249 | github.com/olivere/elastic 250 | github.com/openconfig/gnmi/proto/gnmi 251 | github.com/openconfig/gnmi/proto/gnmi_ext 252 | github.com/opensearch-project/opensearch-go/v2 253 | github.com/opensearch-project/opensearch-go/v2/opensearchapi 254 | github.com/opensearch-project/opensearch-go/v2/opensearchutil 255 | github.com/opentracing/opentracing-go/log 256 | github.com/openzipkin-contrib/zipkin-go-opentracing 257 | github.com/openzipkin/zipkin-go 258 | github.com/openzipkin/zipkin-go/reporter/http 259 | github.com/p4lang/p4runtime/go/p4/config/v1 260 | github.com/p4lang/p4runtime/go/p4/v1 261 | github.com/PaesslerAG/gval 262 | github.com/pborman/ansi 263 | github.com/peterbourgon/unixtransport 264 | github.com/pion/dtls/v2 265 | github.com/prometheus/client_golang/prometheus 266 | github.com/prometheus/client_golang/prometheus/collectors 267 | github.com/prometheus/client_golang/prometheus/promhttp 268 | github.com/prometheus/client_model/go 269 | github.com/prometheus/common/expfmt 270 | github.com/prometheus/common/model 271 | github.com/prometheus-community/pro-bing 272 | github.com/prometheus/procfs 273 | github.com/prometheus/prometheus/prompb 274 | github.com/rabbitmq/amqp091-go 275 | github.com/redis/go-redis/v9 276 | github.com/riemann/riemann-go-client 277 | github.com/riemann/riemann-go-client/proto 278 | github.com/robbiet480/go.nut 279 | github.com/robinson/gos7 280 | github.com/safchain/ethtool 281 | github.com/shirou/gopsutil/v3/cpu 282 | github.com/shirou/gopsutil/v3/disk 283 | github.com/shirou/gopsutil/v3/host 284 | github.com/shirou/gopsutil/v3/load 285 | github.com/shirou/gopsutil/v3/mem 286 | github.com/shirou/gopsutil/v3/net 287 | github.com/shirou/gopsutil/v3/process 288 | github.com/Shopify/sarama 289 | github.com/showwin/speedtest-go/speedtest 290 | github.com/signalfx/golib/v3/datapoint 291 | github.com/signalfx/golib/v3/datapoint/dpsink 292 | github.com/signalfx/golib/v3/event 293 | github.com/signalfx/golib/v3/sfxclient 294 | github.com/sijms/go-ora/v2 295 | github.com/sirupsen/logrus 296 | github.com/sleepinggenius2/gosmi 297 | github.com/sleepinggenius2/gosmi/models 298 | github.com/sleepinggenius2/gosmi/types 299 | github.com/snowflakedb/gosnowflake 300 | github.com/srebhan/cborquery 301 | github.com/srebhan/protobufquery 302 | github.com/stretchr/testify/mock 303 | github.com/stretchr/testify/require 304 | github.com/testcontainers/testcontainers-go 305 | github.com/testcontainers/testcontainers-go/wait 306 | github.com/thomasklein94/packer-plugin-libvirt/libvirt-utils 307 | github.com/tidwall/gjson 308 | github.com/tinylib/msgp/msgp 309 | github.com/urfave/cli/v2 310 | github.com/vapourismo/knx-go/knx 311 | github.com/vapourismo/knx-go/knx/dpt 312 | github.com/vishvananda/netns 313 | github.com/vjeantet/grok 314 | github.com/vmware/govmomi 315 | github.com/vmware/govmomi/object 316 | github.com/vmware/govmomi/performance 317 | github.com/vmware/govmomi/property 318 | github.com/vmware/govmomi/session 319 | github.com/vmware/govmomi/view 320 | github.com/vmware/govmomi/vim25 321 | github.com/vmware/govmomi/vim25/methods 322 | github.com/vmware/govmomi/vim25/mo 323 | github.com/vmware/govmomi/vim25/soap 324 | github.com/vmware/govmomi/vim25/types 325 | github.com/vmware/govmomi/vsan/methods 326 | github.com/vmware/govmomi/vsan/types 327 | github.com/wavefronthq/wavefront-sdk-go/senders 328 | github.com/wvanbergen/kafka/consumergroup 329 | github.com/x448/float16 330 | github.com/xdg/scram 331 | github.com/youmark/pkcs8 332 | github.com/yuin/goldmark 333 | github.com/yuin/goldmark/ast 334 | github.com/yuin/goldmark/extension 335 | github.com/yuin/goldmark/parser 336 | github.com/yuin/goldmark/text 337 | github.com/yuin/goldmark/util 338 | go/ast 339 | golang.org/x/crypto/ocsp 340 | golang.org/x/crypto/pbkdf2 341 | golang.org/x/exp/slices 342 | golang.org/x/mod/modfile 343 | golang.org/x/net/html 344 | golang.org/x/net/html/charset 345 | golang.org/x/net/http2 346 | golang.org/x/net/proxy 347 | golang.org/x/oauth2 348 | golang.org/x/oauth2/clientcredentials 349 | golang.org/x/oauth2/endpoints 350 | golang.org/x/oauth2/google 351 | golang.org/x/sync/errgroup 352 | golang.org/x/sync/semaphore 353 | golang.org/x/sys/unix 354 | golang.org/x/term 355 | golang.org/x/text/cases 356 | golang.org/x/text/encoding 357 | golang.org/x/text/encoding/unicode 358 | golang.org/x/text/language 359 | golang.org/x/text/transform 360 | golang.zx2c4.com/wireguard/wgctrl 361 | golang.zx2c4.com/wireguard/wgctrl/wgtypes 362 | go.mongodb.org/mongo-driver/bson 363 | go.mongodb.org/mongo-driver/bson/primitive 364 | go.mongodb.org/mongo-driver/mongo 365 | go.mongodb.org/mongo-driver/mongo/options 366 | go.mongodb.org/mongo-driver/mongo/readpref 367 | gonum.org/v1/gonum/stat/distuv 368 | google.golang.org/api/idtoken 369 | google.golang.org/api/iterator 370 | google.golang.org/api/option 371 | google.golang.org/api/support/bundler 372 | google.golang.org/genproto/googleapis/api/distribution 373 | google.golang.org/genproto/googleapis/api/metric 374 | google.golang.org/genproto/googleapis/api/monitoredres 375 | google.golang.org/grpc 376 | google.golang.org/grpc/codes 377 | google.golang.org/grpc/credentials 378 | google.golang.org/grpc/credentials/insecure 379 | google.golang.org/grpc/encoding/gzip 380 | google.golang.org/grpc/keepalive 381 | google.golang.org/grpc/metadata 382 | google.golang.org/grpc/peer 383 | google.golang.org/grpc/status 384 | google.golang.org/protobuf/encoding/protojson 385 | google.golang.org/protobuf/proto 386 | google.golang.org/protobuf/reflect/protodesc 387 | google.golang.org/protobuf/reflect/protoreflect 388 | google.golang.org/protobuf/runtime/protoimpl 389 | google.golang.org/protobuf/types/dynamicpb 390 | google.golang.org/protobuf/types/known/durationpb 391 | google.golang.org/protobuf/types/known/timestamppb 392 | go.opentelemetry.io/collector/pdata/plog/plogotlp 393 | go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp 394 | go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp 395 | go/parser 396 | gopkg.in/gorethink/gorethink.v3 397 | gopkg.in/olivere/elastic.v5 398 | gopkg.in/tomb.v1 399 | gopkg.in/yaml.v2 400 | go.starlark.net/lib/json 401 | go.starlark.net/lib/math 402 | go.starlark.net/lib/time 403 | go.starlark.net/resolve 404 | go.starlark.net/starlark 405 | go.starlark.net/starlarkstruct 406 | go/token 407 | hash 408 | hash/fnv 409 | hash/maphash 410 | html 411 | io 412 | io/fs 413 | k8s.io/api/apps/v1 414 | k8s.io/api/core/v1 415 | k8s.io/apimachinery/pkg/api/resource 416 | k8s.io/apimachinery/pkg/apis/meta/v1 417 | k8s.io/apimachinery/pkg/fields 418 | k8s.io/apimachinery/pkg/labels 419 | k8s.io/apimachinery/pkg/util/wait 420 | k8s.io/api/networking/v1 421 | k8s.io/client-go/informers 422 | k8s.io/client-go/kubernetes 423 | k8s.io/client-go/rest 424 | k8s.io/client-go/tools/cache 425 | k8s.io/client-go/tools/clientcmd 426 | layeh.com/radius 427 | layeh.com/radius/rfc2865 428 | log 429 | math 430 | math/big 431 | math/bits 432 | math/rand 433 | mime 434 | modernc.org/sqlite 435 | net 436 | net/http 437 | net/http/cgi 438 | net/http/cookiejar 439 | net/http/httputil 440 | net/smtp 441 | net/textproto 442 | net/url 443 | os 444 | os/exec 445 | os/signal 446 | os/user 447 | path 448 | path/filepath 449 | reflect 450 | regexp 451 | runtime 452 | runtime/metrics 453 | sort 454 | strconv 455 | strings 456 | sync 457 | sync/atomic 458 | syscall 459 | testing 460 | text/template 461 | text/template/parse 462 | time 463 | time/tzdata 464 | unicode 465 | unicode/utf8 466 | unsafe 467 | -------------------------------------------------------------------------------- /examples/imported-packages-go-mod2nix: -------------------------------------------------------------------------------- 1 | bufio 2 | bytes 3 | crypto/sha256 4 | encoding/base64 5 | encoding/json 6 | flag 7 | fmt 8 | github.com/BurntSushi/toml 9 | github.com/nix-community/go-nix/pkg/nar 10 | github.com/sirupsen/logrus 11 | github.com/spf13/cobra 12 | go/ast 13 | golang.org/x/mod/modfile 14 | golang.org/x/mod/module 15 | golang.org/x/tools/go/vcs 16 | go/parser 17 | go/printer 18 | go/token 19 | io 20 | os 21 | os/exec 22 | path 23 | path/filepath 24 | runtime 25 | sort 26 | strconv 27 | strings 28 | sync 29 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "flake-utils": { 4 | "inputs": { 5 | "systems": "systems" 6 | }, 7 | "locked": { 8 | "lastModified": 1694529238, 9 | "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", 10 | "owner": "numtide", 11 | "repo": "flake-utils", 12 | "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", 13 | "type": "github" 14 | }, 15 | "original": { 16 | "owner": "numtide", 17 | "repo": "flake-utils", 18 | "type": "github" 19 | } 20 | }, 21 | "gomod2nix": { 22 | "inputs": { 23 | "flake-utils": "flake-utils", 24 | "nixpkgs": [ 25 | "nixpkgs" 26 | ] 27 | }, 28 | "locked": { 29 | "lastModified": 1701687253, 30 | "narHash": "sha256-qJCMxIKWXonJODPF2oV7mCd0xu7VYVenTucrY0bizto=", 31 | "owner": "nix-community", 32 | "repo": "gomod2nix", 33 | "rev": "001bbfa22e2adeb87c34c6015e5694e88721cabe", 34 | "type": "github" 35 | }, 36 | "original": { 37 | "owner": "nix-community", 38 | "repo": "gomod2nix", 39 | "type": "github" 40 | } 41 | }, 42 | "nixpkgs": { 43 | "locked": { 44 | "lastModified": 1699370855, 45 | "narHash": "sha256-aKYwp26kQqjxOisd1vQiuCWgx+ub4Cgc8TgeZEeoIzw=", 46 | "owner": "Mic92", 47 | "repo": "nixpkgs", 48 | "rev": "6978a16e28c9ba6f717f9c85872faca73c800cb7", 49 | "type": "github" 50 | }, 51 | "original": { 52 | "owner": "Mic92", 53 | "ref": "build-go-module", 54 | "repo": "nixpkgs", 55 | "type": "github" 56 | } 57 | }, 58 | "root": { 59 | "inputs": { 60 | "gomod2nix": "gomod2nix", 61 | "nixpkgs": "nixpkgs" 62 | } 63 | }, 64 | "systems": { 65 | "locked": { 66 | "lastModified": 1681028828, 67 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", 68 | "owner": "nix-systems", 69 | "repo": "default", 70 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", 71 | "type": "github" 72 | }, 73 | "original": { 74 | "owner": "nix-systems", 75 | "repo": "default", 76 | "type": "github" 77 | } 78 | } 79 | }, 80 | "root": "root", 81 | "version": 7 82 | } 83 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "A very basic flake"; 3 | # even more performance: https://github.com/NixOS/nixpkgs/pull/266075/files (optional) 4 | inputs.nixpkgs.url = "github:Mic92/nixpkgs/build-go-module"; 5 | inputs.gomod2nix.url = "github:nix-community/gomod2nix"; 6 | inputs.gomod2nix.inputs.nixpkgs.follows = "nixpkgs"; 7 | 8 | outputs = { self, nixpkgs, gomod2nix }: 9 | let 10 | forAllSystems = nixpkgs.lib.genAttrs [ 11 | "x86_64-linux" 12 | "aarch64-linux" 13 | "x86_64-darwin" 14 | "aarch64-darwin" 15 | ]; 16 | in 17 | { 18 | legacyPackages = forAllSystems (system: 19 | let 20 | pkgs = nixpkgs.legacyPackages.${system}; 21 | in 22 | { 23 | buildGoCache = pkgs.callPackage ./buildGoCache.nix { }; 24 | get-external-imports = pkgs.callPackage ./get-external-imports.nix { }; 25 | example = pkgs.callPackage ./examples/buildGoModule.nix { 26 | inherit (self.legacyPackages.${system}) buildGoCache; 27 | }; 28 | example-no-cache = pkgs.callPackage ./examples/buildGoModule.nix { 29 | inherit (self.legacyPackages.${system}) buildGoCache; 30 | useGoCache = false; 31 | }; 32 | example-proxy-vendor = pkgs.callPackage ./examples/buildGoModule.nix { 33 | inherit (self.legacyPackages.${system}) buildGoCache; 34 | proxyVendor = true; 35 | }; 36 | example-proxy-vendor-no-cache = pkgs.callPackage ./examples/buildGoModule.nix { 37 | inherit (self.legacyPackages.${system}) buildGoCache; 38 | proxyVendor = true; 39 | useGoCache = false; 40 | }; 41 | example-gomod2nix = pkgs.callPackage ./examples/buildGoApplication.nix { 42 | inherit (self.legacyPackages.${system}) buildGoCache; 43 | inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; 44 | }; 45 | example-gomod2nix-no-cache = pkgs.callPackage ./examples/buildGoApplication.nix { 46 | inherit (self.legacyPackages.${system}) buildGoCache; 47 | inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; 48 | useGoCache = false; 49 | }; 50 | }); 51 | checks = forAllSystems (system: 52 | builtins.removeAttrs self.legacyPackages.${system} ["buildGoCache"] 53 | ); 54 | }; 55 | } 56 | -------------------------------------------------------------------------------- /get-external-imports: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu -o pipefail 3 | 4 | if [[ $# -lt 2 ]]; then 5 | echo "Usage: $0 moduledir imported-packages" >&2 6 | exit 1 7 | fi 8 | moduledir="$1" 9 | imported_packages="$(realpath "$2")" 10 | 11 | # use a while loop 12 | if [ ! -f "$moduledir/go.mod" ]; then 13 | echo "no go.mod in the directory: $moduledir" >&2 14 | exit 1 15 | fi 16 | modname=$(awk '/^module / {print $2; exit}' "$moduledir/go.mod") 17 | cd "$moduledir" 18 | go list -f '{{ join .Imports "\n" }}{{ if .TestImports}} 19 | {{ join .TestImports "\n" }}{{ end }}{{ if .XTestImports}} 20 | {{ join .XTestImports "\n" }}{{ end }}' "./..." | LC_ALL=C sort -u | grep -v "$modname" | grep -Ev '^C$' > "$imported_packages" 21 | -------------------------------------------------------------------------------- /get-external-imports.nix: -------------------------------------------------------------------------------- 1 | { stdenv 2 | , makeWrapper 3 | , lib 4 | , gnugrep 5 | , gawk 6 | , coreutils 7 | , go 8 | }: 9 | 10 | stdenv.mkDerivation { 11 | name = "get-external-imports"; 12 | nativeBuildInputs = [ makeWrapper ]; 13 | src = ./.; 14 | installPhase = '' 15 | install -D -m755 get-external-imports $out/bin/get-external-imports 16 | wrapProgram $out/bin/get-external-imports \ 17 | --prefix PATH ":" ${lib.makeBinPath [ gnugrep gawk coreutils go ]} 18 | ''; 19 | } 20 | --------------------------------------------------------------------------------