How to apply an overlay defined in one flake, in my flake?

I want to use this nixpkgs overlay:

https://github.com/tweag/gomod2nix#usage

Which is defined also here:

https://github.com/tweag/gomod2nix/blob/f8ad3b8024896b3c7f571f068c168643708822de/flake.nix#L13

In my flake.nix:

{
  description = "...";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  inputs.gomod2nix.url = "github:tweag/gomod2nix";

  outputs = { self
    , nixpkgs
    , flake-utils
    , gomod2nix
  }:
  flake-utils.lib.eachDefaultSystem (system:
    let
      nixpkgs.overlays = [ gomod2nix.overlays ];
      pkgs = nixpkgs.legacyPackages.${system};
    in
      # ...
  )
}

But I get:

error: attribute 'legacyPackages' missing

       at /nix/store/8q72zd4q155c6wgq7pzcz6xwibzsmlv3-source/flake.nix:25:14:

           24|       nixpkgs.overlays = [ gomod2nix.overlays ];
           25|       pkgs = nixpkgs.legacyPackages.${system};
             |              ^

Which makes sense because the variable nixpkgs is redefined in the line where the I wish to apply the overlay? In anycase, how do I do this? I tried following the WiKi: Flakes - NixOS Wiki But I think it is tailored towards usage in configuration.nix? And not other flakes?

I tried to apply the override via pkgs like this:

      pkgs = nixpkgs.legacyPackages.${system}.appendOverlays [ gomod2nix.overlay ];

But is failed differently:

error: attempt to call something which is not a function but a set

       at /nix/store/2siiq99hnsz7k4q5809i7iqhijl9bpvi-source/lib/fixed-points.nix:69:67:

           68|   #
           69|   extends = f: rattrs: self: let super = rattrs self; in super // f self super;
             |                                                                   ^
           70|

I wrote the flakeExample here https://github.com/GTrunSec/Coding-Dev-Env-With-NixFlake

let me test it.Coding-Dev-Env-With-NixFlake/gomod2nix at main · GTrunSec/Coding-Dev-Env-With-NixFlake · GitHub

1 Like

the way I do it currently is something like so:

