From d30b3ca30a9eb00c4edb5e9505c841867740f029 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Mon, 30 May 2022 19:09:22 +0800 Subject: [PATCH] templates: Add a flake template --- default.nix | 6 +++++- flake.nix | 9 +++++++++ templates/app/.gitignore | 1 + templates/app/default.nix | 10 ++++++++++ templates/app/flake.nix | 23 +++++++++++++++++++++++ templates/app/go.mod | 3 +++ templates/app/gomod2nix.toml | 3 +++ templates/app/main.go | 9 +++++++++ templates/app/shell.nix | 8 ++++++++ 9 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 templates/app/.gitignore create mode 100644 templates/app/default.nix create mode 100644 templates/app/flake.nix create mode 100644 templates/app/go.mod create mode 100644 templates/app/gomod2nix.toml create mode 100644 templates/app/main.go create mode 100644 templates/app/shell.nix diff --git a/default.nix b/default.nix index 8ca23d6..efedc76 100644 --- a/default.nix +++ b/default.nix @@ -5,7 +5,11 @@ buildGoApplication { pname = "gomod2nix"; version = "0.1"; src = lib.cleanSourceWith { - filter = name: type: (! lib.hasSuffix "tests" name) && (! lib.hasSuffix "builder" name); + filter = name: type: builtins.foldl' (v: s: v && ! lib.hasSuffix s name) true [ + "tests" + "builder" + "templates" + ]; src = lib.cleanSource ./.; }; modules = ./gomod2nix.toml; diff --git a/flake.nix b/flake.nix index 4c9c5cd..c6b3a8e 100644 --- a/flake.nix +++ b/flake.nix @@ -8,6 +8,15 @@ outputs = { self, nixpkgs, utils }: { overlays.default = import ./overlay.nix; + + templates = { + app = { + path = ./templates/app; + description = "Gomod2nix packaged application"; + }; + }; + defaultTemplate = self.templates.app; + } // (utils.lib.eachDefaultSystem (system: diff --git a/templates/app/.gitignore b/templates/app/.gitignore new file mode 100644 index 0000000..dfba653 --- /dev/null +++ b/templates/app/.gitignore @@ -0,0 +1 @@ +/gomod2nix-template diff --git a/templates/app/default.nix b/templates/app/default.nix new file mode 100644 index 0000000..17efdcd --- /dev/null +++ b/templates/app/default.nix @@ -0,0 +1,10 @@ +{ buildGoApplication, go, lib }: + +buildGoApplication { + inherit go; + pname = "myapp"; + version = "0.1"; + src = ./.; + modules = ./gomod2nix.toml; + subPackages = [ "." ]; +} diff --git a/templates/app/flake.nix b/templates/app/flake.nix new file mode 100644 index 0000000..41dd183 --- /dev/null +++ b/templates/app/flake.nix @@ -0,0 +1,23 @@ +{ + description = "A basic gomod2nix flake"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.gomod2nix.url = "github:tweag/gomod2nix"; + + outputs = { self, nixpkgs, flake-utils, gomod2nix }: + (flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ gomod2nix.overlays.default ]; + }; + + in + { + packages.default = pkgs.callPackage ./. { }; + devShells.default = import ./shell.nix { inherit pkgs; }; + }) + ); +} diff --git a/templates/app/go.mod b/templates/app/go.mod new file mode 100644 index 0000000..b59038d --- /dev/null +++ b/templates/app/go.mod @@ -0,0 +1,3 @@ +module example.com/gomod2nix-template + +go 1.17 diff --git a/templates/app/gomod2nix.toml b/templates/app/gomod2nix.toml new file mode 100644 index 0000000..d0d5a65 --- /dev/null +++ b/templates/app/gomod2nix.toml @@ -0,0 +1,3 @@ +schema = 1 + +[mod] diff --git a/templates/app/main.go b/templates/app/main.go new file mode 100644 index 0000000..ab71755 --- /dev/null +++ b/templates/app/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello flake") +} diff --git a/templates/app/shell.nix b/templates/app/shell.nix new file mode 100644 index 0000000..8fcd820 --- /dev/null +++ b/templates/app/shell.nix @@ -0,0 +1,8 @@ +{ pkgs ? import { } }: + +pkgs.mkShell { + packages = [ + pkgs.go + pkgs.gomod2nix + ]; +}