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

I want to use this nixpkgs overlay:

Which is defined also here:

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:
      nixpkgs.overlays = [ gomod2nix.overlays ];
      pkgs = nixpkgs.legacyPackages.${system};
      # ...

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;
             |                                                                   ^

I wrote the flakeExample here

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*/ ]; 
  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. :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" }
@nix { "action": "setPhase", "phase": "buildPhase" }
Building subPackage ./cmd/pistol
go build build constraints exclude all Go files in /build/dryh0n2arjysw917gwnfjy9g2qnad2n7-source/vendor/
@nix { "action": "setPhase", "phase": "checkPhase" }
running tests
go build build constraints exclude all Go files in /build/dryh0n2arjysw917gwnfjy9g2qnad2n7-source/vendor/
@nix { "action": "setPhase", "phase": "installPhase" }
@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:

first, check your buildPhase which process generated bin/exc in $GOPATH/bin
For example:

`-- 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: .