nix-community / gomod2nix

Convert applications using Go modules to Nix expressions [maintainer=@adisbladis]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using flakes with go.mod replace directives

albertkoch opened this issue · comments

When building a go application with buildGoApplication, replace directives are always treated as relative values. Unfortunately with flakes, this means they are relative to the source's path in the nix store. E.g. /nix/store/i0mx81w7qj13i90f9xkzk0dzhk6h0hwv-source

{
  inputs = {
    gomod2nix = {
      inputs.nixpkgs.follows = "nixpkgs";
      url = "github:nix-community/gomod2nix";
    };
    nixpkgs.url = "github:NixOS/nixpkgs/master";
  };
  outputs = { self, gomod2nix, nixpkgs }:
    let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
        overlays = [
          gomod2nix.overlays.default
        ];
      };
    in {
      packages.x86_64-linux.default = pkgs.buildGoApplication {
        modules = ./gomod2nix.toml;
        name = "demo";
        pwd = ./.;
        src = pkgs.lib.cleanSource ./.;
      };
    };
}
nix build --impure

This works with a replace directive such as:

replace github.com/owner/example => ../../../home/username/projects/example

But fails with a replace directive such as:

replace github.com/owner/example => /home/username/projects/example
/nix/store/i0mx81w7qj13i90f9xkzk0dzhk6h0hwv-source/home/username/projects/example': No such file or directory

Conversely, when running go mod tidy and gomod2nix to generate gomod2nix.toml, the replace directive must either be absolute, or relative to the source in my home directory. This means I maintain two replace directives, one for building, and one for updating dependencies, and swap between the two.

It would be nice if absolute paths were not concatenated with the nix store directory when building, so an absolute path could be used in both scenarios without constantly swapping directives.

I think this is the nix flakes variant of the issue that was addressed in #2.