Add go template

This commit is contained in:
Torjus Håkestad 2024-10-12 00:39:09 +02:00
commit e1ec9ff94b
Signed by: torjus
SSH Key Fingerprint: SHA256:KjAds8wHfD2mBYK2H815s/+ABcSdcIHUndwHEdSxml4
8 changed files with 125 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
result

14
flake.nix Normal file
View File

@ -0,0 +1,14 @@
{
description = "Collection of flake templates";
outputs =
{ self }:
{
templates = {
go = {
path = ./go;
description = "Go template";
};
};
};
}

1
go/.envrc Normal file
View File

@ -0,0 +1 @@
use flake

2
go/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.direnv
result

27
go/flake.lock Normal file
View File

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1728492678,
"narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

68
go/flake.nix Normal file
View File

@ -0,0 +1,68 @@
{
description = "Example go flake";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs =
{ self, nixpkgs }:
let
allSystems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
forAllSystems =
f:
nixpkgs.lib.genAttrs allSystems (
system:
f {
pkgs = import nixpkgs { inherit system; };
}
);
# Gets version from first line containg Version in main.go
version = nixpkgs.lib.strings.removePrefix "v" (
builtins.elemAt (nixpkgs.lib.strings.split "\"" (
nixpkgs.lib.lists.findFirst (x: nixpkgs.lib.strings.hasInfix "Version" x) null (
nixpkgs.lib.strings.splitString "\n" (builtins.readFile ./main.go)
)
)) 2
);
in
{
overlays.default = final: prev: {
ghettoptt = self.packages.${prev.system}.default;
};
packages = forAllSystems (
{ pkgs }:
{
default =
let
src = pkgs.lib.sourceFilesBySuffices ./. [
"go.mod"
"go.sum"
".go"
];
in
pkgs.buildGoModule {
inherit version;
pname = "example-go";
src = src;
vendorHash = null;
};
}
);
devShells = forAllSystems (
{ pkgs }:
{
default = pkgs.mkShell {
buildInputs = with pkgs; [
go
golangci-lint
];
};
}
);
};
}

3
go/go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.t-juice.club/torjus/go-example
go 1.22.7

9
go/main.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "fmt"
const Version = "v0.0.1"
func main() {
fmt.Println("Hello, World!")
}