From c4ed1f1b80604bee2efc4771f121c8a7242dbfa3 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 12 Dec 2023 11:38:39 -0600 Subject: [PATCH] Use Alejandra to format Nix modules. --- .github/workflows/test.yml | 10 ++ README.md | 29 ++++- flake.nix | 51 ++++---- nix/devShell.nix | 256 +++++++++++++++++++------------------ nix/package.nix | 224 ++++++++++++++++---------------- nix/wraptest.nix | 9 +- shell.nix | 19 +-- 7 files changed, 322 insertions(+), 276 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 32070893b..1d0ea907e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -147,3 +147,13 @@ jobs: nix_path: nixpkgs=channel:nixos-unstable - name: prettier check run: nix develop -c prettier --check . + + alejandra: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 # Check out repo so we can lint it + - uses: cachix/install-nix-action@v24 + with: + nix_path: nixpkgs=channel:nixos-unstable + - name: alejandra check + run: nix develop -c alejandra --check . diff --git a/README.md b/README.md index 2a5a271f7..ef908c0be 100644 --- a/README.md +++ b/README.md @@ -492,6 +492,8 @@ on vastly improving this experience over time. ### Linting +#### Prettier + Ghostty's docs and resources (not including Zig code) are linted using [Prettier](https://prettier.io) with out-of-the-box settings. A Prettier CI check will fail builds with improper formatting. Therefore, if you are @@ -502,7 +504,32 @@ run this from the repo root before you commit: prettier --write . ``` -Make sure your Prettier version matches the version of in [devShell.nix](https://github.com/mitchellh/ghostty/blob/main/nix/devShell.nix). +Make sure your Prettier version matches the version of Prettier in [devShell.nix](https://github.com/mitchellh/ghostty/blob/main/nix/devShell.nix). + +Nix users can use the following command to format with Prettier: + +``` +nix develop -c prettier --write . +``` + +#### Alejandra + +Nix modules are formatted with [Alejandra](https://github.com/kamadorueda/alejandra/). An Alejandra CI check +will fail builds with improper formatting. + +Nix users can use the following command to format with Alejanda: + +``` +nix develop -c alejandra . +``` + +Non-Nix users should install Alejandra and use the follwing command to format with Alejanda: + +``` +alejandra . +``` + +Make sure your Alejandra version matches the version of Alejandra in [devShell.nix](https://github.com/mitchellh/ghostty/blob/main/nix/devShell.nix). ### Nix Package diff --git a/flake.nix b/flake.nix index b0ab45ab0..6106f5e0a 100644 --- a/flake.nix +++ b/flake.nix @@ -31,32 +31,33 @@ }; outputs = { - nixpkgs-unstable - , nixpkgs-stable - , nixpkgs-zig-0-12 - , zig - , zls - , ... - }: builtins.foldl' nixpkgs-stable.lib.recursiveUpdate {} (builtins.map (system: let - pkgs-stable = nixpkgs-stable.legacyPackages.${system}; - pkgs-unstable = nixpkgs-unstable.legacyPackages.${system}; - pkgs-zig-0-12 = nixpkgs-zig-0-12.legacyPackages.${system}; - in { - devShell.${system} = pkgs-stable.callPackage ./nix/devShell.nix { - inherit (pkgs-unstable) tracy; - inherit (zls.packages.${system}) zls; + nixpkgs-unstable, + nixpkgs-stable, + nixpkgs-zig-0-12, + zig, + zls, + ... + }: + builtins.foldl' nixpkgs-stable.lib.recursiveUpdate {} (builtins.map (system: let + pkgs-stable = nixpkgs-stable.legacyPackages.${system}; + pkgs-unstable = nixpkgs-unstable.legacyPackages.${system}; + pkgs-zig-0-12 = nixpkgs-zig-0-12.legacyPackages.${system}; + in { + devShell.${system} = pkgs-stable.callPackage ./nix/devShell.nix { + inherit (pkgs-unstable) tracy; + inherit (zls.packages.${system}) zls; - zig = zig.packages.${system}.master; - wraptest = pkgs-stable.callPackage ./nix/wraptest.nix {}; - }; - - packages.${system} = rec { - ghostty = pkgs-stable.callPackage ./nix/package.nix { - inherit (pkgs-zig-0-12) zig_0_12; + zig = zig.packages.${system}.master; + wraptest = pkgs-stable.callPackage ./nix/wraptest.nix {}; }; - default = ghostty; - }; - # Our supported systems are the same supported systems as the Zig binaries. - }) (builtins.attrNames zig.packages)); + packages.${system} = rec { + ghostty = pkgs-stable.callPackage ./nix/package.nix { + inherit (pkgs-zig-0-12) zig_0_12; + }; + default = ghostty; + }; + + # Our supported systems are the same supported systems as the Zig binaries. + }) (builtins.attrNames zig.packages)); } diff --git a/nix/devShell.nix b/nix/devShell.nix index 662a2c7e6..1c03c6a6c 100644 --- a/nix/devShell.nix +++ b/nix/devShell.nix @@ -1,139 +1,145 @@ { - mkShell -, lib -, stdenv - -, bashInteractive -, gdb -#, glxinfo # unused -, ncurses -, nodejs -, nodePackages -, oniguruma -, parallel -, pkg-config -, python3 -, scdoc -, tracy -, valgrind -#, vulkan-loader # unused -, vttest -, wabt -, wasmtime -, wraptest -, zig -, zip -, zls -, llvmPackages_latest - -, bzip2 -, expat -, fontconfig -, freetype -, glib -, gtk4 -, libadwaita -, harfbuzz -, libpng -, libGL -, libX11 -, libXcursor -, libXext -, libXi -, libXinerama -, libXrandr -, pixman -, zlib -}: -let + mkShell, + lib, + stdenv, + bashInteractive, + gdb, + #, glxinfo # unused + ncurses, + nodejs, + nodePackages, + oniguruma, + parallel, + pkg-config, + python3, + scdoc, + tracy, + valgrind, + #, vulkan-loader # unused + vttest, + wabt, + wasmtime, + wraptest, + zig, + zip, + zls, + llvmPackages_latest, + bzip2, + expat, + fontconfig, + freetype, + glib, + gtk4, + libadwaita, + harfbuzz, + libpng, + libGL, + libX11, + libXcursor, + libXext, + libXi, + libXinerama, + libXrandr, + pixman, + zlib, + alejandra, +}: let # See package.nix. Keep in sync. - rpathLibs = [ - libGL - ] ++ lib.optionals stdenv.isLinux [ - bzip2 - expat - fontconfig - freetype - harfbuzz - libpng - oniguruma - pixman - zlib + rpathLibs = + [ + libGL + ] + ++ lib.optionals stdenv.isLinux [ + bzip2 + expat + fontconfig + freetype + harfbuzz + libpng + oniguruma + pixman + zlib - libX11 - libXcursor - libXi - libXrandr + libX11 + libXcursor + libXi + libXrandr - libadwaita - gtk4 - glib - ]; -in mkShell { - name = "ghostty"; + libadwaita + gtk4 + glib + ]; +in + mkShell { + name = "ghostty"; - nativeBuildInputs = [ - # For builds - llvmPackages_latest.llvm - ncurses - pkg-config - scdoc - zig - zip - zls + nativeBuildInputs = + [ + # For builds + llvmPackages_latest.llvm + ncurses + pkg-config + scdoc + zig + zip + zls - # For web and wasm stuff - nodejs + # For web and wasm stuff + nodejs - # Linting - nodePackages.prettier + # Linting + nodePackages.prettier + alejandra - # Testing - parallel - python3 - tracy - vttest + # Testing + parallel + python3 + tracy + vttest - # wasm - wabt - wasmtime - ] ++ lib.optionals stdenv.isLinux [ - # My nix shell environment installs the non-interactive version - # by default so we have to include this. - bashInteractive + # wasm + wabt + wasmtime + ] + ++ lib.optionals stdenv.isLinux [ + # My nix shell environment installs the non-interactive version + # by default so we have to include this. + bashInteractive - gdb - valgrind - wraptest - ]; + gdb + valgrind + wraptest + ]; - buildInputs = [ - # TODO: non-linux - ] ++ lib.optionals stdenv.isLinux [ - bzip2 - expat - fontconfig - freetype - harfbuzz - libpng - oniguruma - pixman - zlib + buildInputs = + [ + # TODO: non-linux + ] + ++ lib.optionals stdenv.isLinux [ + bzip2 + expat + fontconfig + freetype + harfbuzz + libpng + oniguruma + pixman + zlib - libX11 - libXcursor - libXext - libXi - libXinerama - libXrandr + libX11 + libXcursor + libXext + libXi + libXinerama + libXrandr - # Only needed for GTK builds - libadwaita - gtk4 - glib - ]; + # Only needed for GTK builds + libadwaita + gtk4 + glib + ]; - # This should be set onto the rpath of the ghostty binary if you want - # it to be "portable" across the system. - LD_LIBRARY_PATH = lib.makeLibraryPath rpathLibs; -} + # This should be set onto the rpath of the ghostty binary if you want + # it to be "portable" across the system. + LD_LIBRARY_PATH = lib.makeLibraryPath rpathLibs; + } diff --git a/nix/package.nix b/nix/package.nix index 7a3ebfa05..734aa3b85 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -1,30 +1,26 @@ { - lib -, stdenv - -, bzip2 -, expat -, fontconfig -, freetype -, harfbuzz -, libpng -, pixman -, zlib - -, libGL -, libX11 -, libXcursor -, libXi -, libXrandr - -, glib -, gtk4 -, libadwaita - -, git -, ncurses -, pkg-config -, zig_0_12 + lib, + stdenv, + bzip2, + expat, + fontconfig, + freetype, + harfbuzz, + libpng, + pixman, + zlib, + libGL, + libX11, + libXcursor, + libXi, + libXrandr, + glib, + gtk4, + libadwaita, + git, + ncurses, + pkg-config, + zig_0_12, }: let # The Zig hook has no way to select the release type without actual # overriding of the default flags. @@ -51,99 +47,103 @@ # derivation in your store already. If so, just update the value as above.) zigCacheHash = import ./zigCacheHash.nix; - zigCache = src: stdenv.mkDerivation { - inherit src; - name = "ghostty-cache"; - nativeBuildInputs = [ git zig_0_12.hook ]; + 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 (finalAttrs: { + pname = "ghostty"; + version = "0.1.0"; + + src = ./..; + + nativeBuildInputs = [ + git + ncurses + pkg-config + zig012Hook + ]; + + buildInputs = + [ + libGL + ] + ++ lib.optionals stdenv.isLinux [ + bzip2 + expat + fontconfig + freetype + harfbuzz + libpng + pixman + zlib + + libX11 + libXcursor + libXi + libXrandr + + libadwaita + gtk4 + glib + ]; dontConfigure = true; - dontUseZigBuild = true; - dontUseZigInstall = true; - dontFixup = true; - buildPhase = '' - runHook preBuild + zigBuildFlags = "-Dversion-string=${finalAttrs.version}"; - zig build --fetch - - runHook postBuild + preBuild = '' + 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 ''; - installPhase = '' - runHook preInstall + outputs = ["out" "terminfo" "shell_integration"]; - cp -r --reflink=auto $ZIG_GLOBAL_CACHE_DIR $out + postInstall = '' + terminfo_src=${ + if stdenv.isDarwin + then ''"$out/Applications/Ghostty.app/Contents/Resources/terminfo"'' + else "$out/share/terminfo" + } - runHook postInstall + mkdir -p $terminfo/share + cp -r "$terminfo_src" $terminfo/share/terminfo + + mkdir -p $shell_integration + cp -r $out/share/shell-integration $shell_integration/shell-integration ''; - outputHashMode = "recursive"; - outputHash = zigCacheHash; - }; -in stdenv.mkDerivation (finalAttrs: { - pname = "ghostty"; - version = "0.1.0"; - - src = ./..; - - nativeBuildInputs = [ - git - ncurses - pkg-config - zig012Hook - ]; - - buildInputs = [ - libGL - ] ++ lib.optionals stdenv.isLinux [ - bzip2 - expat - fontconfig - freetype - harfbuzz - libpng - pixman - zlib - - libX11 - libXcursor - libXi - libXrandr - - libadwaita - gtk4 - glib - ]; - - dontConfigure = true; - - zigBuildFlags = "-Dversion-string=${finalAttrs.version}"; - - preBuild = '' - 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" "terminfo" "shell_integration" ]; - - postInstall = '' - terminfo_src=${ - if stdenv.isDarwin - then ''"$out/Applications/Ghostty.app/Contents/Resources/terminfo"'' - else "$out/share/terminfo" - } - - mkdir -p $terminfo/share - cp -r "$terminfo_src" $terminfo/share/terminfo - - mkdir -p $shell_integration - cp -r $out/share/shell-integration $shell_integration/shell-integration - ''; - - meta = with lib; { - homepage = "https://github.com/mitchellh/ghostty"; - license = licenses.mit; - platforms = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ]; - }; -}) + meta = with lib; { + homepage = "https://github.com/mitchellh/ghostty"; + license = licenses.mit; + platforms = ["x86_64-linux" "aarch64-linux" "aarch64-darwin"]; + }; + }) diff --git a/nix/wraptest.nix b/nix/wraptest.nix index 7fdad2e38..a5cd89b75 100644 --- a/nix/wraptest.nix +++ b/nix/wraptest.nix @@ -1,9 +1,8 @@ { - stdenv -, fetchFromGitHub -, autoPatchelfHook + stdenv, + fetchFromGitHub, + autoPatchelfHook, }: - stdenv.mkDerivation rec { version = "0.1.0-e7a96089"; pname = "wraptest"; @@ -39,6 +38,6 @@ stdenv.mkDerivation rec { meta = { description = "Test of DEC VT terminal line-wrapping semantics"; homepage = "https://github.com/mattiase/wraptest"; - platforms = [ "aarch64-linux" "i686-linux" "x86_64-linux" ]; + platforms = ["aarch64-linux" "i686-linux" "x86_64-linux"]; }; } diff --git a/shell.nix b/shell.nix index 6b7e7a250..a15057aa3 100644 --- a/shell.nix +++ b/shell.nix @@ -1,9 +1,12 @@ (import - ( - let flake-compat = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.flake-compat; in - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${flake-compat.locked.rev}.tar.gz"; - sha256 = flake-compat.locked.narHash; - } - ) - { src = ./.; }).shellNix + ( + let + flake-compat = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.flake-compat; + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${flake-compat.locked.rev}.tar.gz"; + sha256 = flake-compat.locked.narHash; + } + ) + {src = ./.;}) +.shellNix