gomod2nix/docs/getting-started.md
2022-06-14 17:54:23 +08:00

1.3 KiB

Getting started with Gomod2nix

Installation

Using Niv

First initialize Niv:

$ niv init --latest
$ niv add tweag/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:tweag/gomod2nix#app

Basic usage

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