From 28dfe79139ff3a8edfc0dae57980e1f6fe362c85 Mon Sep 17 00:00:00 2001 From: Xe Date: Sun, 27 Feb 2022 16:51:56 +0000 Subject: [PATCH] initial commit Signed-off-by: Xe --- .envrc | 1 + .gitignore | 2 ++ README.md | 5 +++++ flake.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ flake.nix | 45 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ main.go | 23 +++++++++++++++++++++++ 7 files changed, 121 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 go.mod create mode 100644 main.go diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..726d2d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result +.direnv diff --git a/README.md b/README.md new file mode 100644 index 0000000..862e1c8 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +## Build + +```console +$ nix build +``` diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..7062b5d --- /dev/null +++ b/flake.lock @@ -0,0 +1,42 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1645433236, + "narHash": "sha256-4va4MvJ076XyPp5h8sm5eMQvCrJ6yZAbBmyw95dGyw4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "7f9b6e2babf232412682c09e57ed666d8f84ac2d", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "utils": { + "locked": { + "lastModified": 1644229661, + "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..23f5358 --- /dev/null +++ b/flake.nix @@ -0,0 +1,45 @@ +{ + description = "A basic Go web server setup"; + + inputs = { + nixpkgs.url = "nixpkgs/nixos-unstable"; + utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, utils }: + utils.lib.eachSystem [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ] (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + version = builtins.substring 0 8 self.lastModifiedDate; + in { + defaultPackage = pkgs.buildGoModule { + pname = "go-hello"; + inherit version; + src = ./.; + + # This hash locks the dependencies of this package. It is + # necessary because of how Go requires network access to resolve + # VCS. See https://www.tweag.io/blog/2021-03-04-gomod2nix/ for + # details. Normally one can build with a fake sha256 and rely on native Go + # mechanisms to tell you what the hash should be or determine what + # it should be "out-of-band" with other tooling (eg. gomod2nix). + # To begin with it is recommended to set this, but one must + # remeber to bump this hash when your dependencies change. + #vendorSha256 = pkgs.lib.fakeSha256; + vendorSha256 = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; + }; + + packages = {}; + + defaultApp = self.defaultPackage.${system}; + + devShell = pkgs.mkShell { + buildInputs = with pkgs; [ go gopls goimports go-tools ]; + }; + }); +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..31d88ad --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module repository.host/org/name + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0f942a1 --- /dev/null +++ b/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" +) + +var ( + bindhost = flag.String("bind", ":3031", "host/port to listen on") +) + +func main() { + flag.Parse() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "hello from nix!") + }) + + log.Printf("listening for HTTP on %s", *bindhost) + log.Fatal(http.ListenAndServe(*bindhost, nil)) +}