flake.nix 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. # Grab a helper func out of the Nix language libraries. Annoyingly
  44. # these are only accessible through legacyPackages right now,
  45. # which forces us to indirect through a platform-specific
  46. # path. The x86_64-linux in here doesn't really matter, since all
  47. # we're grabbing is a pure Nix string manipulation function that
  48. # doesn't build any software.
  49. fileContents = nixpkgs.legacyPackages.x86_64-linux.lib.fileContents;
  50. # tailscaleRev is the git commit at which this flake was imported,
  51. # or the empty string when building from a local checkout of the
  52. # tailscale repo.
  53. tailscaleRev = if builtins.hasAttr "rev" self then self.rev else "";
  54. # tailscale takes a nixpkgs package set, and builds Tailscale from
  55. # the same commit as this flake. IOW, it provides "tailscale built
  56. # from HEAD", where HEAD is "whatever commit you imported the
  57. # flake at".
  58. #
  59. # This is currently unfortunately brittle, because we have to
  60. # specify vendorSha256, and that sha changes any time we alter
  61. # go.mod. We don't want to force a nix dependency on everyone
  62. # hacking on Tailscale, so this flake is likely to have broken
  63. # builds periodically until someone comes through and manually
  64. # fixes them up. I sure wish there was a way to express "please
  65. # just trust the local go.mod, vendorSha256 has no benefit here",
  66. # but alas.
  67. #
  68. # So really, this flake is for tailscale devs to dogfood with, if
  69. # you're an end user you should be prepared for this flake to not
  70. # build periodically.
  71. tailscale = pkgs: pkgs.buildGo120Module rec {
  72. name = "tailscale";
  73. src = ./.;
  74. vendorSha256 = fileContents ./go.mod.sri;
  75. nativeBuildInputs = pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.makeWrapper pkgs.git ];
  76. ldflags = ["-X tailscale.com/version.GitCommit=${tailscaleRev}"];
  77. CGO_ENABLED = 0;
  78. subPackages = [ "cmd/tailscale" "cmd/tailscaled" ];
  79. doCheck = false;
  80. postInstall = pkgs.lib.optionalString pkgs.stdenv.isLinux ''
  81. wrapProgram $out/bin/tailscaled --prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.iproute2 pkgs.iptables pkgs.getent pkgs.shadow ]}
  82. wrapProgram $out/bin/tailscale --suffix PATH : ${pkgs.lib.makeBinPath [ pkgs.procps ]}
  83. sed -i -e "s#/usr/sbin#$out/bin#" -e "/^EnvironmentFile/d" ./cmd/tailscaled/tailscaled.service
  84. install -D -m0444 -t $out/lib/systemd/system ./cmd/tailscaled/tailscaled.service
  85. '';
  86. };
  87. # This whole blob makes the tailscale package available for all
  88. # OS/CPU combos that nix supports, as well as a dev shell so that
  89. # "nix develop" and "nix-shell" give you a dev env.
  90. flakeForSystem = nixpkgs: system: let
  91. pkgs = nixpkgs.legacyPackages.${system};
  92. ts = tailscale pkgs;
  93. in {
  94. packages = {
  95. tailscale = ts;
  96. };
  97. devShell = pkgs.mkShell {
  98. packages = with pkgs; [
  99. curl
  100. git
  101. gopls
  102. gotools
  103. graphviz
  104. perl
  105. go_1_20
  106. yarn
  107. ];
  108. };
  109. };
  110. in
  111. flake-utils.lib.eachDefaultSystem (system: flakeForSystem nixpkgs system);
  112. }
  113. # nix-direnv cache busting line: sha256-lSK9rTz5NDXf5BBELL6YYYtxtjrHjfqEiYwN75hYA2c=