flake.nix 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. go125Version = "1.25.1";
  49. goHash = "sha256-0BDBCc7pTYDv5oHqtGvepJGskGv0ZYPDLp8NuwvRpZQ=";
  50. eachSystem = f:
  51. nixpkgs.lib.genAttrs (import systems) (system:
  52. f (import nixpkgs {
  53. system = system;
  54. overlays = [
  55. (final: prev: {
  56. go_1_25 = prev.go_1_25.overrideAttrs {
  57. version = go125Version;
  58. src = prev.fetchurl {
  59. url = "https://go.dev/dl/go${go125Version}.src.tar.gz";
  60. hash = goHash;
  61. };
  62. };
  63. })
  64. ];
  65. }));
  66. tailscaleRev = self.rev or "";
  67. in {
  68. # tailscale takes a nixpkgs package set, and builds Tailscale from
  69. # the same commit as this flake. IOW, it provides "tailscale built
  70. # from HEAD", where HEAD is "whatever commit you imported the
  71. # flake at".
  72. #
  73. # This is currently unfortunately brittle, because we have to
  74. # specify vendorHash, and that sha changes any time we alter
  75. # go.mod. We don't want to force a nix dependency on everyone
  76. # hacking on Tailscale, so this flake is likely to have broken
  77. # builds periodically until someone comes through and manually
  78. # fixes them up. I sure wish there was a way to express "please
  79. # just trust the local go.mod, vendorHash has no benefit here",
  80. # but alas.
  81. #
  82. # So really, this flake is for tailscale devs to dogfood with, if
  83. # you're an end user you should be prepared for this flake to not
  84. # build periodically.
  85. packages = eachSystem (pkgs: rec {
  86. default = pkgs.buildGo125Module {
  87. name = "tailscale";
  88. pname = "tailscale";
  89. src = ./.;
  90. vendorHash = pkgs.lib.fileContents ./go.mod.sri;
  91. nativeBuildInputs = [pkgs.makeWrapper pkgs.installShellFiles];
  92. ldflags = ["-X tailscale.com/version.gitCommitStamp=${tailscaleRev}"];
  93. env.CGO_ENABLED = 0;
  94. subPackages = [
  95. "cmd/tailscale"
  96. "cmd/tailscaled"
  97. "cmd/tsidp"
  98. ];
  99. doCheck = false;
  100. # NOTE: We strip the ${PORT} and $FLAGS because they are unset in the
  101. # environment and cause issues (specifically the unset PORT). At some
  102. # point, there should be a NixOS module that allows configuration of these
  103. # things, but for now, we hardcode the default of port 41641 (taken from
  104. # ./cmd/tailscaled/tailscaled.defaults).
  105. postInstall =
  106. pkgs.lib.optionalString pkgs.stdenv.isLinux ''
  107. wrapProgram $out/bin/tailscaled --prefix PATH : ${pkgs.lib.makeBinPath [pkgs.iproute2 pkgs.iptables pkgs.getent pkgs.shadow]}
  108. wrapProgram $out/bin/tailscale --suffix PATH : ${pkgs.lib.makeBinPath [pkgs.procps]}
  109. sed -i \
  110. -e "s#/usr/sbin#$out/bin#" \
  111. -e "/^EnvironmentFile/d" \
  112. -e 's/''${PORT}/41641/' \
  113. -e 's/$FLAGS//' \
  114. ./cmd/tailscaled/tailscaled.service
  115. install -D -m0444 -t $out/lib/systemd/system ./cmd/tailscaled/tailscaled.service
  116. ''
  117. + pkgs.lib.optionalString (pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform) ''
  118. installShellCompletion --cmd tailscale \
  119. --bash <($out/bin/tailscale completion bash) \
  120. --fish <($out/bin/tailscale completion fish) \
  121. --zsh <($out/bin/tailscale completion zsh)
  122. '';
  123. };
  124. tailscale = default;
  125. });
  126. devShells = eachSystem (pkgs: {
  127. devShell = pkgs.mkShell {
  128. packages = with pkgs; [
  129. curl
  130. git
  131. gopls
  132. gotools
  133. graphviz
  134. perl
  135. go_1_25
  136. yarn
  137. # qemu and e2fsprogs are needed for natlab
  138. qemu
  139. e2fsprogs
  140. ];
  141. };
  142. });
  143. };
  144. }
  145. # nix-direnv cache busting line: sha256-jsmQ0S1Uh1cU/kr0onYLJY9VYcFx297QZjQALM3wX10=