|
| 1 | +{ |
| 2 | + description = "A simple Go package"; |
| 3 | + |
| 4 | + # Nixpkgs / NixOS version to use. |
| 5 | + inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; |
| 6 | + |
| 7 | + outputs = { self, nixpkgs }: |
| 8 | + let |
| 9 | + |
| 10 | + # to work with older version of flakes |
| 11 | + lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101"; |
| 12 | + |
| 13 | + # Generate a user-friendly version number. |
| 14 | + version = builtins.substring 0 8 lastModifiedDate; |
| 15 | + |
| 16 | + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. |
| 17 | + forAllSystems = nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed; |
| 18 | + |
| 19 | + # Nixpkgs instantiated for supported system types. |
| 20 | + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); |
| 21 | + |
| 22 | + in |
| 23 | + { |
| 24 | + |
| 25 | + # Provide some binary packages for selected system types. |
| 26 | + packages = forAllSystems (system: |
| 27 | + let |
| 28 | + pkgs = nixpkgsFor.${system}; |
| 29 | + in |
| 30 | + { |
| 31 | + default = pkgs.buildGoModule { |
| 32 | + pname = "go-hello"; |
| 33 | + inherit version; |
| 34 | + # In 'nix develop', we don't need a copy of the source tree |
| 35 | + # in the Nix store. |
| 36 | + src = ./.; |
| 37 | + |
| 38 | + # This hash locks the dependencies of this package. It is |
| 39 | + # necessary because of how Go requires network access to resolve |
| 40 | + # VCS. See https://www.tweag.io/blog/2021-03-04-gomod2nix/ for |
| 41 | + # details. Normally one can build with a fake hash and rely on native Go |
| 42 | + # mechanisms to tell you what the hash should be or determine what |
| 43 | + # it should be "out-of-band" with other tooling (eg. gomod2nix). |
| 44 | + # To begin with it is recommended to set this, but one must |
| 45 | + # remember to bump this hash when your dependencies change. |
| 46 | + # vendorHash = pkgs.lib.fakeHash; |
| 47 | + |
| 48 | + vendorHash = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + # Add dependencies that are only needed for development |
| 53 | + devShells = forAllSystems (system: |
| 54 | + let |
| 55 | + pkgs = nixpkgsFor.${system}; |
| 56 | + in |
| 57 | + { |
| 58 | + default = pkgs.mkShell { |
| 59 | + buildInputs = with pkgs; [ go gopls gotools go-tools ]; |
| 60 | + }; |
| 61 | + }); |
| 62 | + }; |
| 63 | +} |
0 commit comments