Getting started with Gomod2nix
December 29, 2025 ยท View on GitHub
Installation
Using Niv
First initialize Niv:
$ niv init --latest
$ niv add nix-community/gomod2nix
Create a shell.nix used for development:
{ pkgs ? (
let
sources = import ./nix/sources.nix;
in
import sources.nixpkgs {
overlays = [
(import "${sources.gomod2nix}/overlay.nix")
];
}
)
}:
let
goEnv = pkgs.mkGoEnv { pwd = ./.; };
in
pkgs.mkShell {
packages = [
goEnv
pkgs.gomod2nix
pkgs.niv
];
}
And a default.nix for building your package
{ pkgs ? (
let
sources = import ./nix/sources.nix;
in
import sources.nixpkgs {
overlays = [
(import "${sources.gomod2nix}/overlay.nix")
];
}
)
}:
pkgs.buildGoApplication {
pname = "myapp";
version = "0.1";
pwd = ./.;
src = ./.;
modules = ./gomod2nix.toml;
}
Using Flakes
The quickest way to get started if using Nix Flakes is to use the Flake template:
$ nix flake init -t github:nix-community/gomod2nix#app
It is also possible to use the container template to build container images:
$ nix flake init -t github:nix-community/gomod2nix#container
Basic usage
After you have entered your development shell you can generate a gomod2nix.toml using:
$ gomod2nix generate
To optimize build performance by pre-compiling dependencies in the build cache, use:
$ gomod2nix generate --with-deps
This generates a list of imported packages that will be pre-compiled during the build, significantly reducing subsequent build times. This is particularly useful for projects with many dependencies.
To speed up development and avoid downloading dependencies again in the Nix store you can import them directly from the Go cache using:
$ gomod2nix import