outputs = { nixpkgs, ... }: {
  nixosConfigurations.myOsConfig = let
    pkgs = import nixpkgs { overlays = [ /*the overlay in question*/ ]; 
  in
  nixpkgs.lib.nixosSystem {
    modules = [ { nixpkgs = { inherit pkgs; }; ];
  };
1 Like

This is the most similar example I needed to see, and from some reason it worked for me after I renamed the gomod2nix flake to gomod2nix-flake :slight_smile: Thanks!

I did a patch PR to fix that. https://github.com/tweag/gomod2nix/pull/4 :smile:

1 Like

I also noticed I had to add gotoml2nix to git otherwise it wouldn’t find it.

Besides that, I get a successful build now but I don’t see any binaries in result - it’s empty while the build log looks OK:

@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/iy37nrhwp574d9wni0cml5wb5i081njr-dryh0n2arjysw917gwnfjy9g2qnad2n7-source
source root is dryh0n2arjysw917gwnfjy9g2qnad2n7-source
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
@nix { "action": "setPhase", "phase": "buildPhase" }
building
Building subPackage ./cmd/pistol
github.com/adrg/xdg
github.com/alessio/shellescape
os/user
archive/tar
github.com/dlclark/regexp2/syntax
github.com/dlclark/regexp2
github.com/alecthomas/chroma
github.com/alecthomas/chroma/formatters/html
github.com/alecthomas/chroma/formatters/svg
github.com/alecthomas/chroma/formatters
github.com/danwakefield/fnmatch
github.com/alecthomas/chroma/lexers/internal
github.com/alecthomas/chroma/lexers/a
github.com/alecthomas/chroma/lexers/b
github.com/alecthomas/chroma/lexers/p
github.com/alecthomas/chroma/lexers/c
github.com/alecthomas/chroma/lexers/j
github.com/alecthomas/chroma/lexers/h
github.com/alecthomas/chroma/lexers/circular
github.com/alecthomas/chroma/lexers/d
github.com/alecthomas/chroma/lexers/e
github.com/alecthomas/chroma/lexers/f
github.com/alecthomas/chroma/lexers/g
github.com/alecthomas/chroma/lexers/i
github.com/alecthomas/chroma/lexers/k
github.com/alecthomas/chroma/lexers/l
github.com/alecthomas/chroma/lexers/m
github.com/alecthomas/chroma/lexers/n
github.com/alecthomas/chroma/lexers/o
github.com/alecthomas/chroma/lexers/q
github.com/alecthomas/chroma/lexers/r
github.com/alecthomas/chroma/lexers/s
github.com/alecthomas/chroma/lexers/t
github.com/alecthomas/chroma/lexers/v
github.com/alecthomas/chroma/lexers/w
github.com/alecthomas/chroma/lexers/x
github.com/alecthomas/chroma/lexers/y
github.com/alecthomas/chroma/lexers/z
github.com/alecthomas/chroma/lexers
github.com/alecthomas/chroma/styles
github.com/dustin/go-humanize
net
crypto/x509
crypto/tls
net/textproto
vendor/golang.org/x/net/http/httpguts
vendor/golang.org/x/net/http/httpproxy
mime/multipart
net/http/httptrace
net/http
github.com/andybalholm/brotli
github.com/dsnet/compress/bzip2/internal/sais
github.com/dsnet/compress/internal
github.com/dsnet/compress/internal/errors
github.com/dsnet/compress
github.com/dsnet/compress/internal/prefix
github.com/dsnet/compress/bzip2
github.com/golang/snappy
github.com/klauspost/compress/flate
github.com/klauspost/compress/gzip
github.com/klauspost/compress/fse
github.com/klauspost/compress/huff0
github.com/klauspost/compress/snappy
github.com/klauspost/compress/zstd/internal/xxhash
github.com/klauspost/compress/zstd
github.com/klauspost/pgzip
github.com/nwaples/rardecode
github.com/pierrec/lz4/v4/internal/lz4errors
github.com/pierrec/lz4/v4/internal/lz4block
github.com/pierrec/lz4/v4/internal/xxh32
github.com/pierrec/lz4/v4/internal/lz4stream
github.com/pierrec/lz4/v4
github.com/ulikunitz/xz/internal/xlog
github.com/ulikunitz/xz/internal/hash
github.com/ulikunitz/xz/lzma
github.com/ulikunitz/xz
github.com/xi2/xz
github.com/mholt/archiver/v3
github.com/rakyll/magicmime
go build github.com/rakyll/magicmime: build constraints exclude all Go files in /build/dryh0n2arjysw917gwnfjy9g2qnad2n7-source/vendor/github.com/rakyll/magicmime
golang.org/x/sys/internal/unsafeheader
golang.org/x/sys/unix
github.com/sirupsen/logrus
github.com/galdor/go-cmdline
@nix { "action": "setPhase", "phase": "checkPhase" }
running tests
go build github.com/rakyll/magicmime: build constraints exclude all Go files in /build/dryh0n2arjysw917gwnfjy9g2qnad2n7-source/vendor/github.com/rakyll/magicmime
@nix { "action": "setPhase", "phase": "installPhase" }
installing
@nix { "action": "setPhase", "phase": "fixupPhase" }
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/v9wdnl20im0c7krpfq3lkimhj8102vnr-pistol-0.1.9-flake
strip is /nix/store/cp1sa3xxvl71cypiinw2c62i5s33chlr-binutils-2.35.1/bin/strip
patching script interpreter paths in /nix/store/v9wdnl20im0c7krpfq3lkimhj8102vnr-pistol-0.1.9-flake
checking for references to /build/ in /nix/store/v9wdnl20im0c7krpfq3lkimhj8102vnr-pistol-0.1.9-flake...

And the current flake.nix: https://github.com/doronbehar/pistol/blob/080369b87d0d52572a2c3ad363ee8e8b86b5ffc5/flake.nix

first, check your buildPhase which process generated bin/exc in $GOPATH/bin
For example:
https://github.com/tweag/gomod2nix/blob/5dffe7b6ef0e5810d7fbd1715803114ae0e4a304/builder/default.nix#L162

installing
/build/go
`-- bin
    `-- gomod2nix

Thanks again! I had a look at the builder.nix and saw that I had to enable cgo with CGO_ENABLED = "1". It’s weird the build didn’t fail - it’s a product of this Go issue:https://github.com/golang/go/issues/24068 .