From 759483247eab2ceee46ff64a3e73daa2a908e016 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 14 Jun 2022 17:35:19 +0800 Subject: [PATCH 1/5] Align flake template with recent API changes and improvements --- shell.nix | 10 ++-------- templates/app/default.nix | 18 ++++++++++++++---- templates/app/shell.nix | 18 ++++++++++++++++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/shell.nix b/shell.nix index 62be6c2..a33b65d 100644 --- a/shell.nix +++ b/shell.nix @@ -1,14 +1,8 @@ { pkgs ? ( let - inherit (builtins) fromJSON readFile; - flakeLock = fromJSON (readFile ./flake.lock); - locked = flakeLock.nodes.nixpkgs.locked; - nixpkgs = assert locked.type == "github"; builtins.fetchTarball { - url = "https://github.com/${locked.owner}/${locked.repo}/archive/${locked.rev}.tar.gz"; - sha256 = locked.narHash; - }; + inherit (builtins) fetchTree fromJSON readFile; in - import nixpkgs { + import (fetchTree (fromJSON (readFile ./flake.lock)).nodes.nixpkgs.locked) { overlays = [ (import ./overlay.nix) ]; diff --git a/templates/app/default.nix b/templates/app/default.nix index 17efdcd..2c29bbc 100644 --- a/templates/app/default.nix +++ b/templates/app/default.nix @@ -1,10 +1,20 @@ -{ buildGoApplication, go, lib }: +{ pkgs ? ( + let + inherit (builtins) fetchTree fromJSON readFile; + inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix; + in + import (fetchTree nixpkgs.locked) { + overlays = [ + (import "${fetchTree gomod2nix.locked}/overlay.nix") + ]; + } + ) +}: -buildGoApplication { - inherit go; +pkgs.buildGoApplication { pname = "myapp"; version = "0.1"; + pwd = ./.; src = ./.; modules = ./gomod2nix.toml; - subPackages = [ "." ]; } diff --git a/templates/app/shell.nix b/templates/app/shell.nix index 8fcd820..b8e49a9 100644 --- a/templates/app/shell.nix +++ b/templates/app/shell.nix @@ -1,8 +1,22 @@ -{ pkgs ? import { } }: +{ pkgs ? ( + let + inherit (builtins) fetchTree fromJSON readFile; + inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix; + in + import (fetchTree nixpkgs.locked) { + overlays = [ + (import "${fetchTree gomod2nix.locked}/overlay.nix") + ]; + } + ) +}: +let + goEnv = pkgs.mkGoEnv { pwd = ./.; }; +in pkgs.mkShell { packages = [ - pkgs.go + goEnv pkgs.gomod2nix ]; } From 59869b91af510a8b59bac16f4125bbf3caed19a9 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 14 Jun 2022 17:54:23 +0800 Subject: [PATCH 2/5] Add getting started docs --- docs/getting-started.md | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 docs/getting-started.md diff --git a/docs/getting-started.md b/docs/getting-started.md new file mode 100644 index 0000000..f7bbc6e --- /dev/null +++ b/docs/getting-started.md @@ -0,0 +1,79 @@ +# 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 +``` From c6bc2d55a2e4ae86d5f94e372f996ce6b7a56c8d Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 14 Jun 2022 17:55:59 +0800 Subject: [PATCH 3/5] Add link to getting started docs in README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f36d038..553b091 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ in pkgs.buildGoApplication { } ``` +For more in-depth usage check the [Getting Started](./docs/getting-started.md) docs. + ## FAQ ### Why not continue work on vgo2nix? From 219f2b41a1d6acb1183142446143fe243ea0853e Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 14 Jun 2022 17:58:17 +0800 Subject: [PATCH 4/5] Github actions: Bump install-nix-action --- .github/workflows/ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4236ff3..4a75242 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,8 +14,8 @@ jobs: env: NIX_PATH: "nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz" steps: - - uses: cachix/install-nix-action@v12 - - uses: actions/checkout@v1 + - uses: cachix/install-nix-action@v17 + - uses: actions/checkout@v3 - name: Check format run: nix-shell --run 'nixpkgs-fmt --check .' @@ -24,8 +24,8 @@ jobs: env: NIX_PATH: "nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz" steps: - - uses: cachix/install-nix-action@v12 - - uses: actions/checkout@v1 + - uses: cachix/install-nix-action@v17 + - uses: actions/checkout@v3 - name: Check format run: nix-shell --run 'golangci-lint run' @@ -34,8 +34,8 @@ jobs: env: NIX_PATH: "nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz" steps: - - uses: cachix/install-nix-action@v12 - - uses: actions/checkout@v1 + - uses: cachix/install-nix-action@v17 + - uses: actions/checkout@v3 - name: "Build gomod2nix" run: nix-shell --run "go build" - name: Run gomod2nix From c6aa07b58f6166279fff8f8c430a3fb306900803 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 14 Jun 2022 18:02:02 +0800 Subject: [PATCH 5/5] README: Add reference to the announcement blog post --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 553b091..7c0f422 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ We need a better build abstraction that takes Go modules into account, while rem Yes. Once the API is considered stable. +## Motivation + +The [announcement blog post](https://www.tweag.io/blog/2021-03-04-gomod2nix/) contains comparisons with other Go build systems for Nix and additional notes on the design choices made. + ## License This project is licensed under the MIT License. See the [LICENSE](LICENSE)