Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nix build to the template #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ tsconfig.tsbuildinfo
# Generated files
.docusaurus
.cache-loader

**/result
lostbean marked this conversation as resolved.
Show resolved Hide resolved
61 changes: 61 additions & 0 deletions hello-go/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions hello-go/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# flake.nix

{
description = "Hello, World! server in Go";

inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachSystem [
"x86_64-linux"
"aarch64-darwin"
"aarch64-linux"
] (system:
let
pkgs = nixpkgs.legacyPackages.${system};
pname = "hello";
src = ./.;

goPackage = pkgs.buildGoModule {
inherit pname src;
version = "0.0.1";
vendorHash = null;
nativeBuildInputs = [ pkgs.stdenv ];
CGO_ENABLED = 0;
};

containerImage = let
content = goPackage.overrideAttrs (old:
old // {
GOOS = "linux";
GOARCH = "arm64";
doCheck = false;
});
in pkgs.dockerTools.buildImage {
name = "hello-world-server";
tag = "latest";
config.Cmd = if system == "aarch64-linux" then
[ "${content}/bin/${pname}" ]
else
[ "${content}/bin/linux_arm64/${pname}" ];
lostbean marked this conversation as resolved.
Show resolved Hide resolved
};

in {
defaultPackage = goPackage;
packages.containerImage = containerImage;
});
}
3 changes: 3 additions & 0 deletions hello-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/hello

go 1.20
17 changes: 17 additions & 0 deletions hello-go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// main.go
package main

import (
"fmt"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}

func main() {
println("Starting hello server!")
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
10 changes: 10 additions & 0 deletions main.star
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ def run(plan, name = "John Snow"):
lib.run_hello(plan, config_json)

postgres.run(plan)

plan.add_service(
name = "go-nix",
config = ServiceConfig(
image = NixBuildSpec(image_name = "hello-world-server", flake_output = "containerImage", flake_location_dir = "./hello-go", build_context_dir = "./"),
env_vars = {
"DEPENDENCY_URL": "http://{}:{}".format("some.url", "8888"),
lostbean marked this conversation as resolved.
Show resolved Hide resolved
},
),
)