mirror of
https://github.com/tweag/gomod2nix.git
synced 2024-11-10 04:21:33 +00:00
79 lines
1.3 KiB
Markdown
79 lines
1.3 KiB
Markdown
# Getting started with Gomod2nix
|
|
|
|
## Installation
|
|
|
|
### Using Niv
|
|
|
|
First initialize Niv:
|
|
``` bash
|
|
$ niv init --latest
|
|
$ niv add tweag/gomod2nix
|
|
```
|
|
|
|
Create a `shell.nix` used for development:
|
|
``` nix
|
|
{ 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
|
|
``` nix
|
|
{ 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:
|
|
``` bash
|
|
$ nix flake init -t github:tweag/gomod2nix#app
|
|
```
|
|
|
|
## Basic usage
|
|
|
|
After you have entered your development shell you can generate a `gomod2nix.toml` using:
|
|
``` bash
|
|
$ 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:
|
|
``` bash
|
|
$ gomod2nix import
|
|
```
|