diff --git a/flake.lock b/flake.lock index 1e151250a..38dc93c9e 100644 --- a/flake.lock +++ b/flake.lock @@ -176,6 +176,22 @@ "type": "github" } }, + "nixpkgs-zig-0-12": { + "locked": { + "lastModified": 1700381855, + "narHash": "sha256-2iN7PIsz7zcGU+va5eUZwNEmBnDGr+sUgoWhpsT5xtw=", + "owner": "vancluever", + "repo": "nixpkgs", + "rev": "d986dbb50cea7804e544ebd77342a2498e399166", + "type": "github" + }, + "original": { + "owner": "vancluever", + "ref": "vancluever-zig-0-12", + "repo": "nixpkgs", + "type": "github" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1689088367, @@ -214,6 +230,7 @@ "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "nixpkgs-unstable": "nixpkgs-unstable", + "nixpkgs-zig-0-12": "nixpkgs-zig-0-12", "zig": "zig", "zls-master": "zls-master" } diff --git a/flake.nix b/flake.nix index 35a222c59..64033b6cb 100644 --- a/flake.nix +++ b/flake.nix @@ -7,9 +7,19 @@ zig.url = "github:mitchellh/zig-overlay"; zls-master.url = "github:zigtools/zls/master"; - # We want to stay as up to date as possible but need to be careful - # that the glibc versions used by our dependencies from Nix are compatible - # with the system glibc that the user is building for. + # This is a nixpkgs mirror (based off of nixos-unstable) that contains + # patches for LLVM 17 and Zig 0.12 (master/nightly). + # + # This gives an up-to-date Zig that contains the nixpkgs patches, + # specifically the ones relating to NativeTargetInfo + # (https://github.com/ziglang/zig/issues/15898) in addition to the base + # hooks. This is used in the package (i.e. packages.ghostty, not the + # devShell) to build a Zig that can be included in a NixOS configuration. + nixpkgs-zig-0-12.url = "github:vancluever/nixpkgs/vancluever-zig-0-12"; + + # We want to stay as up to date as possible but need to be careful that the + # glibc versions used by our dependencies from Nix are compatible with the + # system glibc that the user is building for. nixpkgs.url = "github:nixos/nixpkgs/release-23.05"; # Used for shell.nix @@ -26,6 +36,8 @@ (final: prev: { zigpkgs = inputs.zig.packages.${prev.system}; + zig_0_12 = inputs.nixpkgs-zig-0-12.legacyPackages.${prev.system}.zig_0_12; + # Latest version of Tracy tracy = inputs.nixpkgs-unstable.legacyPackages.${prev.system}.tracy; @@ -36,7 +48,8 @@ # Our supported systems are the same supported systems as the Zig binaries systems = builtins.attrNames inputs.zig.packages; - in flake-utils.lib.eachSystem systems (system: + in + flake-utils.lib.eachSystem systems (system: let pkgs = import nixpkgs { inherit overlays system; }; in rec { devShell = pkgs.devShell; diff --git a/nix/package.nix b/nix/package.nix index 4e30099bc..8119d9af7 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -1,77 +1,120 @@ -# TODO(mitchellh): This currently doesn't fully work. It generates a binary -# that smashes the stack on run. I'm not sure why. -{ stdenv -, lib +{ lib +, stdenv + +, bzip2 +, expat +, fontconfig +, freetype +, harfbuzz +, libpng +, pixman +, zlib + , libGL , libX11 , libXcursor , libXi , libXrandr -, libXxf86vm -, libxcb -, pkg-config -, zig + +, glib +, gtk4 +, libadwaita + , git +, ncurses +, pkg-config +, zig_0_12 }: let - # These are the libraries that need to be added to the rpath for - # the binary so that they run properly on NixOS. - rpathLibs = [ - libGL - ] ++ lib.optionals stdenv.isLinux [ - libX11 - libXcursor - libXi - libXrandr - libXxf86vm - libxcb - ]; + # This hash is the computation of the zigCache fixed-output derivation. This + # allows us to use remote package dependencies without breaking the sandbox. + # + # This will need updating whenever dependencies get updated (e.g. changes are + # made to zig.build.zon). If you see that the main build is trying to reach + # out to the internet and failing, this is likely the cause. Change this + # value back to lib.fakeHash, and re-run. The build failure should emit the + # updated hash, which of course, should be validated before updating here. + # + # (It's also possible that you might see a hash mismatch - without the + # network errors - if you don't have a previous instance of the cache + # derivation in your store already. If so, just update the value as above.) + zigCacheHash = "sha256-2zDoQ4AKHJal7njTvt38RVEkTyPHNNX90QFibKq/xh4="; + + zigCache = src: stdenv.mkDerivation { + inherit src; + name = "ghostty-cache"; + nativeBuildInputs = [ git zig_0_12.hook ]; + + dontConfigure = true; + dontUseZigBuild = true; + dontUseZigInstall = true; + dontFixup = true; + + buildPhase = '' + runHook preBuild + + zig build --fetch + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + cp -r --reflink=auto $ZIG_GLOBAL_CACHE_DIR $out + + runHook postInstall + ''; + + outputHashMode = "recursive"; + outputHash = zigCacheHash; + }; in -stdenv.mkDerivation rec { +stdenv.mkDerivation (finalAttrs: { pname = "ghostty"; version = "0.1.0"; src = ./..; - nativeBuildInputs = [ git pkg-config zig ]; + nativeBuildInputs = [ + git + ncurses + pkg-config + zig_0_12.hook + ]; - buildInputs = rpathLibs ++ [ - # Nothing yet + buildInputs = [ + libGL + ] ++ lib.optionals stdenv.isLinux [ + bzip2 + expat + fontconfig + freetype + harfbuzz + libpng + pixman + zlib + + libX11 + libXcursor + libXi + libXrandr + + libadwaita + gtk4 + glib ]; dontConfigure = true; - dontPatchELF = true; - # The Zig cache goes into $HOME, so we set this to be writable + zigBuildFlags = "-Dversion-string=${finalAttrs.version}"; + preBuild = '' - export HOME=$TMPDIR - ''; - - # Build we do nothing except run hooks - buildPhase = '' - runHook preBuild - runHook postBuild - ''; - - installPhase = '' - runHook preInstall - - export MACH_SDK_PATH=${src}/vendor/mach-sdk - zig build \ - -Dcpu=baseline \ - -Dversion-string="${version}-nixdev" \ - --prefix $out \ - install - - strip -S $out/bin/ghostty - patchelf \ - --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \ - --set-rpath "${lib.makeLibraryPath rpathLibs}" \ - $out/bin/ghostty - - runHook postInstall + rm -rf $ZIG_GLOBAL_CACHE_DIR + cp -r --reflink=auto ${zigCache finalAttrs.src} $ZIG_GLOBAL_CACHE_DIR + chmod u+rwX -R $ZIG_GLOBAL_CACHE_DIR ''; outputs = [ "out" ]; @@ -81,4 +124,4 @@ stdenv.mkDerivation rec { license = licenses.mit; platforms = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ]; }; -} +})