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;
}
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
After you have entered your development shell you can generate a gomod2nix.toml
using:
$ gomod2nix generate
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