flake.nix 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. {
  2. description = "templ";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/release-24.05";
  5. gomod2nix = {
  6. url = "github:nix-community/gomod2nix";
  7. inputs.nixpkgs.follows = "nixpkgs";
  8. };
  9. gitignore = {
  10. url = "github:hercules-ci/gitignore.nix";
  11. inputs.nixpkgs.follows = "nixpkgs";
  12. };
  13. xc = {
  14. url = "github:joerdav/xc";
  15. inputs.nixpkgs.follows = "nixpkgs";
  16. };
  17. };
  18. outputs = { self, nixpkgs, gomod2nix, gitignore, xc }:
  19. let
  20. allSystems = [
  21. "x86_64-linux" # 64-bit Intel/AMD Linux
  22. "aarch64-linux" # 64-bit ARM Linux
  23. "x86_64-darwin" # 64-bit Intel macOS
  24. "aarch64-darwin" # 64-bit ARM macOS
  25. ];
  26. forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
  27. inherit system;
  28. pkgs = import nixpkgs { inherit system; };
  29. });
  30. in
  31. {
  32. packages = forAllSystems ({ system, pkgs, ... }:
  33. let
  34. buildGoApplication = gomod2nix.legacyPackages.${system}.buildGoApplication;
  35. in
  36. rec {
  37. default = templ;
  38. templ = buildGoApplication {
  39. name = "templ";
  40. src = gitignore.lib.gitignoreSource ./.;
  41. # Update to latest Go version when https://nixpk.gs/pr-tracker.html?pr=324123 is backported to release-24.05.
  42. go = pkgs.go;
  43. # Must be added due to bug https://github.com/nix-community/gomod2nix/issues/120
  44. pwd = ./.;
  45. subPackages = [ "cmd/templ" ];
  46. CGO_ENABLED = 0;
  47. flags = [
  48. "-trimpath"
  49. ];
  50. ldflags = [
  51. "-s"
  52. "-w"
  53. "-extldflags -static"
  54. ];
  55. };
  56. });
  57. # `nix develop` provides a shell containing development tools.
  58. devShell = forAllSystems ({ system, pkgs }:
  59. pkgs.mkShell {
  60. buildInputs = with pkgs; [
  61. golangci-lint
  62. cosign # Used to sign container images.
  63. esbuild # Used to package JS examples.
  64. go_1_22
  65. gomod2nix.legacyPackages.${system}.gomod2nix
  66. gopls
  67. goreleaser
  68. gotestsum
  69. ko # Used to build Docker images.
  70. nodejs # Used to build templ-docs.
  71. xc.packages.${system}.xc
  72. ];
  73. });
  74. # This flake outputs an overlay that can be used to add templ and
  75. # templ-docs to nixpkgs as per https://templ.guide/quick-start/installation/#nix
  76. #
  77. # Example usage:
  78. #
  79. # nixpkgs.overlays = [
  80. # inputs.templ.overlays.default
  81. # ];
  82. overlays.default = final: prev: {
  83. templ = self.packages.${final.stdenv.system}.templ;
  84. templ-docs = self.packages.${final.stdenv.system}.templ-docs;
  85. };
  86. };
  87. }