2
0

flake.nix 5.0 KB

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