flake.nix 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. flake-utils.url = "github:numtide/flake-utils";
  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. flake-utils,
  46. flake-compat,
  47. }: let
  48. # tailscaleRev is the git commit at which this flake was imported,
  49. # or the empty string when building from a local checkout of the
  50. # tailscale repo.
  51. tailscaleRev = self.rev or "";
  52. # tailscale takes a nixpkgs package set, and builds Tailscale from
  53. # the same commit as this flake. IOW, it provides "tailscale built
  54. # from HEAD", where HEAD is "whatever commit you imported the
  55. # flake at".
  56. #
  57. # This is currently unfortunately brittle, because we have to
  58. # specify vendorHash, and that sha changes any time we alter
  59. # go.mod. We don't want to force a nix dependency on everyone
  60. # hacking on Tailscale, so this flake is likely to have broken
  61. # builds periodically until someone comes through and manually
  62. # fixes them up. I sure wish there was a way to express "please
  63. # just trust the local go.mod, vendorHash has no benefit here",
  64. # but alas.
  65. #
  66. # So really, this flake is for tailscale devs to dogfood with, if
  67. # you're an end user you should be prepared for this flake to not
  68. # build periodically.
  69. tailscale = pkgs:
  70. pkgs.buildGo123Module rec {
  71. name = "tailscale";
  72. src = ./.;
  73. vendorHash = pkgs.lib.fileContents ./go.mod.sri;
  74. nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.isLinux [pkgs.makeWrapper];
  75. ldflags = ["-X tailscale.com/version.gitCommitStamp=${tailscaleRev}"];
  76. CGO_ENABLED = 0;
  77. subPackages = ["cmd/tailscale" "cmd/tailscaled"];
  78. doCheck = false;
  79. # NOTE: We strip the ${PORT} and $FLAGS because they are unset in the
  80. # environment and cause issues (specifically the unset PORT). At some
  81. # point, there should be a NixOS module that allows configuration of these
  82. # things, but for now, we hardcode the default of port 41641 (taken from
  83. # ./cmd/tailscaled/tailscaled.defaults).
  84. postInstall = pkgs.lib.optionalString pkgs.stdenv.isLinux ''
  85. wrapProgram $out/bin/tailscaled --prefix PATH : ${pkgs.lib.makeBinPath [pkgs.iproute2 pkgs.iptables pkgs.getent pkgs.shadow]}
  86. wrapProgram $out/bin/tailscale --suffix PATH : ${pkgs.lib.makeBinPath [pkgs.procps]}
  87. sed -i \
  88. -e "s#/usr/sbin#$out/bin#" \
  89. -e "/^EnvironmentFile/d" \
  90. -e 's/''${PORT}/41641/' \
  91. -e 's/$FLAGS//' \
  92. ./cmd/tailscaled/tailscaled.service
  93. install -D -m0444 -t $out/lib/systemd/system ./cmd/tailscaled/tailscaled.service
  94. '';
  95. };
  96. # This whole blob makes the tailscale package available for all
  97. # OS/CPU combos that nix supports, as well as a dev shell so that
  98. # "nix develop" and "nix-shell" give you a dev env.
  99. flakeForSystem = nixpkgs: system: let
  100. pkgs = nixpkgs.legacyPackages.${system};
  101. ts = tailscale pkgs;
  102. in {
  103. packages = {
  104. default = ts;
  105. tailscale = ts;
  106. };
  107. devShell = pkgs.mkShell {
  108. packages = with pkgs; [
  109. curl
  110. git
  111. gopls
  112. gotools
  113. graphviz
  114. perl
  115. go_1_23
  116. yarn
  117. # qemu and e2fsprogs are needed for natlab
  118. qemu
  119. e2fsprogs
  120. ];
  121. };
  122. };
  123. in
  124. flake-utils.lib.eachDefaultSystem (system: flakeForSystem nixpkgs system);
  125. }
  126. # nix-direnv cache busting line: sha256-xO1DuLWi6/lpA9ubA2ZYVJM+CkVNA5IaVGZxX9my0j0=