diff --git a/builder/default.nix b/builder/default.nix index 82dd417..fcfd651 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -106,11 +106,13 @@ let sources = toJSON (filterAttrs (n: _: n != defaultPackage) sources); + vendorModules = modulesStruct.vendorModulesTxt or ""; + passthru = { inherit sources; }; - passAsFile = [ "json" "sources" ]; + passAsFile = [ "json" "sources" "vendorModules" ]; } ( '' @@ -122,6 +124,8 @@ let ${internal.symlink} ${concatStringsSep "\n" localReplaceCommands} + cp $vendorModulesPath vendor/modules.txt + mv vendor $out '' ); diff --git a/go.mod b/go.mod index 6cf83b5..f8f73ff 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/nix-community/gomod2nix -go 1.17 +go 1.23 require ( github.com/BurntSushi/toml v1.4.0 diff --git a/go.sum b/go.sum index f718f2d..3e43297 100644 --- a/go.sum +++ b/go.sum @@ -77,23 +77,17 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljT github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -104,11 +98,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -116,38 +105,18 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -155,10 +124,6 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools/go/vcs v0.1.0-deprecated h1:cOIJqWBl99H1dH5LWizPa+0ImeeJq3t3cJjaeOWUAL4= golang.org/x/tools/go/vcs v0.1.0-deprecated/go.mod h1:zUrvATBAvEI9535oC0yWYsLsHIV4Z7g63sNPVMtuBy8= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/gomod2nix.toml b/gomod2nix.toml index 3828b06..8a348d5 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -1,4 +1,5 @@ -schema = 3 +schema = 4 +vendorModulesTxt = "# github.com/BurntSushi/toml v1.4.0\n## explicit; go 1.18\ngithub.com/BurntSushi/toml\ngithub.com/BurntSushi/toml/internal\n# github.com/inconshreveable/mousetrap v1.1.0\n## explicit; go 1.18\ngithub.com/inconshreveable/mousetrap\n# github.com/nix-community/go-nix v0.0.0-20220612195009-5f5614f7ca47\n## explicit; go 1.15\ngithub.com/nix-community/go-nix/pkg/nar\ngithub.com/nix-community/go-nix/pkg/wire\n# github.com/sirupsen/logrus v1.9.3\n## explicit; go 1.13\ngithub.com/sirupsen/logrus\n# github.com/spf13/cobra v1.8.1\n## explicit; go 1.15\ngithub.com/spf13/cobra\n# github.com/spf13/pflag v1.0.5\n## explicit; go 1.12\ngithub.com/spf13/pflag\n# golang.org/x/mod v0.22.0\n## explicit; go 1.22\ngolang.org/x/mod/internal/lazyregexp\ngolang.org/x/mod/modfile\ngolang.org/x/mod/module\ngolang.org/x/mod/semver\n# golang.org/x/sys v0.14.0\n## explicit; go 1.18\ngolang.org/x/sys/execabs\ngolang.org/x/sys/unix\ngolang.org/x/sys/windows\n# golang.org/x/tools/go/vcs v0.1.0-deprecated\n## explicit; go 1.19\ngolang.org/x/tools/go/vcs\n" [mod] [mod."github.com/BurntSushi/toml"] diff --git a/internal/cmd/root.go b/internal/cmd/root.go index b435941..981d672 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -5,10 +5,10 @@ import ( "os" "path/filepath" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" generate "github.com/nix-community/gomod2nix/internal/generate" schema "github.com/nix-community/gomod2nix/internal/schema" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) const directoryDefault = "./" @@ -62,7 +62,7 @@ func generateFunc(cmd *cobra.Command, args []string) { { goMod2NixPath := filepath.Join(outDir, "gomod2nix.toml") outFile := goMod2NixPath - pkgs, err := generate.GeneratePkgs(directory, goMod2NixPath, maxJobs) + generated, err := generate.GeneratePkgs(directory, goMod2NixPath, maxJobs) if err != nil { panic(fmt.Errorf("error generating pkgs: %v", err)) } @@ -75,7 +75,7 @@ func generateFunc(cmd *cobra.Command, args []string) { goPackagePath = tmpProj.GoPackagePath } - output, err := schema.Marshal(pkgs, goPackagePath, subPackages) + output, err := schema.Marshal(generated, goPackagePath, subPackages) if err != nil { panic(fmt.Errorf("error marshaling output: %v", err)) } diff --git a/internal/generate/generate.go b/internal/generate/generate.go index 8980d0f..a212447 100644 --- a/internal/generate/generate.go +++ b/internal/generate/generate.go @@ -140,7 +140,7 @@ builtins.filterSource (name: type: baseNameOf name != ".DS_Store") ( return executor.Wait() } -func GeneratePkgs(directory string, goMod2NixPath string, numWorkers int) ([]*schema.Package, error) { +func GeneratePkgs(directory string, goMod2NixPath string, numWorkers int) (*schema.GeneratePkgsResult, error) { modDownloads, replace, err := common(directory) if err != nil { return nil, err @@ -208,10 +208,45 @@ func GeneratePkgs(directory string, goMod2NixPath string, numWorkers int) ([]*sc return nil, err } + // Dependencies are downloaded, run `go mod vendor` to obtain `vendor/modules.txt` without reverse engineering + const tmpVendorEnvRelative = "vendor-gomod2nix" + tmpVendorEnvAbsolute := filepath.Join(directory, tmpVendorEnvRelative) + err = os.RemoveAll(tmpVendorEnvAbsolute) + if err != nil { + return nil, err + } + defer func() { + _ = os.RemoveAll(tmpVendorEnvAbsolute) + }() + + var modulesTxt string + { + log.Info("Obtaining modules.txt") + cmd := exec.Command( + "go", "mod", "vendor", "-o", tmpVendorEnvRelative, + ) + cmd.Dir = directory + err = cmd.Run() + if err != nil { + return nil, err + } + modulesTxtBytes, err := os.ReadFile(filepath.Join(tmpVendorEnvAbsolute, "modules.txt")) + if err != nil { + return nil, err + } + if len(modulesTxtBytes) == 0 { + return nil, fmt.Errorf("modules.txt has no content") + } + modulesTxt = string(modulesTxtBytes) + } + sort.Slice(packages, func(i, j int) bool { return packages[i].GoPackagePath < packages[j].GoPackagePath }) - return packages, nil + return &schema.GeneratePkgsResult{ + Packages: packages, + ModulesTxt: string(modulesTxt), + }, nil } diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 2ca359f..addde7f 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -2,11 +2,12 @@ package types import ( "bytes" - "github.com/BurntSushi/toml" "os" + + "github.com/BurntSushi/toml" ) -const SchemaVersion = 3 +const SchemaVersion = 4 type Package struct { GoPackagePath string `toml:"-"` @@ -18,6 +19,7 @@ type Package struct { type Output struct { SchemaVersion int `toml:"schema"` Mod map[string]*Package `toml:"mod"` + VendorModules string `toml:"vendorModulesTxt"` // Packages with passed import paths trigger `go install` based on this list SubPackages []string `toml:"subPackages,omitempty"` @@ -26,15 +28,21 @@ type Output struct { GoPackagePath string `toml:"goPackagePath,omitempty"` } -func Marshal(pkgs []*Package, goPackagePath string, subPackages []string) ([]byte, error) { +type GeneratePkgsResult struct { + Packages []*Package + ModulesTxt string +} + +func Marshal(generated *GeneratePkgsResult, goPackagePath string, subPackages []string) ([]byte, error) { out := &Output{ SchemaVersion: SchemaVersion, Mod: make(map[string]*Package), SubPackages: subPackages, GoPackagePath: goPackagePath, + VendorModules: generated.ModulesTxt, } - for _, pkg := range pkgs { + for _, pkg := range generated.Packages { out.Mod[pkg.GoPackagePath] = pkg }