flake.nix 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # flake.nix describes a Nix source repository that provides
  2. # development builds of Tailscale and the fork of the Go compiler
  3. # toolchain that Tailscale maintains. It also provides a development
  4. # environment for working on tailscale, for use with "nix develop".
  5. #
  6. # For more information about this and why this file is useful, see:
  7. # https://nixos.wiki/wiki/Flakes
  8. #
  9. # Also look into direnv: https://direnv.net/, this can make it so that you can
  10. # automatically get your environment set up when you change folders into the
  11. # project.
  12. #
  13. # WARNING: currently, the packages provided by this flake are brittle,
  14. # and importing this flake into your own Nix configs is likely to
  15. # leave you with broken builds periodically.
  16. #
  17. # The issue is that building Tailscale binaries uses the buildGoModule
  18. # helper from nixpkgs. This helper demands to know the content hash of
  19. # all of the Go dependencies of this repo, in the form of a Nix SRI
  20. # hash. This hash isn't automatically kept in sync with changes made
  21. # to go.mod yet, and so every time we update go.mod while hacking on
  22. # Tailscale, this flake ends up with a broken build due to hash
  23. # mismatches.
  24. #
  25. # Right now, this flake is intended for use by Tailscale developers,
  26. # who are aware of this mismatch and willing to live with it. At some
  27. # point, we'll add automation to keep the hashes more in sync, at
  28. # which point this caveat should go away.
  29. #
  30. # See https://github.com/tailscale/tailscale/issues/6845 for tracking
  31. # how to fix this mismatch.
  32. {
  33. inputs = {
  34. nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  35. systems.url = "github:nix-systems/default";
  36. # Used by shell.nix as a compat shim.
  37. flake-compat = {
  38. url = "github:edolstra/flake-compat";
  39. flake = false;
  40. };
  41. };
  42. outputs = {
  43. self,
  44. nixpkgs,
  45. systems,
  46. flake-compat,
  47. }: let
  48. goVersion = nixpkgs.lib.fileContents ./go.toolchain.version;
  49. toolChainRev = nixpkgs.lib.fileContents ./go.toolchain.rev;
  50. gitHash = nixpkgs.lib.fileContents ./go.toolchain.rev.sri;
  51. eachSystem = f:
  52. nixpkgs.lib.genAttrs (import systems) (system:
  53. f (import nixpkgs {
  54. system = system;
  55. overlays = [
  56. (final: prev: {
  57. go_1_25 = prev.go_1_25.overrideAttrs {
  58. version = goVersion;
  59. src = prev.fetchFromGitHub {
  60. owner = "tailscale";
  61. repo = "go";
  62. rev = toolChainRev;
  63. sha256 = gitHash;
  64. };
  65. };
  66. })
  67. ];
  68. }));
  69. tailscaleRev = self.rev or "";
  70. in {
  71. # tailscale takes a nixpkgs package set, and builds Tailscale from
  72. # the same commit as this flake. IOW, it provides "tailscale built
  73. # from HEAD", where HEAD is "whatever commit you imported the
  74. # flake at".
  75. #
  76. # This is currently unfortunately brittle, because we have to
  77. # specify vendorHash, and that sha changes any time we alter
  78. # go.mod. We don't want to force a nix dependency on everyone
  79. # hacking on Tailscale, so this flake is likely to have broken
  80. # builds periodically until someone comes through and manually
  81. # fixes them up. I sure wish there was a way to express "please
  82. # just trust the local go.mod, vendorHash has no benefit here",
  83. # but alas.
  84. #
  85. # So really, this flake is for tailscale devs to dogfood with, if
  86. # you're an end user you should be prepared for this flake to not
  87. # build periodically.
  88. packages = eachSystem (pkgs: rec {
  89. default = pkgs.buildGo125Module {
  90. name = "tailscale";
  91. pname = "tailscale";
  92. src = ./.;
  93. vendorHash = pkgs.lib.fileContents ./go.mod.sri;
  94. nativeBuildInputs = [pkgs.makeWrapper pkgs.installShellFiles];
  95. ldflags = ["-X tailscale.com/version.gitCommitStamp=${tailscaleRev}"];
  96. env.CGO_ENABLED = 0;
  97. subPackages = [
  98. "cmd/tailscale"
  99. "cmd/tailscaled"
  100. "cmd/tsidp"
  101. ];
  102. doCheck = false;
  103. # NOTE: We strip the ${PORT} and $FLAGS because they are unset in the
  104. # environment and cause issues (specifically the unset PORT). At some
  105. # point, there should be a NixOS module that allows configuration of these
  106. # things, but for now, we hardcode the default of port 41641 (taken from
  107. # ./cmd/tailscaled/tailscaled.defaults).
  108. postInstall =
  109. pkgs.lib.optionalString pkgs.stdenv.isLinux ''
  110. wrapProgram $out/bin/tailscaled --prefix PATH : ${pkgs.lib.makeBinPath [pkgs.iproute2 pkgs.iptables pkgs.getent pkgs.shadow]}
  111. wrapProgram $out/bin/tailscale --suffix PATH : ${pkgs.lib.makeBinPath [pkgs.procps]}
  112. sed -i \
  113. -e "s#/usr/sbin#$out/bin#" \
  114. -e "/^EnvironmentFile/d" \
  115. -e 's/''${PORT}/41641/' \
  116. -e 's/$FLAGS//' \
  117. ./cmd/tailscaled/tailscaled.service
  118. install -D -m0444 -t $out/lib/systemd/system ./cmd/tailscaled/tailscaled.service
  119. ''
  120. + pkgs.lib.optionalString (pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform) ''
  121. installShellCompletion --cmd tailscale \
  122. --bash <($out/bin/tailscale completion bash) \
  123. --fish <($out/bin/tailscale completion fish) \
  124. --zsh <($out/bin/tailscale completion zsh)
  125. '';
  126. };
  127. tailscale = default;
  128. });
  129. devShells = eachSystem (pkgs: {
  130. devShell = pkgs.mkShell {
  131. packages = with pkgs; [
  132. curl
  133. git
  134. gopls
  135. gotools
  136. graphviz
  137. perl
  138. go_1_25
  139. yarn
  140. # qemu and e2fsprogs are needed for natlab
  141. qemu
  142. e2fsprogs
  143. ];
  144. };
  145. });
  146. };
  147. }
  148. # nix-direnv cache busting line: sha256-jJSSXMyUqcJoZuqfSlBsKDQezyqS+jDkRglMMjG1K8g=