nix-community / gomod2nix

Convert applications using Go modules to Nix expressions [maintainer=@marcusramberg]
MIT License
230 stars 56 forks source link

gomod2nix generate results in error path is not valid #72

Open storvik opened 2 years ago

storvik commented 2 years ago

Trying to use this to build a go package using some private modules. Looks like I should be able to do that using generate and import, but getting error path is not valid. Tried without private modules and the problem persists.

Describe the bug nix-instantiate returns error: path /nix/store/*hash*-*package* is not valid. when running gomod2nix import.

To Reproduce

  1. go mod init
  2. create main.go file with content:
package main

import (
    "log"

    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
)

func main() {
    zerolog.SetGlobalLevel(zerolog.InfoLevel)

    log.Error().Msg("Error message")
    log.Warn().Msg("Warning message")

}
  1. go mod tidy
  2. gomod2nix generate (succeeds)
  3. gomod2nix import (fails)

Expected behavior Expected go dependencies to be imported into the nix store.

Environment

Tested both OS but the same thing happened.

Additional context The complete log is here

storvik@computer:~/developer/golang/gonixtest$ gomod2nix generate
INFO[0000] Parsing go.mod                                modPath=go.mod
INFO[0000] Downloading dependencies
INFO[0000] Done downloading dependencies
INFO[0000] Wrote: gomod2nix.toml
storvik@computer:~/developer/golang/gonixtest$ gomod2nix import
INFO[0000] Parsing go.mod                                modPath=go.mod
INFO[0000] Downloading dependencies
INFO[0000] Done downloading dependencies
INFO[0000] Importing sources                             goPackagePath=github.com/rs/zerolog
INFO[0000] Importing sources                             goPackagePath=github.com/mattn/go-isatty
INFO[0000] Importing sources                             goPackagePath=golang.org/x/sys
INFO[0000] Importing sources                             goPackagePath=github.com/mattn/go-colorable
error: path '/nix/store/83frfpjglh25xz7s32w28fw1p54k9170-go-isatty_v0.0.14' is not valid
(use '--show-trace' to show detailed location information)
error: path '/nix/store/gfqmz2qc8qsl29xan4kd3bdy7qjdnmdj-zerolog_v1.27.0' is not valid
(use '--show-trace' to show detailed location information)
/home/storvik/.nix-profile/bin/nix-instantiate --eval --expr
builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/storvik/developer/gopath/pkg/mod/github.com/mattn/go-isatty@v0.0.14";
    name = "go-isatty_v0.0.14";
  }
)

error: path '/nix/store/drh29dg8s120a21f1yicfrxsr4ykcwf0-go-colorable_v0.1.12' is not valid
(use '--show-trace' to show detailed location information)
/home/storvik/.nix-profile/bin/nix-instantiate --eval --expr
builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/storvik/developer/gopath/pkg/mod/github.com/rs/zerolog@v1.27.0";
    name = "zerolog_v1.27.0";
  }
)

/home/storvik/.nix-profile/bin/nix-instantiate --eval --expr
builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/storvik/developer/gopath/pkg/mod/github.com/mattn/go-colorable@v0.1.12";
    name = "go-colorable_v0.1.12";
  }
)

panic: exit status 1

goroutine 1 [running]:
github.com/tweag/gomod2nix/internal/cmd.glob..func1(0xb16ee0?, {0x822d54?, 0x0?, 0x0?})
        /build/source/internal/cmd/root.go:109 +0x48
github.com/spf13/cobra.(*Command).execute(0xb16ee0, {0xb4d048, 0x0, 0x0})
        /build/source/vendor/github.com/spf13/cobra/command.go:860 +0x663
github.com/spf13/cobra.(*Command).ExecuteC(0xb169e0)
        /build/source/vendor/github.com/spf13/cobra/command.go:974 +0x3b4
github.com/spf13/cobra.(*Command).Execute(...)
        /build/source/vendor/github.com/spf13/cobra/command.go:902
github.com/tweag/gomod2nix/internal/cmd.Execute()
        /build/source/internal/cmd/root.go:124 +0x25
main.main()
        /build/source/main.go:6 +0x17
error: path '/nix/store/0076f6qx7f05ivh6hi71ddx0rnmplnhw-sys_v0.0.0-20210927094055-39ccf1dd6fa6' is not valid
(use '--show-trace' to show detailed location information)

I've tried to run the nix-instantiate command manually too, but same error.

A working solution to use private repos would be great, this is a promising start. Thanks for your contributions to the community.

jgresty commented 2 years ago

Oddly the expression evaluates perfectly fine in a nix repl, I wish nix-instantiate would give useful errors...

jjthiessen commented 1 year ago

I'm having the same issues. It seems to affect not only all private repos, but also public ones where the module ref doesn't correspond to a released version (updating to a given commit or branch — e.g., go get example.net/fake/mod@branch).

For example, I'm getting this (upon gomod2nix import) in one of my projects:

INFO[0000] Importing sources                             goPackagePath=golang.org/x/oauth2
error: path '/nix/store/m5x1qfq4yqdn8zyx4gm4z12m112mmj9b-oauth2_v0.0.0-20220411215720-9780585627b5' is not valid
(use '--show-trace' to show detailed location information)
/run/current-system/sw/bin/nix-instantiate --eval --expr 
builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/jt/go/pkg/mod/golang.org/x/oauth2@v0.0.0-20220411215720-9780585627b5";
    name = "oauth2_v0.0.0-20220411215720-9780585627b5";
  }
)

Like @jgresty said, running it in nix repl works (and, moreover, resolves the issue for future invocations). Another way around it is to run nix-instantiate without --eval first, like so (original+show-trace, no eval+show-trace, original+show-trace):

$ /run/current-system/sw/bin/nix-instantiate --eval --expr 'builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/jt/go/pkg/mod/golang.org/x/oauth2@v0.0.0-20220411215720-9780585627b5";
    name = "oauth2_v0.0.0-20220411215720-9780585627b5";
  }
)' --show-trace
error: path '/nix/store/m5x1qfq4yqdn8zyx4gm4z12m112mmj9b-oauth2_v0.0.0-20220411215720-9780585627b5' is not valid

       … while adding path '/nix/store/m5x1qfq4yqdn8zyx4gm4z12m112mmj9b-oauth2_v0.0.0-20220411215720-9780585627b5'

       at «string»:1:1:

            1| builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
             | ^
            2|   builtins.path {
$ /run/current-system/sw/bin/nix-instantiate --expr 'builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/jt/go/pkg/mod/golang.org/x/oauth2@v0.0.0-20220411215720-9780585627b5";
    name = "oauth2_v0.0.0-20220411215720-9780585627b5";
  }
)' --show-trace
error: expression does not evaluate to a derivation (or a set or list of those)
$ /run/current-system/sw/bin/nix-instantiate --eval --expr 'builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/jt/go/pkg/mod/golang.org/x/oauth2@v0.0.0-20220411215720-9780585627b5";
    name = "oauth2_v0.0.0-20220411215720-9780585627b5";
  }
)' --show-trace
"/nix/store/c7z9bm9qzashlcqiv5v2jwcch6gq2hwv-m5x1qfq4yqdn8zyx4gm4z12m112mmj9b-oauth2_v0.0.0-20220411215720-9780585627b5"

I haven't looked at the gomod2nix import code, but I imagine it wouldn't be too difficult to update it to make it work (I'm not sure whether it can be done in a single invocation of nix-instantiate, or not, or even whether it's better to run something else here).

🤷

jjthiessen commented 1 year ago

I just noticed that I'm also getting

error: path '/nix/store/mqn6p2m3qvzqwan59c3xlyvjxgjjhyhy-sys_v0.1.0' is not valid
(use '--show-trace' to show detailed location information)
/run/current-system/sw/bin/nix-instantiate --eval --expr 
builtins.filterSource (name: type: baseNameOf name != ".DS_Store") (
  builtins.path {
    path = "/home/jt/go/pkg/mod/golang.org/x/sys@v0.1.0";
    name = "sys_v0.1.0";
  }
)

so I'm guessing that it's actually more general/not specific to the handling of private or untagged module versions.

jjthiessen commented 1 year ago

It looks like nix eval --impure --expr ... would be a suitable replacement (but would require a semi-recent nix version — OTOH, it would work).

jjthiessen commented 1 year ago

@storvik, I should probably have reread your original issue description before trying to add my own notes/comments. I just reread now and saw where you said (only three sentences in, even) that it wasn't specific to private (or untagged) repos (and refs), but I already came here having discovered the same issue from working with private modules, and just latched on to that instead.

Still, either using nix eval --impure --expr ... or nix-instantiate --expr ...; nix-instantiate --eval --expr ... should work.